import pygame from pygame.locals import * import sys import random # Initialize Pygame pygame.init() # Define constants vec = pygame.math.Vector2 HEIGHT = 450 WIDTH = 400 ACC = 0.5 FRIC = -0.12 FPS = 60 FramePerSec = pygame.time.Clock() # Set up the display displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") # Define the Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Create player surface self.surf = pygame.Surface((30, 30)) self.surf.fill((128,255,40)) # Set initial position of the player self.rect = self.surf.get_rect(center=(10,420)) # Define player's position, velocity, and acceleration vectors self.pos = vec((10, 385)) self.vel = vec(0,0) self.acc = vec(0,0) # Method to move the player def move(self): # Apply gravity self.acc = vec(0,0.5) # Check for key presses to move left or right pressed_keys = pygame.key.get_pressed() if pressed_keys[K_LEFT]: self.acc.x = -ACC if pressed_keys[K_RIGHT]: self.acc.x = ACC # Apply friction self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc # Wrap around screen edges if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH self.rect.midbottom = self.pos # Method to update player position based on collisions def update(self): hits = pygame.sprite.spritecollide(P1 , platforms, False) if P1.vel.y > 0: if hits: self.pos.y = hits[0].rect.top + 1 self.vel.y = 0 # Method to make the player jump def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.vel.y = -15 # Define the Platform class class platform(pygame.sprite.Sprite): def __init__(self): super().__init__() # Create platform surface self.surf = pygame.Surface((random.randint(50,100), 12)) self.surf.fill((0,255,0)) # Set random position for the platform within screen boundaries self.rect = self.surf.get_rect(center = (random.randint(0,WIDTH-10), random.randint(0, HEIGHT-30))) # Function to generate platforms def plat_gen(): while len(platforms) < 7: width = random.randrange(50,100) p = platform() p.rect.center = (random.randrange(0, WIDTH - width), random.randrange(-50, 0)) platforms.add(p) all_sprites.add(p) # Create instances of Player and Platform classes PT1 = platform() P1 = Player() # Adjust the first platform's properties to act as ground PT1.surf = pygame.Surface((WIDTH, 20)) PT1.surf.fill((255,0,0)) PT1.rect = PT1.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) # Group all sprites and platforms all_sprites = pygame.sprite.Group() all_sprites.add(PT1) all_sprites.add(P1) platforms = pygame.sprite.Group() platforms.add(PT1) # Generate additional platforms for x in range(random.randint(5, 6)): pl = platform() platforms.add(pl) all_sprites.add(pl) # Main game loop while True: # Handle events for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: P1.jump() # Check if the player is at the top third of the screen if P1.rect.top <= HEIGHT / 3: # Move the player and platforms upwards P1.pos.y += abs(P1.vel.y) for plat in platforms: plat.rect.y += abs(P1.vel.y) # Remove platforms that have moved off-screen if plat.rect.top >= HEIGHT: plat.kill() # Fill the display surface with black color displaysurface.fill((0,0,0)) # Move the player, update its position, and generate platforms P1.move() P1.update() plat_gen() # Draw all sprites on the display surface for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) # Update the display pygame.display.update() FramePerSec.tick(FPS)