-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactored code, added new Button class, reduced rerenders #25
Conversation
created seperate functions for start and init to avoid rerendering the entire game screen every frame employed the usage of new Button class to reduce the hard coded functionalities for buttons in the main screen
… starting and running to avoid rendering the ui every frame
There's an issue I'm facing and that is the fact that in the scorewindow after a game is over when I press the |
Hello. I'm on leave right now, and was able to take a quick look.
In testing 4d6dc05, I reproduced what you described, and also an exception;
The exception was not reproducible in repeated execution unless score.pkl was removed from the activity root directory. Hope that helps! |
Interesting, thanks. 4d6dc05 was 326 flake8 messages. 17bdd8b brings it down to 240. A further I've looked at the problem you described. An unusual excursion from the Sugargame test activity is that Make Them Fall calls Later, It is not a pattern I've seen in any of the other activities, so it was a pattern introduced by this activity. (Other activities that try to make a button class in Pygame are Pycut and Math Hurdler). Further debugging will be required. 😁 |
Okay, I've found the problem with some --- a/main.py
+++ b/main.py
@@ -56,6 +55,7 @@ class game:
sound = True
def __init__(self):
+ print("game.__init__")
self.crashed = False
self.running_mode = None
self.clock = pygame.time.Clock()
@@ -69,6 +69,7 @@ class game:
self.maxscore = [0, 0, 0, 0, 0, 0]
def run_game(self, window, gamenumber):
+ print("game.run_game entry", window, gamenumber)
self.running_mode = window()
self.running_mode.crashed = self.crashed
s = self.running_mode.run(self.gameDisplay, self.info)
@@ -77,6 +78,7 @@ class game:
self.run_game(window, gamenumber)
self.start()
+ print("game.run_game return", window, gamenumber)
def show_help(self):
a = rules()
@@ -92,6 +94,7 @@ class game:
self.maxscore = pickle.load(inp)
def start(self):
+ print("game.start")
self.gameDisplay = pygame.display.get_surface()
@@ -155,9 +200,14 @@ class game:
def run(self):
+ print("game.run entry")
self.gameDisplay = pygame.display.get_surface()
while not self.crashed:
@@ -169,6 +219,7 @@ class game:
event = pygame.event.poll()
if event.type == pygame.QUIT:
+ print("game.run quit")
return
for btn in self.buttons:
@@ -178,6 +229,7 @@ class game:
pygame.display.update()
self.clock.tick(60)
+ print("game.run return")
return
Briefly, the welcome screen buttons think they have been pressed. In turn, because the score window button has just been pressed and the mouse is still down. You can see this by pressing the Back Home button in the lower right corner, which corresponds to no button on the welcome screen. The welcome screen is shown. If you click on the top right of the Back Home button, the welcome screen detects this as Cardiac game, and begins to play it. Here's the log;
A couple of ways you could fix this;
|
The button now has property press, which is turned true when button is pressed when button is released, the action is for the button is called
Sometimes the Score was none and was compared to int maxscore This would cause TypeError and cause game to crash
I have made some changes, I will be making a few commits for flake8 fixes. |
Reviewed 31af7c4, not tested. |
Please don't put in |
Let me rephrase that; use the opportunity to remove any |
I see, It was done by the original author I suppose, I haven't taken a much look in other files, except for the main.py and scorescreen.py which I refactored, I just did auto pep8 in all files because of which it was in my commits, Fixing it now though 🫡 |
- a was assigned in show_help and reassgined to self.running_mode - a has thus been eliminated - s has been renamed to score_data for more clarity of its purpose
If 861d2c9 would lead to other flake8 fixes, it'll be better to put it in a separate PR else it's fine as is. |
Sorry for the delay. Reviewed the aggregate change (not the commits), and have following comments;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, wouldn't it be better to have a generic name for the file instead of guy.py and a generic name for the class too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree with the creation of a generic Game class, these should also exist as they're supposed to handle different pygame events in different ways.
They can inherit from the generic Game class and implement their run
function to handle what needs to be handled, this also makes debugging easier as the game modes are modular.
Reviewed 1a8c7e0, not tested. Also add licenses to the necessary files. |
Reviewed 0b6a605, not tested. |
- This class acts as a generic spike and is used in Game to generate spikes as needed - The class contains logic to move the spike when updates
- Game class can be used to create a game with any configuration - This can be used to replace the hardcoded games like 2panewindow, 3panewindow etc - The class constructor takes in the background_image the key map for the controller and optional speed and optional border_width - Generator class is used in Game class to generate the spikes in game as defined by spikes_config defined for each game - It also handles the logic to delete the spikes when they have covered their allocated path
- the Guy class represents the falling guys which one may consider as the player characters - this class handles the logic for moving the guy left and right and also the collision checking
- main.py now uses the Game class to defined each game mode instead of hardcoded imports - modified the run_game function declaration and its calls according to newer methods - keymaps for each call are hardcoded and defined in the button callback lambda functions for each game mode button
- now that Game class replaces the hard coded game modes in main.py these can be deleted
Also Deleted white as it was not being used
and also moved it closer to function for better readability
- moved keymap iteration inside a conditional which checks for KEYDOWN events
Tested 77ada49, key presses aren't handled and this is seen in the logs. Traceback (most recent call last):
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 128, in _keydown_cb
return self._keyevent(widget, event, pygame.KEYDOWN)
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 169, in _keyevent
mod = self._keymods()
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 145, in _keymods
mod |= self.__keystate[key_val] and mod_val
IndexError: list index out of range
Traceback (most recent call last):
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 128, in _keydown_cb
return self._keyevent(widget, event, pygame.KEYDOWN)
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 169, in _keyevent
mod = self._keymods()
File "/home/ibiam/Activities/make-them-fall-activity/sugargame/event.py", line 145, in _keymods
mod |= self.__keystate[key_val] and mod_val
IndexError: list index out of range |
No, nothing springs to mind. |
#23 stated a lot of software design issues and issues with performance and also the architecture of the codebase
while this PR does not aim to be a direct resolution to that issue, this PR does fix a lot of problems mentioned in it