如何让屏幕上的游戏停留在屏幕上

时间:2013-11-23 22:50:03

标签: python pygame

import pygame, random, time
from time import sleep
from pygame import*
pygame.init()
myname=input('What is your name')
#set the window size
window= pygame.display.set_mode((800,600) ,0,24)
pygame.display.set_caption("Fruit Catch")
gameover=pygame.image.load('fifa.jpg')
#game variables
myscore=0
mylives=3
mouth_x=300
fruit_x=250
fruit_y=75
fruitlist=['broccoli.gif','chicken.gif']
#prepare for screen
myfont=pygame.font.SysFont("Britannic Bold", 55)
label1=myfont.render(myname, 1, (240, 0, 0))
label3=myfont.render(str(mylives), 1, (20, 255, 0))
#grapchics
fruit=pygame.image.load('data/chicken.png')
mouth=pygame.image.load('data/v.gif')
backGr=pygame.image.load('data/kfc.jpg')
#endless loop
running=True
while running:
    if fruit_y>=460:#check if at bottom, if so prepare new fruit
       fruit_x=random.randrange(50,530,1)
       fruit_y=75
       fruit=pygame.image.load('data/'+fruitlist[random.randrange(0,2,1)])
       caught= fruit_x>=mouth_x and fruit_x<=mouth_x+300
    else:fruit_y+=5


   #check collision
    if fruit_y>=456:
       mylives-=1
    if fruit_y>=440:
            if fruit_x>=mouth_x and fruit_x<=mouth_x+300 :
                    myscore+=1
                    fruit_y=600#move it off screen
                    pygame.mixer.music.load('data/eating.wav')
    if mylives==0:
       window.blit(backg,(0,0))
       pygame.time.delay(10)
       window.blit(gameover,(0,0))
       pygame.display.update()        
    #detect key events
    for event in pygame.event.get():
            if (event.type==pygame.KEYDOWN):
                if (event.key==pygame.K_LEFT):
                        mouth_x-=55
                if (event.key==pygame.K_RIGHT):
                        mouth_x+=55

    label3=myfont.render(str(mylives), 1, (20, 255, 0))
    label2=myfont.render(str(myscore), 1, (20, 255, 0))

    window.blit(backGr,(0,0))
    window.blit(mouth, (mouth_x,440))
    window.blit(fruit,(fruit_x, fruit_y))
    window.blit(label1, (174, 537))
    window.blit(label2, (700, 157))
    window.blit(label3, (700, 400))
    pygame.display.update()

当我的生命达到0时,屏幕上的游戏会出现,但它会在正常背景之间闪烁。 我怎么能让它留在屏幕上? 我认为这与更新有关,但是如果我把它拿出来就不起作用

1 个答案:

答案 0 :(得分:0)

如果line为0,那么你运行一些blits并更新两次

首先在if mylives==0:里面(因为你使用延迟(10)而停留一段时间)

循环结束时的第二次。

使用:

if mylives==0:
   #blit and update game over
else:
   #blit and update normal game