From 183be3d5b3b1a3a84b4f1542de30b7f65c38ab78 Mon Sep 17 00:00:00 2001 From: megamichi Date: Sat, 24 Feb 2024 21:59:53 +0100 Subject: [PATCH] tiptop gui --- __pycache__/button.cpython-311.pyc | Bin 2460 -> 2818 bytes button.py | 14 +++- main.py | 100 ++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 35 deletions(-) diff --git a/__pycache__/button.cpython-311.pyc b/__pycache__/button.cpython-311.pyc index 40e5a34cab3af985e0dc6291445ec40e4880304e..7a184789bc2efa4a123d1a9dbacefaf282eac66b 100644 GIT binary patch delta 589 zcmbOu+$6@goR^o20SIn~-%35kI+1UVE<1<|1x!Gi8Hhgz0Ey`gHOw^(@lYuSh8mU{ z)`_>Z8QCVYGuouG-(o3F%}FZ)nO4LJB0vV+VlB^1Dap9SmXVs7o>5Z74HPQ|NhmligT zb%EL&7;XrQO@6}Y%*4jLS(AyIak3SYHd6}AqvrLv{wq#_T9L{XY z%$CAFxu1EX3Wr}2$WJf91S628$qW&g24q86a+8BuDj20EpJ7pxAu&T_j`R%)#mNq=vXfm{b;Le!Fi1&% z22#MpS?QVBR+AgyzXf`4K0x|~| z7bkHP7Z(TrffKS$PU1#5h?5yD7hW=-`ObUuzAtaT?O02lCX;c*R!HT z>V-|;cdH}oJE3YAq)L3lsIg0EO^$NkJ#??N94sA-vs;*#TBM7tlmOO&djYd)JQ0yi zg?ac~i!M^J;qx~@s2p36w;{`}<^HZTPtXiUEiqT0X43qY*v)%J3@aPZ#GaHIXcvX3 z14^s--o|d!0Z6kCH3u2i6}tkBJ;cV^n}57$A6I9HY9f(lx%lL2N*;Sbzv!0b%nSO9 vdI#ohgA(l!-9pnrfw~!2pXyA-H8;!FEFEI!@y>}XaI?R76}k6+3g1>=*D` None: - self.pos = x,y + def __init__(self, screen, color, pos, width, height, text) -> None: + self.pos = pos self.größe = width,height self.color = color self.screen = screen @@ -15,8 +15,16 @@ class Button(): self.größe[0], self.größe[1]) ) + pygame.draw.rect(self.screen, + (0,150,0), + (self.pos[0]-self.größe[0]//2, + self.pos[1]-self.größe[1]//2, + self.größe[0], + self.größe[1]), + 2 + ) - font = pygame.font.Font(None, 36) + font = pygame.font.Font(None, 30) text_surface = font.render(self.text, True, (0,0,0)) text_rect = text_surface.get_rect() text_rect.center = (self.pos[0], diff --git a/main.py b/main.py index 4dfafb6..c186aa0 100644 --- a/main.py +++ b/main.py @@ -35,8 +35,9 @@ def draw_feld(feld, color_key, block_size): (a * block_size, b * block_size, block_size, block_size), ) + def verarbeite_feld(feld): - feldx,feldy = len(feld[0]),len(feld) + feldx, feldy = len(feld[0]), len(feld) feld2 = copy.deepcopy(feld) for y in range(len(feld)): for x in range(len(feld[0])): @@ -84,12 +85,26 @@ default_font = pygame.font.SysFont("sans", 14) display_mode = "border" feld, feldx, feldy = make_feld(20, display_mode) -acolor = (255, 0, 0) -wechselfarbe = "r" using_element = "a" pixelsize = 20 +button_abstandy = 30 -buttons = [Button(screen,(200,200,200),100,100,30,30,"*")] +buttons = [ + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 25, 25, "\/"), + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Debug"), + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Reset"), + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Element"), + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Zoom +"), + Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Zoom -"), +] +buttona = {"dropdown": 0, "debug": 1, "reset": 2, "element": 3, "plus": 4, "minus": 5} +for i, button in enumerate(buttons): + i += 1 + button.pos = (screensize[0] - 70, button_abstandy * i) +buttonhovered = [False, False, False, False, False, False] +buttons_to_render = 6 +dropdown = True +preview_buttonhovered = False show_debug = False running = True if __name__ == "__main__": @@ -118,6 +133,9 @@ if __name__ == "__main__": else: display_mode = "border" feld, feldx, feldy = make_feld(pixelsize, display_mode) + for i, button in enumerate(buttons): + i += 1 + button.pos = (screensize[0] - 70, button_abstandy * i) preview_pressed_keys = pressed_keys ## event managment @@ -127,43 +145,61 @@ if __name__ == "__main__": if event.type == pygame.WINDOWRESIZED: screensize = pygame.display.get_window_size() feld, feldx, feldy = make_feld(pixelsize, display_mode) - r, g, b = acolor - if wechselfarbe == "r": - r -= 1 - g += 1 - if r == 0: - wechselfarbe = "g" - if wechselfarbe == "g": - g -= 1 - b += 1 - if g == 0: - wechselfarbe = "b" - if wechselfarbe == "b": - b -= 1 - r += 1 - if b == 0: - wechselfarbe = "r" - acolor = r, g, b - color_key = {"#": (100, 100, 100), "a": acolor} + for i, button in enumerate(buttons): + i += 1 + button.pos = (screensize[0] - 70, button_abstandy * i) + color_key = {"#": (100, 100, 100), "a": (200,200,80)} draw_feld(feld, color_key, pixelsize) mx, my = pygame.mouse.get_pos() mousepressed = pygame.mouse.get_pressed()[0] - for button in buttons: - button.draw() - - if mousepressed: - for button in buttons: - if button.check_press((mx,my)): - show_debug = not show_debug - else: - feld[my // pixelsize][mx // pixelsize] = using_element + for button in buttons[:buttons_to_render]: + button.draw() + + if mousepressed: + for i, button in enumerate(buttons[:buttons_to_render]): + if button.check_press((mx, my)): + buttonhovered[i] = True + else: + buttonhovered[i] = False + if not True in buttonhovered: + feld[my // pixelsize][mx // pixelsize] = using_element + + if mousepressed and not preview_mousepressed: + if buttonhovered[buttona["dropdown"]]: + dropdown = not dropdown + if dropdown: + buttons_to_render = len(buttons) + buttons[buttona["dropdown"]].text = "\/" + else: + buttons_to_render = 1 + buttons[buttona["dropdown"]].text = "/\\" + if buttonhovered[buttona["debug"]]: + show_debug = not show_debug + if buttonhovered[buttona["reset"]]: + if display_mode == "border": + display_mode = "loop" + else: + display_mode = "border" + feld, feldx, feldy = make_feld(pixelsize, display_mode) + if buttonhovered[buttona["plus"]]: + pixelsize += 2 + feld, feldx, feldy = make_feld(pixelsize, display_mode) + if buttonhovered[buttona["minus"]]: + pixelsize -= 2 + feld, feldx, feldy = make_feld(pixelsize, display_mode) + if buttonhovered[buttona["element"]]: + if using_element == "a": + using_element = "#" + else: + using_element = "a" + preview_buttonhovered = buttonhovered preview_mousepressed = pygame.mouse.get_pressed()[0] feld = verarbeite_feld(feld) if show_debug: get_debug_text() ## bildschirm aktuallisierung pygame.display.flip() - clock.tick(30) + clock.tick(60) screen.fill((0, 0, 0))