so I made a simple game where boxes fall from the top of the screen and you have the dodge them from the bottom of the screen. When a box falls to the bottom of the screen without hitting you get a point.

The part I'm struggling with is how to change the score variable. For now, I have two for loops looping through a dictionary where I stored all my bullet instances, once when updating the bullets and the other for checking if a bullet has touched the ground and if it did, score += 1. I don't really like this structure because looping through the exact same dictionary twice seems stupid and might slow down the speed with much more complex programs. The problem is that the updating loop is inside my Bullet class and the other is in the main game function, with score as local variable.

A solution to this would be to change score to a global variable but I want to keep the variable in the game function since it's a variable that belongs to the game and also, the guy who taught me python told me to avoid global variables, and try to pass variables through functions

1 Answer 1


Firstly you do not need to make your variable global, but define accessor and settor functions as applicable.

Secondly, you could make use of an observer pattern (possibly with event aggregator) to observe the state of your bullets, when the state is updated to the one which indicates that it can no longer hit by the player (perhaps out of range, or missed, whatever) a score change event is raised (in this case, a positive event) your score which subscribes to score change events, is then notified of the positive change and should update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.