import pygame from pygame.locals import * pygame.init() SCREEN_WIDTH = 500 SCREEN_HEIGHT = 400 # set up window window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Basic Game') # set up colors BLACK = (0,0,0) WHITE = (255,255,255) GRAY = (47, 79, 79) # Dark Slate Gray GRAY_HOVER = (68, 100, 100) # lighter gray for hover SKY_BLUE = (0, 191, 255) # Deep Sky Blue GOLD = (218, 165, 32) # Gold DARK_RED = (178, 34, 34) # Firebrick SEA_GREEN = (60, 179, 113) # Medium Sea Green # create the text for the win screen basicFont = pygame.font.SysFont(None, 80) text = basicFont.render('You Won!', True, GRAY) textRect = text.get_rect() textRect.centerx = window.get_rect().centerx textRect.centery = window.get_rect().centery - 25 # centered but slightly towards top to make room for button # create the play again button for win screen buttonFont = pygame.font.SysFont(None, 40) buttonText = buttonFont.render('Play Again', True, WHITE) buttonTextRect = buttonText.get_rect() buttonTextRect.centerx = window.get_rect().centerx buttonTextRect.centery = window.get_rect().centery + 50 # centered but below win text BUTTON_PADDING = 10 buttonRect = Rect( buttonTextRect.x - BUTTON_PADDING, buttonTextRect.y - BUTTON_PADDING, buttonTextRect.width + (BUTTON_PADDING * 2), buttonTextRect.height + (BUTTON_PADDING * 2) ) class Player: def __init__(self, rect, speed, size): self.rect = rect self.speed = speed self.size = size def handleKeys(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: # left arrow key, move player left self.rect.x = max(self.rect.x - self.speed, 0) if keys[pygame.K_RIGHT]: # right arrow key, move player right self.rect.x = min(self.rect.x + self.speed, SCREEN_WIDTH - self.size) if keys[pygame.K_UP]: # up arrow key, move player up self.rect.y = max(self.rect.y - self.speed, 0) if keys[pygame.K_DOWN]: # down arrow key, move player down self.rect.y = min(self.rect.y + self.speed, SCREEN_HEIGHT - self.size) def draw(self, surface, color): pygame.draw.rect(surface, color, self.rect) class Obstacle: def __init__(self, rect, xVelocity, yVelocity): self.xVelocity = xVelocity self.yVelocity = yVelocity self.rect = rect def move(self): self.rect.y -= self.yVelocity self.rect.x -= self.xVelocity if self.rect.y > (SCREEN_HEIGHT - self.rect.height) or self.rect.y < 0: self.yVelocity = -self.yVelocity if self.rect.x > (SCREEN_WIDTH - self.rect.width) or self.rect.x < 0: self.xVelocity = -self.xVelocity def draw(self, surface, color): pygame.draw.rect(surface, color, self.rect) # returns True if user wins, False if they quit def runGame(): # create the goal rect goal = Rect(400, 100, 50, 150) # create some obstacles obstacles = [Obstacle(Rect(200,100,50,50), 0, -15), Obstacle(Rect(340,300,50,50), 0, 25), Obstacle(Rect(100,240,50,50), 0, 5)] # create the player rect in the bottom left corner PLAYER_SIZE = 20 playerRect = Rect(PLAYER_SIZE + 10, SCREEN_HEIGHT-(PLAYER_SIZE + 10), PLAYER_SIZE, PLAYER_SIZE) player = Player(playerRect, speed=5, size=PLAYER_SIZE) running = True win = False while running: # check if user quit for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # set background color window.fill(GRAY) # resets the frame # draw the obstacles and portal onto the screen pygame.draw.rect(window, GOLD, goal) for obstacle in obstacles: obstacle.move() obstacle.draw(window, DARK_RED) # update player movement player.handleKeys() # redraw the player player.draw(window, SKY_BLUE) # update the screen pygame.display.flip() for obstacle in obstacles: if player.rect.colliderect(obstacle.rect): # colliding with obstacle, reset to start position player.rect = Rect(PLAYER_SIZE+10, SCREEN_HEIGHT-(PLAYER_SIZE + 10), PLAYER_SIZE, PLAYER_SIZE) if player.rect.colliderect(goal): # player is colliding with the goal, they win running = False win = True # set frame rate pygame.time.Clock().tick(30) return win def showWinScreen(): quit = False while not quit: # check if user quit for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # left mouse button clicked if buttonRect.collidepoint(event.pos): # clicked play again button return True mousePositon = pygame.mouse.get_pos() if buttonRect.collidepoint(mousePositon): color = GRAY_HOVER else: color = GRAY # show the win screen window.fill(GOLD) window.blit(text, textRect) pygame.draw.rect(window, color, buttonRect) window.blit(buttonText, buttonTextRect) pygame.display.flip() # closed without clicking play again return False while True: if runGame(): # user won if not showWinScreen(): # user quits break else: # user quit while playing break pygame.quit()