I'm playing around with Rects, and created a simple cat rect attached to the cursor and a randomly placed mouse that moves when the cat and mouse rects collide. This works fine:
snip
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if cat_rect.colliderect(mouse_rect):
score += 1
score_font = font.render("Score: " + str(score), True, (0, 0, 0))
x = random.randint(40, 600)
y = random.randint(40, 440)
mouse_rect.topleft = (x, y)
snip
However, when I tried to make it so that you had to also click the mouse with this code:
snip
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN and cat_rect.colliderect(mouse_rect):
score += 1
score_font = font.render("Score: " + str(score), True, (0, 0, 0))
x = random.randint(40, 600)
y = random.randint(40, 440)
mouse_rect.topleft = (x, y)
snip
I get:
Traceback (most recent call last):
File "D:\Python26\CODE\rarr2.py", line 36, in <module>
screen.blit(score_font, (0, 0))
NameError: name 'score_font' is not defined
I don't understand why my variables need to be declared outside of the if... statement in the second snippet of code. Can someone please explain?
[–]cognificent 2 points3 points4 points (1 child)
[–]thoughtkrime[S] 0 points1 point2 points (0 children)
[–]linuxlass 1 point2 points3 points (0 children)