From b1b8dd803a8f6f33ede3444a606e32daa451eaaf Mon Sep 17 00:00:00 2001 From: Agathe Date: Thu, 14 Sep 2023 15:28:44 +0200 Subject: [PATCH] Continue the implementation of the game --- screens/game.kv | 54 ++++++++++++---------- screens/game.py | 50 +++++++++++++------- tools/kivy_tools/image_with_text_button.kv | 20 ++++---- tools/kivy_tools/image_with_text_button.py | 14 +++--- tools/kivy_tools/screen.py | 9 +++- tools/postrias.py | 9 ++-- 6 files changed, 92 insertions(+), 64 deletions(-) diff --git a/screens/game.kv b/screens/game.kv index 9628053..1a6e845 100644 --- a/screens/game.kv +++ b/screens/game.kv @@ -2,6 +2,7 @@ #:import PATH_IMAGES tools.path.PATH_IMAGES #:import PATH_TEXT_FONT tools.path.PATH_TEXT_FONT #:import TEXT_FONT_COLOR tools.constants.TEXT_FONT_COLOR +#:import partial functools.partial #:set center_x_faction 0.11 #:set center_x_resource 0.95 #:set upper_y_indic 0.93 @@ -151,42 +152,35 @@ pos_hint: {"center_x": 0.5, "center_y": center_img_center_y} allow_stretch: True keep_ratio:False - text: root.decision_text text_font_name: root.font_name text_color: TEXT_FONT_COLOR # Decision left - ImageWithText: + ImageWithTextButton: id: decision_no source: PATH_IMAGES + "decision_choice_background.png" size_hint: side_img_width, side_img_height pos_hint: {"center_x": side_img_center_x, "center_y":side_img_center_y} allow_stretch: True keep_ratio: False - text: root.decision_no_text text_font_name: root.font_name text_color: TEXT_FONT_COLOR - Button: - size_hint: side_img_width, side_img_height - pos_hint: {"center_x": side_img_center_x, "center_y":side_img_center_y+0.1} - background_color: (0,1,0,0.5) - on_release: - root.choose_answer("left") + release_function: partial(root.choose_answer, "left") # Decision right - ImageWithText: + ImageWithTextButton: id: decision_yes source: PATH_IMAGES + "decision_choice_background.png" size_hint: side_img_width, side_img_height pos_hint: {"center_x": 1-side_img_center_x, "center_y":side_img_center_y} allow_stretch: True keep_ratio: False - text: root.decision_yes_text text_font_name: root.font_name text_color: TEXT_FONT_COLOR + release_function: partial(root.choose_answer, "right") # Decision guillotine - Image: + ImageWithTextButton: id: decision_guillotine source: PATH_IMAGES + "guillotine.png" size_hint: None, 0.15 @@ -194,53 +188,63 @@ allow_stretch: True keep_ratio: False width: self.height + release_function: partial(root.choose_answer, "down") # Event background - ImageWithText: + ImageWithTextButton: id: event source: PATH_IMAGES + "event_background.png" size_hint: center_img_width, center_img_height pos_hint: {"center_x": 0.5, "center_y": center_img_center_y} allow_stretch: True - keep_ratio:False - text: root.event_text + keep_ratio: False text_font_name: root.font_name text_color: TEXT_FONT_COLOR + release_function: root.go_to_next_card # Decree background - ImageWithText: + ImageWithTextButton: id: decree_center source: PATH_IMAGES + "decree_background.png" size_hint: center_img_width, center_img_height pos_hint: {"center_x": 0.5, "center_y": center_img_center_y} allow_stretch: True - keep_ratio:False - text: root.decree_center_text + keep_ratio: False text_font_name: root.font_name text_color: TEXT_FONT_COLOR # Decree left - ImageWithText: + ImageWithTextButton: id: decree_left source: PATH_IMAGES + "decree_choice_background.png" size_hint: side_img_width, side_img_height pos_hint: {"center_x": side_img_center_x, "center_y":side_img_center_y} allow_stretch: True - keep_ratio:False - text: root.decree_left_text + keep_ratio: False text_font_name: root.font_name text_color: TEXT_FONT_COLOR + release_function: partial(root.choose_answer, "left") # Decree right - ImageWithText: + ImageWithTextButton: id: decree_right source: PATH_IMAGES + "decree_choice_background.png" size_hint: side_img_width, side_img_height pos_hint: {"center_x": 1-side_img_center_x, "center_y":side_img_center_y} allow_stretch: True - keep_ratio:False - text: root.decree_right_text + keep_ratio: False text_font_name: root.font_name text_color: TEXT_FONT_COLOR + release_function: partial(root.choose_answer, "right") - + # Decree down + ImageWithTextButton: + id: decree_down + source: PATH_IMAGES + "decree_choice_background.png" + size_hint: side_img_width, side_img_height + pos_hint: {"center_x": 0.5, "center_y": 0.1} + allow_stretch: True + keep_ratio: False + text_font_name: root.font_name + text_color: TEXT_FONT_COLOR + release_function: partial(root.choose_answer, "down") diff --git a/screens/game.py b/screens/game.py index 9baf60e..24afe32 100644 --- a/screens/game.py +++ b/screens/game.py @@ -21,7 +21,6 @@ PATH_TEXT_FONT, PATH_IMAGES, ) -from tools.constants import TEXT from tools import ( music_mixer, game @@ -49,18 +48,8 @@ class GameScreen(ImprovedScreen): weapons_value = StringProperty("0") tools_value = StringProperty("0") - # Create text for decisions - decision_text = StringProperty() - decision_no_text = StringProperty() - decision_yes_text = StringProperty() - - # Create text for events - event_text = StringProperty() - - # Create text for decrees - decree_center_text = StringProperty(TEXT.game["decree"]) - decree_right_text = StringProperty() - decree_left_text = StringProperty() + # Boolean indicating if the current moment is an answer or not + is_answer = False def hide_cards(self, *_): """ @@ -73,7 +62,8 @@ def hide_cards(self, *_): "decision_guillotine", "decree_center", "decree_left", - "decree_right"] + "decree_right", + "decree_down"] for card in cards_list: self.disable_widget(self.ids[card]) @@ -91,7 +81,8 @@ def __init__(self, **kw): self.decree_cards = ["decree_center", "decree_left", - "decree_right"] + "decree_right", + "decree_down"] self.event_cards = ["event"] @@ -100,6 +91,7 @@ def on_pre_enter(self, *args): # Load the gameplay json game.load_resources() game.reset_variables() + self.update_display_resources() # Hide all cards self.hide_cards() @@ -127,6 +119,7 @@ def display_card(self, *args): self.ids["decree_center"].text = game.text_dict["card"] self.ids["decree_left"].text = game.text_dict["left"] self.ids["decree_right"].text = game.text_dict["right"] + self.ids["decree_down"].text = game.text_dict["down"] for card in self.decree_cards: self.enable_widget(self.ids[card]) if game.phase == "event": @@ -137,6 +130,7 @@ def display_card(self, *args): def choose_answer(self, choice: Literal["left", "right", "down"], *args): game.make_choice(choice=choice) game.end_day() + self.display_answer() self.start_day() def update_display_resources(self): @@ -151,11 +145,35 @@ def update_display_resources(self): self.weapons_value = str(game.weapons) self.tools_value = str(game.tools) + def display_answer(self): + """ + Display the card containing the answer to the previous card. + It also displays the effects of the decision or the event. TODO + """ + self.is_answer = True + self.update_display_resources() + + def go_to_next_card(self): + """ + Go to the next card when clicking on the next button. + When an answer is currently displayed, we'll go to the next day. + When an event is displayed, we'll go to the answer to this answer. + """ + if self.is_answer: + self.is_answer = False + self.start_day() + else: + game.end_day() + self.display_answer() + def start_day(self, *args): + """ + Start a new day with a new batch of cards. + """ if not game.game_over: game.start_day() + self.hide_cards() self.display_card() - self.update_display_resources() else: self.update_display_resources() self.manager.current = "game_over" diff --git a/tools/kivy_tools/image_with_text_button.kv b/tools/kivy_tools/image_with_text_button.kv index fc1af99..c234ecf 100644 --- a/tools/kivy_tools/image_with_text_button.kv +++ b/tools/kivy_tools/image_with_text_button.kv @@ -1,22 +1,22 @@ #:kivy 2.1.0 : - Image: - id: image_widget - source: root.image_source - pos_hint: root.image_pos_hint - size_hint: root.image_size_hint - allow_stretch: True - keep_ratio: False Label: id: text_widget - text: root.label_text + text: root.text size: root.size pos: root.pos shorten: False text_size: (root.width*root.text_filling_ratio,None) - color: root.label_text_color - font_name: root.label_font_name + color: root.text_color + font_name: root.text_font_name font_size: root.text_font_size * root.parent.font_ratio halign: root.text_halign valign: root.text_valign + Button: + id: button_widget + size: root.size + pos: root.pos + background_color: (0, 0, 0, 0) + on_release: + root.release_function() diff --git a/tools/kivy_tools/image_with_text_button.py b/tools/kivy_tools/image_with_text_button.py index 7e1e61e..38a611b 100644 --- a/tools/kivy_tools/image_with_text_button.py +++ b/tools/kivy_tools/image_with_text_button.py @@ -8,7 +8,7 @@ ### Kivy imports ### -from kivy.uix.button import Button +from kivy.uix.image import Image from kivy.properties import ( StringProperty, ObjectProperty @@ -19,19 +19,17 @@ ############# -class ImageWithTextButton(Button): +class ImageWithTextButton(Image): """ Image class with a text label on it and a transparent button. """ # Add new attributes to manage the text - image_source = StringProperty() - image_size_hint = ObjectProperty((1, 1)) - image_pos_hint = ObjectProperty({"x":0, "y": 0}) - label_text = StringProperty() - label_font_name = StringProperty("Roboto") - label_text_color = ObjectProperty([0, 1, 0, 1]) + text = StringProperty() + text_font_name = StringProperty("Roboto") + text_color = ObjectProperty([0, 1, 0, 1]) text_filling_ratio = 0.9 text_halign = "center" text_valign = "center" text_font_size = 15 + release_function = ObjectProperty() diff --git a/tools/kivy_tools/screen.py b/tools/kivy_tools/screen.py index c860edd..180398b 100644 --- a/tools/kivy_tools/screen.py +++ b/tools/kivy_tools/screen.py @@ -11,6 +11,7 @@ from kivy.core.window import Window from kivy.uix.screenmanager import Screen from kivy.uix.widget import Widget +from kivy.uix.button import Button from kivy.properties import ( StringProperty, NumericProperty, @@ -131,11 +132,15 @@ def disable_widget(self, widget: Widget): Disable the given widget. """ widget.opacity = 0 - widget.disabled = True + if len(widget.children) == 2 and type(widget.children[0]) == Button: + print("toto") + widget.children[0].disabled = True def enable_widget(self, widget: Widget): """ Enable the given widget. """ widget.opacity = 1 - widget.disabled = False + if len(widget.children) == 2 and type(widget.children[0]) == Button: + widget.children[0].disabled = False + print(widget.children[0].disabled) diff --git a/tools/postrias.py b/tools/postrias.py index 60089ec..7832118 100644 --- a/tools/postrias.py +++ b/tools/postrias.py @@ -235,7 +235,7 @@ def make_choice(self, choice: Literal["left", "down", "right"]): if choice == "left": choice = "no" elif choice == "down": - choice = "execute" + choice = "guillotine" elif choice == "right": choice = "yes" @@ -257,7 +257,10 @@ def make_choice(self, choice: Literal["left", "down", "right"]): if self.phase in ("decree", "event"): self.text_dict["answer"] = TEXT.answer[self.phase] elif self.phase == "decision": - self.text_dict["answer"] = \ - TEXT.answer[choice][self.gameplay["decision"][self.card_id]["complainant"]] + if choice in ["yes", "no"]: + self.text_dict["answer"] = \ + TEXT.answer[choice][self.gameplay["decision"][self.card_id]["complainant"]] + else: + self.text_dict["answer"] = TEXT.answer[choice] game = Game() \ No newline at end of file