sand/main.py

229 lines
9.2 KiB
Python
Raw Normal View History

2024-02-18 22:01:54 +01:00
#!/usr/bin/python3
import pygame
2024-02-23 19:35:20 +01:00
from button import *
2024-02-18 22:01:54 +01:00
import copy
import random
2024-02-23 17:31:58 +01:00
2024-02-18 22:01:54 +01:00
pygame.init()
2024-02-23 17:31:58 +01:00
2024-02-18 22:01:54 +01:00
## funktionen
def get_debug_text():
2024-02-23 17:31:58 +01:00
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()))
2024-02-18 22:01:54 +01:00
line_counter = 1
globalvars = globals()
for a in globalvars:
2024-02-23 17:31:58 +01:00
text(f"{str(a)} = {str(globalvars[a])}", line_counter)
2024-02-18 22:01:54 +01:00
line_counter += 1
2024-02-23 17:31:58 +01:00
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):
2024-02-18 22:01:54 +01:00
for a in range(len(feld[0])):
for b in range(len(feld)):
2024-02-23 16:31:04 +01:00
if feld[b][a] != " ":
2024-02-23 17:31:58 +01:00
pygame.draw.rect(
screen,
color_key[feld[b][a]],
(a * block_size, b * block_size, block_size, block_size),
)
2024-02-24 21:59:53 +01:00
2024-02-26 21:14:50 +01:00
def finde_nachbarn(matrix, x, y):
feldx, feldy = len(feld[0]), len(feld)
over = (y - 1) % feldy , x % feldx
overr = (y - 1) % feldy , (x + 1) % feldx
overl = (y - 1) % feldy , (x - 1) % feldx
r = y , (x + 1) % feldx
l = y , (x - 1) % feldx
bellow = (y + 1) % feldy , x % feldx
bellowr = (y + 1) % feldy , (x + 1) % feldx
bellowl = (y + 1) % feldy , (x - 1) % feldx
return [overl,over, overr, r, l, bellowl, bellow, bellowr]
2024-02-18 22:01:54 +01:00
def verarbeite_feld(feld):
2024-02-25 11:35:01 +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-23 15:17:07 +01:00
poschar = feld[y][x]
2024-02-25 11:35:01 +01:00
if poschar == "a": # fals aktuelles checkendes element = "a"
# bekomme alle benötigten nachtbaren
2024-02-26 21:14:50 +01:00
nachtbaren = finde_nachbarn(feld, x ,y)
2024-02-26 21:18:54 +01:00
bellow = feld[nachtbaren[6][0]][nachtbaren[6][1]]
bellowr = feld[nachtbaren[7][0]][nachtbaren[7][1]]
bellowl = feld[nachtbaren[5][0]][nachtbaren[5][1]]
2024-02-23 15:17:07 +01:00
direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung
2024-02-25 11:35:01 +01:00
if bellow == " " and not bellow == "#": # checke unten
2024-02-23 17:31:58 +01:00
feld2[y][x] = feld[(y + 1) % feldy][x]
feld2[(y + 1) % feldy][x] = feld[y][x]
2024-02-23 15:17:07 +01:00
2024-02-25 11:35:01 +01:00
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
2024-02-23 16:55:55 +01:00
2024-02-25 11:35:01 +01:00
elif bellowl == " " and not bellowl == "#" and direction == -1: # checke unten links fals richtung nach links
2024-02-23 17:31:58 +01:00
feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
2024-02-25 11:35:01 +01:00
return feld2 # gib kopie zurück
2024-02-18 22:01:54 +01:00
2024-02-26 21:14:50 +01:00
## Funktion zum Verarbeiten des Feldes
2024-02-23 17:31:58 +01:00
def make_feld(size, mode):
x = screensize[0] // size
y = screensize[1] // size
2024-02-23 16:31:04 +01:00
feld = [[" " for _ in range(x)] for _ in range(y)]
if mode == "border":
for x in range(x):
2024-02-23 17:31:58 +01:00
feld[y - 1][x] = "#"
return feld, x, y
2024-02-25 11:35:01 +01:00
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)
2024-02-18 22:01:54 +01:00
## klassen
2024-02-23 17:31:58 +01:00
2024-02-18 22:01:54 +01:00
## variablen
2024-02-23 17:31:58 +01:00
screen = pygame.display.set_mode((800, 500), pygame.RESIZABLE)
2024-02-18 22:01:54 +01:00
pygame.display.set_caption("SAND")
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
default_font = pygame.font.SysFont("sans", 14)
2024-02-23 16:31:04 +01:00
2024-02-25 11:35:01 +01:00
display_mode = "border" # loop oder border # durchfallen oder mit boden
2024-02-23 17:31:58 +01:00
feld, feldx, feldy = make_feld(20, display_mode)
2024-02-23 16:31:04 +01:00
2024-02-18 22:01:54 +01:00
using_element = "a"
2024-02-25 11:35:01 +01:00
pixelsize = 20 # größe eines pixels
button_abstandy = 30 # abstand von buttons
2024-02-24 21:59:53 +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, "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]
2024-02-25 11:35:01 +01:00
buttons_to_render = 6 # button begrenzung
2024-02-24 21:59:53 +01:00
dropdown = True
2024-02-25 11:35:01 +01:00
before_buttonhovered = False
2024-02-25 11:43:30 +01:00
key_counter = 0
pressed_keys = pygame.key.get_pressed()
2024-02-18 22:01:54 +01:00
show_debug = False
running = True
if __name__ == "__main__":
while running:
## key managment
2024-02-25 11:43:30 +01:00
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()
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()
2024-02-18 22:01:54 +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-25 11:35:01 +01:00
remake_feld()
color_key = {"#": (100, 100, 100), "a": (200,200,80)} # element:farbe
draw_feld(feld, color_key, pixelsize) # male feld mit color_key
2024-02-23 19:35:20 +01:00
2024-02-23 17:31:58 +01:00
mx, my = pygame.mouse.get_pos()
2024-02-18 22:01:54 +01:00
mousepressed = pygame.mouse.get_pressed()[0]
2024-02-24 21:59:53 +01:00
2024-02-25 11:35:01 +01:00
for button in buttons[:buttons_to_render]: # male jeden button
2024-02-23 19:35:20 +01:00
button.draw()
2024-02-24 21:59:53 +01:00
2024-02-23 17:31:58 +01:00
if mousepressed:
2024-02-25 11:35:01 +01:00
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)):
2024-02-24 21:59:53 +01:00
buttonhovered[i] = True
2024-02-23 19:35:20 +01:00
else:
2024-02-24 21:59:53 +01:00
buttonhovered[i] = False
2024-02-25 11:35:01 +01:00
if not True in buttonhovered: # wenn nicht maus über button
2024-02-24 21:59:53 +01:00
feld[my // pixelsize][mx // pixelsize] = using_element
2024-02-25 11:35:01 +01:00
if mousepressed and not before_mousepressed: # einmalklick von maus erfassen
if buttonhovered[buttona["dropdown"]]: # falls button "dropdown" mit index von buttona = True
2024-02-24 21:59:53 +01:00
dropdown = not dropdown
if dropdown:
2024-02-25 11:35:01 +01:00
buttons_to_render = len(buttons) # dropdown menü begrenzen indem man nicht alle buttons verarbeitet
2024-02-24 21:59:53 +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"]]:
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"
2024-02-25 11:35:01 +01:00
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
2024-02-18 22:01:54 +01:00
get_debug_text()
## bildschirm aktuallisierung
pygame.display.flip()
2024-02-24 21:59:53 +01:00
clock.tick(60)
2024-02-23 17:31:58 +01:00
screen.fill((0, 0, 0))