Compare commits

...

2 commits

Author SHA1 Message Date
9d37e2464c aff 2024-02-23 19:35:20 +01:00
e3078e876f formatiert 2024-02-23 17:31:58 +01:00
3 changed files with 95 additions and 58 deletions

Binary file not shown.

31
button.py Normal file
View file

@ -0,0 +1,31 @@
import pygame
class Button():
def __init__(self, screen, color, x, y, width, height, text) -> None:
self.pos = x,y
self.größe = width,height
self.color = color
self.screen = screen
self.text = text
def draw(self):
pygame.draw.rect(self.screen,
self.color,
(self.pos[0]-self.größe[0]//2,
self.pos[1]-self.größe[1]//2,
self.größe[0],
self.größe[1])
)
font = pygame.font.Font(None, 36)
text_surface = font.render(self.text, True, (0,0,0))
text_rect = text_surface.get_rect()
text_rect.center = (self.pos[0],
self.pos[1])
self.screen.blit(text_surface, text_rect)
def check_press(self, mouse):
if (self.pos[0] - self.größe[0]//2 <= mouse[0] <= self.pos[0] + self.größe[0]//2 and
self.pos[1] - self.größe[1]//2 <= mouse[1] <= self.pos[1] + self.größe[1]//2):
return True
else:
return False

118
main.py
View file

@ -1,95 +1,95 @@
#!/usr/bin/python3
import pygame
import pygame.gfxdraw
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()))
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)
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):
#print(feld)
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 get_block_size():
return 20,20
def get_meightboars(feld,position):
nachtbaren = []
return nachtbaren
def save_list(liste,index1,index2):
if index2 > len(liste[0]) and index1 > len(liste):
return liste[index1][index2]
else:
return "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]
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]
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]
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]
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
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
feld[y - 1][x] = "#"
return feld, x, y
## klassen
## variablen
screen = pygame.display.set_mode((800,500),pygame.RESIZABLE)
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)
feld, feldx, feldy = make_feld(20, display_mode)
acolor = (255,0,0)
acolor = (255, 0, 0)
wechselfarbe = "r"
using_element = "a"
pixelsize = 20
buttons = [Button(screen,(200,200,200),100,100,30,30,"*")]
show_debug = False
running = True
if __name__ == "__main__":
@ -101,10 +101,10 @@ if __name__ == "__main__":
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)
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)
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]:
@ -117,7 +117,7 @@ if __name__ == "__main__":
display_mode = "loop"
else:
display_mode = "border"
feld,feldx,feldy = make_feld(pixelsize,display_mode)
feld, feldx, feldy = make_feld(pixelsize, display_mode)
preview_pressed_keys = pressed_keys
## event managment
@ -126,8 +126,8 @@ if __name__ == "__main__":
exit()
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
feld,feldx,feldy = make_feld(pixelsize,display_mode)
r,g,b = acolor
feld, feldx, feldy = make_feld(pixelsize, display_mode)
r, g, b = acolor
if wechselfarbe == "r":
r -= 1
g += 1
@ -143,21 +143,27 @@ if __name__ == "__main__":
r += 1
if b == 0:
wechselfarbe = "r"
acolor = r,g,b
color_key = {"#":(100,100,100),
"a":acolor}
draw_feld(feld,color_key,pixelsize)
mx,my = pygame.mouse.get_pos()
acolor = r, g, b
color_key = {"#": (100, 100, 100), "a": acolor}
draw_feld(feld, color_key, pixelsize)
mx, my = pygame.mouse.get_pos()
mousepressed = pygame.mouse.get_pressed()[0]
if mousepressed :
feld[my//pixelsize][mx//pixelsize] = using_element
for button in buttons:
button.draw()
if mousepressed:
for button in buttons:
if button.check_press((mx,my)):
show_debug = not show_debug
else:
feld[my // pixelsize][mx // pixelsize] = using_element
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)
screen.fill((0,0,0))
screen.fill((0, 0, 0))