regeländerung und formatierung
This commit is contained in:
parent
c48222991b
commit
c110dd67c7
1 changed files with 94 additions and 54 deletions
148
main.py
148
main.py
|
@ -31,58 +31,66 @@ def draw_feld(feld, block_size):
|
||||||
if feld[b][a]:
|
if feld[b][a]:
|
||||||
pygame.draw.rect(
|
pygame.draw.rect(
|
||||||
screen,
|
screen,
|
||||||
(255,255,255),
|
(255, 255, 255),
|
||||||
(a * block_size, b * block_size, block_size, block_size),
|
(a * block_size, b * block_size, block_size, block_size),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def finde_nachbarn(x, y):
|
def finde_nachbarn(x, y):
|
||||||
feldx, feldy = len(feld[0]), len(feld)
|
feldx, feldy = len(feld[0]), len(feld)
|
||||||
ov = (y - 1) % feldy , x % feldx
|
ov = (y - 1) % feldy, x % feldx
|
||||||
ovr = (y - 1) % feldy , (x + 1) % feldx
|
ovr = (y - 1) % feldy, (x + 1) % feldx
|
||||||
ovl = (y - 1) % feldy , (x - 1) % feldx
|
ovl = (y - 1) % feldy, (x - 1) % feldx
|
||||||
|
|
||||||
r = y , (x + 1) % feldx
|
r = y, (x + 1) % feldx
|
||||||
l = y , (x - 1) % feldx
|
l = y, (x - 1) % feldx
|
||||||
|
|
||||||
|
be = (y + 1) % feldy, x % feldx
|
||||||
|
ber = (y + 1) % feldy, (x + 1) % feldx
|
||||||
|
bel = (y + 1) % feldy, (x - 1) % feldx
|
||||||
|
return [ovl, ov, ovr, r, l, bel, be, ber]
|
||||||
|
|
||||||
be = (y + 1) % feldy , x % feldx
|
|
||||||
ber = (y + 1) % feldy , (x + 1) % feldx
|
|
||||||
bel = (y + 1) % feldy , (x - 1) % feldx
|
|
||||||
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
|
||||||
for y in range(feldy): # spalte
|
for y in range(feldy): # spalte
|
||||||
for x in range(feldx): # zeile
|
for x in range(feldx): # zeile
|
||||||
lives = 0
|
lives = 0
|
||||||
for pos in finde_nachbarn(x ,y):
|
for pos in finde_nachbarn(x, y):
|
||||||
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:
|
||||||
|
new = True
|
||||||
else:
|
else:
|
||||||
if lives == 3:new = True
|
if lives == 3:
|
||||||
else: new = False
|
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
|
||||||
old_feld_size = len(feld[0]),len(feld)
|
old_feld_size = len(feld[0]), len(feld)
|
||||||
new_feld = [[" " for _ in range(sx)] for _ in range(sy)]
|
new_feld = [[" " for _ in range(sx)] for _ in range(sy)]
|
||||||
|
|
||||||
for y in range(len(new_feld)):
|
for y in range(len(new_feld)):
|
||||||
|
@ -91,13 +99,16 @@ def resize_feld(feld, new_size, fill_with=False):
|
||||||
new_feld[y][x] = feld[y][x]
|
new_feld[y][x] = feld[y][x]
|
||||||
else:
|
else:
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
@ -108,11 +119,11 @@ screensize = pygame.display.get_window_size()
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
default_font = pygame.font.SysFont("sans", 14)
|
default_font = pygame.font.SysFont("sans", 14)
|
||||||
|
|
||||||
feld, feldx, feldy = make_feld(20,True)
|
feld, feldx, feldy = make_feld(20, True)
|
||||||
|
|
||||||
pause = False
|
pause = False
|
||||||
pixelsize = 20 # größe eines pixels
|
pixelsize = 20 # größe eines pixels
|
||||||
button_abstandy = 30 # abstand von buttons
|
button_abstandy = 30 # abstand von buttons
|
||||||
|
|
||||||
buttons = [
|
buttons = [
|
||||||
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 25, 25, "\/"),
|
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 25, 25, "\/"),
|
||||||
|
@ -123,12 +134,20 @@ 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
|
||||||
dropdown = True
|
dropdown = True
|
||||||
before_buttonhovered = [False,False,False]
|
before_buttonhovered = [False, False, False]
|
||||||
key_counter = 0
|
key_counter = 0
|
||||||
pressed_keys = pygame.key.get_pressed()
|
pressed_keys = pygame.key.get_pressed()
|
||||||
show_debug = False
|
show_debug = False
|
||||||
|
@ -143,18 +162,27 @@ 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)
|
||||||
|
|
||||||
## event managment
|
## event managment
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
|
@ -162,34 +190,40 @@ if __name__ == "__main__":
|
||||||
exit()
|
exit()
|
||||||
if event.type == pygame.WINDOWRESIZED:
|
if event.type == pygame.WINDOWRESIZED:
|
||||||
screensize = pygame.display.get_window_size()
|
screensize = pygame.display.get_window_size()
|
||||||
feld, feldx, feldy = resize_feld(feld,pixelsize)
|
feld, feldx, feldy = resize_feld(feld, pixelsize)
|
||||||
setbutton_pos()
|
setbutton_pos()
|
||||||
draw_feld(feld, pixelsize) # male feld mit color_key
|
draw_feld(feld, pixelsize) # male feld mit color_key
|
||||||
|
|
||||||
mx, my = pygame.mouse.get_pos()
|
mx, my = pygame.mouse.get_pos()
|
||||||
mousepressed = pygame.mouse.get_pressed()
|
mousepressed = pygame.mouse.get_pressed()
|
||||||
|
|
||||||
for button in buttons[:buttons_to_render]: # male jeden button
|
for button in buttons[:buttons_to_render]: # male jeden button
|
||||||
button.draw()
|
button.draw()
|
||||||
|
|
||||||
if True in mousepressed:
|
if True in mousepressed:
|
||||||
for i, button in enumerate(buttons[:buttons_to_render]):
|
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
|
# checke alle button ob die maus auf einen ist und schreibe es in die liste buttonhovered
|
||||||
if button.check_hovered((mx, my)):
|
if button.check_hovered((mx, my)):
|
||||||
buttonhovered[i] = True
|
buttonhovered[i] = True
|
||||||
else:
|
else:
|
||||||
buttonhovered[i] = False
|
buttonhovered[i] = False
|
||||||
if not True in buttonhovered : # wenn nicht maus über button
|
if not True in buttonhovered: # wenn nicht maus über button
|
||||||
if mousepressed[0] == True:
|
if mousepressed[0] == True:
|
||||||
feld[my // pixelsize][mx // pixelsize] = True
|
feld[my // pixelsize][mx // pixelsize] = True
|
||||||
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
|
||||||
|
@ -197,27 +231,33 @@ if __name__ == "__main__":
|
||||||
if buttonhovered[buttona["debug"]]:
|
if buttonhovered[buttona["debug"]]:
|
||||||
show_debug = not show_debug
|
show_debug = not show_debug
|
||||||
if buttonhovered[buttona["reset"]]:
|
if buttonhovered[buttona["reset"]]:
|
||||||
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"]]:
|
||||||
feld, feldx, feldy = make_feld(pixelsize,True)
|
feld, feldx, feldy = make_feld(pixelsize, True)
|
||||||
if buttonhovered[buttona["pause"]]:
|
if buttonhovered[buttona["pause"]]:
|
||||||
pause = not pause
|
pause = not pause
|
||||||
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:
|
||||||
feld = verarbeite_feld(feld) # feld neu machen
|
feld = verarbeite_feld(feld) # feld neu machen
|
||||||
if show_debug: # debug text zeichnen
|
if show_debug: # debug text zeichnen
|
||||||
get_debug_text()
|
get_debug_text()
|
||||||
## bildschirm aktuallisierung
|
## bildschirm aktuallisierung
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
Loading…
Reference in a new issue