Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 当我在游戏循环中定义一个变量,然后在游戏循环中定义一个函数时_Python_Python 3.x_Pygame - Fatal编程技术网

Python 当我在游戏循环中定义一个变量,然后在游戏循环中定义一个函数时

Python 当我在游戏循环中定义一个变量,然后在游戏循环中定义一个函数时,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在使用pygame用python编写一个游戏。我在游戏循环中创建了一些变量和一个函数,然后在游戏循环中创建了一个函数 以下是代码的重要部分: import pygame import random pygame.init() white=(255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display_width=800 display_hieght=600 game

我正在使用pygame用python编写一个游戏。我在游戏循环中创建了一些变量和一个函数,然后在游戏循环中创建了一个函数

以下是代码的重要部分:

import pygame
import random
pygame.init()


white=(255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)



display_width=800
display_hieght=600


gameDisplay= pygame.display.set_mode((display_width,display_hieght))
pygame.display.set_caption('slither')







clock=pygame.time.Clock()
font=pygame.font.SysFont(None,25)

lead_x_change = 0
lead_y_change = 0

def message_to_screen(msg,colour,x,y):
    screen_text = font.render(msg,True,colour)
    gameDisplay.blit(screen_text,[x,y])
def gameloop():
    gameExit = False
    gameOver = False
    lead_x = display_width / 2
    lead_y = display_hieght - 5
    blocksize=int(10)
    FPS = 100
    score=0

    def shoot(event):
        pos = pygame.mouse.get_pos()
        if pos >= (590, 400):
            lead_x_change += 1
            lead_y_change += 0.1
        if pos >= (590, 400) and pos <= (580, 0):
            lead_x_change += 1
            lead_y_change += 0.2

    while not gameExit:
        pygame.display.update()
        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen('Game over, press Y to play again or N to quit',red,display_width*0.25,display_hieght/2)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key== pygame.K_n:
                        gameExit = True
                        gameOver = False

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                gameExit = True
            if event.type==pygame.MOUSEBUTTONDOWN:
                   shoot(pygame.MOUSEBUTTONUP)




        if lead_x >display_width -50:
            lead_x=display_width - 50
        elif lead_x <0:
            lead_x=0

        lead_x += lead_x_change
        lead_y += lead_y_change



        gameDisplay.fill(white)
        block=pygame.draw.rect(gameDisplay,red,(lead_x,lead_y,50,5))
        obj=pygame.draw.circle(gameDisplay,black,(425,595),blocksize)
        pygame.Rect(block)
        pygame.Rect(obj)



        message_to_screen('score='+str(score),black,0,0)

        pygame.display.update()


    clock.tick(FPS)


    pygame.quit()
    quit()
gameloop()
导入pygame
随机输入
pygame.init()
白色=(255255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0255,0)
蓝色=(0,0255)
显示宽度=800
显示高度=600
gameDisplay=pygame.display.set_模式((显示宽度,显示高度))
pygame.display.set_标题('slither'))
clock=pygame.time.clock()
font=pygame.font.SysFont(无,25)
lead_x_change=0
潜在客户变更=0
def消息到屏幕(消息、颜色、x、y):
screen\u text=font.render(味精、真、彩色)
blit(屏幕文本,[x,y])
def gameloop():
gameExit=False
gameOver=False
引线x=显示宽度/2
铅=显示器高度-5
块大小=整数(10)
FPS=100
分数=0
def喷射(事件):
pos=pygame.mouse.get_pos()
如果位置>=(590400):
lead_x_change+=1
铅含量变化+=0.1
如果pos>=(590400)且pos显示宽度为-50:
铅x=显示器宽度-50

elif lead_x
lead_x_change
lead_y_change
gameloop
函数的局部变量,只能在该函数中访问。
shot
功能无法访问它们,这正是
+=
操作员试图执行的操作

如果您希望
shot
具有访问权限,一个解决方案是将它们设置为全局变量

lead_x_change = 0    # global variable
lead_y_change = 0    # global variable

def gameloop():
    gameExit = False
    gameOver = False
    lead_x = display_width / 2
    lead_y = display_hieght - 5
    blocksize=int(10)
    FPS = 100
    score=0

def shoot(event):
    global lead_x_change, lead_y_change
    # Variable in global scope can be only read by a function, unless specially declared as global (above)
    pos = pygame.mouse.get_pos()
    if pos >= (590, 400):
        lead_x_change += 1
        lead_y_change += 0.1
    if pos >= (590, 400) and pos <= (580, 0):
        lead_x_change += 1
        lead_y_change += 0.2
lead_x_change=0#全局变量
lead_y_change=0#全局变量
def gameloop():
gameExit=False
gameOver=False
引线x=显示宽度/2
铅=显示器高度-5
块大小=整数(10)
FPS=100
分数=0
def喷射(事件):
全球潜在客户变更,潜在客户变更
#全局范围内的变量只能由函数读取,除非特别声明为全局(如上所述)
pos=pygame.mouse.get_pos()
如果位置>=(590400):
lead_x_change+=1
铅含量变化+=0.1

如果pos>=(590400)和pos,请向我们显示完整的代码。我不建议毫无理由地使用全局变量,而是将所需的参数传递给函数。