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) 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)) test3_text = sw_font.render('test3', True, (255, 255, 255)) intro_screen = True #Images star_img = pygame.image.load('img/star.png') star_img = pygame.transform.scale(star_img, (8, 7)) 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)\ #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 print("jfdsiughriu") 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): # 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 test3_button.draw(): self.state = "main_game" # Set the window title pygame.display.set_caption("Planet Conflict | Intro screen") def main_game(self): # Render stars star_group.draw(screen) star_group.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() 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) test3_button = Button((screen_width // 2) - 47, (screen_height // 2) + 44, test3_text) # Main game loop run = True while run: # Handle events globally 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() # Handle game states game_state.state_manager() # Update the display pygame.display.flip() # Control frame rate clock.tick(60)