import pygame import sys pygame.init() clock = pygame.time.Clock() #load all images btn_start_img = pygame.image.load('images/start btn.png').convert_alpha() #start button image btn_exit_img = pygame.image.load('images/exit btn.png').convert_alpha() #exit button image maze1 = pygame.image.load('images/maze 1.png').convert() #background image for L1 maze2 = pygame.image.load('images/maze 2.png').convert() #background image for L2 maze3 = pygame.image.load('images/maze3.png').convert() #background image for L3 #set up the window displayWidth = 700 displayHeight = 700 pygame_pressed = 0 # find where this goes player = Player(100, screen_height - 130) screen = pygame.display.set_mode((displayWidth, displayHeight )) pygame.display.set_caption('Maze Game') #button class class Button(): def __init__(self, x, y, image, scale): width = image.get_width() height = image.get_height() self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) self.rect = self.image.get_rect() self.rect.topleft = (x, y) self.clicked = False def draw(self): action = False #mouse position pos = pygame.mouse.get_pos() #has mouse been moved over button or clicked? if self.rect.collidepoint(pos): #[0] means left mouse button!!! if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: self.clicked = True action = True #checking if button been clicked more than once if pygame_pressed()[0] == 0: self.clicked = False #button on screen screen.blit(self.image, (self.rect.x, self.rect.y)) return action class GameState: def __init__(self): self.state = 'intro' def intro(self): for event in pygame.event.get(): #quit game if event.type == pygame.QUIT: run = False screen.fill((174, 198, 207)) #make buttons start_button = Button(100, 200, btn_start_img, 0.5) exit_button = Button(450, 200, btn_exit_img, 0.5) if start_button.draw(): self.state = "level1" if exit_button.draw(): run = False pygame.display.update() def level1(self): for event in pygame.event.get(): #quit game if event.type == pygame.QUIT: run = False screen.blit(maze1, (0, 0)) #***rest of level 1 code here!!*** class Player(): def __init__(self, x, y): img = pygame.image.load('img/player image name here') self.image = pygame.transform.scale(img, (40, 80)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y def update(self): dx = 0 dy = 0 #getting keypresses key = pygame.get_keypressed() if key[pygame.K_SPACE]: self.vel_y = -15 if key[pygame.K_LEFT]: dx -= 5 if key[pygame.K_RIGHT]: dx += 5 #cheack for collision #update player coordinates self.rect.x += dx self.rect.y += dy #draw player onto screen screen.blit(self.image, self.rect) def state_manager(self): if self.state == 'intro': self.intro() if self.state == 'level1': self.level1() game_state = GameState() #game loop run = True while run: player.update() game_state.state_manager() clock.tick(60) pygame.quit()