rules in extra datei

This commit is contained in:
Michael S. 2024-02-28 19:47:39 +01:00
parent c110dd67c7
commit 79a048dfd6
4 changed files with 57 additions and 33 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
__pycache__
*.swp

View file

@ -1,39 +1,52 @@
import pygame import pygame
class Button():
class Button:
def __init__(self, screen, color, pos, width, height, text) -> None: def __init__(self, screen, color, pos, width, height, text) -> None:
self.pos = pos self.pos = pos
self.größe = width, height self.größe = width, height
self.color = color self.color = color
self.screen = screen self.screen = screen
self.text = text self.text = text
def draw(self): def draw(self):
pygame.draw.rect(self.screen, pygame.draw.rect(
self.screen,
self.color, self.color,
(self.pos[0]-self.größe[0]//2, (
self.pos[0] - self.größe[0] // 2,
self.pos[1] - self.größe[1] // 2, self.pos[1] - self.größe[1] // 2,
self.größe[0], self.größe[0],
self.größe[1]) self.größe[1],
),
) )
pygame.draw.rect(self.screen, pygame.draw.rect(
self.screen,
(0, 150, 0), (0, 150, 0),
(self.pos[0]-self.größe[0]//2, (
self.pos[0] - self.größe[0] // 2,
self.pos[1] - self.größe[1] // 2, self.pos[1] - self.größe[1] // 2,
self.größe[0], self.größe[0],
self.größe[1]), self.größe[1],
2 ),
2,
) )
font = pygame.font.Font(None, 30) font = pygame.font.Font(None, 30)
text_surface = font.render(self.text, True, (0, 0, 0)) text_surface = font.render(self.text, True, (0, 0, 0))
text_rect = text_surface.get_rect() text_rect = text_surface.get_rect()
text_rect.center = (self.pos[0], text_rect.center = (self.pos[0], self.pos[1])
self.pos[1])
self.screen.blit(text_surface, text_rect) self.screen.blit(text_surface, text_rect)
def check_hovered(self, mouse): def check_hovered(self, mouse):
if (self.pos[0] - self.größe[0]//2 <= mouse[0] <= self.pos[0] + self.größe[0]//2 and if (
self.pos[1] - self.größe[1]//2 <= mouse[1] <= self.pos[1] + self.größe[1]//2): 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 return True
else: else:
return False return False

19
main.py
View file

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pygame import pygame
from button import * from button import Button
from rules import pixel
import copy import copy
import random import random
@ -60,16 +61,7 @@ def verarbeite_feld(feld):
for pos in finde_nachbarn(x, y): for pos in finde_nachbarn(x, y):
if feld[pos[0]][pos[1]]: if feld[pos[0]][pos[1]]:
lives += 1 lives += 1
if feld[y][x]: new = pixel(feld[y][x], lives)
if lives <= 1 or lives >= 4:
new = False
else:
new = True
else:
if lives == 3:
new = True
else:
new = False
feld2[y][x] = new feld2[y][x] = new
return feld2 # gib kopie zurück return feld2 # gib kopie zurück
@ -183,6 +175,11 @@ if __name__ == "__main__":
pause = not pause pause = not pause
if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]: if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]:
feld, feldx, feldy = make_feld(pixelsize, False) feld, feldx, feldy = make_feld(pixelsize, False)
if (
pressed_keys[pygame.K_RETURN]
and not before_pressed_keys[pygame.K_RETURN]
):
feld, feldx, feldy = make_feld(pixelsize, True)
## event managment ## event managment
for event in pygame.event.get(): for event in pygame.event.get():

12
rules.py Normal file
View file

@ -0,0 +1,12 @@
def pixel(state, lives):
if state:
if lives <= 1 or lives >= 4:
new = False
else:
new = True
else:
if lives == 3:
new = True
else:
new = False
return new