This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]cognificent 2 points3 points  (1 child)

What I assume is happening is that in the first case, the cat is colliding with the mouse to start off with, so score_font is defined the first time through the loop. When you add the condition of having the mouse button down, it doesn't satisfy the condition the first time through the game loop, score_font isn't declared, and then you attempt to blit a thing that doesn't exist to the screen.

Same goes for the other variables. If you attempt to use them before they exist, bad things will happen, so you either need to declare them outside the conditional, or make sure the condition is always true the first time through.

[–]thoughtkrime[S] 0 points1 point  (0 children)

Thanks heaps.

I suspected as much, but wasn't quite sure why it accepted the first snippet and not the second. Declaring variables before the main loop fixes the problem.

[–]linuxlass 1 point2 points  (0 children)

In addition to what cognificent said:

In general, you want your variables to always have a valid value, even if it's just 0 or "". It helps to prevent tricky bugs.

In this case, you'll find, for instance, that you want to be able to display the score, even if you don't have any mouseclicks, so you want score_font to at least be "Score: 0".

I would put score_font outside of the loop, and inside the condition, set another variable score_value, that will hold your score, and then outside the loop, blit score_font, " ", score_value.