2013-04-25 112 views
-1

我正在製作一款非常基礎的遊戲,我正在努力使鳥(玩家)能夠躲閃岩石,如果鳥兒被岩石擊中,它就會死亡。但我不知道如何讓遊戲知道鴨子是否被撞到了岩石上。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

回答

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

這將根據鳥的居中位置進行擴展。

+0

的spritecollideany的工作,但我想我已經創建rects什麼做esiest方式 – Serial 2013-04-26 01:23:53

+0

對不起,它一直因爲我已經使用了pygame。此參考可能有所幫助: http://www.pygame.org/docs/ref/rect.html 您需要做的是給它一個屬性;也許加載圖像到矩形? 我沒有pygame安裝,所以我不能嘗試和檢查 - 對不起 – elder4222 2013-04-26 01:31:34

+0

確定感謝您的幫助,但! – Serial 2013-04-26 01:39:27