-1

我正在制作一个非常基本的游戏,并且我正在尝试让鸟(玩家)躲避岩石,如果鸟被岩石击中,它就会死亡。但我不知道如何让游戏知道鸭子是否被岩石击中。

这是我的代码:

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

4

2 回答 2

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... 

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

于 2013-04-25T22:58:58.730 回答
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)
于 2013-04-27T00:04:56.307 回答