sand/main.py

179 lines
5.5 KiB
Python
Raw Normal View History

2024-02-18 22:01:54 +01:00
#!/usr/bin/python3
import pygame
import pygame.gfxdraw
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):
# print(feld)
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-18 22:01:54 +01:00
def get_block_size():
2024-02-23 17:31:58 +01:00
return 20, 20
def get_meightboars(feld, position):
2024-02-23 15:17:07 +01:00
nachtbaren = []
return nachtbaren
2024-02-23 17:31:58 +01:00
def save_list(liste, index1, index2):
2024-02-23 15:17:07 +01:00
if index2 > len(liste[0]) and index1 > len(liste):
return liste[index1][index2]
else:
return "a"
2024-02-23 17:31:58 +01:00
2024-02-18 22:01:54 +01:00
def verarbeite_feld(feld):
feld2 = copy.deepcopy(feld)
2024-02-23 15:17:07 +01:00
for y in range(len(feld)):
for x in range(len(feld[0])):
poschar = feld[y][x]
if poschar == "a":
# uberprüfe unteren block
2024-02-23 17:31:58 +01:00
bellow = feld[(y + 1) % feldy][x]
bellowr = feld2[(y + 1) % feldy][(x + 1) % feldx]
bellowl = feld2[(y + 1) % feldy][(x - 1) % feldx]
2024-02-23 15:17:07 +01:00
direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung
2024-02-23 16:31:04 +01:00
if bellow == " " and not bellow == "#":
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-23 16:55:55 +01:00
elif bellowr == " " and not bellowr == "#":
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-23 16:55:55 +01:00
elif bellowl == " " and not bellowl == "#":
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-18 22:01:54 +01:00
return feld2
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-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
display_mode = "border"
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-23 17:31:58 +01:00
acolor = (255, 0, 0)
2024-02-18 22:01:54 +01:00
wechselfarbe = "r"
using_element = "a"
pixelsize = 20
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
2024-02-23 17:31:58 +01:00
feld, feldx, feldy = make_feld(pixelsize, display_mode)
2024-02-18 22:01:54 +01:00
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]:
pixelsize -= 2
2024-02-23 17:31:58 +01:00
feld, feldx, feldy = make_feld(pixelsize, display_mode)
2024-02-18 22:01:54 +01:00
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
2024-02-23 16:55:55 +01:00
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
2024-02-18 22:01:54 +01:00
if using_element == "a":
using_element = "#"
else:
using_element = "a"
if pressed_keys[pygame.K_r] and not preview_pressed_keys[pygame.K_r]:
2024-02-23 16:55:55 +01:00
if display_mode == "border":
display_mode = "loop"
else:
display_mode = "border"
2024-02-23 17:31:58 +01:00
feld, feldx, feldy = make_feld(pixelsize, display_mode)
2024-02-18 22:01:54 +01:00
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()
2024-02-23 17:31:58 +01:00
feld, feldx, feldy = make_feld(pixelsize, display_mode)
r, g, b = acolor
2024-02-18 22:01:54 +01:00
if wechselfarbe == "r":
r -= 1
g += 1
if r == 0:
wechselfarbe = "g"
if wechselfarbe == "g":
g -= 1
b += 1
if g == 0:
wechselfarbe = "b"
if wechselfarbe == "b":
b -= 1
r += 1
if b == 0:
wechselfarbe = "r"
2024-02-23 17:31:58 +01:00
acolor = r, g, b
color_key = {"#": (100, 100, 100), "a": acolor}
draw_feld(feld, color_key, pixelsize)
mx, my = pygame.mouse.get_pos()
2024-02-18 22:01:54 +01:00
mousepressed = pygame.mouse.get_pressed()[0]
2024-02-23 17:31:58 +01:00
if mousepressed:
feld[my // pixelsize][mx // pixelsize] = using_element
2024-02-18 22:01:54 +01:00
preview_mousepressed = pygame.mouse.get_pressed()[0]
feld = verarbeite_feld(feld)
if show_debug:
get_debug_text()
## bildschirm aktuallisierung
pygame.display.flip()
clock.tick(30)
2024-02-23 17:31:58 +01:00
screen.fill((0, 0, 0))