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 class Player: JUMP_STRENGTH = 15 GRAVITY = 1 def __init__(self, rect, speed, size): self.rect = rect self.speed = speed self.size = size self.yVelocity = 0 self.isJumping = False 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] and not self.isJumping: # up arrow key, move player up self.isJumping = True self.yVelocity = -self.JUMP_STRENGTH def applyGravity(self): self.yVelocity += self.GRAVITY self.rect.y += self.yVelocity if self.rect.y >= SCREEN_HEIGHT - self.size: self.rect.y = SCREEN_HEIGHT - self.size self.isJumping = False self.yVelocity = 0 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) # 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() player.applyGravity() 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 win = True running = False # redraw the player player.draw(window, SKY_BLUE) # 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()