import pygame import sys # Initialize Pygame pygame.init() # Screen dimensions screen_width = 1000 screen_height = 1000 tile_size = 50 black = (0, 0, 0) player_size = 65 # Create the screen screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Pygame Structure") def draw_grid(): # Draw vertical lines for x in range(0, screen_width, tile_size): pygame.draw.line(screen, black, (x, 0), (x, screen_height)) # Draw horizontal lines for y in range(0, screen_height, tile_size): pygame.draw.line(screen, black, (0, y), (screen_width, y)) # Load the background image background_image = pygame.image.load('images/Background/BG.png') background_image = pygame.transform.scale(background_image, (screen_width, screen_height)) # Load images idle_images = [pygame.transform.scale(pygame.image.load(f'images/Player/idle ({i}).png'), (player_size, player_size)) for i in range(1, 16)] run_images = [pygame.transform.scale(pygame.image.load(f'images/Player/Run ({i}).png'), (player_size, player_size)) for i in range(1, 16)] jump_images = [pygame.transform.scale(pygame.image.load(f'images/Player/Jump ({i}).png'), (player_size, player_size)) for i in range(1, 16)] # Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.idle_images = idle_images self.run_images = run_images self.jump_images = jump_images self.image = self.idle_images[0] self.rect = self.image.get_rect() self.rect.center = (screen_width // 2, screen_height // 2) self.vel_y = 0 self.on_ground = False self.flip = False self.anim_index = 0 self.anim_speed = 0.5 self.anim_images = self.idle_images def update(self): keys = pygame.key.get_pressed() self.handle_movement(keys) self.handle_animation() # Gravity self.vel_y += 0.75 if self.vel_y > 10: self.vel_y = 10 # Update position self.rect.y += self.vel_y # Screen collision if self.rect.bottom > screen_height: self.rect.bottom = screen_height self.vel_y = 0 self.on_ground = True elif self.rect.top < 0: self.rect.top = 0 if self.rect.right > screen_width: self.rect.right = screen_width elif self.rect.left < 0: self.rect.left = 0 def handle_movement(self, keys): if keys[pygame.K_LEFT]: self.rect.x -= 5 self.flip = True self.anim_images = self.run_images elif keys[pygame.K_RIGHT]: self.rect.x += 5 self.flip = False self.anim_images = self.run_images else: self.anim_images = self.idle_images if (keys[pygame.K_SPACE] or keys[pygame.K_UP]) and self.on_ground: self.vel_y = -15 self.on_ground = False if not self.on_ground: self.anim_images = self.jump_images def handle_animation(self): self.anim_index += self.anim_speed if self.anim_index >= len(self.anim_images): self.anim_index = 0 self.image = self.anim_images[int(self.anim_index)] if self.flip: self.image = pygame.transform.flip(self.image, True, False) # Create player sprite group player = Player() all_sprites = pygame.sprite.Group(player) # Main loop clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update all_sprites.update() # Draw screen.blit(background_image, (0, 0)) draw_grid() all_sprites.draw(screen) # Update display pygame.display.flip() clock.tick(60) # Quit Pygame pygame.quit() sys.exit()