Untitled

 avatar
unknown
plain_text
a month ago
5.9 kB
3
Indexable
import pygame
import random
import sys

# 1. Initialize Pygame
pygame.init()

# 2. Game Constants
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 700
FPS = 60

# Colors
ROAD_COLOR = (50, 50, 50)
LANE_MARKER_COLOR = (255, 255, 255)
PLAYER_COLOR = (0, 150, 255)
OBSTACLE_COLOR = (220, 50, 50)
COIN_COLOR = (255, 215, 0)
TEXT_COLOR = (255, 255, 255)

# Setup Window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Mini Temple Runner")
clock = pygame.time.Clock()

# Lane configurations
LANES = [125, 250, 375] # X coordinates for left, center, right lanes

# 3. Game Classes
class Player:
    def __init__(self):
        self.lane_index = 1  # Start in the middle lane (index 1)
        self.x = LANES[self.lane_index]
        self.y = SCREEN_HEIGHT - 120
        self.width = 40
        self.height = 60
        self.score = 0

    def move_left(self):
        if self.lane_index > 0:
            self.lane_index -= 1
            self.x = LANES[self.lane_index]

    def move_right(self):
        if self.lane_index < 2:
            self.lane_index += 1
            self.x = LANES[self.lane_index]

    def draw(self):
        # Draw player centered on the lane
        pygame.draw.rect(screen, PLAYER_COLOR, (self.x - self.width//2, self.y, self.width, self.height), border_radius=5)

class Obstacle:
    def __init__(self, speed):
        self.lane_index = random.randint(0, 2)
        self.x = LANES[self.lane_index]
        self.y = -60
        self.width = 50
        self.height = 40
        self.speed = speed

    def update(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x - self.width//2, self.y, self.width, self.height), border_radius=3)

class Coin:
    def __init__(self, speed):
        self.lane_index = random.randint(0, 2)
        self.x = LANES[self.lane_index]
        self.y = -40
        self.radius = 15
        self.speed = speed

    def update(self):
        self.y += self.speed

    def draw(self):
        pygame.draw.circle(screen, COIN_COLOR, (self.x, int(self.y)), self.radius)

# 4. Main Game Loop
def main():
    player = Player()
    obstacles = []
    coins = []
    
    game_speed = 7
    spawn_timer = 0
    game_over = False

    font = pygame.font.SysFont("Arial", 30)
    over_font = pygame.font.SysFont("Arial", 50)

    while True:
        # Event Handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                
            if event.type == pygame.KEYDOWN:
                if not game_over:
                    if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                        player.move_left()
                    if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                        player.move_right()
                else:
                    if event.key == pygame.K_SPACE:
                        main() # Restart game

        if not game_over:
            # Increase difficulty slowly
            game_speed += 0.002
            player.score += 1 # Passive score from running

            # Spawn Management (Obstacles & Coins)
            spawn_timer += 1
            if spawn_timer >= 45: # Every 45 frames
                spawn_timer = 0
                if random.random() < 0.6:
                    obstacles.append(Obstacle(game_speed))
                else:
                    coins.append(Coin(game_speed))

            # Update Obstacles
            for obstacle in obstacles[:]:
                obstacle.update()
                if obstacle.y > SCREEN_HEIGHT:
                    obstacles.remove(obstacle)
                
                # Collision Detection (Box vs Box)
                p_rect = pygame.Rect(player.x - player.width//2, player.y, player.width, player.height)
                o_rect = pygame.Rect(obstacle.x - obstacle.width//2, obstacle.y, obstacle.width, obstacle.height)
                if p_rect.colliderect(o_rect):
                    game_over = True

            # Update Coins
            for coin in coins[:]:
                coin.update()
                if coin.y > SCREEN_HEIGHT:
                    coins.remove(coin)
                
                # Collision Detection (Box vs Circle-approximation)
                p_rect = pygame.Rect(player.x - player.width//2, player.y, player.width, player.height)
                c_rect = pygame.Rect(coin.x - coin.radius, coin.y - coin.radius, coin.radius*2, coin.radius*2)
                if p_rect.colliderect(c_rect):
                    player.score += 500 # Bonus score
                    coins.remove(coin)

        # 5. Drawing Graphics
        # Draw Background (Road & Lanes)
        screen.fill((30, 100, 30)) # Green environment
        pygame.draw.rect(screen, ROAD_COLOR, (50, 0, SCREEN_WIDTH - 100, SCREEN_HEIGHT)) # Grey Road
        
        # Lane Dividers
        pygame.draw.line(screen, LANE_MARKER_COLOR, (187, 0), (187, SCREEN_HEIGHT), 3)
        pygame.draw.line(screen, LANE_MARKER_COLOR, (312, 0), (312, SCREEN_HEIGHT), 3)

        # Draw Entities
        player.draw()
        for obstacle in obstacles:
            obstacle.draw()
        for coin in coins:
            coin.draw()

        # UI Text (Score)
        score_text = font.render(f"Score: {int(player.score)}", True, TEXT_COLOR)
        screen.blit(score_text, (10, 10))

        # Game Over Screen Overlay
        if game_over:
            over_text = over_font.render("GAME OVER", True, (255, 0, 0))
            restart_text = font.render("Press SPACE to Restart", True, TEXT_COLOR)
            screen.blit(over_text, (SCREEN_WIDTH // 2 - 120, SCREEN_HEIGHT // 2 - 50))
            screen.blit(restart_text, (SCREEN_WIDTH // 2 - 140, SCREEN_HEIGHT // 2 + 20))

        pygame.display.flip()
        clock.tick(FPS)

if __name__ == "__main__":
    main()
Editor is loading...
Leave a Comment