I'm currently making a shooter game and in my while run
function I call a class function called collision()
. I have it check if the player hitbox is colliding with any obj in the list. In this situation I'm checking if it's colliding with the ground. Then if it's not colliding with the ground I say that the gunner is not colliding with anything which then makes it into a falling state. Once it's in this falling state it changes the y by the speed of the obj until it starts colliding with something again. But when I run this code it just falls infinitely.
Here's a snippet of my code:
class Gunner(pygame.sprite.Sprite):
def __init__(self, frames, x, y, type, speed):
pygame.sprite.Sprite.__init__(self)
self.sprites = {
"running" : [get_image(bg_run_sheet, i, 48, 48, 1, 'Black') for i in range(bg_run_frames)],
"idle" : [get_image(bg_idle_sheet , i, 48, 48, 1, 'Black') for i in range(bg_idle_frames)]
}
self.current_sprite = 0
self.direction = 1
self.flip = False
self.idle = True
self.speed = speed
self.collide = True
self.falling = False
self.image = self.sprites["idle"][self.current_sprite]
self.rect = self.image.get_rect()
self.rect.bottomleft = (x, y)
self.type = type
self.hitbox = pygame.draw.rect(screen,(0,0,0),(self.rect.x + 11, self.rect.y + 6, 29, 37))
return
def update(self):
self.current_sprite += 1
if self.idle:
if self.current_sprite >= bg_idle_frames:
self.current_sprite = 0
self.image = self.sprites['idle'][self.current_sprite]
else:
if self.current_sprite >= bg_run_frames:
self.current_sprite = 0
self.image = self.sprites['running'][self.current_sprite]
if not self.collide:
self.falling = True
return
def move(self, moving_left, moving_right, floors):
dx = 0
dy = 0
if not self.falling:
if moving_left:
dx = -self.speed
self.flip = True
if moving_right:
self.flip = False
dx = self.speed
else:
dy += self.speed
self.rect.x += dx
self.rect.y += dy
self.hitbox.x = self.rect.x + 11
self.hitbox.y = self.rect.y + 6
def draw(self):
screen.blit(pygame.transform.flip(self.image.convert_alpha(), self.flip, False), self.rect)
def collision(self, list):
for i in range(len(list)):
if self.hitbox.colliderect(list[i]):
self.collide = True
break
else:
self.collide = False
return
So that's my gunner class
while run:
#What keys are pressed
keys = pygame.key.get_pressed()
#hitbox
floors = [pygame.draw.rect(screen,(0,0,0), (0, 207, 112, 2)), pygame.draw.rect(screen,(0,0,0), (0, 303, 63, 2)), pygame.draw.rect(screen,(0,0,0), (64, 319, 65, 2)), pygame.draw.rect(screen,(0,0,0), (128, 335, 176, 2)), pygame.draw.rect(screen,(0,0,0), (304, 255, 128, 2)), pygame.draw.rect(screen,(0,0,0), (432, 239, 32, 2)), pygame.draw.rect(screen,(0,0,0), (288, 127, 212, 2))]
#Screen color
screen.blit(bg_img,(0,0))
#players
player.move(moving_left, moving_right, floors)
player.draw()
#check if player is on the floor
player.collision(floors)
#Quit Program
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
player.idle = False
if event.key == pygame.K_a:
moving_left = True
player.idle = False
if event.key == pygame.K_d:
moving_right = True
player.idle = False
if event.type == pygame.KEYUP:
player.idle = True
moving_left = False
moving_right = False
moving_sprites.update()
pygame.display.flip()
pygame.display.update()
clock.tick(12)