"""Undo Doodle (P) Programmieren Sie das Zeichenprogramm wie im Skript beschrieben auf Basis Ihrer Klasse Game. Der Zustand (Liste von Strichen) und die Historie werden nie verändert, sondern bei jedem Strich neu erzeugt.""" import pygame from game import Game WHITE = (255, 255, 255) BLACK = (0, 0, 0) class Doodle(Game): def __init__(self): super().__init__("drawing game!") self.points = [] self.line = [] self.history = [] self.mouse_pressed = False def handle_event(self, event): if not super().handle_event(event): return False if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.mouse_pressed = True self.points = [event.pos] if event.type == pygame.MOUSEMOTION: if self.mouse_pressed: self.points = self.points + [event.pos] if event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and self.mouse_pressed: self.mouse_pressed = False self.points = self.points + [event.pos] self.history = self.history + [self.line] self.line = self.line + [self.points] self.points = [] if event.type == pygame.KEYDOWN: if event.key == pygame.K_z: self.undo() return True def undo(self): if self.history: self.line = self.history[-1] self.history = self.history[:-1] def draw_game(self): self.screen.fill(WHITE) for line in self.line: if len(line) >=2: pygame.draw.lines(self.screen, BLACK, False, line, 2) elif len(line) ==1: pygame.draw.circle(self.screen, BLACK, line[0],2, 2) if len(self.points) >= 2: pygame.draw.lines( self.screen, BLACK, False, self.points, 2 ) elif len(self.points) == 1: pygame.draw.circle( self.screen, BLACK, self.points[0], 2 ) pygame.display.flip() if __name__ == "__main__": doodle = Doodle() doodle.run()