Pygame中的精灵健康

时间:2013-04-25 22:39:47

标签: python pygame

我正在做一个非常基本的游戏,而我正试图让它(鸟)玩家躲开岩石,如果鸟被岩石击中它就死了。但我无法弄清楚如果鸭子被岩石击中,如何让游戏知道。

这是我的代码:

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时退出。

2 个答案:

答案 0 :(得分:4)

我有一段时间没有做过pygame,但请参考: http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollideany

Pygame有一个内置的碰撞功能,spritecollideany()

或者你可以使用它:

def checkCollision(bird, rock):
    if bird.x == rock.x and bird.y == rock.y:
        sys.exit()

在鸟类和岩石类中,确保x和y可以访问。这只有在鸟的角落等于岩石的角落时才有效,但你可以添加条款来检查。例如:

if bird.x == rock.x and bird.y == rock.y or bird.x == rock.x + rock.length and... 

这将根据鸟的居中位置进行扩展。

答案 1 :(得分:1)

这是一个程序,我可以检测到球是否击中了桨,希望它有所帮助。一直到底(在一个单独的代码框中的东西)只是我检测它们是否发生碰撞的代码。

import pygame

# Constants
WIDTH = 700
HEIGHT = 500
SCREEN_AREA = pygame.Rect(0, 0, WIDTH, HEIGHT)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Initialization
pygame.init()
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.mouse.set_visible(0)
pygame.display.set_caption("Breakout Recreation WIP")
clock = pygame.time.Clock()

# Variables
paddle = pygame.Rect(350, 480, 50, 10)
ball = pygame.Rect(10, 250, 15, 15)
paddle_movement_x = 0
ball_direction = (1, 1)
balls = 3
done = False

while not done and balls > 0:

    # Process events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            paddle_movement_x = -2
        elif keys[pygame.K_RIGHT]:
            paddle_movement_x = 2
        else:
            paddle_movement_x = 0

    # Move paddle
    paddle.move_ip(paddle_movement_x, 0)
    paddle.clamp_ip(SCREEN_AREA)

    # Move ball
    ball.move_ip(*ball_direction)
    if ball.right > WIDTH or ball.left < 0:
        ball_direction = -ball_direction[0], ball_direction[1]
    elif ball.top < 0 or paddle.colliderect(ball):
        ball_direction = ball_direction[0], -ball_direction[1]
    elif ball.bottom > HEIGHT:
        balls = balls - 1
        ball_direction = (1, 1)
        ball = pygame.Rect(10, 250, 15, 15)

    ball.clamp_ip(SCREEN_AREA)

    # Redraw screen
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, paddle)
    pygame.draw.rect(screen, WHITE, ball)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()

这是代码,我基本上改变了球的方向:

    # Move ball
    ball.move_ip(*ball_direction)
    if ball.right > WIDTH or ball.left < 0:
        ball_direction = -ball_direction[0], ball_direction[1]
    elif ball.top < 0 or paddle.colliderect(ball):
        ball_direction = ball_direction[0], -ball_direction[1]
    elif ball.bottom > HEIGHT:
        balls = balls - 1
        ball_direction = (1, 1)
        ball = pygame.Rect(10, 250, 15, 15)