Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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-速度变量不能从5更改_Python_Pygame - Fatal编程技术网

Python Pygame-速度变量不能从5更改

Python Pygame-速度变量不能从5更改,python,pygame,Python,Pygame,我完全不知道为什么,但当我尝试将速度变量从5更改为任何其他数字时,重复一次后,整个过程就会挂起 我的游戏只是一个由鼠标控制的矩形,它必须在随机产生高度的两堵墙之间移动。以下是我的主要功能: def main(): pygame.init() # Game sounds pygame.mixer.music.set_volume(1.0) song = "Music/boss.WAV" pygame.mixer.music.load(song)

我完全不知道为什么,但当我尝试将
速度
变量从
5
更改为任何其他数字时,重复一次后,整个过程就会挂起

我的游戏只是一个由鼠标控制的矩形,它必须在随机产生高度的两堵墙之间移动。以下是我的主要功能:

def main():

    pygame.init()

    # Game sounds
    pygame.mixer.music.set_volume(1.0)

    song = "Music/boss.WAV"
    pygame.mixer.music.load(song)
    pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
    pygame.mixer.music.play()

    passSound = pygame.mixer.Sound("Sound Effects/passPillar.wav")
    passSound.set_volume(0.3)
    levelUpSound = pygame.mixer.Sound("Sound Effects/levelUp.wav")
    levelUpSound.set_volume(0.5) 

    # Set the width and height of the screen [width,height]
    width = 1000
    height = 800
    size = [width, height]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("boxes")

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # Hide the mouse cursor
    pygame.mouse.set_visible(False)

    # the player and its variables
    playerDimension = 50

    playerX = width/4

    playerScore = 0

    playerLevel = 1

    scoreBarWidth = 50

    # the walls and their variables
    extraWidth = 70

    wallWidth = playerDimension/2

    wallHeight_1 = randint( (height/5) , (height*4/5) )
    wallHeight_2 = height - wallHeight_1 - playerDimension+extraWidth

    wall_Y_1 = 0
    wall_Y_2 = wallHeight_1 + playerDimension+extraWidth

    walls_X = width - wallWidth    

    speed = 5

    # Game over and Intro screen
    gameOver = False

    introScreen = True

    # Fonts
    font = pygame.font.SysFont("Avenir Next", 26)

    titleFont = pygame.font.SysFont("Avenir Next", 36)

    # Game loop
    while not done:

        # ALL EVENT PROCESSING
        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                done = True

            elif event.type == pygame.constants.USEREVENT:

                # When the song stops playing, start song again
                pygame.mixer.music.load(song)
                pygame.mixer.music.play()

            elif event.type == pygame.KEYDOWN:

                if event.key == pygame.K_r:

                    if gameOver:

                        gameOver = False

                        introScreen = True

                        playerLevel = 1
                        playerScore = 0

                        walls_X = width - wallWidth

                if event.key == pygame.K_SPACE:

                    if introScreen:

                        introScreen = False

                        levelUpSound.play()

        # ALL GAME LOGIC

        # GET MOUSE POSITION
        if not gameOver:

            pos = pygame.mouse.get_pos()
            y = pos[1]

        if not gameOver and not introScreen:

            # Format is x,y,w,h twice for both rects
            touchingUpper = collisionDetect(playerX, y, playerDimension, playerDimension, walls_X, wall_Y_1, wallWidth, wallHeight_1)

            touchingLower = collisionDetect(playerX, y, playerDimension, playerDimension, walls_X, wall_Y_2, wallWidth, wallHeight_2)

            gameOver = touchingUpper or touchingLower

            if walls_X == 0: # Move wall back to start and change heights

                wallHeight_1 = randint( (height/5) , (height*4/5) )
                wallHeight_2 = height - wallHeight_1 - playerDimension+extraWidth

                wall_Y_1 = 0
                wall_Y_2 = wallHeight_1 + playerDimension+extraWidth

                walls_X = width - wallWidth

            else:

                walls_X = walls_X - speed

            if walls_X == playerX:

                playerScore += 1

                if playerScore < 5:

                    passSound.play()

                elif playerScore == 5:

                    playerScore = 0

                    playerLevel += 1

                    levelUpSound.play()

            scoreBarWidth = playerScore*50



        # ALL CODE TO DRAW
        screen.fill(screenColour)

        # the player
        pygame.draw.rect(screen, playerColour, [playerX, y, playerDimension, playerDimension], 0)

        pygame.draw.rect(screen, wallColour, [walls_X, wall_Y_1, wallWidth, wallHeight_1])#the upper wall
        pygame.draw.rect(screen, wallColour, [walls_X, wall_Y_2, wallWidth, wallHeight_2])#the lower wall

        for i in range(0, playerLevel):

            pygame.draw.rect(screen, levelsColour, [20 + (i*40), 20, 30, 30], 2) #levels

        pygame.draw.rect(screen, scorebarColour, [20, 60, scoreBarWidth, 30], 0) #scorebar filler
        pygame.draw.rect(screen, scorebarOutlineColour, [20, 60, 5*50, 30], 2) #scorebar outline

        if gameOver:

            text1 = titleFont.render("Game Over!", True, wallColour)
            text1_rect = text1.get_rect()
            text1_x = screen.get_width() * 1.7 / 3 - text1_rect.width / 2
            text1_y = screen.get_height() * 1.2 / 3 - text1_rect.height / 2
            screen.blit(text1, [text1_x, text1_y])

            text2 = font.render("You reached level " + str(playerLevel) + ".", True, wallColour)
            text2_rect = text2.get_rect()
            text2_x = screen.get_width() * 1.7 / 3 - text2_rect.width / 2
            text2_y = screen.get_height() / 2 - text2_rect.height / 2 
            screen.blit(text2, [text2_x, text2_y])

            text3 = font.render("Press R to restart.", True, wallColour)
            text3_rect = text3.get_rect()
            text3_x = screen.get_width() * 1.7 / 3 - text3_rect.width / 2
            text3_y = screen.get_height() * 1.8 / 3 - text3_rect.height / 2 
            screen.blit(text3, [text3_x, text3_y])

        elif introScreen:

            text1 = titleFont.render("Press Space to start!", True, wallColour)
            text1_rect = text1.get_rect()
            text1_x = screen.get_width() / 2 - text1_rect.width / 2
            text1_y = screen.get_height() * 1 / 3 - text1_rect.height / 2
            screen.blit(text1, [text1_x, text1_y])

        # Update the screen
        pygame.display.flip()

        # Limit frames per second
        clock.tick(200)

    # quit.
    pygame.quit()
def main():
pygame.init()
#游戏声音
pygame.mixer.music.set_音量(1.0)
song=“Music/boss.WAV”
pygame.mixer.music.load(歌曲)
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
passSound=pygame.mixer.Sound(“音效/passPillar.wav”)
passSound.set_音量(0.3)
levelUpSound=pygame.mixer.Sound(“音效/levelUp.wav”)
levelUpSound.设置音量(0.5)
#设置屏幕的宽度和高度[宽度,高度]
宽度=1000
高度=800
大小=[宽度、高度]
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“框”)
#循环,直到用户单击关闭按钮。
完成=错误
#用于管理屏幕更新的速度
clock=pygame.time.clock()
#隐藏鼠标光标
pygame.mouse.set_可见(False)
#玩家及其变量
playerDimension=50
playerX=宽度/4
playerScore=0
playerLevel=1
scoreBarWidth=50
#墙及其变量
超宽=70
墙宽=播放器尺寸/2
墙高_1=randint((高度/5),(高度*4/5))
墙高_2=高度-墙高_1-播放器尺寸+额外宽度
墙_Y_1=0
墙_Y_2=墙高_1+播放器尺寸+额外宽度
墙X=宽度-墙宽度
速度=5
#游戏结束和介绍屏幕
gameOver=False
introScreen=True
#字体
font=pygame.font.SysFont(“Avenir Next”,26)
titleFont=pygame.font.SysFont(“Avenir Next”,36)
#游戏循环
虽然没有这样做:
#所有事件处理
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
elif event.type==pygame.constants.USEREVENT:
#当歌曲停止播放时,再次开始播放歌曲
pygame.mixer.music.load(歌曲)
pygame.mixer.music.play()
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K\r:
如果游戏结束:
gameOver=False
introScreen=True
playerLevel=1
playerScore=0
墙X=宽度-墙宽度
如果event.key==pygame.K_空间:
如果进入屏幕:
introScreen=False
levelUpSound.play()
#全博弈逻辑
#获取鼠标位置
如果没有结束:
pos=pygame.mouse.get_pos()
y=位置[1]
如果不是gameOver和introScreen:
#两个矩形的格式为x、y、w、h两次
touchingUpper=碰撞检测(playerX,y,playerDimension,playerDimension,walls_X,wall_y_1,wallWidth,wallHeight_1)
touchingLower=碰撞检测(playerX,y,playerDimension,playerDimension,walls_X,wall_y_2,wallWidth,wallHeight_2)
gameOver=触摸上方或下方
如果墙_X==0:#将墙移回起点并更改高度
墙高_1=randint((高度/5),(高度*4/5))
墙高_2=高度-墙高_1-播放器尺寸+额外宽度
墙_Y_1=0
墙_Y_2=墙高_1+播放器尺寸+额外宽度
墙X=宽度-墙宽度
其他:
墙X=墙X-速度
如果墙_X==playerX:
playerScore+=1
如果playerScore<5:
passSound.play()
elif playerScore==5:
playerScore=0
playerLevel+=1
levelUpSound.play()
scoreBarWidth=球员核心*50
#要绘制的所有代码
屏幕填充(屏幕颜色)
#球员
pygame.draw.rect(屏幕,玩家颜色,[playerX,y,playerDimension,playerDimension],0)
pygame.draw.rect(屏幕、墙颜色、[walls\ux、wall\uy\u1、wallWidth、wallHeight\u1])上墙
pygame.draw.rect(屏幕、墙颜色、[walls\ux、wall\uy\u2、wallWidth、wallHeight\u2])下墙
对于范围内的i(0,播放器级别):
pygame.draw.rect(屏幕,关卡颜色,[20+(i*40),20,30,30],2)#关卡
pygame.draw.rect(屏幕,记分栏颜色,[20,60,记分栏宽度,30],0)#记分栏填充
pygame.draw.rect(屏幕,记分栏输出线颜色,[20,60,5*50,30],2)#记分栏轮廓
如果游戏结束:
text1=titleFont.render(“游戏结束!”,True,wallColour)
text1_rect=text1.get_rect()
text1_x=screen.get_width()*1.7/3-text1_rect.width/2
text1_y=screen.get_height()*1.2/3-text1_rect.height/2
blit(text1,[text1\u x,text1\u y])
text2=font.render(“您达到了级别”+str(playerLevel)+>”,True,wallColour)
text2_rect=text2.get_rect()
text2_x=screen.get_width()*1.7/3-text2_rect.width/2
text2_y=screen.get_height()/2-text2_rect.height/2
blit(text2,[text2_x,text2_y])
text3=font.render(“按R键重新启动)”,True,wallColour)
text3_rect=text3.get_rect()
text3_x=screen.get_width()*1.7/3-text3_rect.width/2
text3_y=screen.get_height()*1.8/3-text3_rect.height/2
blit(text3,[text3_x,text3_y])
elif屏幕:
text1=titleFont.render(“按空格开始!”,True,wallColour)
text1_rect=text1.get_
if walls_X == 0: # Move wall back to start and change heights
if walls_X <= 0: # Move wall back to start and change heights
walls_X = walls_X - speed
if walls_X == 0: # Move wall back to start and change heights