59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import pygame
|
|
from game import Game
|
|
|
|
class DrawGame(Game):
|
|
def __init__(self):
|
|
super().__init__("Zeichenprogramm", fps=60, size=(800, 600))
|
|
self.state = []
|
|
self.history = []
|
|
self.drawing = False
|
|
self.current_stroke = []
|
|
|
|
def handle_event(self, event):
|
|
if event.type == pygame.QUIT:
|
|
return False
|
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
self.drawing = True
|
|
self.current_stroke = [event.pos]
|
|
|
|
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
|
|
if self.drawing and len(self.current_stroke) > 1:
|
|
self.push_history()
|
|
new_state = [stroke[:] for stroke in self.state]
|
|
new_state.append(self.current_stroke[:])
|
|
self.state = new_state
|
|
self.drawing = False
|
|
self.current_stroke = []
|
|
|
|
elif event.type == pygame.MOUSEMOTION and self.drawing:
|
|
self.current_stroke.append(event.pos)
|
|
|
|
elif event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE:
|
|
if self.history:
|
|
self.state = self.history.pop()
|
|
|
|
return True
|
|
|
|
def push_history(self):
|
|
snapshot = [stroke[:] for stroke in self.state]
|
|
self.history.append(snapshot)
|
|
if len(self.history) > 5:
|
|
self.history.pop(0)
|
|
|
|
def draw_game(self):
|
|
self.screen.fill((255, 255, 255))
|
|
|
|
for stroke in self.state:
|
|
if len(stroke) > 1:
|
|
pygame.draw.lines(self.screen, (0, 0, 0), False, stroke, 2)
|
|
|
|
if self.drawing and len(self.current_stroke) > 1:
|
|
pygame.draw.lines(self.screen, (0, 0, 0), False, self.current_stroke, 2)
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
DrawGame().run()
|