sand/main.py
2024-02-24 21:59:53 +01:00

205 lines
7.7 KiB
Python

#!/usr/bin/python3
import pygame
from button import *
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, color_key, block_size):
for a in range(len(feld[0])):
for b in range(len(feld)):
if feld[b][a] != " ":
pygame.draw.rect(
screen,
color_key[feld[b][a]],
(a * block_size, b * block_size, block_size, 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])):
poschar = feld[y][x]
if poschar == "a":
# uberprüfe unteren block
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 == "#":
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 bellowl == " " and not bellowl == "#":
feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
return feld2
def make_feld(size, mode):
x = screensize[0] // size
y = screensize[1] // size
feld = [[" " for _ in range(x)] for _ in range(y)]
if mode == "border":
for x in range(x):
feld[y - 1][x] = "#"
return feld, x, y
## klassen
## variablen
screen = pygame.display.set_mode((800, 500), pygame.RESIZABLE)
pygame.display.set_caption("SAND")
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
default_font = pygame.font.SysFont("sans", 14)
display_mode = "border"
feld, feldx, feldy = make_feld(20, display_mode)
using_element = "a"
pixelsize = 20
button_abstandy = 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__":
while running:
## key managment
pressed_keys = pygame.key.get_pressed()
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)
mx, my = pygame.mouse.get_pos()
mousepressed = pygame.mouse.get_pressed()[0]
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(60)
screen.fill((0, 0, 0))