Cr1ms0n Sl4y3r
Cr1ms0n Sl4y3r

Reputation: 21

Creating a scoring system in Pygame

I'm creating a game for my manga comic franchise, Rebound Ready!, which combines basketball and Pokémon. The idea of the game is that you play mini basketball matches, and points are awarded if you score a basket (1, 2, or 3 points can be awarded depending on a few factors).

If you play through the game, before the points are awarded, there is an accuracy meter that you have to complete, which determines how effective your shot is. Once you click the meter, the tracker (which is what indicates where you have landed) will stop - if you land on one of three colours on the accuracy meter (red, green and orange), you will get a certain amount of points - red being the least, green being the most.

I have drafted a way that the point-awarding system could work. I have tried two ways of attempting this:

## Note that a, b, c, and d aren't specific values + tracker isn't the variable used in the code, but I used it here so that I could demonstrate what I was trying to do.
## Method 1
if tracker >= a and tracker <= b:
  points+x
elif tracker >= a and tracker <= b:
  points+x
elif tracker >= a and tracker <= b:
  points+x

## Method 2
if tracker >= a and tracker <= b:
 points+x

if tracker >= b and tracker <= c:
 points+x

if tracker >= c and tracker <= d:
 points+x

To make sure this did work, I wrote print(tracker), but nothing was happening. Can anyone tell me why the points variable isn't being changed by x and if there is another possible solution to what I am trying to achieve?

To see the full code for the game, click here.

Upvotes: 1

Views: 407

Answers (2)

Glenn Keates
Glenn Keates

Reputation: 131

In Python, integers are immutable meaning they can't be changed and therefore you need to reassign them. At the moment, points is not actually changing. You are doing the addition but not actually storing the result of it back to points

There are 2 options for achieving the above: points = points + a or points += a

Neither option is necessarily better, so it's up to you which one you use.

I also recommend using Method 1 instead of Method 2 as it will come out of the if-elif-else statement after the first true statement, preventing unnecessary evaluations

Upvotes: 1

Joshua Voskamp
Joshua Voskamp

Reputation: 2054

You're not actually changing the value of points: use points = points + x, or shorter, points += x.

Also note, Method 1 uses the same conditions each time; to work, it would need to be

if tracker >= a and tracker < b:
  points += x
elif tracker >= b and tracker < c:
  points += x
elif tracker >= c and tracker <= d:
  points += x

I would recommend Method 1 over Method 2, in particular because

  • when you use >= and <= as your boundary conditions in every comparison, and make each comparison as a standalone if, you may double-count points: if tracker == b, then both tracker <= b (in the first if condition), and tracker >= b (in the second if condition) will be True: result would be add x to points twice.
  • tracker will only ever have one value; comparing it in multiple ifs instead of if-elif-else will always make every comparison.

Upvotes: 2

Related Questions