![]() |
|
variable isn't recognized by if structure - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: variable isn't recognized by if structure (/thread-14903.html) |
variable isn't recognized by if structure - Prof_Jar_Jar - Dec-23-2018 Hey there! I don't get this: I have a code that goes like this (simplified, but essentially that): def q():
loop = True
CC = 0
WC = 3
while loop == True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mposx,mposy = pygame.mouse.get_pos()
if 200 < mposx < 300 and 150 < mposy < 200:
print ('Correct')
CC = CC+1
if 400 < mposx < 500 and 150 < mposy < 200:
print ('Wrong')
WC = WC-1
print (CC)
print (WC)
if CC == 10:
loop = False
check = True
return check
print ('Win')
if WC == 0:
loop = False
check = False
return check
print ('Fail')
pygame.display.update()
clock.tick(30)But when I run that and I click on the wrong button in the game, it prints WC as running down to 0, but then restores it to 3 and drops out of the loop, the second time it hits 0, but without printing 'Fail'.So it basically says in the print line "WC is O" and the next line "if WC == 0" doesnt get activated. I am so confused and would be very thankful for any help! ---------------------------------------------------------------- Update: When I just run this: q()
print ('End')it works, but when I run this:q()
check = q()
print (check)
print ('End')the problem I described above occursMaybe that's helping? For me it's just confusing... RE: variable isn't recognized by if structure - Windspar - Dec-23-2018 You need a better test code. How I would test code. import pygame
pygame.init()
class SimpleButton:
def __init__(self, caption, font_color, button_color, rect):
font = pygame.font.Font(None, 16)
self.rect = rect
self.color = button_color
self.text = font.render(caption, 1, font_color)
self.text_rect = self.text.get_rect()
self.text_rect.center = rect.center
def draw(self, surface):
surface.fill(self.color, self.rect)
surface.blit(self.text, self.text_rect)
class Scene:
def __init__(self):
self.rect = pygame.Rect(0, 0, 800, 600)
pygame.display.set_caption('Example')
self.surface = pygame.display.set_mode(self.rect.size)
self.clock = pygame.time.Clock()
correct_rect = pygame.Rect(200, 150, 100, 50)
wrong_rect = correct_rect.move(200, 0)
self.buttons = [
SimpleButton('Correct', (255,255,255), (0,200,0), correct_rect),
SimpleButton('Wrong', (255,255,255), (200,0,0), wrong_rect)
]
self.correct_count = 0
self.wrong_count = 3
self.font = pygame.font.Font(None, 16)
self.correct_text = self.font.render(str(self.correct_count), 1, (0, 255, 0))
self.wrong_text = self.font.render(str(self.wrong_count), 1, (255, 0, 0))
self.end_text = None
def loop(self):
self.running = True
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if self.buttons[0].rect.collidepoint(*event.pos):
self.correct_count += 1
if self.correct_count == 10:
self.end_text = self.font.render('Win', 1, (0, 255, 0))
self.correct_count = 0
self.wrong_count = 3
self.wrong_text = self.font.render(str(self.wrong_count), 1, (255, 0, 0))
self.correct_text = self.font.render(str(self.correct_count), 1, (0, 255, 0))
elif self.buttons[1].rect.collidepoint(*event.pos):
self.wrong_count -= 1
if self.wrong_count == 0:
self.end_text = self.font.render('Lost', 1, (255, 0, 0))
self.correct_count = 0
self.wrong_count = 3
self.correct_text = self.font.render(str(self.correct_count), 1, (0, 255, 0))
self.wrong_text = self.font.render(str(self.wrong_count), 1, (255, 0, 0))
self.surface.fill((0,0,0))
self.surface.blit(self.correct_text, (10, 10))
self.surface.blit(self.wrong_text, (100, 10))
if self.end_text:
self.surface.blit(self.end_text, (200, 10))
for button in self.buttons:
button.draw(self.surface)
pygame.display.flip()
self.clock.tick(30)
pygame.quit()
scene = Scene()
scene.loop()
RE: variable isn't recognized by if structure - Prof_Jar_Jar - Dec-23-2018 As I said, I'm new to Python and thats beyond my current level. Thanks for trying to improve my code, but I don't understand that anymore. I am aware, that I am overcomplicating a lot of things, but I can better understand them if I do them very primitively. Do you have any idea, how the "check = q()" command changes so much in "the q()-loop"? RE: variable isn't recognized by if structure - Windspar - Dec-23-2018 You need to do a better test example. Remove all pygame stuff from q. All q does is run an infinite loop. So it unclear where the error is. It always prints zero then three. Never ending. |