我的 Hitbox 在 pygame 中无法正常工作

问题描述

我在解决游戏中的碰撞盒问题时遇到了麻烦。我有一个僵尸,当你攻击它时,它会返回 -15 或 +15,这取决于它面向的位置。它有效。但我的问题是,当僵尸到达某个坐标时,它会以与之前行走的方式相反的方式行走。 这让玩家有机会从后面攻击僵尸,如果你攻击它,它会向后移动 -15,但我希望当你从后面攻击它时它会向前移动。 这是所有命中框的代码

class Enemy(object):

    ZwalkRight = [pygame.image.load('ZR1.png'),pygame.image.load('ZR2.png'),pygame.image.load('ZR1.png'),]
    ZwalkLeft = [pygame.image.load('ZL1.png'),pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'),]
    #this imports the images of the zombie
    def __init__(self,x,y,width,height,end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x,self.end]
        self.walkcount = 0
        self.velo = 3
        self.hitBox = (self.x + 20,self.y,28,60)
        self.health = 10
        self.visible = True
        #sets all the varibles for the zombie



    def draw(self,win):
        self.move()#this tells it to do everthin in the move function when it is drawn
        if self.visible:#this tells the program to dellete itself if it is 'dead'.
            if self.walkcount + 1 >= 33:
                self.walkcount = 0

            if self.velo > 0:
                screen.blit(self.ZwalkRight[self.walkcount//3],(self.x,self.y))#this sets the walking animation for when it moves right
                self.walkcount += 1
            else:
                screen.blit(self.ZwalkLeft[self.walkcount //3],self.y))#same thing  here exept its for when it moves left
                self.walkcount += 1
            pygame.draw.rect(screen,(255,0),(self.hitBox[0],self.hitBox[1] - 20,50,10))# this is the health segment for the zombie
            pygame.draw.rect(screen,(0,255,50 - ((50/10) * (10 - self.health)),10))
            self.hitBox = (self.x,44,74)

以及第二个僵尸或克隆

class Enemy1(object):

    ZwalkRight = [pygame.image.load('ZR1.png'),74)

这是玩家的hitBox

class Player(object):
    #filles all the code for the player

    def __init__(self,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.velo = 8
        self.left = False
        self.right = False
        self.walkcount = 0
        self.Idlecount = 0
        self.guncount = 0
        self.gunisfired = False
        self.isIdle = False
        self.standing = True
        self.punchislaunched = False
        self.punchcount = 0
        self.hitBox = (self.x + 20,60)
        self.health = 10
        #sets the varibles

    def draw(self,screen):
        #draws all the chracter animations to the screen
        if self.walkcount + 1 >= 27:  #<------------ this tells the project to set the players walking image to the first one
            self.walkcount = 0
        elif self.Idlecount + 1 >= 27: #<------------ same thing exept its for the idle animation
            self.Idlecount = 0
        if not(self.standing):#<---------- this signifies whether the character is walking or standing
            if self.left:
                screen.blit(walkLeft[self.walkcount//3],self.y))# <--------- tels it that when it moves left it plays a walking animation
                self.walkcount += 1
                #if self.gunisfired == True:
                #   if self.guncount//5 >= len(ShootR):
                #       self.guncount = 0
                #       self.gunisfired == False
                #   screen.blit(ShootR[self.guncount//5],self.y))
                #   self.guncount += 1

            if self.right:
                screen.blit(walkRight[self.walkcount//3],self.y)) # < ------- same thing here exept its for right
                self.walkcount += 1
                #if self.gunisfired == True:
                #   if self.guncount//5 >= len(ShootR):
                #       self.guncount = 0
                #       self.gunisfired == False
                #   screen.blit(ShootR[self.guncount//5],self.y))
                #   self.guncount += 1
        else: # this else controls what happens if you are not standing
            if self.left:# this means that if you were last looking left you will be able to do some certain actions whilst facing left
                if self.isIdle == True:# this means if you last walked left the idle animation will face left
                    if self.Idlecount//3 >= len(IdleL):
                        self.Idlecount = 0
                        self.isIdle == False
                    screen.blit(IdleL[self.Idlecount//3],self.y))
                    self.Idlecount += 1
                if self.gunisfired == True:# same thing here exept its for the gun shot animation
                    if self.guncount//7 >= len(ShootL):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootL[self.guncount//7],self.y))
                    self.guncount += 1
                if self.punchislaunched == True:# it applies here as well but it is for the punch animation
                    if self.punchcount//5 >= len(PunchL):
                        self.punchcount = 0
                        self.punchislaunched == False
                    screen.blit(PunchL[self.punchcount//5],self.y))
                    self.punchcount += 1
            else:# this else is to signify that everything that happend in the if statments
                if self.isIdle == True:
                    if self.Idlecount//3 >= len(IdleR):
                        self.Idlecount = 0
                        self.isIdle == False
                    screen.blit(IdleR[self.Idlecount//3],self.y))
                    self.Idlecount += 1
                if self.gunisfired == True:
                    if self.guncount//7 >= len(ShootR):
                        self.guncount = 0
                        self.gunisfired == False
                    screen.blit(ShootR[self.guncount//7],self.y))
                    self.guncount += 1
                if self.punchislaunched == True:
                    if self.punchcount//5 >= len(PunchR):
                        self.punchcount = 0
                        self.punchislaunched == False
                    screen.blit(PunchR[self.punchcount//5],self.y))
                    self.punchcount += 1
        self.hitBox = (self.x,51,74)# this create an invisible hitBox
        pygame.draw.rect(screen,10))#this makes the health bar
        pygame.draw.rect(screen,10))#this code is to tell the program that when it losses health it clears a segment of green and replaces it with red
        #pygame.draw.rect(screen,self.hitBox,2)

        pygame.display.update()

这是我设置碰撞的地方

velotimeser = 1

timeForLevel = 0
font = pygame.font.SysFont('comicsans',30,True)
man = Player(300,508,64,64)
zombie = Enemy(100,450)
zombie1 = Enemy1(100,450)
randomside = 0
gunCapacity = 1
points = 0
bullets = []
intro = True
game_intro()
pygame.mixer.music.play(-1)

 # in seconds

run = True

while run:

    clock.tick(27)
    #this is the hitBox for when the zombie collides with the player and the player hasnt shot a bullet or has launched a punch that it should deduct a peice of health and 5 points and move the player back depending on where the zombie is facing


    timeForLevel += 0.05
    
    


    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == False and zombie.visible == True and zombie.velo > 0 and man.x > zombie.x:
            man.x += 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == False and zombie.visible == True and zombie.velo > 0 and man.x < zombie.x:
            man.x -= 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == False and zombie.visible == True and zombie.velo < 0 and man.x < zombie.x:
            man.x -= 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == False and zombie.visible == True and zombie.velo < 0 and man.x > zombie.x:
            man.x += 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    #same thing here expet its for when the zombie is facing left       
    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == False and zombie.visible == True and zombie.velo < 0:
            man.x -= 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    #this hitBox detects if the player is touching the zombie whilst punching it
    if man.hitBox[1] < zombie.hitBox[1] + zombie.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie.hitBox[0] and man.hitBox[0] < zombie.hitBox[0] + zombie.hitBox[2] and man.punchislaunched == True and zombie.visible == True:
            zombie.hit()
            points += 5 

    #this is the hitBox for when the zombie collides with the player and the player hasnt shot a bullet or has launched a punch that it should deduct a peice of health and 5 points and move the player back depending on where the zombie is facing
    if man.hitBox[1] < zombie1.hitBox[1] + zombie1.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie1.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie1.hitBox[0] and man.hitBox[0] < zombie1.hitBox[0] + zombie1.hitBox[2] and man.punchislaunched == False and zombie1.visible == True and zombie.velo > 0:
            man.x += 15
            pygame.time.delay(10)
            points -= 5
            man.hit()
            
    #same thing here expet its for when the zombie is facing left       
    if man.hitBox[1] < zombie1.hitBox[1] + zombie1.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie1.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie1.hitBox[0] and man.hitBox[0] < zombie1.hitBox[0] + zombie1.hitBox[2] and man.punchislaunched == False and zombie1.visible == True and zombie1.velo < 0:
            man.x -= 15
            pygame.time.delay(10)
            points -= 5
            man.hit()

    #this hitBox detects if the player is touching the zombie whilst punching it
    if man.hitBox[1] < zombie1.hitBox[1] + zombie1.hitBox[3] and man.hitBox[1] + man.hitBox[3] > zombie1.hitBox[1]:
        if man.hitBox[0] + man.hitBox[2] > zombie1.hitBox[0] and man.hitBox[0] < zombie1.hitBox[0] + zombie1.hitBox[2] and man.punchislaunched == True and zombie1.visible == True:
            zombie1.hit()
            points += 5 

    #this tels it to shoot one bullet at a time instead of sending a group of bullets to the zombie
    if gunCapacity > 0:
        gunCapacity += 1
    if gunCapacity> 10:
        gunCapacity = 0

    #this tels it to close the programe if you press the big red cross at the top right
    for event in pygame.event.get():


        if event.type == pygame.QUIT:
            run = False
       
    #this sets the collision for when the zombie  touches the bullet
    for bullet in bullets:
        if bullet.y - bullet.radius < zombie.hitBox[1] + zombie.hitBox[3] and bullet.y + bullet.radius > zombie.hitBox[1]:
            if bullet.x + bullet.radius > zombie.hitBox[0] and bullet.x - bullet.radius < zombie.hitBox[0] + zombie.hitBox[2] and zombie.visible == True:
                hitsound.play()
                zombie.hit()
                points += 1
                bullets.pop(bullets.index(bullet))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)