import pygame from pygame import mixer import os import random import csv import button mixer.init() 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('Shooter') #set framerate clock = pygame.time.Clock() FPS = 60 #define game variables GRAVITY = 0.75 SCROLL_THRESH = 200 ROWS = 16 COLS = 150 TILE_SIZE = SCREEN_HEIGHT // ROWS TILE_TYPES = 21 MAX_LEVELS = 3 screen_scroll = 0 bg_scroll = 0 level = 1 start_game = False start_intro = False #define player moving_left = False moving_right = False shoot = False grenade = False grenade_thrown = False #load music and sounds pygame.mixer.music.load('audio/music2.mp3') pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1, 0.0, 5000) jump_fx = pygame.mixer.Sound('audio/jump.wav') jump_fx.set_volume(0.05) shot_fx = pygame.mixer.Sound('audio/shot.wav') shot_fx.set_volume(0.05) grenade_fx = pygame.mixer.Sound('audio/grenade.wav') grenade_fx.set_volume(0.05) #load images #button images start_img = pygame.image.load('img/start_btn.png').convert_alpha() exit_img = pygame.image.load('img/exit_btn.png').convert_alpha() #background pine1_img = pygame.image.load('img/Background/pine1.png').convert_alpha() pine2_img = pygame.image.load('img/Background/mountain.png').convert_alpha() mountain_img = pygame.image.load('img/Background/mountain.png').convert_alpha() sky_img = pygame.image.load('img/Background/sky_cloud.png').convert_alpha #storing tiles img_list = [] for x in range(TILE_TYPES): img = pygame.image.load(f'img/Tile/{x}.png') img = pygame.transform.scale(img, (TILE_SIZE, TILE_SIZE)) img_list.append(img) #bullet bullet_img = pygame.image.load('img/icons/bullet.png').convert_alpha() #grenade grenade_img = pygame.image.load('img/icons/grenade.png').convert_alpha() #pick up boxes health_box_img = pygame.image.load('img/icons/health_box.png').convert_alpha() ammo_box_img = pygame.image.load('img/icons/ammo_box.png').convert_alpha() grenade_box_img = pygame.image.load('img/icons/grenade_box.png').convert_alpha() item_boxes = { 'Health' :health_box_img, 'Ammo' :ammo_box_img, 'Grenade' :grenade_box_img } #define colours BG = (144, 201, 120) RED = (255, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLACK = (0,0,0) PINK = (235, 65, 54) #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) width = sky_img.get_width() for x in range(5): screen.blit(sky_img, ((x*width) - bg_scroll * 0.5, 0)) screen.blit(mountain_img, ((x*width) - bg_scroll * 0.5, 0)) screen.blit(pine1_img, ((x*width) - bg_scroll * 0.5, 0)) screen.blit(pine2_img, ((x*width) - bg_scroll * 0.5, 0)) #reset level function def reset_level(): enemy_group.empty() bullet_group.empty() grenade_group.empty() explosion_group.empty() item_box_group.empty() decoration_group.empty() water_group.empty() exit_group.empty() #create empty tile list data = [] for row in range(ROWS): r = [-1] * COLS data.append(r) return data class Soldier(pygame.sprite.Sprite): def __init__(self, char_type, x, y, scale, speed, ammo, grenades): 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.grenades = grenades self.health = 100 self.max_health = self.health self.direction = 1 self.vel_y = 0 self.jump = False self.in_air = True self.flip = False self.animation_list = [] self.frame_index = 0 self.action = 0 self.update_time = pygame.time.get_ticks() #ai specific variables self.move_counter = 0 self.vision = pygame.Rect(0, 0, 150, 20) self.idling = False self.idling_counter = 0 #load all iamges for the players animation_types = ['Idle', 'Run', 'Jump', 'Death'] for animation in animation_types: #reset temporary list of images temp_list = [] #count number of files in the folder num_of_frames = len(os.listdir(f'img/{self.char_type}/{animation}')) for i in range(num_of_frames): img = pygame.image.load(f'img'/{self.char_type}/{animation}/{i}).convert_alpha() 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) self.width = self.image.get_width() self.height = self.image.get_height()