import pygame, sys, random pygame.init() clock = pygame.time.Clock() #Screen dimensions screen_height = 900 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 #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)) #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 = "main_game" def intro(self): # 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): # Render stars star_group.draw(screen) star_group.update() # Update and draw player player.update() # 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, (10, 50)) 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 Player(): def __init__(self, x, y): self.image = pygame.image.load('img/Fighter_1.png') 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 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 self.rect.x = new_x self.rect.y = new_y # Draw player screen.blit(self.image, self.rect) 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 player = Player(screen_width // 2, screen_height - 100) 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)) # Handle game states game_state.state_manager() # Update the display pygame.display.flip() # Control frame rate clock.tick(60)