,我会添加到这个代码是什么使“mylives”下去(pygame的)

import pygame, random 

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

caught= fruit_x>=mouth_x and fruit_x<=mouth_x+300

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

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

它基本上是一个简单的水果追赶游戏 我怎么可能让这个如果有碗和水果我之间没有接触生命下降了吗? 我设法让+1分数工作,但没有-1分。 任何想法家伙? 我是新来的python和pygame,所以它可能很简单,我没有想到。,我会添加到这个代码是什么使“mylives”下去(pygame的)

回答:

如果你想要它失去生命,如果你错过了,或者如果你击中了你的分数就加一个,就用另一个。如果你击中了+1分,否则-1命中。这里是一个例子:

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

以上是 ,我会添加到这个代码是什么使“mylives”下去(pygame的) 的全部内容, 来源链接: utcz.com/qa/264982.html

回到顶部