import pygame import os pygame.init() SCREEN_WIDTH = 800 SCREEN_HEIGHT = int(SCREEN_WIDTH * 0.8) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Super Goomba') # Set FPS clock = pygame.time.Clock() FPS = 60 # Define Game Variables GRAVITY = 0.75 TILE_SIZE = 40 moving_left = False moving_right = False #Load Images bullet_img = pygame.image.load('Images/icons/fireball.png').convert_alpha() item_box = pygame.image.load('Images/icons/item_box.png').convert_alpha() # Define Colours BG = (144, 201, 120) RED = (255, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLACK = (0, 0, 0) #define font font = pygame.font.SysFont('Futura', 30) def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) def draw_bg(): screen.fill(BG) pygame.draw.line(screen, RED, (0, 300), (SCREEN_WIDTH, 300)) class Player(pygame.sprite.Sprite): def __init__(self, char_type, x, y, scale, speed, ammo): pygame.sprite.Sprite.__init__(self) self.alive = True self.char_type = char_type self.speed = speed self.ammo = ammo self.start_ammo = ammo self.shoot_cooldown = 0 self.health = 100 self.max_health = self.health self.direction = 1 self.vel_y = 0 self.jump = False self.in_air = True self.flip = True self.animation_list = [] self.frame_index = 0 self.action = 0 self.update_time = pygame.time.get_ticks() # Load all images for player animation_types = ['Idle', 'Run', 'Jump', 'Death'] for animation in animation_types: # Reset temporary list of images temp_list = [] num_of_frames = len(os.listdir(f'Images/{self.char_type}/{animation}')) for i in range(num_of_frames): img = pygame.image.load(f'Images/{self.char_type}/{animation}/{i}.png') img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale))) temp_list.append(img) self.animation_list.append(temp_list) self.image = self.animation_list[self.action][self.frame_index] self.rect = self.image.get_rect() self.rect.center = (x, y) def update(self): self.update_animation() #self.check_alive() #update cooldown if self.shoot_cooldown > 0: self.shoot_cooldown -= 1 def move(self, moving_left, moving_right): dx = 0 dy = 0 # Left and Right Movement if moving_left: dx = -self.speed self.flip = False self.direction = -1 if moving_right: dx = self.speed self.flip = True self.direction = 1 # Jump if self.jump and not self.in_air: self.vel_y = -11 self.jump = False self.in_air = True # Gravity self.vel_y += GRAVITY if self.vel_y > 10: self.vel_y = 10 dy += self.vel_y # Collision with Floor if self.rect.bottom + dy > 300: dy = 300 - self.rect.bottom self.in_air = False self.rect.x += dx self.rect.y += dy def shoot(self): if self.shoot_cooldown == 0 and self.ammo > 0: self.shoot_cooldown = 20 bullet = Bullet(self.rect.centerx + (0.6 * self.rect.size[0] * self.direction), self.rect.centery, self.direction) bullet_group.add(bullet) self.ammo -= 1 def update_animation(self): ANIMATION_COOLDOWN = 100 if self.action < len(self.animation_list): if self.frame_index < len(self.animation_list[self.action]): self.image = self.animation_list[self.action][self.frame_index] # Check if enough time has passed since the last update if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN: self.update_time = pygame.time.get_ticks() self.frame_index += 1 # If the animation has run out of frames, reset to the start if self.frame_index >= len(self.animation_list[self.action]): if self.action == 3: self.frame_index = len(self.animation_list[self.action]) - 1 else: self.frame_index = 0 def update_action(self, new_action): # Check if the new action is different to the previous one if new_action != self.action: self.action = new_action # Update the animation settings self.frame_index = 0 self.update_time = pygame.time.get_ticks() def update_action(self, new_action): #check if the new action is different to the previous one if new_action != self.action: self.action = new_action #update the animation settings self.frame_index = 0 self.update_time = pygame.time.get_ticks() def draw(self): screen.blit(pygame.transform.flip(self.image, self.flip, False), self.rect) class ItemBox(pygame.sprite.Sprite): def __init__(self, item_type, x, y): pygame.sprite.Sprite.__init__(self) self.item_type = item_type self.image = item_box[self.item_type] self.rect = self.image.get_rect() self.rect.midtop = (x + TILE_SIZE // 2, y + (TILE_SIZE - self.image.get_height())) def update(self): #check if the player has picked up the box if pygame.sprite.collide_rect(self, player): #delete the item box self.kill() class HealthBar(): def __init__(self, x, y, health, max_health): self.x = x self.y = y self.health = health self.max_health = max_health def draw(self, health): #update with new health self.health = health #calculate health ratio ratio = self.health / self.max_health pygame.draw.rect(screen, BLACK, (self.x - 2, self.y - 2, 154, 24)) pygame.draw.rect(screen, RED, (self.x, self.y, 150, 20)) pygame.draw.rect(screen, GREEN, (self.x, self.y, 150 * ratio, 20)) class Bullet(pygame.sprite.Sprite): def __init__(self, x, y, direction): pygame.sprite.Sprite.__init__(self) self.speed = 10 self.image = bullet_img self.rect = self.image.get_rect() self.rect.center = (x, y) self.direction = direction def update(self): #move bullet self.rect.x += (self.direction * self.speed) #check if bullet has gone off screen if self.rect.right < 0 or self.rect.left > SCREEN_WIDTH: self.kill() #check collision with characters if pygame.sprite.spritecollide(player, bullet_group, False): if player.alive: player.health -= 5 self.kill() if pygame.sprite.spritecollide(enemy, bullet_group, False): if enemy.alive: enemy.health -= 25 self.kill() #create sprite groups enemy_group = pygame.sprite.Group() bullet_group = pygame.sprite.Group() item_box_group = pygame.sprite.Group() player = Player('player', 200, 200, 3, 5, 0) health_bar = HealthBar(10, 10, player.health, player.health) enemy = Player('enemy', 400, 200, 3, 5, 20) enemy_group.add(enemy) run = True while run: clock.tick(FPS) draw_bg() health_bar.draw(player.health) player.update() player.draw() for enemy in enemy_group: enemy.update() enemy.draw() #update and draw groups bullet_group.update() item_box_group.update() item_box_group.draw(screen) if player.alive: if player.in_air: player.update_action(2) # 2: jump elif moving_left or moving_right: player.update_action(1) # 1: run else: player.update_action(0) # 0: idle player.move(moving_left, moving_right) for event in pygame.event.get(): # Quit Game if event.type == pygame.QUIT: run = False # Keyboard Input if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: moving_left = True if event.key == pygame.K_d: moving_right = True if event.key == pygame.K_w and player.alive: player.jump = True if event.key == pygame.K_ESCAPE: run = False if event.type == pygame.KEYUP: if event.key == pygame.K_a: moving_left = False if event.key == pygame.K_d: moving_right = False pygame.display.update() pygame.quit()