2013-11-23 34 views
1

我是python和pygame的新手,迄今为止我设法让所有的东西都能正常工作,但却无法解决如何让我的生活下去。我怎样才能获得生命数量的变化(pygame)?

如果你还没有制定出一个简单的水果捕捉游戏。

我已经设法让我的分数上升。我曾试着说过,如果水果低于某个x坐标,就会夺走生命,但它不起作用。

import time 
import random 
import pygame 
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") 
#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)]) 
    else:fruit_y+=5 
    #check collision 
    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') 



    #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 

    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

你也可以显示你估计尝试添加到这个来减少生命的代码。因为目前清楚的是,根本不会碰到“mylives”或“label3”。 – timday

+0

为什么你不这样做:if fruit_x <123:mylives- = 1? – user2746752

回答

1

检查碰撞时,请使用else

#check collision 
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') 
     else: 
      mylives-= 1 

这应该工作。