0

I am having trouble with fixing this hitbox problem in my game. I have a zombie that is meant to go back when you attack it -15 or +15 in its X depending on where it is facing. and it works. but my problem is that when the zombie gets to a certain coordinate it goes the opposite way it was previously walking. that gives the player a chance to attack the zombie from behind and if you attack it it goes backwards by -15 but I want it to go forward when you attack it from the back. here is the code for all the hitboxes

class Enemy(object):

    ZwalkRight = [pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'), pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'),pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'), pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),]
    ZwalkLeft = [pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'),pygame.image.load('ZL1.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.x, self.y))#same thing  here exept its for when it moves left
                self.walkcount += 1
            pygame.draw.rect(screen, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))# this is the health segment for the zombie
            pygame.draw.rect(screen, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - ((50/10) * (10 - self.health)), 10))
            self.hitbox = (self.x, self.y, 44, 74)

and for the second zombie or clone

class Enemy1(object):

    ZwalkRight = [pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'), pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'),pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'), pygame.image.load('ZR1.png'), pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),pygame.image.load('ZR2.png'),]
    ZwalkLeft = [pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'), pygame.image.load('ZL2.png'),pygame.image.load('ZL1.png'),pygame.image.load('ZL1.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.x, self.y))#same thing  here exept its for when it moves left
                self.walkcount += 1
            pygame.draw.rect(screen, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))# this is the health segment for the zombie
            pygame.draw.rect(screen, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - ((50/10) * (10 - self.health)), 10))
            self.hitbox = (self.x, self.y, 44, 74)

and this is the hitbox for the player

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

    def __init__(self, x, y, width, 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, self.y, 28, 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.x,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.x,self.y))
                #   self.guncount += 1

            if self.right:
                screen.blit(walkRight[self.walkcount//3], (self.x,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.x,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.x,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.x,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.x,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.x,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.x,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.x,self.y))
                    self.punchcount += 1
        self.hitbox = (self.x, self.y, 51, 74)# this create an invisible hitbox
        pygame.draw.rect(screen, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))#this makes the health bar
        pygame.draw.rect(screen, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 50 - ((50/10) * (10 - self.health)), 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, (255,0,0), self.hitbox,2)

        pygame.display.update()

and this is where I set the collisions

velotimeser = 1

timeForLevel = 0
font = pygame.font.SysFont('comicsans', 30, True)
man = Player(300, 508, 64, 64)
zombie = Enemy(100, 508, 64, 64, 450)
zombie1 = Enemy1(100, 508, 64, 64, 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))
Zina
  • 149
  • 8
  • Use [`pygame.Rect`](https://www.pygame.org/docs/ref/rect.html) objects and [`colliderect()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect) to detect the collision between the bounding rectangles of 2 objects or 2 images – Rabbid76 Dec 30 '20 at 15:07
  • Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Rabbid76 Dec 30 '20 at 15:10

0 Answers0