GoL/main.py

260 lines
9.1 KiB
Python
Raw Normal View History

2024-02-28 17:44:06 +01:00
#!/usr/bin/python3
import pygame
2024-02-28 19:47:39 +01:00
from button import Button
from rules import pixel
2024-02-28 17:44:06 +01:00
import copy
import random
pygame.init()
## funktionen
def get_debug_text():
def text(text, line_counter):
debug_text = default_font.render(str(text), True, (255, 255, 255))
screen.blit(debug_text, (0, line_counter * default_font.get_height()))
line_counter = 1
globalvars = globals()
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}", line_counter)
line_counter += 1
def centered_text(text=str, pos=tuple, color=tuple):
text = default_font.render(str(text), True, color, (255, 255, 255))
screen.blit(text, (pos[0] - text.get_width() / 2, pos[1]))
def draw_feld(feld, block_size):
for a in range(len(feld[0])):
for b in range(len(feld)):
if feld[b][a]:
pygame.draw.rect(
screen,
2024-02-28 18:52:11 +01:00
(255, 255, 255),
2024-02-28 17:44:06 +01:00
(a * block_size, b * block_size, block_size, block_size),
)
def finde_nachbarn(x, y):
feldx, feldy = len(feld[0]), len(feld)
2024-02-28 18:52:11 +01:00
ov = (y - 1) % feldy, x % feldx
ovr = (y - 1) % feldy, (x + 1) % feldx
ovl = (y - 1) % feldy, (x - 1) % feldx
2024-02-28 17:44:06 +01:00
2024-02-28 18:52:11 +01:00
r = 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]
2024-02-28 17:44:06 +01:00
2024-02-29 14:47:37 +01:00
## Funktion zum Verarbeiten des Feldes
2024-02-28 17:44:06 +01:00
def verarbeite_feld(feld):
2024-02-28 18:52:11 +01:00
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
2024-02-28 17:44:06 +01:00
lives = 0
2024-02-28 18:52:11 +01:00
for pos in finde_nachbarn(x, y):
2024-02-28 17:44:06 +01:00
if feld[pos[0]][pos[1]]:
lives += 1
2024-02-29 14:47:37 +01:00
feld2[y][x] = pixel(feld[y][x], str(lives))
2024-02-28 18:52:11 +01:00
return feld2 # gib kopie zurück
2024-02-28 17:44:06 +01:00
2024-02-28 18:52:11 +01:00
def make_feld(size, rand):
2024-02-28 17:44:06 +01:00
x = screensize[0] // size
y = screensize[1] // size
if rand:
feld = [[random.choice([False, True]) for _ in range(x)] for _ in range(y)]
2024-02-28 18:52:11 +01:00
else:
feld = [[False for _ in range(x)] for _ in range(y)]
2024-02-28 17:44:06 +01:00
return feld, x, y
2024-02-28 18:52:11 +01:00
2024-02-28 17:44:06 +01:00
def resize_feld(feld, new_size, fill_with=False):
sx = screensize[0] // new_size
sy = screensize[1] // new_size
2024-02-28 18:52:11 +01:00
old_feld_size = len(feld[0]), len(feld)
2024-02-28 17:44:06 +01:00
new_feld = [[" " for _ in range(sx)] for _ in range(sy)]
for y in range(len(new_feld)):
for x in range(len(new_feld[0])):
if x < old_feld_size[0] and y < old_feld_size[1]:
new_feld[y][x] = feld[y][x]
else:
new_feld[y][x] = fill_with
2024-02-28 18:52:11 +01:00
return new_feld, sx, sy
2024-02-28 17:44:06 +01:00
def setbutton_pos():
global buttons
for i, button in enumerate(buttons):
i += 1
button.pos = (screensize[0] - 70, button_abstandy * i)
2024-02-28 18:52:11 +01:00
2024-02-28 17:44:06 +01:00
## klassen
## variablen
screen = pygame.display.set_mode((800, 500), pygame.RESIZABLE)
pygame.display.set_caption("GoL")
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
default_font = pygame.font.SysFont("sans", 14)
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = make_feld(20, True)
2024-02-28 17:44:06 +01:00
pause = False
2024-02-28 18:52:11 +01:00
pixelsize = 20 # größe eines pixels
button_abstandy = 30 # abstand von buttons
2024-02-28 17:44:06 +01:00
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, "Zoom"),
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Pause"),
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Randomize"),
Button(screen, (200, 200, 200), (screensize[0] - 70, 50), 100, 25, "Clear"),
]
2024-02-28 18:52:11 +01:00
buttona = {
"dropdown": 0,
"debug": 1,
"reset": 2,
"zoom": 3,
"pause": 4,
"random": 5,
"clear": 6,
}
2024-02-28 17:44:06 +01:00
setbutton_pos()
2024-02-28 18:52:11 +01:00
buttonhovered = [False, False, False, False, False, False, False]
buttons_to_render = 6 # button begrenzung
2024-02-28 17:44:06 +01:00
dropdown = True
2024-02-28 18:52:11 +01:00
before_buttonhovered = [False, False, False]
2024-02-28 17:44:06 +01:00
key_counter = 0
pressed_keys = pygame.key.get_pressed()
show_debug = False
running = True
if __name__ == "__main__":
while running:
## key managment
key_counter += 1
if key_counter == 5:
key_counter = 0
pressed_keys = pygame.key.get_pressed()
if True in pressed_keys:
if pressed_keys[pygame.K_ESCAPE]:
exit()
2024-02-28 18:52:11 +01:00
if (
pressed_keys[pygame.K_PLUS]
and not before_pressed_keys[pygame.K_PLUS]
):
2024-02-28 17:44:06 +01:00
pixelsize += 2
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = resize_feld(feld, pixelsize)
if (
pressed_keys[pygame.K_MINUS]
and not before_pressed_keys[pygame.K_MINUS]
):
2024-02-28 17:44:06 +01:00
pixelsize -= 2
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = resize_feld(feld, pixelsize)
2024-02-28 17:44:06 +01:00
if pressed_keys[pygame.K_d] and not before_pressed_keys[pygame.K_d]:
show_debug = not show_debug
2024-02-28 18:52:11 +01:00
if (
pressed_keys[pygame.K_SPACE]
and not before_pressed_keys[pygame.K_SPACE]
):
2024-02-28 17:44:06 +01:00
pause = not pause
if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]:
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = make_feld(pixelsize, False)
2024-02-28 19:47:39 +01:00
if (
pressed_keys[pygame.K_RETURN]
and not before_pressed_keys[pygame.K_RETURN]
):
feld, feldx, feldy = make_feld(pixelsize, True)
2024-02-28 17:44:06 +01:00
## 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()
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = resize_feld(feld, pixelsize)
2024-02-28 17:44:06 +01:00
setbutton_pos()
2024-02-28 18:52:11 +01:00
draw_feld(feld, pixelsize) # male feld mit color_key
2024-02-28 17:44:06 +01:00
mx, my = pygame.mouse.get_pos()
mousepressed = pygame.mouse.get_pressed()
2024-02-28 18:52:11 +01:00
for button in buttons[:buttons_to_render]: # male jeden button
2024-02-28 17:44:06 +01:00
button.draw()
if True in mousepressed:
2024-02-28 18:52:11 +01:00
for i, button in enumerate(buttons[:buttons_to_render]):
2024-02-28 17:44:06 +01:00
# 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
2024-02-28 18:52:11 +01:00
if not True in buttonhovered: # wenn nicht maus über button
2024-02-28 17:44:06 +01:00
if mousepressed[0] == True:
feld[my // pixelsize][mx // pixelsize] = True
else:
feld[my // pixelsize][mx // pixelsize] = False
2024-02-28 18:52:11 +01:00
if (
mousepressed[0] and not before_mousepressed[0]
): # einmalklick von maus erfassen
if buttonhovered[
buttona["dropdown"]
]: # falls button "dropdown" mit index von buttona = True
2024-02-28 17:44:06 +01:00
dropdown = not dropdown
if dropdown:
2024-02-28 18:52:11 +01:00
buttons_to_render = len(
buttons
) # dropdown menü begrenzen indem man nicht alle buttons verarbeitet
2024-02-28 17:44:06 +01:00
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"]]:
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = make_feld(pixelsize, False)
2024-02-28 17:44:06 +01:00
if buttonhovered[buttona["zoom"]]:
pixelsize += 2
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = resize_feld(
feld, pixelsize, False
) # make_feld(pixelsize,False)
2024-02-28 17:44:06 +01:00
if buttonhovered[buttona["clear"]]:
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = make_feld(pixelsize, False)
2024-02-28 17:44:06 +01:00
if buttonhovered[buttona["random"]]:
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = make_feld(pixelsize, True)
2024-02-28 17:44:06 +01:00
if buttonhovered[buttona["pause"]]:
pause = not pause
if mousepressed[2] and not before_mousepressed[2]:
if buttonhovered[buttona["zoom"]]:
pixelsize -= 2
2024-02-28 18:52:11 +01:00
feld, feldx, feldy = resize_feld(
feld, pixelsize, False
) # make_feld(pixelsize,False)
2024-02-28 17:44:06 +01:00
2024-02-28 18:52:11 +01:00
before_buttonhovered = (
buttonhovered # bevorige maus und button hover daten erfassen
)
2024-02-28 17:44:06 +01:00
before_mousepressed = mousepressed
before_pressed_keys = pressed_keys
if not pause:
2024-02-28 18:52:11 +01:00
feld = verarbeite_feld(feld) # feld neu machen
if show_debug: # debug text zeichnen
2024-02-28 17:44:06 +01:00
get_debug_text()
## bildschirm aktuallisierung
pygame.display.flip()
clock.tick(60)
screen.fill((0, 0, 0))