2017-12-24 1 views
2

충돌이 있지만 코드가 블록에 닿으면 코드가 작동하기를 바랍니다. print('x'). 그것은 조류의 올바른 위치에 있지는 않지만 그렇게하고 있습니다. 긴 코드에 사과드립니다 만 실행하는 데 필요합니다.왜 pygame rect가 올바른 위치에 있지 않습니까?

import random 
import pygame 

vec = pygame.math.Vector2 

BLACK = (0,0,0) 
RED = (255,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(topleft=(x,y)) 

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

새는 enter image description here처럼 보이는, 그리고 나는 RECT가 enter image description here 싶습니다하지만 게임에 더 enter image description here처럼 작동합니다. 문제는 직사각형이 가장 자리에 그 중심을 가지고 있지만 그것이 어떻게 바뀌는지를 모른다는 것입니다.

답변

4

직사각형의 왼쪽 상단에 이미지를 블릿해야합니다. 당신은 topleft 속성 self.pos에, 당신은 여전히에서 이미지를 블럭 전송 할 수 self.pos에 중심을 설정하고 블리트 위치로 RECT를 통과 (다음 파이 게임은 왼쪽 상단 좌표를 사용),

self.rect.center = self.pos 
window.blit(self.skin, self.rect) 

또는 설정할 수 있습니다 self.pos :

self.rect.topleft = self.pos 
window.blit(self.skin, self.pos) 
1

파이 게임에서 직사각형 그리기, 제 2 개 파라미터들이 X 및 Y의 시작점이 후 다음의 두 파라미터는 폭과 높이이다. 그러면 왼쪽 상단 구석에 직사각형이 그려집니다. 당신이해야 할 일은, 새가 이미지의 가장자리 인 경우 가장자리를 이미지로 표현하는 것입니다. 당신은 단지 약간의 조정을위한 + 기호를 사용하여 사진을 편집 할 수없는 경우, 대체

pygame.draw.rect(screen, red, [topLeftX, topLeftY, birdWidth, birdHeight]) 

: 당신은 다음과 같은 작업을 수행 할 수 있습니다.

관련 문제