Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我已经为我的flappy dragon创建了Hitbox,但是这些Hitbox要么没有碰撞,要么没有';我不能产出。我该如何解决这个问题?_Python_Python 3.x_Pygame_Pygame Clock_Pygame Tick - Fatal编程技术网

Python 我已经为我的flappy dragon创建了Hitbox,但是这些Hitbox要么没有碰撞,要么没有';我不能产出。我该如何解决这个问题?

Python 我已经为我的flappy dragon创建了Hitbox,但是这些Hitbox要么没有碰撞,要么没有';我不能产出。我该如何解决这个问题?,python,python-3.x,pygame,pygame-clock,pygame-tick,Python,Python 3.x,Pygame,Pygame Clock,Pygame Tick,我正试图为我的Python编程类制作一个flappybird游戏,我使用了drag.hit()这样的提示,每次假设Hitbox发生碰撞时,它们都会打印(“hit”)。然而,巨龙和矮胖的hitbox的碰撞不起作用,我似乎无法让它起作用 这是我的密码: pygame.init() class player(object): **class for dragon** def __init__(self, x, y, width, height): self.hit

我正试图为我的Python编程类制作一个flappybird游戏,我使用了drag.hit()这样的提示,每次假设Hitbox发生碰撞时,它们都会打印(“hit”)。然而,巨龙和矮胖的hitbox的碰撞不起作用,我似乎无法让它起作用

这是我的密码:

    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()
pygame.init()
职业玩家(对象):**龙的职业**
定义初始值(自、x、y、宽度、高度):
self.hitbox=(self.x+5,self.y+3,67,65)
def抽奖(自我,获胜):**龙的hitbox抽奖**
self.hitbox=(self.x+5,self.y+3,67,65)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
def命中(自身):**碰撞hitbox时,打印命中**
打印(“点击”)
树桩类(对象):**障碍物1类**
定义初始值(自、x、y、宽度、高度):
self.hitbox=(self.x+8,self.y+10,self.width-18,self.height-15)
def抽签(自我,赢):
win.blit(pygame.transform.scale(self.walkLeft[self.count//3],(75200)),(self.x,self.y))#fx来放大或缩小对象。变换、标注、放置
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)#参数-窗口,颜色,尺寸,厚度
self.hitbox=(self.x+8,self.y+10,self.width-18,self.height-15)#x-start,y-start,长度,呼吸
类树桩(对象):**障碍物2类**
定义初始值(自、x、y、宽度、高度):
self.hitbox=(self.x+8,self.y+10,self.width-18,self.height-20)
def抽签(自我,赢):
self.hitbox=(self.x+8,self.y+10,self.width-18,self.height-20)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
win.blit(pygame.transform.scale(self.walkLeft[self.count//1],(75200)),(self.x,self.y))
def重画游戏窗口():
全局步行计数
blit(bg,(bgX,0))#这将在(0,0)处绘制我们的背景图像
blit(bg,(bgX2,0))#这将在(1024,0)处绘制我们的背景图像
拖动。拖动(赢)
障碍物中的障碍物:
障碍。平局(赢)
pygame.display.update()
速度=30**为fps分配变量**
drag=玩家(95396//2,75,73)#龙类,不需要
树桩=树桩(81031075200)#树桩等级,不需要
Istump=stumper(810,-20,75200)
障碍=[]
#主回路
运行=真
运行时:
重画游戏窗口()
pygame。时间。延迟(25)
障碍物中的障碍物:
如果drag.hitbox[1]stump.hitbox[1]:#必须创建播放器实例才能工作,因此使用类是无用的
如果drag.hitbox[0]+drag.hitbox[2]>Istump.hitbox[0]和drag.hitbox[0]stump.hitbox[0]和drag.hitbox[0]
在玩家类中,将pygame rect用于你的hitbox:

self.hitbox = pygame.Rect(self.x + 5, self.y +3, 67, 65)
然后对您的对象执行同样的操作,以便玩家和障碍物都是pygame.Rect对象。要检测玩家是否处于障碍物中,您只需使用:

if player.hitbox.colliderect(obstacle.hitbox):
    print('hit')
用于Hitboxism,并通过查找碰撞