Python 返回按钮数量不确定后,屏幕显示错误消息,即使所有按钮都正确
这一个有点复杂,但基本上在随机获得大量正确答案后,我得到了一个不正确的屏幕,尽管所有答案都是正确的。这可能与gameloop有关,但任何帮助都是值得的,因为我现在很困惑。请随意复制和粘贴,因为我认为我的解释没有很好地说明全部问题Python 返回按钮数量不确定后,屏幕显示错误消息,即使所有按钮都正确,python,function,class,pygame,Python,Function,Class,Pygame,这一个有点复杂,但基本上在随机获得大量正确答案后,我得到了一个不正确的屏幕,尽管所有答案都是正确的。这可能与gameloop有关,但任何帮助都是值得的,因为我现在很困惑。请随意复制和粘贴,因为我认为我的解释没有很好地说明全部问题 import pygame import random pygame.init() #TO DO: Make questions and make them random and fix go back into incorrect bug. #Title pyg
import pygame
import random
pygame.init()
#TO DO: Make questions and make them random and fix go back into incorrect bug.
#Title
pygame.display.set_caption('Average Speed Game')
#Screen
screenWidth=900
screenHeight=500
win=pygame.display.set_mode((screenWidth,screenHeight))
#Framerate
clock= pygame.time.Clock()
#background image
bg=pygame.image.load('C:\\Users\\18597\\Desktop\\My Python Scripts\\speed\\bg.png')
#button colors
buttonWhite=(255,255,255)
buttonPink=(255,182,193)
class car(object):
def __init__(self,x):
self.x=x
self.y=300
self.image=pygame.image.load('C:\\Users\\18597\\Desktop\\My Python Scripts\\speed\\car.png')
#Car Instance
global racer
racer=car(0)
class question(object):
def __init__(self,b,c,d):
self.a=['Time/Distance','Direction+Time','13mph']
self.b=b
self.c=c
self.d=d
self.q=['What is the Formula for Speed','What is the Formula for Velocity', 'What is the average speed of 13miles in 6 min.']
self.font=pygame.font.SysFont('Courier',30,True,False)
def draw(self,win):
while newQ ==True:
self.font
q1Text= self.font.render(self.q[0],1,(255,182,193))
win.blit(q1Text,(200,50))
button('',130,150,300,50,buttonWhite,buttonWhite,145,145,buttonPink,buttonWhite,'inco')
q1TextA= self.font.render(self.a[0],1,(255,182,193))
win.blit(q1TextA,(150,150))
button('',530,150,300,50,buttonWhite,buttonWhite,145,145,buttonPink,buttonWhite,'co')
q1TextB= self.font.render(self.b,1,(255,182,193))
win.blit(q1TextB,(550,150))
button('',130,240,350,50,buttonWhite,buttonWhite,145,145,buttonPink,buttonWhite,'inco')
q1TextC= self.font.render(self.c,1,(255,182,193))
win.blit(q1TextC,(150,250))
button('',530,240,300,50,buttonWhite,buttonWhite,145,145,buttonPink,buttonWhite,'inco')
q1TextD= self.font.render(self.d,1,(255,182,193))
win.blit(q1TextD,(550,250))
break
#Button for Start
def button(msg,x,y,w,h,inactive,active,xT,yT,inactiveT,activeT,action=None):
#Use location of mouse to track the button
mouse= pygame.mouse.get_pos()
#Tracks mouse clicks
click=pygame.mouse.get_pressed()
#0 is x, 1 is y, 2 is width, 3 is height
if x+w> mouse[0] >x and y+h>mouse[1]>y:
pygame.draw.rect(win, inactive,(x,y,w,h))
#Click[0] is left mouse click
if click[0] == 1 and action!= None:
if action == 'co':
correctPage()
elif action == 'inco':
incorrectPage()
elif action == 'backQ':
gameLoop()
elif action == 'start':
gameLoop()
else:
pygame.draw.rect(win, active,(x,y,w,h))
buttonText= pygame.font.SysFont('Courier',30,True,False)
buttonType= buttonText.render(msg,1,(activeT))
buttonText2= pygame.font.SysFont('Courier',30,True,False)
buttonType2= buttonText2.render(msg,1,(inactiveT))
if x+w> mouse[0] >x and y+h>mouse[1] > y:
win.blit(buttonType2,(xT,yT))
else:
win.blit(buttonType,(xT,yT))
buttonText2= pygame.font.SysFont('Courier',30,True,False)
buttonType2= buttonText2.render(msg,1,(inactiveT))
def start():
start=True
while start:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
win.blit(bg,(0,0))
startFont=pygame.font.SysFont('Courier',50,True,False)
startText= startFont.render('Start Game',1,(255,182,193))
win.blit(startText,(315,100))
button('Start',275,220,300,50,buttonWhite,buttonPink,380,230,buttonPink,buttonWhite,'start')
pygame.display.update()
clock.tick(15)
def incorrectPage():
inco=True
while inco:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
win.blit(bg,(0,0))
incoFont=pygame.font.SysFont('Courier',50,True,False)
incoText= incoFont.render('Incorrect',1,(255,182,193))
win.blit(incoText,(315,100))
button('Back',275,220,300,50,buttonWhite,buttonPink,380,230,buttonPink,buttonWhite,'backQ')
if racer.x>=200:
racer.x=-200
win.blit(racer.image,(racer.x,270))
pygame.display.update()
clock.tick(15)
def correctPage():
co=True
while co:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
win.blit(bg,(0,0))
coFont=pygame.font.SysFont('Courier',50,True,False)
coText= coFont.render('Correct!',1,(255,182,193))
win.blit(coText,(315,100))
button('Back',275,220,300,50,buttonWhite,buttonPink,380,230,buttonPink,buttonWhite,'backQ')
racer.x+200
win.blit(racer.image,(racer.x,270))
pygame.display.update()
clock.tick(15)
global q1
q1=question('B.Distance/Time','C.Accleration/Time','D.Distance+Time')
def redrawGameWin():
win.blit(bg,(0,0))
q1.draw(win)
win.blit(racer.image,(racer.x,270))
pygame.display.update()
clock.tick(15)
def gameLoop():
global newQ
newQ=True
#global q1
#q1=question('A.Time/Distance','B.Distance/Time','C.Accleration/Time','D.Distance+Time')
global font
q1.font
run= True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
redrawGameWin()
start()
pygame.quit()
当你没有问题的时候会发生什么?