komentare überall
This commit is contained in:
parent
183be3d5b3
commit
401201aba3
3 changed files with 63 additions and 62 deletions
Binary file not shown.
|
@ -30,7 +30,7 @@ class Button():
|
|||
text_rect.center = (self.pos[0],
|
||||
self.pos[1])
|
||||
self.screen.blit(text_surface, text_rect)
|
||||
def check_press(self, mouse):
|
||||
def check_hovered(self, mouse):
|
||||
if (self.pos[0] - self.größe[0]//2 <= mouse[0] <= self.pos[0] + self.größe[0]//2 and
|
||||
self.pos[1] - self.größe[1]//2 <= mouse[1] <= self.pos[1] + self.größe[1]//2):
|
||||
return True
|
||||
|
|
123
main.py
123
main.py
|
@ -37,29 +37,29 @@ def draw_feld(feld, color_key, block_size):
|
|||
|
||||
|
||||
def verarbeite_feld(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])):
|
||||
feldx, feldy = len(feld[0]), len(feld) # bekomme feld größe
|
||||
feld2 = copy.deepcopy(feld) # mache neue kopie
|
||||
for y in range(feldy): # spalte
|
||||
for x in range(feldx): # zeile
|
||||
poschar = feld[y][x]
|
||||
if poschar == "a":
|
||||
# uberprüfe unteren block
|
||||
if poschar == "a": # fals aktuelles checkendes element = "a"
|
||||
# bekomme alle benötigten nachtbaren
|
||||
bellow = feld[(y + 1) % feldy][x]
|
||||
bellowr = feld2[(y + 1) % feldy][(x + 1) % feldx]
|
||||
bellowl = feld2[(y + 1) % feldy][(x - 1) % feldx]
|
||||
direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung
|
||||
if bellow == " " and not bellow == "#":
|
||||
if bellow == " " and not bellow == "#": # checke unten
|
||||
feld2[y][x] = feld[(y + 1) % feldy][x]
|
||||
feld2[(y + 1) % feldy][x] = feld[y][x]
|
||||
|
||||
elif bellowr == " " and not bellowr == "#":
|
||||
feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
|
||||
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
|
||||
elif bellowr == " " and not bellowr == "#" and direction == 1: # checke unten rechts fals richtung nach rechts
|
||||
feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx] # tausche in kopie aktuelle position mit "a"
|
||||
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x] # tausche in kopie neue position mit bevorigen element
|
||||
|
||||
elif bellowl == " " and not bellowl == "#":
|
||||
elif bellowl == " " and not bellowl == "#" and direction == -1: # checke unten links fals richtung nach links
|
||||
feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
|
||||
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
|
||||
return feld2
|
||||
return feld2 # gib kopie zurück
|
||||
|
||||
|
||||
def make_feld(size, mode):
|
||||
|
@ -71,7 +71,12 @@ def make_feld(size, mode):
|
|||
feld[y - 1][x] = "#"
|
||||
return feld, x, y
|
||||
|
||||
|
||||
def remake_feld():
|
||||
global feld,feldx,feldy
|
||||
feld, feldx, feldy = make_feld(pixelsize, display_mode)
|
||||
for i, button in enumerate(buttons):
|
||||
i += 1
|
||||
button.pos = (screensize[0] - 70, button_abstandy * i)
|
||||
## klassen
|
||||
|
||||
|
||||
|
@ -82,12 +87,12 @@ screensize = pygame.display.get_window_size()
|
|||
clock = pygame.time.Clock()
|
||||
default_font = pygame.font.SysFont("sans", 14)
|
||||
|
||||
display_mode = "border"
|
||||
display_mode = "border" # loop oder border # durchfallen oder mit boden
|
||||
feld, feldx, feldy = make_feld(20, display_mode)
|
||||
|
||||
using_element = "a"
|
||||
pixelsize = 20
|
||||
button_abstandy = 30
|
||||
pixelsize = 20 # größe eines pixels
|
||||
button_abstandy = 30 # abstand von buttons
|
||||
|
||||
buttons = [
|
||||
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 25, 25, "\/"),
|
||||
|
@ -102,75 +107,69 @@ 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
|
||||
buttons_to_render = 6 # button begrenzung
|
||||
dropdown = True
|
||||
preview_buttonhovered = False
|
||||
before_buttonhovered = False
|
||||
show_debug = False
|
||||
running = True
|
||||
if __name__ == "__main__":
|
||||
while running:
|
||||
## key managment
|
||||
pressed_keys = pygame.key.get_pressed()
|
||||
if True in pressed_keys:
|
||||
if pressed_keys[pygame.K_ESCAPE]:
|
||||
exit()
|
||||
if pressed_keys[pygame.K_PLUS] and not before_pressed_keys[pygame.K_PLUS]:
|
||||
pixelsize += 2
|
||||
remake_feld()
|
||||
if pressed_keys[pygame.K_MINUS] and not before_pressed_keys[pygame.K_MINUS]:
|
||||
pixelsize -= 2
|
||||
remake_feld()
|
||||
if pressed_keys[pygame.K_d] and not before_pressed_keys[pygame.K_d]:
|
||||
show_debug = not show_debug
|
||||
if pressed_keys[pygame.K_SPACE] and not before_pressed_keys[pygame.K_SPACE]:
|
||||
if using_element == "a":
|
||||
using_element = "#"
|
||||
else:
|
||||
using_element = "a"
|
||||
if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]:
|
||||
if display_mode == "border":
|
||||
display_mode = "loop"
|
||||
else:
|
||||
display_mode = "border"
|
||||
remake_feld()
|
||||
|
||||
if pressed_keys[pygame.K_ESCAPE]:
|
||||
exit()
|
||||
if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]:
|
||||
pixelsize += 2
|
||||
feld, feldx, feldy = make_feld(pixelsize, display_mode)
|
||||
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]:
|
||||
pixelsize -= 2
|
||||
feld, feldx, feldy = make_feld(pixelsize, display_mode)
|
||||
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
|
||||
show_debug = not show_debug
|
||||
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
|
||||
if using_element == "a":
|
||||
using_element = "#"
|
||||
else:
|
||||
using_element = "a"
|
||||
if pressed_keys[pygame.K_r] and not preview_pressed_keys[pygame.K_r]:
|
||||
if display_mode == "border":
|
||||
display_mode = "loop"
|
||||
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
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
exit()
|
||||
if event.type == pygame.WINDOWRESIZED:
|
||||
screensize = pygame.display.get_window_size()
|
||||
feld, feldx, feldy = make_feld(pixelsize, display_mode)
|
||||
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)
|
||||
remake_feld()
|
||||
color_key = {"#": (100, 100, 100), "a": (200,200,80)} # element:farbe
|
||||
draw_feld(feld, color_key, pixelsize) # male feld mit color_key
|
||||
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
mousepressed = pygame.mouse.get_pressed()[0]
|
||||
|
||||
for button in buttons[:buttons_to_render]:
|
||||
for button in buttons[:buttons_to_render]: # male jeden button
|
||||
button.draw()
|
||||
|
||||
if mousepressed:
|
||||
for i, button in enumerate(buttons[:buttons_to_render]):
|
||||
if button.check_press((mx, my)):
|
||||
for i, button in enumerate(buttons[:buttons_to_render]):
|
||||
# checke alle button ob die maus auf einen ist und schreibe es in die liste buttonhovered
|
||||
if button.check_hovered((mx, my)):
|
||||
buttonhovered[i] = True
|
||||
else:
|
||||
buttonhovered[i] = False
|
||||
if not True in buttonhovered:
|
||||
if not True in buttonhovered: # wenn nicht maus über button
|
||||
feld[my // pixelsize][mx // pixelsize] = using_element
|
||||
|
||||
if mousepressed and not preview_mousepressed:
|
||||
if buttonhovered[buttona["dropdown"]]:
|
||||
if mousepressed and not before_mousepressed: # einmalklick von maus erfassen
|
||||
if buttonhovered[buttona["dropdown"]]: # falls button "dropdown" mit index von buttona = True
|
||||
dropdown = not dropdown
|
||||
if dropdown:
|
||||
buttons_to_render = len(buttons)
|
||||
buttons_to_render = len(buttons) # dropdown menü begrenzen indem man nicht alle buttons verarbeitet
|
||||
buttons[buttona["dropdown"]].text = "\/"
|
||||
else:
|
||||
buttons_to_render = 1
|
||||
|
@ -194,10 +193,12 @@ if __name__ == "__main__":
|
|||
using_element = "#"
|
||||
else:
|
||||
using_element = "a"
|
||||
preview_buttonhovered = buttonhovered
|
||||
preview_mousepressed = pygame.mouse.get_pressed()[0]
|
||||
feld = verarbeite_feld(feld)
|
||||
if show_debug:
|
||||
|
||||
before_buttonhovered = buttonhovered # bevorige maus und button hover daten erfassen
|
||||
before_mousepressed = mousepressed
|
||||
before_pressed_keys = pressed_keys
|
||||
feld = verarbeite_feld(feld) # feld neu machen
|
||||
if show_debug: # debug text zeichnen
|
||||
get_debug_text()
|
||||
## bildschirm aktuallisierung
|
||||
pygame.display.flip()
|
||||
|
|
Loading…
Reference in a new issue