2017-12-23 2 views
0

私はフラジィな鳥のレプリカを作っています。私は衝突の仕組みを得ることができません。現時点では私は.coliderect()を使用しようとしているが、私は100%確実ではない。ここに2つのクラスがあります。私はプログラムに何かをさせたい、あるいは鳥とパイプが衝突するときにはただprint('x')ですが、プログラムが何もしない、または出力しない瞬間に衝突したとき。pygameの衝突をどのようにするにはどうすればいいですか?

import pygame 

vec = pygame.math.Vector2 
BLACK = (0,0,0) 
WIDTH = 500 
HEIGHT = 400 

pygame.init() 
window = pygame.display.set_mode((WIDTH, HEIGHT)) 
pygame.display.set_caption('Flappy Bird') 
clock = pygame.time.Clock() 

class Bird(): 

    def __init__(self): 

     self.skin = pygame.image.load('bird2.png') 
     self.rect = self.skin.get_rect() 
     self.rect.center = (WIDTH/2, HEIGHT/2) 

     self.vx = 0 
     self.vy = 0 

     self.pos = vec(WIDTH/2, HEIGHT/2) 
     self.vel = vec(0, 0) 
     self.acc = vec(0, 0) 

    def update(self): 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

     window.fill(BLACK) 
     self.acc = vec(0, 0.7) #having 0.5 adds gravity 
     self.vy = 0 

     keys = pygame.key.get_pressed() 

     if keys[pygame.K_SPACE]: 
      self.vel.y = -7 

     if keys[pygame.K_LEFT]: 
      self.acc.x = -0.5 #change to y to add vertical motion 

     if keys[pygame.K_RIGHT]: 
      self.acc.x = 0.5 #change to y to add vertical motion 

     #applys friction 
     self.acc.x += self.vel.x * -0.08 #FRICTION 
     #motion equations 
     self.vel += self.acc 
     self.pos += self.vel + 0.5 * self.acc 

     self.rect.center = self.pos 

     window.blit(self.skin, self.pos) 


class Pipe(): 

    def __init__(self,x,y): 
     self.image = pygame.Surface((50,60)) 
     self.image.fill(RED) 
     self.rect = self.image.get_rect() 
     self.pos = vec(x,y) 

    def blit_pipe(self): 
     window.blit(self.image, self.pos) 

def border_check(): 
    if (flappy.pos.y)+32 > HEIGHT: #this is the very top of the flappy 
     print("You are under\n") 
     pygame.quit() 
     quit() 

    if flappy.pos.y < 0: 
     print("You are over\n") #this is the very top of the flappy 
     pygame.quit() 
     quit() 


pipey = Pipe(300,200) 
pipey.blit_pipe() 

pipey2 = Pipe(100,200) 
pipey2.blit_pipe() 

flappy = Bird() 

window.blit(flappy.skin, flappy.pos) 


while True: 

    border_check() 
    flappy.update() 
    pipey.blit_pipe() 
    pipey2.blit_pipe() 

    if flappy.rect.colliderect(pipey.rect): 
     print('x') 

    clock.tick(30) 
    pygame.display.update() 
+0

申し訳ありませんが、これまでに短縮しようとしました – Crawley

答えて

1

彼らがまだデフォルトcoordsの(0, 0)に位置しているので、あなたはPipeインスタンスのrect Sの位置を設定することはありません。座標を設定するにはいくつかの方法があります。例えば、topleft引数として座標をget_rectに渡すことができます。

self.rect = self.image.get_rect(topleft=(x, y)) 

衝突検出に問題がある場合は、実際の位置を確認するために矩形を印刷します。

+0

ありがとうございました!なぜそれは特別なことですか?それはポイント(0,0)があるかどうかですか? – Crawley

+1

これは、rectの左上の座標を 'x'と' y'に設定するだけです。また、他の[rect attributes](http://www.pygame.org/docs/ref/rect.html)を 'center'や' bottomleft'や 'x、y'のように使うこともできます:' self .image.get_rect(x = x、y = y) '。あるいは 'Bird'クラスと同じようにして、次の行に位置を設定することもできます。 – skrx

関連する問題