import os import pygame from pygame.locals import * import sys import random import time pygame.init() vec = pygame.math.Vector2 HEIGHT = 450 WIDTH = 400 ACC = 0.5 FRIC = -0.12 FPS = 60 PLAYER_SIZE = (30, 30) FramePerSec = pygame.time.Clock() displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Cat Platformer") # --- Load Animated Background Frames --- bg_frames = [] bg_folder = "bg_frames" # Folder containing split gif frames like frame_001.png # Try to load frames safely if os.path.exists(bg_folder): for filename in sorted(os.listdir(bg_folder)): if filename.endswith((".png", ".jpg", ".gif")): try: frame = pygame.image.load(os.path.join(bg_folder, filename)).convert() frame = pygame.transform.scale(frame, (WIDTH, HEIGHT)) bg_frames.append(frame) except pygame.error as e: print(f"Failed to load {filename}: {e}") else: print("❌ Folder 'bg_frames' does not exist.") pygame.quit() sys.exit() # Check if any frames were loaded if not bg_frames: print("❌ No background frames were loaded! Make sure you have .png/.jpg/.gif files in 'bg_frames'") pygame.quit() sys.exit() bg_frame_index = 0 bg_frame_delay = 5 bg_timer = 0 def update_background(): global bg_timer, bg_frame_index bg_timer += 1 if bg_timer >= bg_frame_delay: bg_timer = 0 bg_frame_index = (bg_frame_index + 1) % len(bg_frames) displaysurface.blit(bg_frames[bg_frame_index], (0, 0)) CHARACTER_IMAGES = [ pygame.transform.scale(pygame.image.load("cat1.gif").convert_alpha(), PLAYER_SIZE), pygame.transform.scale(pygame.image.load("cat2.gif").convert_alpha(), PLAYER_SIZE), pygame.transform.scale(pygame.image.load("cat3.gif").convert_alpha(), PLAYER_SIZE), ] def draw_text(text, font_size, color, center): font = pygame.font.SysFont("Verdana", font_size) surface = font.render(text, True, color) rect = surface.get_rect(center=center) displaysurface.blit(surface, rect) def start_screen(): selected = 0 cat_spacing = 80 total_width = cat_spacing * (len(CHARACTER_IMAGES) - 1) start_x = WIDTH // 2 - total_width // 2 play_button = pygame.Rect(WIDTH//2 - 60, 300, 120, 40) while True: update_background() draw_text("Choose Your Cat!", 30, (255, 255, 255), (WIDTH // 2, 50)) for i, img in enumerate(CHARACTER_IMAGES): x = start_x + i * cat_spacing y = 150 border_rect = pygame.Rect(x - 5, y - 5, PLAYER_SIZE[0] + 10, PLAYER_SIZE[1] + 10) if i == selected: pygame.draw.rect(displaysurface, (255, 255, 0), border_rect, 3) displaysurface.blit(img, (x, y)) pygame.draw.rect(displaysurface, (50, 200, 50), play_button, border_radius=5) draw_text("PLAY", 20, (255, 255, 255), play_button.center) pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_LEFT: selected = (selected - 1) % len(CHARACTER_IMAGES) elif event.key == K_RIGHT: selected = (selected + 1) % len(CHARACTER_IMAGES) elif event.type == MOUSEBUTTONDOWN: if play_button.collidepoint(event.pos): return selected class Player(pygame.sprite.Sprite): def __init__(self, image): super().__init__() self.surf = image self.rect = self.surf.get_rect() self.pos = vec((10, 360)) self.vel = vec(0, 0) self.acc = vec(0, 0) self.jumping = False self.score = 0 def move(self): self.acc = vec(0, 0.5) pressed_keys = pygame.key.get_pressed() if pressed_keys[K_LEFT]: self.acc.x = -ACC if pressed_keys[K_RIGHT]: self.acc.x = ACC self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH self.rect.midbottom = self.pos def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits and not self.jumping: self.jumping = True self.vel.y = -15 def cancel_jump(self): if self.jumping and self.vel.y < -3: self.vel.y = -3 def update(self): hits = pygame.sprite.spritecollide(self, platforms, False) if self.vel.y > 0 and hits: if self.pos.y < hits[0].rect.bottom: if hits[0].point: hits[0].point = False self.score += 1 self.pos.y = hits[0].rect.top + 1 self.vel.y = 0 self.jumping = False class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((random.randint(50, 100), 12)) self.surf.fill((0, 255, 0)) self.rect = self.surf.get_rect(center=(random.randint(0, WIDTH - 10), random.randint(0, HEIGHT - 30))) self.point = True self.speed = random.randint(-1, 1) self.moving = True def move(self): hits = self.rect.colliderect(P1.rect) if self.moving: self.rect.move_ip(self.speed, 0) if hits: P1.pos += (self.speed, 0) if self.rect.left > WIDTH: self.rect.right = 0 if self.rect.right < 0: self.rect.left = WIDTH def check(platform, groupies): if pygame.sprite.spritecollideany(platform, groupies): return True for entity in groupies: if abs(platform.rect.top - entity.rect.bottom) < 50 and abs(platform.rect.bottom - entity.rect.top) < 50: return True return False def plat_gen(): while len(platforms) < 6: p = Platform() while check(p, platforms): p = Platform() platforms.add(p) all_sprites.add(p) def main_game(selected_cat): global P1, all_sprites, platforms P1 = Player(CHARACTER_IMAGES[selected_cat]) PT1 = Platform() PT1.surf = pygame.Surface((WIDTH, 20)) PT1.surf.fill((255, 0, 0)) PT1.rect = PT1.surf.get_rect(center=(WIDTH / 2, HEIGHT - 10)) PT1.point = False PT1.moving = False all_sprites = pygame.sprite.Group() platforms = pygame.sprite.Group() all_sprites.add(P1) all_sprites.add(PT1) platforms.add(PT1) for _ in range(random.randint(4, 5)): pl = Platform() while check(pl, platforms): pl = Platform() platforms.add(pl) all_sprites.add(pl) while True: P1.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_SPACE: P1.jump() if event.type == KEYUP: if event.key == K_SPACE: P1.cancel_jump() if P1.rect.top > HEIGHT: return if P1.rect.top <= HEIGHT / 3: P1.pos.y += abs(P1.vel.y) for plat in platforms: plat.rect.y += abs(P1.vel.y) if plat.rect.top >= HEIGHT: plat.kill() plat_gen() update_background() score_text = pygame.font.SysFont("Verdana", 20).render(str(P1.score), True, (123, 255, 0)) displaysurface.blit(score_text, (WIDTH / 2, 10)) for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) entity.move() pygame.display.update() FramePerSec.tick(FPS) # Game loop while True: cat_index = start_screen() main_game(cat_index)