I'm making a Start Screen class for my game, it has 3 buttons and they're spread out horizontally on the screen. The screen also has a scrolling background, moves from top to bottom. It's a very basic Start Screen but the code is 111 lines long. I'm sure there's a much more efficient way of doing things, any tips on how to shorten the file or do things better is very much appreciated, thank you!
Code:
import pygame
import game
import math
pygame.init()
def main():
fps = 60
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
FONT = pygame.font.SysFont("comicsans", 30)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Space Avoiders")
background = pygame.image.load("background1.png").convert()
tiles = math.ceil(SCREEN_WIDTH / background.get_height()) + 1
clock = pygame.time.Clock()
scroll = 0
def redraw_window(scroll):
screen.blit(screen, (0, 0))
for i in range(-1, tiles):
screen.blit(background, (0, i * background.get_height() + scroll))
def start_menu_buttons():
mouse = pygame.mouse.get_pos()
#Buttons
start = pygame.image.load("Sprites/Buttons/Start.jpg")
start_hover = pygame.image.load("Sprites/Buttons/Start_Hover.jpg")
start_rect = start.get_rect()
start_x = 0
start_y = screen.get_height() / 2
start_rect.x = start_x
start_rect.y = start_y
settings = pygame.image.load("Sprites/Buttons/Settings.jpg")
settings_hover = pygame.image.load("Sprites/Buttons/Settings_Hover.jpg")
settings_clicked = pygame.image.load("Sprites/Buttons/Settings_Clicked.jpg")
settings_rect = settings.get_rect()
settings_x = screen.get_width()/2 - (settings_rect.width / 2)
settings_y = screen.get_height() / 2
settings_rect.x = settings_x
settings_rect.y = settings_y
customize = pygame.image.load("Sprites/Buttons/Customize.jpg")
customize_hover = pygame.image.load("Sprites/Buttons/Customize_Hover.jpg")
customize_clicked = pygame.image.load("Sprites/Buttons/Customize_Clicked.jpg")
customize_rect = customize.get_rect()
customize_x = screen.get_width() - customize_rect.width
customize_y = screen.get_height() / 2
customize_rect.x = customize_x
customize_rect.y = customize_y
start_blit = start
settings_blit = settings
customize_blit = customize
collide_start = start_rect.colliderect(mouse, (1,1))
collide_settings = settings_rect.colliderect(mouse, (1,1))
collide_customize = customize_rect.colliderect(mouse, (1,1))
button_pressed = pygame.mouse.get_pressed()[0]
if collide_start:
start_blit = start_hover
if collide_start and pygame.mouse.get_pressed()[0]:
pygame.display.quit()
game.game()
if collide_settings:
settings_blit = settings_hover
if collide_settings and button_pressed:
settings_blit = settings_clicked
if collide_customize:
customize_blit = customize_hover
if collide_customize and button_pressed:
customize_blit = customize_clicked
screen.blits(((start_blit, (start_x, start_y)),
(settings_blit, (settings_x, settings_y)),
(customize_blit, (customize_x, customize_y))))
run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
redraw_window(scroll)
start_menu_buttons()
pygame.display.update()
scroll += 1.5
if scroll >= background.get_height():
scroll = 0
pygame.quit()
if __name__ == "__main__":
main()