import pygame from pygame.locals import * import sys import random # Initialize pygame pygame.init() # Vector for easy 2D calculations vec = pygame.math.Vector2 # Screen dimensions HEIGHT = 450 WIDTH = 400 # Physics constants ACC = 0.5 FRIC = -0.12 # Frames per second FPS = 60 FramePerSec = pygame.time.Clock() # Set up display displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") # Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Create player surface and rectangle self.surf = pygame.Surface((30, 30)) self.surf.fill((128,255,40)) self.rect = self.surf.get_rect() # Initial position, velocity, and acceleration self.pos = vec((10, 360)) self.vel = vec(0,0) self.acc = vec(0,0) self.jumping = False # Move player def move(self): self.acc = vec(0,0.5) pressed_keys = pygame.key.get_pressed() # Apply acceleration based on key presses 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 # Player jump def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits and not self.jumping: self.jumping = True self.vel.y = -15 # Cancel jump if player releases jump key def cancel_jump(self): if self.jumping: if self.vel.y < -3: self.vel.y = -3 # Update player position def update(self): hits = pygame.sprite.spritecollide(P1 , platforms, False) if P1.vel.y > 0: if hits: if self.pos.y < hits[0].rect.bottom: self.pos.y = hits[0].rect.top + 1 self.vel.y = 0 self.jumping = False # Platform class class Platform(pygame.sprite.Sprite): def __init__(self): super().__init__() # Create platform surface and rectangle self.surf = pygame.Surface((random.randint(50,100), 12)) self.surf.fill((0,255,0)) self.rect = self.surf.get_rect(center = (random.randint(0,WIDTH-10), random.randint(0, HEIGHT-30))) # Move platform (not used in this game) def move(self): pass # Check if platform overlaps with others def check(platform, groupies): if pygame.sprite.spritecollideany(platform,groupies): return True else: for entity in groupies: if entity == platform: continue if (abs(platform.rect.top - entity.rect.bottom) < 50) and (abs(platform.rect.bottom - entity.rect.top) < 50): return True C = False # Generate platforms def plat_gen(): while len(platforms) < 6 : width = random.randrange(50,100) p = Platform() C = True while C: p = Platform() p.rect.center = (random.randrange(0, WIDTH - width), random.randrange(-50, 0)) C = check(p, platforms) platforms.add(p) all_sprites.add(p) # Initialize main platform and player PT1 = Platform() P1 = Player() # Customize main platform PT1.surf = pygame.Surface((WIDTH, 20)) PT1.surf.fill((255,0,0)) PT1.rect = PT1.surf.get_rect(center = (WIDTH/2, HEIGHT - 10)) # Create sprite groups 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(4,5)): C = True pl = Platform() while C: pl = Platform() C = check(pl, platforms) platforms.add(pl) all_sprites.add(pl) # Main game loop while True: P1.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: P1.jump() if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: P1.cancel_jump() # Move player and platforms if P1.rect.top <= HEIGHT / 3: P1.pos.y += abs(P1.vel.y) for plat in platforms: plat.rect.y += abs(P1.vel.y) if plat.rect.top >= HEIGHT: plat.kill() plat_gen() displaysurface.fill((0,0,0)) # Draw sprites and update their positions for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) entity.move() pygame.display.update() FramePerSec.tick(FPS)