From c46f8a809e35206ec55a757ce7b8ac882d2ba5f5 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 13 Aug 2022 21:46:39 +0200 Subject: [PATCH] Add color and size change for pen --- test_kivy_draw.py | 58 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/test_kivy_draw.py b/test_kivy_draw.py index 53878e9..71edb32 100755 --- a/test_kivy_draw.py +++ b/test_kivy_draw.py @@ -39,7 +39,9 @@ class ScrollableLabel(ScrollView): class MyPaintWidget(Label): _game_state = {"game_started": False, "game_paused": False, - "game_finished": False + "game_finished": False, + "word passed": timedelta(0), + "word found": timedelta(0) } _game_wordlists = {"possible_wordlist": set(), "found_words": set(), @@ -49,7 +51,7 @@ class MyPaintWidget(Label): "current_width": 4 } current_word = "" - + message_label = None time = timedelta(seconds=DRAWING_TIME) _popup = None @@ -118,6 +120,33 @@ class MyPaintWidget(Label): def next_word(self, passed=False): if not passed: self._game_wordlists["found_words"].add(self.current_word) + if self.message_label and self.current_word: + if passed: + self.message_label.color = '#FF2A40' + self.message_label.text = f'"{self.current_word}" a été passé !' + else: + self.message_label.color = '#5BB834' + self.message_label.text = f'"{self.current_word}" a été trouvé !' + + self.current_word = choice(list(self._game_wordlists["possible_wordlist"])) + self._game_wordlists["possible_wordlist"].discard(self.current_word) + self.clear_drawing() + + def pass_word(self): + self._game_wordlists["unfound_words"].add(self.current_word) + self.next_word(True) + if self.time - timedelta(seconds=PASS_PENALTY) > timedelta(0): + self.time -= timedelta(seconds=PASS_PENALTY) + else: + self.time = timedelta(0) + + @property + def is_game_playing(self): + return (self._game_state["game_started"] + and not self._game_state["game_paused"] + and not self._game_state["game_finished"]) + + self.current_word = choice(list(self._game_wordlists["possible_wordlist"])) self._game_wordlists["possible_wordlist"].discard(self.current_word) self.clear_drawing() @@ -194,10 +223,11 @@ class MyPaintWidget(Label): def clear_drawing(self): self.canvas.before.clear() -class SizeButton(Widget, ButtonBehavior): - def __init__(self, size=1, **kwargs): +class SizeButton(ButtonBehavior, Widget): + def __init__(self, callback, size=1, **kwargs): super().__init__(**kwargs) self.dot_size = size + self.callback = callback with self.canvas: Color(0,0,0, mode="rgb") self.bg = Rectangle(pos=self.pos, size=self.size) @@ -210,6 +240,8 @@ class SizeButton(Widget, ButtonBehavior): self.bg.size = self.size self.bind(size=redraw, pos=redraw) + def on_release(self): + self.callback(self.dot_size) class MyPaintApp(App): state = False @@ -234,7 +266,7 @@ class MyPaintApp(App): color_picker_button.bind(on_release=self.open_color_picker) self.right_row.add_widget(color_picker_button) for i in range(1,6): - size_button = SizeButton(2*i) + size_button = SizeButton(self.set_cursor_size, 2*i) self.right_row.add_widget(size_button) middle_row = BoxLayout(orientation="horizontal", size_hint=(1, 0.8)) @@ -246,16 +278,27 @@ class MyPaintApp(App): bottom_row.add_widget(clearbtn) top_row = BoxLayout(orientation='horizontal', spacing=5, size_hint=(1, .1)) - self.word_label = Label(text="Mot:", color=[0,0,0,1], halign="left") + self.word_label = Label(text="Mot:", color=[0,0,0,1], halign="left", valign="top") + def redraw(self, obj): + self.text_size = obj + self.word_label.bind(size=redraw) + self.time_label = Label(text=f"{self.painter.time}", color=[0,0,0,1]) + message_label = Label(color=[0,0,0,1]) top_row.add_widget(self.word_label) + top_row.add_widget(message_label) top_row.add_widget(self.time_label) + self.painter.message_label = message_label parent.add_widget(top_row) parent.add_widget(middle_row) parent.add_widget(bottom_row) return parent + def set_cursor_size(self, size): + print(size) + self.painter.pen_characteristics["current_width"] = size + def open_color_picker(self, _): clr_picker = ColorPicker() clr_picker.color = self.painter.pen_characteristics["current_color"] @@ -269,7 +312,6 @@ class MyPaintApp(App): popup.open() def update_time(self, _): - self.word_label.text_size = (self.word_label.size[0], None) if self.painter.is_game_playing: if self.painter.time > timedelta(0): self.painter.time -= timedelta(milliseconds=100) @@ -284,12 +326,14 @@ class MyPaintApp(App): if self.state: Window.clearcolor = (1, 1, 1, 1) self.painter.pen_characteristics["current_color"] = (0,0,0,1) + self.right_row.children[-1].background_color = self.painter.current_color self.word_label.color = (0,0,0,1) self.time_label.color = (0,0,0,1) self.state = False else: Window.clearcolor = (0,0,0,1) self.painter.pen_characteristics["current_color"] = (1,1,1,1) + self.right_row.children[-1].background_color = self.painter.current_color self.word_label.color = (1,1,1,1) self.time_label.color = (1,1,1,1) self.state = True