Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame中的帮助;游戏功能“;for循环值既不增加也不减少_Python_Pygame_Raspberry Pi - Fatal编程技术网

Python pygame中的帮助;游戏功能“;for循环值既不增加也不减少

Python pygame中的帮助;游戏功能“;for循环值既不增加也不减少,python,pygame,raspberry-pi,Python,Pygame,Raspberry Pi,我正在使用pygame开发一个游戏,该游戏将显示两个按钮和一个图像。用户必须选择描述图像的正确按钮。我遇到的困难是,我无法为(循环线101)增加I变量。我注意到while循环正在无限地运行,即使我试着将其制动。当我点击按钮时,我没有得到响应(st2 s2t函数中编程的正确或错误) 总而言之,我试图从游戏功能中捕获用户的响应,并确定用户是否通过st2或s2t做出了正确的决定。返回游戏函数并递增i,执行下一个elif语句,再次调用st2或s2t,依此类推 # -*- coding: utf-8 -*

我正在使用pygame开发一个游戏,该游戏将显示两个按钮和一个图像。用户必须选择描述图像的正确按钮。我遇到的困难是,我无法为(循环线101)增加I变量。我注意到while循环正在无限地运行,即使我试着将其制动。当我点击按钮时,我没有得到响应(st2 s2t函数中编程的正确或错误)

总而言之,我试图从游戏功能中捕获用户的响应,并确定用户是否通过st2或s2t做出了正确的决定。返回游戏函数并递增i,执行下一个elif语句,再次调用st2或s2t,依此类推

# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 17:15:42 2020

@author: okand
"""
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 17:15:42 2020

@author: okand
"""
import glob
import pygame
import random #we use this to make a random right button
import time 
import os 
pygame.init() # insslatize all pygame 

"setting up the display parmeters"
display_width = 600
display_height = 600

"setting up the clors and the bright's"
black = (0,0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0, 0, 255)
bright_red = (255,0,0)
bright_green = (0,255,0)


""""Setting display. note the starderd we use is 600*600 display limtation"""
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GUI Speech Recognition') #title of the window
gameDisplay.fill(white)


""" Loading images to the paython from the pic file """
current_path = os.path.dirname(__file__)

image_path = os.path.join('image')
def load_the_image(image):
    images = [
        load_the_image('lalaa.jpg'),
        load_the_image('Ross.jpg'),
        load_the_image('gator.jpg'),
        load_the_image('blue_sea_water.jpg'),
        load_the_image('mountains.jpg'),
        load_the_image('elif.jpg')
   ]


def close():
    pygame.quit()
    quit()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',30)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    

    
def text_objects(text, font):
    textSurface = font.render(text, True, alpha)
    return textSurface, textSurface.get_rect()




""" This function is to set the button paramters x_axis Y_axis width hight 
Also, when you hit thr button it will take an action """
def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()         
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)



def s2t():
    carImg= game()
    gameDisplay.blit(carImg,(0,0))    
    message_display('good job')
    print('good job')
  
def st2():
    carImg= game()
    gameDisplay.blit(carImg,(0,0))  
    message_display('wrong')
    print('wrong')
    



"""This function is for checing the value i  of the , changes the loaded picutres as well 
as the button options. to get the response s2t & st2 functions are called"""    

def game ():
   aseel = 1
   while aseel == 1: 
       
       for event in pygame.event.get():
        for i in range(1,3):
            

            if i == 1:
                 carImg = pygame.image.load(os.path.join(image_path, 'lalaa.jpg'))
                 gameDisplay.blit(carImg,(0,0))
                 pygame.display.update()
            
                 NewRightButton = 1   #random.randint(1,2)
                 
                 if NewRightButton == 1:
                    button("Dog",150,450,100,50,green,bright_green,s2t)
                    button("Cat",50,450,100,50,blue,bright_green,st2)
                    #code that says wich the right button is
                    pygame.display.update() 
                    # time.sleep(3)        
                    break  
                 else:
                    button("Cat",150,450,100,50,green,bright_green,st2)
                    button("Dog",50,450,100,50,blue,bright_green,s2t)
                    #code that says wich the right button is
                    #This makes the buttons swap back and forth
                    pygame.display.update() 
                    # time.sleep(3)
                    break 
                 aseel = aseel + 1
                 print (aseel)
            elif i == 2: 
                 carImg = pygame.image.load(os.path.join(image_path, 'Ross.jpg'))
                 gameDisplay.blit(carImg,(0,0))
                 pygame.display.update()
            
                 NewRightButton = 1   #random.randint(1,2)
                 
                 if NewRightButton == 1:
                    button("ross",150,450,100,50,green,bright_green,s2t)
                    button("Cat",50,450,100,50,blue,bright_green,st2)
                    #code that says wich the right button is
                    pygame.display.update() 
                    # time.sleep(3)        
                    break  
                 else:
                    button("Cat",150,450,100,50,green,bright_green,st2)
                    button("ross",50,450,100,50,blue,bright_green,s2t)
                    #code that says wich the right button is
                    #This makes the buttons swap back and forth
                    pygame.display.update() 
                    # time.sleep(3)
                    break 
            elif i == 3: 
                 carImg = pygame.image.load(os.path.join(image_path, 'gator.jpg'))
                 gameDisplay.blit(carImg,(0,0))
                 pygame.display.update()
            
                 NewRightButton = 1   #random.randint(1,2)
                 
                 if NewRightButton == 1:
                    button("gator",150,450,100,50,green,bright_green,s2t)
                    button("Cat",50,450,100,50,blue,bright_green,st2)
                    #code that says wich the right button is
                    pygame.display.update() 
                    # time.sleep(3)        
                    break  
                 else:
                    button("Cat",150,450,100,50,green,bright_green,st2)
                    button("gator",50,450,100,50,blue,bright_green,s2t)
                    #code that says wich the right button is
                    #This makes the buttons swap back and forth
                    pygame.display.update() 
                    # time.sleep(3)
                    break 
                 aseel = aseel + 1 
                 print (aseel)
                
            else :
                aseel = aseel + 1
                continue 
            print ('end of the game')
            aseel = aseel + 1
             
        
     
 
                
        
            
def trake_mouse():   #To  trake mosue postion
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            print (event)

"""" In The main function is to show the frist screen with 
two buttons options play or quit. if the user choes quit. the game will quit.
On the other hand, if the user choses to play button. it is going to take him 
to game function  """
def main ():
     while True:
        for event in pygame.event.get():
                 if event.type == pygame.QUIT:
                        pygame.quit()
                        quit()
        pygame.display.update()
        button("Quit",450,250,100,50,red,bright_red,close)
        button("play",150,250,100,50,green,bright_green,game)
        pygame.display.update()   
     
        
if __name__ == '__main__':
    main()



您的问题是,在内部循环中,您迭代范围1和3,然后尝试更新
aseel=aseel+1
,但这永远不会发生,因为您已经打破了循环。if语句
if i==2
if i==3
也是如此:它们永远不会发生,因为您已经在第一个if语句中中断了循环。 我不完全确定这个循环的作用,但是如果您执行以下更改,值
aseel
应该会更新

while aseel == 1: 
   
   for event in pygame.event.get():
    for i in range(1,3):
        
        if i == 1:
             ...
             
             if NewRightButton == 1:
                ...
                # time.sleep(3)        
                # break -> remove this  
             else:
                ...
                # time.sleep(3)
                # break -> remove this 
             aseel = aseel + 1  # should be executed now
             print (aseel)
        elif i == 2: 
             ...
        
             NewRightButton = 1   #random.randint(1,2)
             
             if NewRightButton == 1:
                ...
                # time.sleep(3)        
                # break -> remove this  
             else:
                button("Cat",150,450,100,50,green,bright_green,st2)
                button("ross",50,450,100,50,blue,bright_green,s2t)
                #code that says wich the right button is
                #This makes the buttons swap back and forth
                pygame.display.update() 
                # time.sleep(3)
                # break -> remove this 
        elif i == 3: 
             ...
             NewRightButton = 1   #random.randint(1,2)
             
             if NewRightButton == 1:
                ... 
                # time.sleep(3)        
                # break -> remove this   
             else:
                ...
                # time.sleep(3)
                # break -> remove this
             aseel = aseel + 1 
             print (aseel)
            
        else : # it will only loop from 1 to 3, so this case will never happen -> you might as well remove it
            aseel = aseel + 1
            continue 
        print ('end of the game')
        aseel = aseel + 1

另外,我想指出的是,您在这个循环中的实现没有意义,因为您正在从1到3进行迭代,然后在每个if语句中执行某些内容。如果所有循环都将以任何方式执行,那么为什么首先需要一个循环?此外,您正在多次更新
aseel
,并且在第一条语句中更新
aseel
后,while循环应该退出,其余的if语句将永远不会发生!似乎您不完全确定在这个循环中要做什么。请再看一看。

请提供所需信息。显示中间结果与您预期的不同之处。我们应该能够复制和粘贴一个连续的代码块,执行该文件,并再现您的问题以及跟踪问题点的输出。当您试图询问如何处理可变增量时,发布超过200行的代码太多了。请重复并从。除此之外,您已经问了多个问题,但没有跟踪任何给您带来麻烦的值。您不必调用
pygame.display.update()
,这可能需要多次。一个是Enough,我建议您考虑使用类。如果你没有一种跟踪一切的方法,像这样的项目很快就会变得非常复杂。为什么你要在事件循环中进行循环?把它放到活动之外谢谢你的回复。我之所以更新aseel值是因为我想测试为什么循环没有改变I值,而I值会改变图片。