From 143df98e05e3743988a86baf74184be5a9815844 Mon Sep 17 00:00:00 2001 From: naimabannert Date: Thu, 5 Nov 2026 12:18:31 +0100 Subject: [PATCH] Hausaufgabe --- Undo Doodle.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Undo Doodle.py diff --git a/Undo Doodle.py b/Undo Doodle.py new file mode 100644 index 0000000..f7c9b65 --- /dev/null +++ b/Undo Doodle.py @@ -0,0 +1,46 @@ +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() \ No newline at end of file