formatiert

This commit is contained in:
Michael S. 2024-02-23 17:31:58 +01:00
parent 1971b72419
commit e3078e876f

113
main.py
View file

@ -3,43 +3,56 @@ import pygame
import pygame.gfxdraw import pygame.gfxdraw
import copy import copy
import random import random
pygame.init() pygame.init()
## funktionen ## funktionen
def get_debug_text(): def get_debug_text():
def text(text,line_counter): def text(text, line_counter):
debug_text = default_font.render(str(text), True, (255,255,255)) debug_text = default_font.render(str(text), True, (255, 255, 255))
screen.blit(debug_text,(0,line_counter*default_font.get_height())) screen.blit(debug_text, (0, line_counter * default_font.get_height()))
line_counter = 1 line_counter = 1
globalvars = globals() globalvars = globals()
for a in globalvars: for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}",line_counter) text(f"{str(a)} = {str(globalvars[a])}", line_counter)
line_counter += 1 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): def centered_text(text=str, pos=tuple, color=tuple):
#print(feld) 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)
for a in range(len(feld[0])): for a in range(len(feld[0])):
for b in range(len(feld)): for b in range(len(feld)):
if feld[b][a] != " ": if feld[b][a] != " ":
pygame.draw.rect(screen,color_key[feld[b][a]], pygame.draw.rect(
(a*block_size, screen,
b*block_size, color_key[feld[b][a]],
block_size, (a * block_size, b * block_size, block_size, block_size),
block_size)) )
def get_block_size(): def get_block_size():
return 20,20 return 20, 20
def get_meightboars(feld,position):
def get_meightboars(feld, position):
nachtbaren = [] nachtbaren = []
return nachtbaren return nachtbaren
def save_list(liste,index1,index2):
def save_list(liste, index1, index2):
if index2 > len(liste[0]) and index1 > len(liste): if index2 > len(liste[0]) and index1 > len(liste):
return liste[index1][index2] return liste[index1][index2]
else: else:
return "a" return "a"
def verarbeite_feld(feld): def verarbeite_feld(feld):
feld2 = copy.deepcopy(feld) feld2 = copy.deepcopy(feld)
for y in range(len(feld)): for y in range(len(feld)):
@ -47,45 +60,48 @@ def verarbeite_feld(feld):
poschar = feld[y][x] poschar = feld[y][x]
if poschar == "a": if poschar == "a":
# uberprüfe unteren block # uberprüfe unteren block
bellow = feld[(y+1)%feldy][x] bellow = feld[(y + 1) % feldy][x]
bellowr = feld2[(y+1)%feldy][(x+1)%feldx] bellowr = feld2[(y + 1) % feldy][(x + 1) % feldx]
bellowl = 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 direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung
if bellow == " " and not bellow == "#": if bellow == " " and not bellow == "#":
feld2[y][x] = feld[(y+1)%feldy][x] feld2[y][x] = feld[(y + 1) % feldy][x]
feld2[(y+1)%feldy][x] = feld[y][x] feld2[(y + 1) % feldy][x] = feld[y][x]
elif bellowr == " " and not bellowr == "#": elif bellowr == " " and not bellowr == "#":
feld2[y][x] = feld[(y+1)%feldy][(x+direction)%feldx] feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
feld2[(y+1)%feldy][(x+direction)%feldx] = feld[y][x] feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
elif bellowl == " " and not bellowl == "#": elif bellowl == " " and not bellowl == "#":
feld2[y][x] = feld[(y+1)%feldy][(x+direction)%feldx] feld2[y][x] = feld[(y + 1) % feldy][(x + direction) % feldx]
feld2[(y+1)%feldy][(x+direction)%feldx] = feld[y][x] feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
return feld2 return feld2
def make_feld(size,mode):
x = screensize[0]//size def make_feld(size, mode):
y = screensize[1]//size x = screensize[0] // size
y = screensize[1] // size
feld = [[" " for _ in range(x)] for _ in range(y)] feld = [[" " for _ in range(x)] for _ in range(y)]
if mode == "border": if mode == "border":
for x in range(x): for x in range(x):
feld[y-1][x] = "#" feld[y - 1][x] = "#"
return feld ,x,y return feld, x, y
## klassen ## klassen
## variablen ## variablen
screen = pygame.display.set_mode((800,500),pygame.RESIZABLE) screen = pygame.display.set_mode((800, 500), pygame.RESIZABLE)
pygame.display.set_caption("SAND") pygame.display.set_caption("SAND")
screensize = pygame.display.get_window_size() 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)
display_mode = "border" display_mode = "border"
feld,feldx,feldy = make_feld(20,display_mode) feld, feldx, feldy = make_feld(20, display_mode)
acolor = (255,0,0) acolor = (255, 0, 0)
wechselfarbe = "r" wechselfarbe = "r"
using_element = "a" using_element = "a"
pixelsize = 20 pixelsize = 20
@ -101,10 +117,10 @@ if __name__ == "__main__":
exit() exit()
if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]: if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]:
pixelsize += 2 pixelsize += 2
feld,feldx,feldy = make_feld(pixelsize,display_mode) feld, feldx, feldy = make_feld(pixelsize, display_mode)
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]: if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]:
pixelsize -= 2 pixelsize -= 2
feld,feldx,feldy = make_feld(pixelsize,display_mode) feld, feldx, feldy = make_feld(pixelsize, display_mode)
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]: if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug show_debug = not show_debug
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]: if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
@ -117,7 +133,7 @@ if __name__ == "__main__":
display_mode = "loop" display_mode = "loop"
else: else:
display_mode = "border" display_mode = "border"
feld,feldx,feldy = make_feld(pixelsize,display_mode) feld, feldx, feldy = make_feld(pixelsize, display_mode)
preview_pressed_keys = pressed_keys preview_pressed_keys = pressed_keys
## event managment ## event managment
@ -126,8 +142,8 @@ 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 = make_feld(pixelsize,display_mode) feld, feldx, feldy = make_feld(pixelsize, display_mode)
r,g,b = acolor r, g, b = acolor
if wechselfarbe == "r": if wechselfarbe == "r":
r -= 1 r -= 1
g += 1 g += 1
@ -143,15 +159,14 @@ if __name__ == "__main__":
r += 1 r += 1
if b == 0: if b == 0:
wechselfarbe = "r" wechselfarbe = "r"
acolor = r,g,b acolor = r, g, b
color_key = {"#":(100,100,100), color_key = {"#": (100, 100, 100), "a": acolor}
"a":acolor} draw_feld(feld, color_key, pixelsize)
draw_feld(feld,color_key,pixelsize) mx, my = pygame.mouse.get_pos()
mx,my = pygame.mouse.get_pos()
mousepressed = pygame.mouse.get_pressed()[0] mousepressed = pygame.mouse.get_pressed()[0]
if mousepressed : if mousepressed:
feld[my//pixelsize][mx//pixelsize] = using_element feld[my // pixelsize][mx // pixelsize] = using_element
preview_mousepressed = pygame.mouse.get_pressed()[0] preview_mousepressed = pygame.mouse.get_pressed()[0]
feld = verarbeite_feld(feld) feld = verarbeite_feld(feld)
@ -160,4 +175,4 @@ if __name__ == "__main__":
## bildschirm aktuallisierung ## bildschirm aktuallisierung
pygame.display.flip() pygame.display.flip()
clock.tick(30) clock.tick(30)
screen.fill((0,0,0)) screen.fill((0, 0, 0))