This commit is contained in:
Michael S. 2024-02-23 19:35:20 +01:00
parent e3078e876f
commit 9d37e2464c
3 changed files with 43 additions and 21 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

33
main.py
View file

@ -1,6 +1,6 @@
#!/usr/bin/python3
import pygame
import pygame.gfxdraw
from button import *
import copy
import random
@ -26,7 +26,6 @@ def centered_text(text=str, pos=tuple, color=tuple):
def draw_feld(feld, color_key, block_size):
# print(feld)
for a in range(len(feld[0])):
for b in range(len(feld)):
if feld[b][a] != " ":
@ -36,24 +35,8 @@ def draw_feld(feld, color_key, block_size):
(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"
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])):
@ -106,6 +89,7 @@ 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__":
@ -162,14 +146,21 @@ if __name__ == "__main__":
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]
for button in buttons:
button.draw()
if mousepressed:
feld[my // pixelsize][mx // pixelsize] = using_element
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