#!/usr/bin/python3 import pygame import pygame.gfxdraw 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): #print(feld) for a in range(len(feld[0])): for b in range(len(feld)): #print(a,b) if feld[b][a] == ".": print(".") #pygame.gfxdraw.box(screen,pygame.Rect( # a*block_size[0], # b*block_size[1], # (a+1)*(block_size[0]), # (b+1)*(block_size[1])),(100,100,100)) else: #pygame.gfxdraw.box(screen,pygame.Rect( # a*block_size[0], # b*block_size[1], # (a+1)*(block_size[0]), # (b+1)*(block_size[1])),color_key[feld[b][a]]) pygame.draw.rect(screen,color_key[feld[b][a]], (a*block_size[0], b*block_size[1], (a+1)*(block_size[0]), (b+1)*(block_size[1]))) def get_block_size(): return 20,20 def verarbeite_feld(feld): feld2 = copy.deepcopy(feld) for y in range(len(feld)-1): for x in range(len(feld[0])-2): x += 1 if feld[y][x] == "a": if feld[y+1][x] == " ": feld2[y][x] = feld[y+1][x] feld2[y+1][x] = feld[y][x] elif feld[y+1][x] == "#": break elif feld2[y+1][x+1] == " " or feld2[y+1][x-1] == " ": # Überprüfen, seiten direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung if feld[y+1][x+direction] == " ": feld2[y][x] = feld[y+1][x+direction] feld2[y+1][x+direction] = feld[y][x] break else: direction = -direction if feld2[y+1][x+direction] == " ": feld2[y][x] = feld[y+1][x+direction] feld2[y+1][x+direction] = feld[y][x] break else: feld2[y][x] = "." else: feld2[y][x] = "." return feld2 def make_feld(size): x = screensize[0]//size y = screensize[1]//size return [[" " for _ in range(x)] for _ in range(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) feld = make_feld(20) acolor = (255,0,0) 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 if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS]: pixelsize -= 2 if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]: show_debug = not show_debug if pressed_keys[pygame.K_RETURN] and not preview_pressed_keys[pygame.K_RETURN]: if using_element == "a": using_element = "#" else: using_element = "a" if pressed_keys[pygame.K_r] and not preview_pressed_keys[pygame.K_r]: feld = make_feld(pixelsize) print("reset") 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 = make_feld(pixelsize) r,g,b = acolor 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" acolor = r,g,b color_key = {" ":(0,0,0), #".":(0,100,100,100), "#":(100,100,100), "a":acolor} draw_feld(feld,color_key,(pixelsize,pixelsize)) mx,my = pygame.mouse.get_pos() mousepressed = pygame.mouse.get_pressed()[0] if mousepressed : 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((100,0,0))