import pygame, sys, random, math pygame.init() clock = pygame.time.Clock() # Screen dimensions screen_height = 800 screen_width = 900 screen = pygame.display.set_mode((screen_width, screen_height)) # Font font_size = 30 sw_font = pygame.font.Font("img/Starjedi.ttf", font_size) font_1 = pygame.font.Font("img/PLANK___.TTF", font_size) play_text = sw_font.render('play', True, (255, 255, 255)) # Renders white text test1_text = sw_font.render('test1', True, (255, 255, 255)) test2_text = sw_font.render('test2', True, (255, 255, 255)) exit_text = sw_font.render('exit', True, (255, 255, 255)) intro_screen = True delay = 750 round = 1 enemy_count = 0 ammo = 12 # Images star_img = pygame.image.load('img/star.png') star_img = pygame.transform.scale(star_img, (3, 2)) def draw_grid(): # Vertical line (splits the width) pygame.draw.line(screen, (255, 255, 255), (screen_width / 2, 0), (screen_width / 2, screen_height), 5) # Horizontal line (splits the height) pygame.draw.line(screen, (255, 255, 255), (0, screen_height / 2), (screen_width, screen_height / 2), 5) def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) def kill_s(group): for sprite in group: sprite.kill() # Background 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) # Moving update def update(self): self.y += self.speed self.rect.y = int(self.y) # Back to top if self.rect.top > screen_height: self.y = 0 self.rect.x = random.randrange(0, screen_width) # Generating stars 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) # Creating buttons 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 # Get mouse pos pos = pygame.mouse.get_pos() # Check mouseover and clicked conditions 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 # Draw button screen.blit(self.image, self.rect) return action # Controlling different screens class GameState(): def __init__(self): self.state = "intro" def intro(self): global round # Draw and check the play button if play_button.draw(): self.state = "main_game" if test1_button.draw(): self.state = "main_game" if test2_button.draw(): self.state = "main_game" if exit_button.draw(): pygame.quit() sys.exit() pygame.display.set_caption("Planet Conflict | Intro screen") def main_game(self): global ammo, enemy_count # Render stars star_group.draw(screen) star_group.update() # Ammo draw_text('ammo: ' + str(ammo), font_1, (255, 255, 255), 10, (screen_height - 55)) # Update and draw player 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) print(enemy_count) if enemy_count == 0: kill_s(enemy_group1) # Check for collisions between enemy lasers and player if pygame.sprite.spritecollideany(player, enemy_laser_group): print("Player hit!") # Kill player sprite kill_s(player_group) # Set the window title pygame.display.set_caption("Planet Conflict | Main Game") def state_manager(self): if self.state == "intro": self.intro() elif self.state == "main_game": self.main_game() class Laser(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('img/laser.png') self.image = pygame.transform.rotate(self.image, 90) self.image = pygame.transform.scale(self.image, (12, 12)) 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 # Adjust speed as needed 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() 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 key = pygame.key.get_pressed() if key[pygame.K_LEFT]: dx -= 5 if key[pygame.K_RIGHT]: dx += 5 if key[pygame.K_UP]: dy -= 5 if key[pygame.K_DOWN]: dy += 5 # Update player position 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 < 0: new_y = 0 elif new_y > screen_height - self.rect.height: new_y = screen_height - self.rect.height if round == 1: # Shooting laser with delay if key[pygame.K_SPACE]: current_time = pygame.time.get_ticks() if ammo > 0: if current_time - self.last_shot > delay: laser = Laser((new_x + 42), (new_y)) laser_group.add(laser) self.last_shot = current_time ammo -= 1 if round == 2: # Shooting laser with delay if key[pygame.K_SPACE]: current_time = pygame.time.get_ticks() if ammo > 0: if current_time - self.last_shot > delay: 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 # Draw player screen.blit(self.image, self.rect) # Add player to a sprite group player_group = pygame.sprite.Group() player = Player((screen_width // 2 - 49), screen_height - 100) player_group.add(player) 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 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 # Draw white outline around the enemy rect #pygame.draw.rect(screen, (255, 255, 255), self.rect, 2) 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 = 5000 # 5 seconds 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) # Rotate the image self.image = pygame.transform.rotate(self.original_image, int(angle) - 90) self.rect = self.image.get_rect(center=self.rect.center) # Shooting at the player 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 # Draw white outline around the enemy rect # pygame.draw.rect(screen, (255, 255, 255), self.rect, 2) enemy_group = pygame.sprite.Group() enemy_group1 = pygame.sprite.Group() # Pos of enemies for each round if round == 1: # Back right enemy = Enemy((screen_width // 2 + 114), 10) enemy_group.add(enemy) # Back middle right enemy = Enemy((screen_width // 2 + 14), 10) enemy_group.add(enemy) # Back middle left enemy = Enemy((screen_width // 2 - 86), 10) enemy_group.add(enemy) # Back left enemy = Enemy((screen_width // 2 - 186), 10) enemy_group.add(enemy) # Front middle enemy = Enemy((screen_width // 2 - 36), 70) enemy_group.add(enemy) # Front right enemy = Enemy((screen_width // 2 + 64), 70) enemy_group.add(enemy) # Front left enemy = Enemy((screen_width // 2 - 136), 70) enemy_group.add(enemy) enemy = Enemy1(10, 10) enemy_group1.add(enemy) enemy_count = 7 print(enemy_count) if round == 2: #right # Back right enemy = Enemy((screen_width // 2 + 264), 10) enemy_group.add(enemy) # Back middle right enemy = Enemy((screen_width // 2 + 164), 10) enemy_group.add(enemy) # Back middle left enemy = Enemy((screen_width // 2 + 64), 10) enemy_group.add(enemy) # Back left enemy = Enemy((screen_width // 2 - 36), 10) enemy_group.add(enemy) # Front middle enemy = Enemy((screen_width // 2 + 116), 70) enemy_group.add(enemy) # Front right enemy = Enemy((screen_width // 2 + 214), 70) enemy_group.add(enemy) # Front left enemy = Enemy((screen_width // 2 + 14), 70) enemy_group.add(enemy) #left # Back middle right enemy = Enemy((screen_width // 2 -136), 10) enemy_group.add(enemy) # Back middle left enemy = Enemy((screen_width // 2 - 236), 10) enemy_group.add(enemy) # Back left enemy = Enemy((screen_width // 2 - 336), 10) enemy_group.add(enemy) # Front middle enemy = Enemy((screen_width // 2 - 186), 70) enemy_group.add(enemy) # Front right enemy = Enemy((screen_width // 2 - 86), 70) enemy_group.add(enemy) # Front left enemy = Enemy((screen_width // 2 - 286), 70) enemy_group.add(enemy) enemy = Enemy1(15, 125) enemy_group1.add(enemy) enemy_count = 13 print(enemy_count) game_state = GameState() # Create buttons play_button = Button((screen_width // 2) - 41, (screen_height // 2) - 97, play_text) test1_button = Button((screen_width // 2) - 47, (screen_height // 2) - 50, test1_text) test2_button = Button((screen_width // 2) - 47, (screen_height // 2) - 3, test2_text) exit_button = Button((screen_width // 2) - 37, (screen_height // 2) + 44, exit_text) # Main game loop run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Fill screen with black screen.fill((0, 0, 0)) # Draw grid # draw_grid() # Handle game states game_state.state_manager() # Update the display pygame.display.flip() # Control frame rate clock.tick(60)