我正在制作一个非常基本的游戏,并且我正在尝试让鸟(玩家)躲避岩石,如果鸟被岩石击中,它就会死亡。但我不知道如何让游戏知道鸭子是否被岩石击中。
这是我的代码:
import os, sys
import random
import time
img_path = os.path.join('C:\Python27', 'player.png')
img_path2 = os.path.join('C:\Python27', 'rock.png')
class Bird(object):
def __init__(self):
self.image = pygame.image.load(img_path)
self.x = 0
self.y = 0
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 2
if key[pygame.K_DOWN]:
self.y += dist
elif key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_RIGHT]:
self.x += dist
elif key[pygame.K_LEFT]:
self.x -= dist
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
def bird_health(self):
health = 1
if health ==0:
sys.exit()
def background(self, surface):
bg = os.path.join('C:\Python27', 'bg.png')
self.image2 = pygame.image.load(bg)
surface.blit(self.image2, (0,0))
class Rock(object):
def __init__(self, x=640, y=0,):
self.image = pygame.image.load(img_path2)
self.x = x
self.y = y
dist = 10
self.dist = dist
def rock(self):
dist = 10
self.x -=dist
def rock_draw(self, surface):
surface.blit(self.image, (self.x, self.y))
pygame.init()
screen = pygame.display.set_mode((640, 200))
bird = Bird() # create an instance
rock = Rock()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # quit the screen
running = False
if rock.x < 0:
y = random.randint(10, 190)
rock = Rock(640, y)
bird.handle_keys()
rock.rock()
screen.fill((255,255,255))
bird.background(screen)
bird.draw(screen)
rock.rock_draw(screen)
pygame.display.update()
clock.tick(40)
现在我只想让它在鸟的健康 = 0 时退出。