import pygame, sys, random, math pygame.init() clock = pygame.time.Clock() screen_height = 800 screen_width = 900 screen = pygame.display.set_mode((screen_width, screen_height)) font_size = 30 sw_font = pygame.font.Font("img/Starjedi.ttf", font_size) font_1 = pygame.font.Font("img/PLANK___.TTF", font_size) font_2 = pygame.font.Font("img/PLANK___.TTF", 20) play_text = sw_font.render('play', True, (255, 255, 255)) exit_text = sw_font.render('exit', True, (255, 255, 255)) star_img = pygame.image.load('img/star.png') star_img = pygame.transform.scale(star_img, (3, 2)) prev_score = 0 def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) #kill sprite groups def kill_s(group): for sprite in group: sprite.kill() #stars class Stars(pygame.sprite.Sprite): def __init__(self, image, pos_x, pos_y): super().__init__() self.image = image self.rect = self.image.get_rect() self.rect.center = [pos_x, pos_y] self.speed = random.uniform(0.9, 1) self.y = float(self.rect.y) def update(self): self.y += self.speed self.rect.y = int(self.y) if self.rect.top > screen_height: self.y = 0 self.rect.x = random.randrange(0, screen_width) star_group = pygame.sprite.Group() for star in range(175): new_star = Stars(star_img, random.randrange(0, screen_width), random.randrange(0, screen_height)) star_group.add(new_star) class Button(): def __init__(self,x,y, image): self.image = image self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.clicked = False def draw(self): action = False pos = pygame.mouse.get_pos() #check clicked if self.rect.collidepoint(pos): if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: action = True self.clicked = True if pygame.mouse.get_pressed()[0] == 0: self.clicked = False screen.blit(self.image, self.rect) return action class GameState(): def __init__(self): self.state = "intro" self.game_over_time = None def intro(self): global round, called #buttons if play_button.draw() or pygame.key.get_pressed()[pygame.K_SPACE]: self.state = "main_game" round = 1 called = 0 if exit_button.draw(): pygame.quit() sys.exit() pygame.display.set_caption("Planet Conflict | Intro screen") #game code def main_game(self): global ammo, enemy_count, round, called, enemies_killed, prev_score star_group.draw(screen) star_group.update() #setting up rounds if round == 1 and called == 0: round1() called = 1 if round == 2 and called == 1: if self.game_over_time is None: self.game_over_time = pygame.time.get_ticks() elif pygame.time.get_ticks() - self.game_over_time >= 300: round2() called = 2 self.game_over_time = None if round == 3 and called == 2: if self.game_over_time is None: self.game_over_time = pygame.time.get_ticks() elif pygame.time.get_ticks() - self.game_over_time >= 300: round3() called = 3 self.game_over_time = None #call player and enemies player_group.update() player_group.draw(screen) laser_group.update() enemy_group.update() enemy_group.draw(screen) enemy_group1.update() enemy_group1.draw(screen) enemy_laser_group.update() # Check for collisions between lasers and enemies collisions = pygame.sprite.groupcollide(laser_group, enemy_group, True, True) if collisions: enemy_count -= len(collisions) enemies_killed += len(collisions) #changing rounds if enemy_count <= 0: if round == 1 and called == 1: round += 1 kill_s(player_group) elif round == 2 and called == 2: round += 1 kill_s(player_group) elif round == 3 and called == 3: if self.game_over_time is None: self.game_over_time = pygame.time.get_ticks() elif pygame.time.get_ticks() - self.game_over_time >= 5000: self.state = "intro" self.game_over_time = None kill_s(player_group) draw_text('you win!', font_1, (255, 255, 255), (screen_width // 2) - 85, (screen_height // 2) - 47) if exit_button.draw(): pygame.quit() sys.exit() #out of ammo if ammo <= 0: if self.game_over_time is None: self.game_over_time = pygame.time.get_ticks() elif pygame.time.get_ticks() - self.game_over_time >= 2000: if enemy_count > 0: self.state = "game_over" if enemy_count <= 0: kill_s(enemy_group1) kill_s(enemy_laser_group) # Check for collisions between enemy lasers and player if pygame.sprite.spritecollideany(player, enemy_laser_group): self.state = "game_over" #score score = enemies_killed if self.state == "game_over" and score >= prev_score: prev_score = score if score >= prev_score: high_score = score else: high_score = prev_score draw_text('ammo: ' + str(ammo), font_2, (255, 255, 255), 10, (screen_height - 35)) draw_text('high score: ' + str(high_score), font_2, (255, 255, 255), (screen_width - 220), (screen_height - 35)) pygame.display.set_caption("Planet Conflict | Main Game") #player dead code def game_over(self): global enemy_group, enemy_group1, enemy_laser_group, player_group, laser_group # Clear groups enemy_group.empty() enemy_group1.empty() enemy_laser_group.empty() laser_group.empty() player_group.empty() #text for game over if ammo <= 0: draw_text('ran out of ammo', font_1, (255, 255, 255), ((screen_width // 2) - 150), ((screen_height // 2) - 94)) draw_text('died on round: ' + str(round), font_1, (255, 255, 255), ((screen_width // 2) - 150), ((screen_height // 2) - 60)) draw_text('enemies killed: ' + str(enemies_killed), font_1, (255, 255, 255), ((screen_width // 2) - 180), ((screen_height // 2) - 24)) draw_text('you lose', font_1, (255, 255, 255), ((screen_width // 2) - 100), ((screen_height // 2) + 15)) else: draw_text('died on round: ' + str(round), font_1, (255, 255, 255), ((screen_width // 2) - 150), ((screen_height // 2) - 60)) draw_text('enemies killed: ' + str(enemies_killed), font_1, (255, 255, 255), ((screen_width // 2) - 180), ((screen_height // 2) - 24)) draw_text('you lose', font_1, (255, 255, 255), ((screen_width // 2) - 100), ((screen_height // 2) + 15)) if self.game_over_time is None: self.game_over_time = pygame.time.get_ticks() elif pygame.time.get_ticks() - self.game_over_time >= 3000: self.state = "intro" self.game_over_time = None def state_manager(self): if self.state == "intro": self.intro() elif self.state == "main_game": self.main_game() elif self.state == "game_over": self.game_over() #laser class Laser(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('img/laser2.png') self.image = pygame.transform.scale(self.image, (18, 30)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y def update(self): self.rect.y -= 10 screen.blit(self.image, self.rect) laser_group = pygame.sprite.Group() class EnemyLaser(pygame.sprite.Sprite): def __init__(self, x, y, target_x, target_y, angle): pygame.sprite.Sprite.__init__(self) self.original_image = pygame.image.load('img/laser.png') self.original_image = pygame.transform.scale(self.original_image, (12, 12)) self.image = pygame.transform.rotate(self.original_image, angle) self.rect = self.image.get_rect(center=(x, y)) # Calculate direction direction = pygame.math.Vector2(target_x - x + 50, target_y - y) direction = direction.normalize() self.speed_x = direction.x * 12 self.speed_y = direction.y * 12 def update(self): self.rect.x += self.speed_x self.rect.y += self.speed_y screen.blit(self.image, self.rect) enemy_laser_group = pygame.sprite.Group() #player code class Player(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load('img/Fighter_1.png') self.image = pygame.transform.scale(self.image, (92.5, 67.2)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.last_shot = pygame.time.get_ticks() def update(self): global new_x, new_y, ammo dx = 0 dy = 0 #movement key = pygame.key.get_pressed() if key[pygame.K_LEFT] or key[pygame.K_a]: dx -= 5 if key[pygame.K_RIGHT] or key[pygame.K_d]: dx += 5 if key[pygame.K_UP] or key[pygame.K_w]: dy -= 5 if key[pygame.K_DOWN] or key[pygame.K_s]: dy += 5 new_x = self.rect.x + dx new_y = self.rect.y + dy # Check if player is within screen bounds if new_x < 0: new_x = 0 elif new_x > screen_width - self.rect.width: new_x = screen_width - self.rect.width if new_y < 260: new_y = 260 elif new_y > screen_height - self.rect.height: new_y = screen_height - self.rect.height #shooting if round == 1: if key[pygame.K_SPACE]: current_time = pygame.time.get_ticks() if ammo > 0: if current_time - self.last_shot > 750: laser = Laser((new_x + 38), (new_y)) laser_group.add(laser) self.last_shot = current_time ammo -= 1 if round == 2: if key[pygame.K_SPACE]: current_time = pygame.time.get_ticks() if ammo > 0: if current_time - self.last_shot > 750: laser = Laser((new_x + 61), (new_y + 15)) laser_group.add(laser) laser = Laser((new_x + 15), (new_y + 15)) laser_group.add(laser) self.last_shot = current_time ammo -= 2 if round == 3: if key[pygame.K_SPACE]: current_time = pygame.time.get_ticks() if ammo > 0: if current_time - self.last_shot > 600: laser = Laser((new_x + 65), (new_y + 15)) laser_group.add(laser) laser = Laser((new_x + 19), (new_y + 15)) laser_group.add(laser) self.last_shot = current_time ammo -= 2 self.rect.x = new_x self.rect.y = new_y screen.blit(self.image, self.rect) player_group = pygame.sprite.Group() class Enemy(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('img/enemy_1.png') self.image = pygame.transform.rotate(self.image, 180) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.move_direction = 1 self.move_counter = 0 #enemy movement def update(self): self.rect.x += self.move_direction self.move_counter += 1 if abs(self.move_counter) > 90: self.move_direction *= -1 self.move_counter *= -1 enemy_group = pygame.sprite.Group() class Enemy1(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.original_image = pygame.image.load('img/enemy_1.png') self.image = self.original_image self.image = pygame.transform.rotate(self.image, 180) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.height = 56 self.width = 76 self.last_shot_time = pygame.time.get_ticks() self.shoot_interval = 4000 def update(self): global new_x, new_y # Calculate the angle to the player rel_x, rel_y = new_x - self.rect.x, new_y - self.rect.y angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) self.image = pygame.transform.rotate(self.original_image, int(angle) - 90) self.rect = self.image.get_rect(center=self.rect.center) #shooting current_time = pygame.time.get_ticks() if current_time - self.last_shot_time > self.shoot_interval: enemy_laser = EnemyLaser(self.rect.centerx, self.rect.centery, new_x, new_y, int(angle)) enemy_laser_group.add(enemy_laser) self.last_shot_time = current_time enemy_group1 = pygame.sprite.Group() def round1(): global ammo, enemy_count, enemies_killed, player enemy = Enemy((screen_width // 2 + 114), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 14), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 86), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 186), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 36), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 64), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 136), 70) enemy_group.add(enemy) enemy = Enemy1(10, 10) enemy_group1.add(enemy) player = Player((screen_width // 2 - 49), screen_height - 100) player_group.add(player) enemy_count = 7 enemies_killed = 0 ammo = 12 def round2(): global ammo, enemy_count, player enemy = Enemy((screen_width // 2 + 264), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 164), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 64), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 36), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 116), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 214), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 14), 70) enemy_group.add(enemy) enemy = Enemy1((screen_width - 70), 125) enemy_group1.add(enemy) enemy = Enemy((screen_width // 2 -136), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 236), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 336), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 186), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 86), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 286), 70) enemy_group.add(enemy) enemy = Enemy1(15, 125) enemy_group1.add(enemy) player = Player((screen_width // 2 - 49), screen_height - 100) player_group.add(player) enemy_count = 13 ammo = 20 def round3(): global ammo, enemy_count, player enemy = Enemy((screen_width // 2 + 114), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 14), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 86), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 186), 10) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 36), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 64), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 136), 70) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 86), 130) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 + 14), 130) enemy_group.add(enemy) enemy = Enemy((screen_width // 2 - 36), 190) enemy_group.add(enemy) enemy = Enemy1(10, 10) enemy_group1.add(enemy) enemy = Enemy1(70, 10) enemy_group1.add(enemy) enemy = Enemy1((screen_width - 70), 10) enemy_group1.add(enemy) enemy = Enemy1((screen_width - 130), 10) enemy_group1.add(enemy) player = Player((screen_width // 2 - 49), screen_height - 100) player_group.add(player) enemy_count = 10 ammo = 20 game_state = GameState() #Create buttons play_button = Button((screen_width // 2) - 41, (screen_height // 2) - 47, play_text) exit_button = Button((screen_width // 2) - 37, (screen_height // 2) - 5, exit_text) # Main game loop run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill((0, 0, 0)) # Handle game states game_state.state_manager() pygame.display.flip() clock.tick(60)