import pygame from pygame.locals import * import sys import random import time # Initialize pygame pygame.init() # Define constants vec = pygame.math.Vector2 HEIGHT = 500 WIDTH = 350 ACC = 0.5 FRIC = -0.12 FPS = 60 background_img = pygame.image.load("sky_1.png") background_img = pygame.transform.scale(background_img, (WIDTH, HEIGHT)) # Set up the game window FramePerSec = pygame.time.Clock() displaysurface = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Game") def show_start_screen(): title_font = pygame.font.SysFont("verdana", 25) subtitle_font = pygame.font.SysFont("verdana", 22) instr_font = pygame.font.SysFont("verdana", 16) while True: displaysurface.fill((128, 0, 128)) # Render main title title = title_font.render("Make It Out The Vale", True, (255, 255, 255)) title_rect = title.get_rect(center=(WIDTH // 2, 80)) displaysurface.blit(title, title_rect) # Render subtitle subtitle = subtitle_font.render("PLATFORMER", True, (255, 255, 255)) subtitle_rect = subtitle.get_rect(center=(WIDTH // 2, title_rect.bottom + 25)) displaysurface.blit(subtitle, subtitle_rect) # Instructions instructions = [ "Use LEFT and RIGHT arrow keys to move.", "Press UP arrow to jump.", "Jump on platforms to earn points.", "Don't fall off the screen!", "Press SPACE to start the game."] for i, line in enumerate(instructions): text = instr_font.render(line, True, (255, 255, 255)) displaysurface.blit(text, (WIDTH // 2 - text.get_width() // 2, subtitle_rect.bottom + 35 + i * 30)) pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: return #play again screen once player dies def show_game_over_screen(score): font = pygame.font.SysFont("verdana", 30) small_font = pygame.font.SysFont("verdana", 16) while True: displaysurface.fill((0, 0, 0)) game_over_text = font.render("Game Over", True, (255, 0, 0)) score_text = small_font.render(f"Your Score: {score}", True, (255, 255, 255)) restart_text = small_font.render("Would You Like To Play Again? Y/N", True, (255, 255, 255)) displaysurface.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, 150)) displaysurface.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 200)) displaysurface.blit(restart_text, (WIDTH // 2 - restart_text.get_width() // 2, 250)) pygame.display.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_y: return elif event.key == pygame.K_n: pygame.quit() sys.exit() class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.surf = pygame.Surface((30, 30)) self.surf.fill((200, 80, 175)) self.rect = self.surf.get_rect() self.pos = vec((10, 360)) self.vel = vec(0, 0) self.acc = vec(0, 0) self.jumping = False self.score = 0 def move(self): self.acc = vec(0, 0.5) pressed_keys = pygame.key.get_pressed() if pressed_keys[K_LEFT]: self.acc.x = -ACC if pressed_keys[K_RIGHT]: self.acc.x = ACC self.acc.x += self.vel.x * FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc if self.pos.x > WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = WIDTH self.rect.midbottom = self.pos def jump(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits and not self.jumping: self.jumping = True self.vel.y = -15 def cancel_jump(self): if self.jumping and self.vel.y < -3: self.vel.y = -3 def update(self): hits = pygame.sprite.spritecollide(self, platforms, False) if self.vel.y > 0 and hits: if self.pos.y < hits[0].rect.bottom: if hits[0].point: hits[0].point = False self.score += 1 self.pos.y = hits[0].rect.top + 1 self.vel.y = 0 self.jumping = False class platform(pygame.sprite.Sprite): def __init__(self): super().__init__() 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))) self.point = True self.speed = random.randint(-1, 1) self.moving = True def move(self): hits = self.rect.colliderect(P1.rect) if self.moving: self.rect.move_ip(self.speed, 0) if hits: P1.pos += (self.speed, 0) if self.speed > 0 and self.rect.left > WIDTH: self.rect.right = 0 if self.speed < 0 and self.rect.right < 0: self.rect.left = WIDTH def check(platform, groupies): if pygame.sprite.spritecollideany(platform, groupies): return True 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 return False 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) # Start screen show_start_screen() #Mmain game loop while True: P1 = Player() PT1 = platform() PT1.surf = pygame.Surface((WIDTH, 20)) PT1.surf.fill((255, 0, 0)) PT1.rect = PT1.surf.get_rect(center=(WIDTH / 2, HEIGHT - 10)) PT1.point = False PT1.moving = False all_sprites = pygame.sprite.Group() all_sprites.add(PT1) all_sprites.add(P1) platforms = pygame.sprite.Group() platforms.add(PT1) for _ 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) #Game Loop running = True while running: 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_UP: P1.jump() if P1.rect.top > HEIGHT: running = False break 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.blit(background_img, (0, 0)) score_text = pygame.font.SysFont("Verdana", 20).render(str(P1.score), True, (123, 255, 0)) displaysurface.blit(score_text, (WIDTH/2, 10)) for entity in all_sprites: displaysurface.blit(entity.surf, entity.rect) entity.move() pygame.display.update() FramePerSec.tick(FPS) # Show Game Over and Ask to Play Again show_game_over_screen(P1.score)