当玩家失去但没有任何东西出现并且游戏立即重新启动时,我试图制作一些文本显示。它可以到达else语句,因为它能够打印东西,但它似乎没有“blit”表面,或者它可以快速查看。
有问题的代码盯着底部,但我想我也会包含主要功能。我不知道我是否正确,但它是否在绘制后立即在文本框中绘制背景?如果是这样,我该如何解决这个问题呢?
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT, \
TILESIZE, floorx, floory, \
floorCovered, tilesNeeded, OUTSIDEDECOMAPPING, \
L_Monster, R_Monster, BGIMAGE, \
bounceHeight, playerObj, R_Bird, \
L_Bird, direction, birdx, \
birdDir
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
FPSCLOCK = pygame.time.Clock()
BASICFONT = pygame.font.Font('freesansbold.ttf', 32)
pygame.display.set_caption('Alpha One')
# Set up the background image.
boardImage = pygame.image.load('bg.png')
# Use smoothscale() to stretch the board image to fit the entire board:
boardImageRect = boardImage.get_rect()
boardImageRect.topleft = (0, 0)
BGIMAGE = pygame.image.load('bg.png')
# Use smoothscale() to stretch the background image to fit the entire window:
BGIMAGE = pygame.transform.smoothscale(BGIMAGE, (WINDOWWIDTH, WINDOWHEIGHT))
# BGIMAGE.blit(boardImage, boardImageRect)
# #Draw the background
# DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect())
# #Draw the Floor
# drawFloor()
L_Monster = pygame.image.load('ghost.png')
R_Monster = pygame.transform.flip(L_Monster, True, False)
R_Bird = pygame.image.load('bird.png')
L_Bird = pygame.transform.flip(R_Bird, True, False)
# pygame.display.flip()
#Main Game Loop
while True:
runGame()
def runGame():
invulnerableMode = False
invulnerableStartTime = 0
gameOverMode = False
gameOverStartTime = 0
winMode = False
bounceHeight = 30
BOUNCEHEIGHT = 30
camerax = 0
cameray = 0
birdx = WINDOWWIDTH - 50
birdDir = 'right'
gameOverSurf = BASICFONT.render('Game Over', True, WHITE)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.center = (WINDOWWIDTH, WINDOWHEIGHT)
#declares the player object
playerObj = {'surface': pygame.transform.scale(L_Monster,(STARTSIZE, STARTSIZE)),
'facing': LEFT,
'size': STARTSIZE,
'x': WINDOWWIDTH,
'y': WINDOWHEIGHT,
'bounce':0,
'health': MAXHEALTH}
#declares the bird object
birdsObj = {'surface': pygame.transform.scale(R_Bird,(STARTSIZE, STARTSIZE)),
'facing': RIGHT,
'size': STARTSIZE,
'x': (0 - STARTSIZE*2),
'y': STARTSIZE * 2}
#declare the current position of keys
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
spaceDown = False
shiftDown = False
while True:
#redraw the background
DISPLAYSURF.blit(BGIMAGE, BGIMAGE.get_rect())
#redraw the floor
drawFloor()
if invulnerableMode and time.time() - invulnerableStartTime > INVULNTIME:
invulnerableMode = False
playerCenterx = playerObj['x'] + int(playerObj['size'] / 2)
playerCentery = playerObj['y'] + int(playerObj['size'] / 2)
if (camerax + HALF_WINDOWWIDTH) - playerCenterx > CAMERASLACK:
camerax = playerCenterx + CAMERASLACK - HALF_WINDOWWIDTH
elif playerCenterx - (camerax +HALF_WINDOWWIDTH) > CAMERASLACK:
camerax = playerCenterx - CAMERASLACK - HALF_WINDOWWIDTH
if (cameray + HALF_WINDOWHEIGHT) - playerCentery > CAMERASLACK:
cameray = playerCentery + CAMERASLACK - HALF_WINDOWHEIGHT
elif playerCentery - (cameray +HALF_WINDOWHEIGHT) > CAMERASLACK:
cameray = playerCentery - CAMERASLACK - HALF_WINDOWHEIGHT
#Initial player positions and facing and making rect
flashIsOn = round(time.time(), 1) * 10 % 2 == 1
if not gameOverMode and not (invulnerableMode and flashIsOn):
playerObj['rect'] = pygame.Rect((playerObj['x'] - (WINDOWWIDTH),
playerObj['y'] - (STARTSIZE + FLOORSIZE - 5 ) - getBounceAmount(playerObj['bounce'] , BOUNCERATE, BOUNCEHEIGHT),
playerObj['size'],
playerObj['size']))
DISPLAYSURF.blit(playerObj['surface'], playerObj['rect'])
#detect when the keys are press and set their vars to True
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key in (K_UP, K_w):
moveDown = False
moveUp = True
elif event.key in (K_DOWN, K_s):
moveUp = False
moveDown = True
elif event.key in (K_LEFT, K_a):
moveRight = False
moveLeft = True
if playerObj['facing'] == RIGHT:
playerObj['surface'] = pygame.transform.scale(L_Monster, (playerObj['size'], playerObj['size']))
playerObj['facing'] = LEFT
elif event.key in (K_RIGHT, K_d):
moveLeft = False
moveRight = True
if playerObj['facing'] == LEFT:
playerObj['surface'] = pygame.transform.scale(R_Monster, (playerObj['size'], playerObj['size']))
playerObj['facing'] = RIGHT
elif event.key in (K_SPACE, K_BACKSPACE):
spaceDown = True
elif event.key in (K_LSHIFT, K_RSHIFT):
shiftDown = True
elif winMode and event.key == K_r:
return
#Detect when the key comes up and set the var to false
elif event.type == KEYUP:
if event.key in (K_LEFT, K_a):
moveLeft = False
elif event.key in (K_RIGHT, K_d):
moveRight = False
elif event.key in (K_UP, K_w):
moveUp = False
elif event.key in (K_DOWN, K_s):
moveDown = False
elif event.key in (K_SPACE, K_BACKSPACE):
spaceDown = False
elif event.key in (K_LSHIFT, K_RSHIFT):
shiftDown = False
elif event.key == K_ESCAPE:
terminate()
#declares the bird rect from the bird object
birdsObj['rect'] = pygame.Rect((birdsObj['x'],
birdsObj['y'],
birdsObj['size'],
birdsObj['size']))
#Decide what way the bird has to go
if birdsObj['x'] >= (WINDOWWIDTH +(STARTSIZE*2)):
birdDir = 'left'
elif birdsObj['x'] <= (0 - (STARTSIZE*2)):
birdDir = 'right'
#go that way
if birdDir == 'left':
birdsObj['x'] -= 5
if birdsObj['facing'] == RIGHT:
birdsObj['surface'] = pygame.transform.scale(L_Bird, (playerObj['size'], playerObj['size']))
birdsObj['facing'] = LEFT
elif birdDir == 'right':
birdsObj['x'] += 5
if birdsObj['facing'] == LEFT:
birdsObj['surface'] = pygame.transform.scale(R_Bird, (playerObj['size'], playerObj['size']))
birdsObj['facing'] = RIGHT
#draw the bird
DISPLAYSURF.blit(birdsObj['surface'], birdsObj['rect'])
if playerObj['rect'].colliderect(birdsObj['rect']):
gameOverMode = True
#actually move the player
if not gameOverMode:
if moveLeft and playerObj['x'] > (WINDOWWIDTH):
playerObj['x'] -= MOVERATE
if moveRight and playerObj['x'] <= ((WINDOWWIDTH*2) - STARTSIZE):
playerObj['x'] += MOVERATE
# if moveUp and playerObj['y'] > (0 + STARTSIZE * 2):
# playerObj['y'] -= MOVERATE
if moveDown and playerObj['y'] < WINDOWHEIGHT:
playerObj['y'] += MOVERATE
if (moveLeft or moveRight or moveUp or moveDown) or playerObj['bounce'] != 0:
playerObj['bounce'] += 1
if moveLeft and spaceDown:
BOUNCEHEIGHT = 400
elif moveRight and spaceDown:
BOUNCEHEIGHT = 400
else:
BOUNCEHEIGHT = 30
if moveLeft and shiftDown:
MOVERATE = 18
elif moveRight and shiftDown:
MOVERATE = 18
else:
MOVERATE = 9
if playerObj['bounce'] > BOUNCERATE:
playerObj['bounce'] = 0
*else:
# game is over, show "game over" text
DISPLAYSURF.blit(gameOverSurf, gameOverRect)
print (gameOverMode)
if time.time() - gameOverStartTime > GAMEOVERTIME:
return*
if winMode:
DISPLAYSURF.blit(winSurf, winRect)
DISPLAYSURF.blit(winSurf2, winRect2)
pygame.display.update()
FPSCLOCK.tick(FPS)
答案 0 :(得分:2)
您正在绘制游戏结束文本,但直到循环重新开始之前才更新显示(因此游戏结束文本可能仅渲染1帧)。尝试这样的事情:
else:
# game is over, show "game over" text
gameOverStartTime = time.time()
DISPLAYSURF.blit(gameOverSurf, gameOverRect)
pygame.display.update()
print (gameOverMode)
while time.time() - gameOverStartTime < GAMEOVERTIME:
pass
这将绘制游戏结束文本,更新屏幕,然后循环,直到超过GAMEOVERTIME
定义的时间量(我认为你实际上并没有在任何地方做,至少不在您发布的代码中)。请注意,我给你的这个循环不允许你处理任何输入事件或任何事情,直到分配的时间到期,但它将保证游戏结束文本将在你的时间内呈现给屏幕想。