我完全不知道为什么,但是当我尝试将speed
变量从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()
有谁知道为什么会这样?最终目标是使速度与playerLevel成比例,以增加难度。
答案 0 :(得分:0)
看起来你的名字可能会影响某些东西。我设法重现你的错误(虽然我必须创建所有颜色并注释掉声音),但是当我将速度重命名为wall_speed时,一切都开始工作
答案 1 :(得分:0)
而不是
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
walls_X
的初始值为975
(width - wallWidth
),然后您按speed
重复减少{ - {1}}至speed == 5
在语句中测试970, 965, 960, 955...
的值时会出现问题:
walls_X
如果if walls_X == 0: # Move wall back to start and change heights
不除数speed
(即它与975
不同),1, 3, 5, 13, 15, ...
永远不会达到walls_X
并且您的测试0
将永远无法通过。