diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..182ed0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.swp \ No newline at end of file diff --git a/button.py b/button.py index cb368cd..99bd55c 100644 --- a/button.py +++ b/button.py @@ -1,39 +1,52 @@ import pygame -class Button(): + +class Button: def __init__(self, screen, color, pos, width, height, text) -> None: self.pos = pos - self.größe = width,height + 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]) + 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], + ), ) - pygame.draw.rect(self.screen, - (0,150,0), - (self.pos[0]-self.größe[0]//2, - self.pos[1]-self.größe[1]//2, - self.größe[0], - self.größe[1]), - 2 + pygame.draw.rect( + self.screen, + (0, 150, 0), + ( + self.pos[0] - self.größe[0] // 2, + self.pos[1] - self.größe[1] // 2, + self.größe[0], + self.größe[1], + ), + 2, ) - + 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.center = (self.pos[0], - self.pos[1]) + text_rect.center = (self.pos[0], self.pos[1]) self.screen.blit(text_surface, text_rect) + 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 - self.pos[1] - self.größe[1]//2 <= mouse[1] <= self.pos[1] + self.größe[1]//2): + 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 - diff --git a/main.py b/main.py index 48df7ee..e4308b7 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import pygame -from button import * +from button import Button +from rules import pixel import copy import random @@ -60,16 +61,7 @@ def verarbeite_feld(feld): for pos in finde_nachbarn(x, y): if feld[pos[0]][pos[1]]: lives += 1 - if feld[y][x]: - if lives <= 1 or lives >= 4: - new = False - else: - new = True - else: - if lives == 3: - new = True - else: - new = False + new = pixel(feld[y][x], lives) feld2[y][x] = new return feld2 # gib kopie zurück @@ -183,6 +175,11 @@ if __name__ == "__main__": pause = not pause if pressed_keys[pygame.K_r] and not before_pressed_keys[pygame.K_r]: 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 for event in pygame.event.get(): diff --git a/rules.py b/rules.py new file mode 100644 index 0000000..05a2c2a --- /dev/null +++ b/rules.py @@ -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