regeländerung und formatierung

This commit is contained in:
Michael S. 2024-02-28 18:52:11 +01:00
parent c48222991b
commit c110dd67c7

72
main.py
View file

@ -50,6 +50,7 @@ def finde_nachbarn(x, y):
bel = (y + 1) % feldy, (x - 1) % feldx bel = (y + 1) % feldy, (x - 1) % feldx
return [ovl, ov, ovr, r, l, bel, be, ber] return [ovl, ov, ovr, r, l, bel, be, ber]
def verarbeite_feld(feld): def verarbeite_feld(feld):
feldx, feldy = len(feld[0]), len(feld) # bekomme feld größe feldx, feldy = len(feld[0]), len(feld) # bekomme feld größe
feld2 = copy.deepcopy(feld) # mache neue kopie feld2 = copy.deepcopy(feld) # mache neue kopie
@ -60,25 +61,32 @@ def verarbeite_feld(feld):
if feld[pos[0]][pos[1]]: if feld[pos[0]][pos[1]]:
lives += 1 lives += 1
if feld[y][x]: if feld[y][x]:
if lives <= 1:new = False if lives <= 1 or lives >= 4:
elif lives >= 4:new = False new = False
else: new = True
else: else:
if lives == 3:new = True new = True
else: new = False else:
if lives == 3:
new = True
else:
new = False
feld2[y][x] = new feld2[y][x] = new
return feld2 # gib kopie zurück return feld2 # gib kopie zurück
## Funktion zum Verarbeiten des Feldes ## Funktion zum Verarbeiten des Feldes
def make_feld(size, rand): def make_feld(size, rand):
x = screensize[0] // size x = screensize[0] // size
y = screensize[1] // size y = screensize[1] // size
if rand: if rand:
feld = [[random.choice([False, True]) for _ in range(x)] for _ in range(y)] feld = [[random.choice([False, True]) for _ in range(x)] for _ in range(y)]
else: feld = [[False for _ in range(x)] for _ in range(y)] else:
feld = [[False for _ in range(x)] for _ in range(y)]
return feld, x, y return feld, x, y
def resize_feld(feld, new_size, fill_with=False): def resize_feld(feld, new_size, fill_with=False):
sx = screensize[0] // new_size sx = screensize[0] // new_size
sy = screensize[1] // new_size sy = screensize[1] // new_size
@ -93,11 +101,14 @@ def resize_feld(feld, new_size, fill_with=False):
new_feld[y][x] = fill_with new_feld[y][x] = fill_with
return new_feld, sx, sy return new_feld, sx, sy
def setbutton_pos(): def setbutton_pos():
global buttons global buttons
for i, button in enumerate(buttons): for i, button in enumerate(buttons):
i += 1 i += 1
button.pos = (screensize[0] - 70, button_abstandy * i) button.pos = (screensize[0] - 70, button_abstandy * i)
## klassen ## klassen
@ -123,7 +134,15 @@ buttons = [
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Randomize"), Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Randomize"),
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Clear"), Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Clear"),
] ]
buttona = {"dropdown": 0, "debug": 1, "reset": 2, "zoom": 3, "pause": 4, "random": 5, "clear": 6} buttona = {
"dropdown": 0,
"debug": 1,
"reset": 2,
"zoom": 3,
"pause": 4,
"random": 5,
"clear": 6,
}
setbutton_pos() setbutton_pos()
buttonhovered = [False, False, False, False, False, False, False] buttonhovered = [False, False, False, False, False, False, False]
buttons_to_render = 6 # button begrenzung buttons_to_render = 6 # button begrenzung
@ -143,15 +162,24 @@ if __name__ == "__main__":
if True in pressed_keys: if True in pressed_keys:
if pressed_keys[pygame.K_ESCAPE]: if pressed_keys[pygame.K_ESCAPE]:
exit() exit()
if pressed_keys[pygame.K_PLUS] and not before_pressed_keys[pygame.K_PLUS]: if (
pressed_keys[pygame.K_PLUS]
and not before_pressed_keys[pygame.K_PLUS]
):
pixelsize += 2 pixelsize += 2
feld, feldx, feldy = resize_feld(feld, pixelsize) feld, feldx, feldy = resize_feld(feld, pixelsize)
if pressed_keys[pygame.K_MINUS] and not before_pressed_keys[pygame.K_MINUS]: if (
pressed_keys[pygame.K_MINUS]
and not before_pressed_keys[pygame.K_MINUS]
):
pixelsize -= 2 pixelsize -= 2
feld, feldx, feldy = resize_feld(feld, pixelsize) feld, feldx, feldy = resize_feld(feld, pixelsize)
if pressed_keys[pygame.K_d] and not before_pressed_keys[pygame.K_d]: if pressed_keys[pygame.K_d] and not before_pressed_keys[pygame.K_d]:
show_debug = not show_debug show_debug = not show_debug
if pressed_keys[pygame.K_SPACE] and not before_pressed_keys[pygame.K_SPACE]: if (
pressed_keys[pygame.K_SPACE]
and not before_pressed_keys[pygame.K_SPACE]
):
pause = not pause pause = not pause
if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]: if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]:
feld, feldx, feldy = make_feld(pixelsize, False) feld, feldx, feldy = make_feld(pixelsize, False)
@ -185,11 +213,17 @@ if __name__ == "__main__":
else: else:
feld[my // pixelsize][mx // pixelsize] = False feld[my // pixelsize][mx // pixelsize] = False
if mousepressed[0] and not before_mousepressed[0]: # einmalklick von maus erfassen if (
if buttonhovered[buttona["dropdown"]]: # falls button "dropdown" mit index von buttona = True mousepressed[0] and not before_mousepressed[0]
): # einmalklick von maus erfassen
if buttonhovered[
buttona["dropdown"]
]: # falls button "dropdown" mit index von buttona = True
dropdown = not dropdown dropdown = not dropdown
if dropdown: if dropdown:
buttons_to_render = len(buttons) # dropdown menü begrenzen indem man nicht alle buttons verarbeitet buttons_to_render = len(
buttons
) # dropdown menü begrenzen indem man nicht alle buttons verarbeitet
buttons[buttona["dropdown"]].text = "\/" buttons[buttona["dropdown"]].text = "\/"
else: else:
buttons_to_render = 1 buttons_to_render = 1
@ -200,7 +234,9 @@ if __name__ == "__main__":
feld, feldx, feldy = make_feld(pixelsize, False) feld, feldx, feldy = make_feld(pixelsize, False)
if buttonhovered[buttona["zoom"]]: if buttonhovered[buttona["zoom"]]:
pixelsize += 2 pixelsize += 2
feld, feldx, feldy = resize_feld(feld,pixelsize,False)#make_feld(pixelsize,False) feld, feldx, feldy = resize_feld(
feld, pixelsize, False
) # make_feld(pixelsize,False)
if buttonhovered[buttona["clear"]]: if buttonhovered[buttona["clear"]]:
feld, feldx, feldy = make_feld(pixelsize, False) feld, feldx, feldy = make_feld(pixelsize, False)
if buttonhovered[buttona["random"]]: if buttonhovered[buttona["random"]]:
@ -210,9 +246,13 @@ if __name__ == "__main__":
if mousepressed[2] and not before_mousepressed[2]: if mousepressed[2] and not before_mousepressed[2]:
if buttonhovered[buttona["zoom"]]: if buttonhovered[buttona["zoom"]]:
pixelsize -= 2 pixelsize -= 2
feld, feldx, feldy = resize_feld(feld,pixelsize,False)#make_feld(pixelsize,False) feld, feldx, feldy = resize_feld(
feld, pixelsize, False
) # make_feld(pixelsize,False)
before_buttonhovered = buttonhovered # bevorige maus und button hover daten erfassen before_buttonhovered = (
buttonhovered # bevorige maus und button hover daten erfassen
)
before_mousepressed = mousepressed before_mousepressed = mousepressed
before_pressed_keys = pressed_keys before_pressed_keys = pressed_keys
if not pause: if not pause: