我怎样才能获得生命变化的数量(pygame)? [英] How can I get the number of lives change (pygame)?

查看:61
本文介绍了我怎样才能获得生命变化的数量(pygame)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 和 pygame 的新手,到目前为止我设法让一切正常,但不知道如何让我的生活下降.

I'm new to python and pygame and so far ive managed to get everything working, but cant work out how to make my lives go down.

如果你还没有玩过它,这是一个简单的水果捕捉游戏.

If you haven't worked out its a simple fruit catching game.

我已经设法让我的分数上升了.我试过说如果水果低于某个 x 坐标会带走一条生命,但它不起作用.

I've managed to make my score go up. I've tried saying if the fruit is below a certain x co ordinate take away a life but it doesn't work.

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()

推荐答案

检查碰撞时,使用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

应该可以.

这篇关于我怎样才能获得生命变化的数量(pygame)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆