为什么pygame rect不在正确的位置?

时间:2017-12-24 14:18:18

标签: python pygame

我遇到了碰撞,但是我希望我的代码可以工作,以便当鸟接触到这些块时,print('x')。它正在这样做,但不是在鸟的正确位置。为长代码道歉,但它需要运行。

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,我希望矩形为enter image description here,但在游戏中它更像enter image description here。我认为问题在于矩形的中心位于topleft,但我不知道如何改变它

2 个答案:

答案 0 :(得分:4)

您需要在矩形的左上角处对图像进行blit。您可以将中心设置为self.pos并将矩形作为blit位置传递(然后pygame使用左上角坐标),

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

或将topleft属性设置为self.pos,然后您仍然可以在self.pos处对图像进行blit:

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

答案 1 :(得分:1)

在pygame中绘制矩形时,前两个参数是X和Y起点,接下来的两个参数是宽度和高度。这将从左上角绘制矩形。你需要做的是,如果鸟是图像的边缘,则有一个图像的边缘。然后你可以做类似的事情:

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

另一种方法是,如果您无法修改图片,只需使用+符号进行微调。