import pygame from game import Game class Doodle(Game): def __init__(self): super().__init__("Undo Doodle") self.strokes = [] self.history = [] self.current_stroke = [] def handle_event(self, event): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: self.current_stroke = [event.pos] if ( event.type == pygame.MOUSEMOTION and event.buttons[0] == 1 and self.current_stroke ): self.current_stroke.append(event.pos) if event.type == pygame.MOUSEBUTTONUP and event.button == 1: if self.current_stroke: self.history = (self.history + [self.strokes])[-5:] self.strokes = self.strokes + [self.current_stroke] self.current_stroke = [] if event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE: if self.history: self.strokes = self.history[-1] self.history = self.history[:-1] return super().handle_event(event) def draw_game(self): self.screen.fill("white") for stroke in self.strokes + [self.current_stroke]: if len(stroke) >= 2: pygame.draw.lines(self.screen, "black", False, stroke, 3) super().draw_game() if __name__ == "__main__": Doodle().run()