首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我已经为我的flappy龙创建了hitboxes,但是hitboxes要么不碰撞,要么不产生输出。我该怎么解决这个问题呢?

我已经为我的flappy龙创建了hitboxes,但是hitboxes要么不碰撞,要么不产生输出。我该怎么解决这个问题呢?
EN

Stack Overflow用户
提问于 2020-01-12 00:01:44
回答 1查看 29关注 0票数 0

我正在尝试为我的Python编程类创建一个flappybird游戏,我使用了像drag.hit()这样的提示,这会导致它们在每次命中框发生冲突时打印(“点击”)。然而,龙和笨拙的hitbox的碰撞不起作用,我似乎不能让它起作用。

下面是我的代码:

代码语言:javascript
复制
    pygame.init()

class player(object):   **class for dragon**
    def __init__(self, x, y, width, height):
        self.hitbox = (self.x + 5, self.y +3, 67, 65)

 def draw(self,win):     **drawing of hitbox for dragon**
        self.hitbox = (self.x + 5, self.y +3, 67, 65)     
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def hit(self):        **During collision of hitbox, print hit**
        print("hit")


class stumpy(object):    **class for obstacle 1**
    def __init__(self, x, y, width, height):
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-15)


    def draw(self, win):
        win.blit(pygame.transform.scale(self.walkLeft[self.count//3], (75, 200)), (self.x, self.y))      #fx to scale up or down object. transform, dimension, placement
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)  #arguments - window, colour, dimension, thickness
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-15) #x-start, y-start, length, breath

class stumper(object):      **class for obstacle 2**
    def __init__(self, x, y, width, height):

        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-20)     


    def draw(self,win):
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-20)
        pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
        win.blit(pygame.transform.scale(self.walkLeft[self.count//1], (75,200)), (self.x,self.y))


def redrawGameWindow():
    global walkCount
    win.blit(bg, (bgX,0))  # This will draw our background image at (0,0)
    win.blit(bg, (bgX2,0))  # This will draw our background image at (1024,0)
    drag.draw(win)

    for obstacle in obstacles:
        obstacle.draw(win)

    pygame.display.update()

speed = 30             **assign variable for fps**
drag = player(95, 396//2, 75, 73)       #class for dragon, unnecessary
stump = stumpy(810, 310, 75, 200)     #class for stump, unnecessary
Istump = stumper(810, -20, 75, 200)


obstacles= []
#main loop
run = True
while run:
    redrawGameWindow()
    pygame.time.delay(25)

    for obstacle in obstacles:

        if drag.hitbox[1] < Istump.hitbox[1] + Istump.hitbox[3] and drag.hitbox[1] + drag.hitbox[3] > stump.hitbox[1]:      #must create player instance for this to work so using class is useless
            if drag.hitbox[0] + drag.hitbox[2] > Istump.hitbox[0] and drag.hitbox[0] < Istump.hitbox[0] + Istump.hitbox[2]:
                 if drag.hitbox[0] + drag.hitbox[2] > stump.hitbox[0] and drag.hitbox[0] < stump.hitbox[0] + stump.hitbox[2]:
                     drag.hit()
                     obstacles.append(stumpy(250, 250, 100, 100))



    redrawGameWindow()

pygame.quit()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-15 15:03:59

在你的player类中,对你的hitbox使用pygame rect:

代码语言:javascript
复制
self.hitbox = pygame.Rect(self.x + 5, self.y +3, 67, 65)

然后对您的对象执行相同的操作,以便玩家和障碍物都是pygame.Rect对象。要检测玩家是否在障碍物中,只需使用:

代码语言:javascript
复制
if player.hitbox.colliderect(obstacle.hitbox):
    print('hit')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59696071

复制
相关文章

相似问题

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