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 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, 60) text = basicFont.render('You Won!', True, WHITE, GRAY) textRect = text.get_rect() textRect.centerx = window.get_rect().centerx textRect.centery = window.get_rect().centery # player vars PLAYER_SIZE = 20 PLAYER_SPEED = 5 def handleKeys(player): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: # left arrow key, move player left player.x = max(player.x - PLAYER_SPEED, 0) if keys[pygame.K_RIGHT]: # right arrow key, move player right player.x = min(player.x + PLAYER_SPEED, SCREEN_WIDTH - PLAYER_SIZE) if keys[pygame.K_UP]: # up arrow key, move player up player.y = max(player.y - PLAYER_SPEED, 0) if keys[pygame.K_DOWN]: # down arrow key, move player down player.y = min(player.y + PLAYER_SPEED, SCREEN_HEIGHT - PLAYER_SIZE) # a function to move a given obstacle and return its new velocity def moveObstacle(obstacle, velocity): obstacle.y -= velocity if obstacle.y > (SCREEN_HEIGHT - obstacle.height) or obstacle.y < 0: return -velocity return velocity # create the goal rect goal = Rect(400, 100, 50, 150) # create some obstacles obstacles = [Rect(200,100,50,50), Rect(340,300,50,50), Rect(100,240,50,50)] obstacleVelocities = [-15, 25, 5] # create the player in the bottom left corner player = Rect(PLAYER_SIZE + 10, SCREEN_HEIGHT-(PLAYER_SIZE + 10), PLAYER_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) veloIndex = 0 for obstacle in obstacles: newVelocity = moveObstacle(obstacle, obstacleVelocities[veloIndex]) obstacleVelocities[veloIndex] = newVelocity veloIndex += 1 pygame.draw.rect(window, DARK_RED, obstacle) # update player movement handleKeys(player) for obstacle in obstacles: if player.colliderect(obstacle): player = Rect(PLAYER_SIZE+10, SCREEN_HEIGHT-(PLAYER_SIZE + 10), PLAYER_SIZE, PLAYER_SIZE) if player.colliderect(goal): # player is colliding with the goal, they win win = True running = False # redraw the player pygame.draw.rect(window, SKY_BLUE, player) # update the screen pygame.display.flip() # set frame rate pygame.time.Clock().tick(30) if win: # show the win screen window.fill(GOLD) window.blit(text, textRect) pygame.display.flip() quit = False while not quit: # check if user quit for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True pygame.quit()