When the image is more than 100 pixels the hitbox isn't properly aligned and so so the objects pass through the image, part of the image can exit out of bounds and the image of the character can't go to the top of the screen. Here is the code if someone can help.
import random import pygame from pygame.constants
import QUIT, K_DOWN, K_UP, K_LEFT, K_RIGHT
pygame.init()
FPS = pygame.time.Clock()
HEIGHT = 800
WIDTH = 1200
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
COLOR_BLUE = (0, 0, 255)
COLOR_GREEN = (0, 255, 0)
main_display = pygame.display.set_mode((WIDTH, HEIGHT))
bg = pygame.transform.scale(pygame.image.load("floor master 6.png"), (WIDTH, HEIGHT))
bg_X1 = 0
bg_X2 = bg.get_width()
bg_move = 3
class Player:
def init(self, image, position, hitbox_size, move_speed):
self.image = image
self.rect = pygame.Rect(position, hitbox_size)
self.hitbox_size = hitbox_size
self.move_speed = move_speed
def update(self, keys):
if keys[K_DOWN] and self.rect.bottom < HEIGHT:
self.rect.y += self.move_speed
if keys[K_RIGHT] and self.rect.right < WIDTH:
self.rect.x += self.move_speed
if keys[K_LEFT] and self.rect.left > 0:
self.rect.x -= self.move_speed
if keys[K_UP] and self.rect.top > 0:
self.rect.y -= self.move_speed
def draw(self, surface):
surface.blit(self.image, self.rect)
def create_enemy():
enemy_size = (30, 30)
enemy = pygame.Surface(enemy_size)
enemy.fill(COLOR_BLUE)
enemy_rect = pygame.Rect(WIDTH, random.randint(0, HEIGHT), *enemy_size)
enemy_move = [random.randint(-10, -1), 0]
return [enemy, enemy_rect, enemy_move]
def create_bonus():
bonus_size = (30, 30)
bonus = pygame.Surface(bonus_size)
bonus.fill(COLOR_GREEN)
bonus_rect = pygame.Rect(random.randint(0, WIDTH - bonus_size[0]), 0, *bonus_size)
bonus_speed = [0, random.randint(2, 8)]
return [bonus, bonus_rect, bonus_speed]
CREATE_ENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATE_ENEMY, 1500)
CREATE_BONUS = pygame.USEREVENT + 2
pygame.time.set_timer(CREATE_BONUS, 4000)
enemies = []
bonuses = []
score = 0
hitbox_width = 300 # Replace with the desired hitbox width
hitbox_height = 300 # Replace with the desired hitbox height
hitbox_x = (WIDTH - hitbox_width) // 2
hitbox_y = (HEIGHT - hitbox_height) // 2
player_image = pygame.transform.scale(pygame.image.load("knight yes.png").convert_alpha(), (300, 300))
player = Player(player_image, (hitbox_x, hitbox_y), (hitbox_width, hitbox_height), 5)
playing = True
while playing:
FPS.tick(120)
for event in pygame.event.get():
if event.type == QUIT:
playing = False
if event.type == CREATE_ENEMY:
enemies.append(create_enemy())
if event.type == CREATE_BONUS:
bonuses.append(create_bonus())
keys = pygame.key.get_pressed()
player.update(keys)
bg_X1 -= bg_move
bg_X2 -= bg_move
if bg_X1 < -bg.get_width():
bg_X1 = bg.get_width()
if bg_X2 < -bg.get_width():
bg_X2 = bg.get_width()
main_display.blit(bg, (bg_X1, 0))
main_display.blit(bg, (bg_X2, 0))
for enemy in enemies:
enemy[1] = enemy[1].move(enemy[2])
main_display.blit(enemy[0], enemy[1])
if player.rect.colliderect(enemy[1]):
playing = False
for bonus in bonuses:
bonus[1] = bonus[1].move(bonus[2])
main_display.blit(bonus[0], bonus[1])
if player.rect.colliderect(bonus[1]):
score += 100
bonuses.pop(bonuses.index(bonus))
main_display.blit(player.image, player.rect)
main_display.blit(pygame.font.Font(None, 36).render(str(score), True, COLOR_WHITE), (WIDTH-50, 20))
pygame.display.flip()
for enemy in enemies:
if enemy[1].left < 0:
enemies.pop(enemies.index(enemy))
for bonus in bonuses:
if bonus[1].top > HEIGHT:
bonuses.pop(bonuses.index(bonus))
pygame.quit()
I tried creating variables for the size of the hitboxes, changing the hitbox size and the image size, asking chatGPT but nothing seemed to fix the issue.