首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pygame中大小不同的精灵的碰撞

pygame中大小不同的精灵的碰撞
EN

Stack Overflow用户
提问于 2020-08-18 01:43:45
回答 1查看 51关注 0票数 1

我正在尝试编写一些类似于flappy bird游戏的代码。在我的程序中,我希望能够检查鸟是否与管道接触,如果是,游戏应该结束。我有一个管道类和一个鸟类,还有两个管道类的实例:

代码语言:javascript
运行
复制
bird = bird(125,185)
pipe1 = pipe(300, 255)
pipe2 = pipe(520, 255)

在我的管道类中,我有两个与管道高度和位置相关的方法:

代码语言:javascript
运行
复制
def __init__(self, x, y):
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,145))
    self.x = x
    self.y = y
    self.width = 52
    self.height = 145

def resize(self): # The height of the pipe will vary randomly
    self.height = random.randint(100,150)
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,self.height))
    self.y = 400 - self.height

def draw(self, win):
    if self.x > -52:
        self.x -= 5 # Pipe will move to the left of the screen
    else:
        self.x = 400
        self.resize()
    win.blit(self.bottom, (self.x,self.y))
    self.top = pygame.transform.rotate(self.bottom, 180) # There is a bottom pipe and a top pipe
    win.blit(self.top, (self.x,0)) 

正如您所看到的,对于每个管道类实例,屏幕上都绘制了两个管道。一个在屏幕顶部,另一个在底部与之相对。然后,两个管道都在屏幕上移动。

以下是bird类中的一些代码:

代码语言:javascript
运行
复制
def __init__(self, x, y):
    self.bird1 = pygame.transform.scale(pygame.image.load("bird1.png"), (34,24))
    self.bird2 = pygame.transform.scale(pygame.image.load("bird2.png"), (34,24))
    self.bird3 = pygame.transform.scale(pygame.image.load("bird3.png"), (34,24))
    self.birdpics = [self.bird1, self.bird2, self.bird3]
    self.x = x
    self.y = y
    self.width = 34
    self.height = 24
    self.moveCount = 0
    self.jumpCount = 7
    self.isJump = False
    self.image = self.birdpics[self.moveCount]

def draw(self, win):
    self.moveCount += 1
    if self.moveCount >= len(self.birdpics):
        self.moveCount = 0
    self.image = self.birdpics[self.moveCount]
    win.blit(self.image, (self.x,self.y)) 
    # The image of the bird on the screen switches between 3 images. The height and width of the bird stay constant.

当用户单击鼠标左键时,小鸟会跳跃,并可以根据用户的鼠标点击在管道之间“飞行”(每次单击鼠标时,小鸟的y坐标都会增加,而当用户什么都不做时,小鸟的y坐标会稳步减少)。现在我怎样才能找到一种方法来检测小鸟是否真的接触了其中一个管道,在这种情况下,游戏将结束?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-18 02:24:16

要检查pipe\bird碰撞,请在pipe类中为上下部管道创建一个矩形,然后创建一个测试碰撞的方法:

代码语言:javascript
运行
复制
def __init__(self, x, y):
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,145))
    self.x = x
    self.y = y
    self.width = 52
    self.height = 145
    self.rects = [] # top\bottom pipe

def resize(self): # The height of the pipe will vary randomly
    self.height = random.randint(100,150)
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,self.height))
    self.y = 400 - self.height

def draw(self, win):
    if self.x > -52:
        self.x -= 5 # Pipe will move to the left of the screen
    else:
        self.x = 400
        self.resize()
    win.blit(self.bottom, (self.x,self.y))
    self.top = pygame.transform.rotate(self.bottom, 180) # There is a bottom pipe and a top pipe
    win.blit(self.top, (self.x,0)) 
    # save rect list for top\bottom pipe
    self.rects=[
         pygame.rect((self.x,self.y),(self.width,self.height)), # bottom
         pygame.rect((self.x,0),(self.width,self.height))  # top
         ]

def CheckCollide(bird):  # did bird collide with pipe
    birdrect = pygamerect(bird.x. bird.y, bird.width, bird,height)
    for r in self.rects:
       if birdrect.colliderect(r):
           return True  # bird hit pipe
    return False # no collision
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63455996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档