python
import pygame
import math
import random
# ----------------------------
# Config
# ----------------------------
WIDTH, HEIGHT = 1200, 700
FPS = 60
SKY = (135, 206, 235)
GROUND = (70, 120, 60)
DIRT = (100, 70, 40)
WHITE = (255, 255, 255)
BLACK = (20, 20, 20)
RED = (220, 60, 60)
YELLOW = (255, 220, 50)
GRAVITY = 0.45
BIKE_ACCEL = 0.25
BIKE_BRAKE = 0.2
MAX_SPEED = 9
FRICTION = 0.03
# ----------------------------
# Terrain
# ----------------------------
def terrain_height(x):
"""
Terrain montagneux procédural.
Plus x avance, plus il y a des bosses/reliefs.
"""
base = 520
h = (
80 * math.sin(x * 0.006) +
45 * math.sin(x * 0.013) +
25 * math.sin(x * 0.027)
)
# montée globale progressive
slope = -0.03 * x
# petites variations
bumps = 10 * math.sin(x * 0.08)
return base + h + slope + bumps
def terrain_slope(x):
dx = 1
return terrain_height(x + dx) - terrain_height(x - dx)
# ----------------------------
# Joueur BMX
# ----------------------------
class Bike:
def __init__(self):
self.x = 100
self.y = terrain_height(self.x) - 40
self.vx = 0
self.vy = 0
self.angle = 0
self.angular_vel = 0
self.on_ground = True
self.alive = True
self.score = 0
self.max_x = self.x
def update(self, keys):
if not self.alive:
return
# Contrôles
if keys[pygame.K_RIGHT]:
self.vx += BIKE_ACCEL
if keys[pygame.K_LEFT]:
self.vx -= BIKE_BRAKE
# rotation en l'air
if not self.on_ground:
if keys[pygame.K_a]:
self.angular_vel -= 0.01
if keys[pygame.K_d]:
self.angular_vel += 0.01
# Limites vitesse
self.vx = max(-4, min(MAX_SPEED, self.vx))
# friction
if self.on_ground:
self.vx *= (1 - FRICTION)
# gravité
self.vy += GRAVITY
# déplacement
self.x += self.vx
self.y += self.vy
self.angle += self.angular_vel
# amortissement rotation
self.angular_vel *= 0.99
# collision terrain
ground_y = terrain_height(self.x) - 20
slope = terrain_slope(self.x)
terrain_angle = math.atan2(slope, 2)
if self.y >= ground_y:
self.y = ground_y
self.vy = 0
self.on_ground = True
# Le vélo suit la pente
self.angle += (terrain_angle - self.angle) * 0.15
# petit boost si on descend
if slope > 1:
self.vx += 0.02
# saut auto léger selon relief
if abs(slope) > 6 and abs(self.vx) > 2:
self.vy = -2
self.on_ground = False
else:
self.on_ground = False
# Mort si retourné
if abs(self.angle) > 1.7:
self.alive = False
# Mort si trop lent et redescend derrière
if self.x < self.max_x - 150:
self.alive = False
self.max_x = max(self.max_x, self.x)
self.score = int(self.max_x / 10)
def draw(self, screen, camera_x):
sx = int(self.x - camera_x)
sy = int(self.y)
# géométrie du BMX
wheel_r = 18
wheel_dist = 50
cos_a = math.cos(self.angle)
sin_a = math.sin(self.angle)
def rot(px, py):
return (
sx + px * cos_a - py * sin_a,
sy + px * sin_a + py * cos_a
)
rear = rot(-wheel_dist / 2, 0)
front = rot(wheel_dist / 2, 0)
seat = rot(-10, -28)
handle = rot(18, -25)
center = rot(0, -10)
head = rot(0, -48)
# roues
pygame.draw.circle(screen, BLACK, (int(rear[0]), int(rear[1])), wheel_r, 3)
pygame.draw.circle(screen, BLACK, (int(front[0]), int(front[1])), wheel_r, 3)
# cadre
pygame.draw.line(screen, RED, rear, center, 4)
pygame.draw.line(screen, RED, center, front, 4)
pygame.draw.line(screen, RED, center, seat, 4)
pygame.draw.line(screen, RED, front, handle, 4)
pygame.draw.line(screen, RED, seat, handle, 4)
# rider
pygame.draw.line(screen, BLACK, seat, head, 4) # corps
arm_end = ((handle[0] + head[0]) / 2, (handle[1] + head[1]) / 2)
pygame.draw.line(screen, BLACK, head, arm_end, 3)
pygame.draw.line(screen, BLACK, arm_end, handle, 3)
pygame.draw.line(screen, BLACK, center, (center[0] - 5, center[1] + 25), 3)
pygame.draw.line(screen, BLACK, center, (center[0] + 10, center[1] + 25), 3)
pygame.draw.circle(screen, YELLOW, (int(head[0]), int(head[1] - 8)), 10)
# ----------------------------
# Dessin terrain
# ----------------------------
def draw_terrain(screen, camera_x):
points = []
step = 8
for sx in range(0, WIDTH + step, step):
world_x = sx + camera_x
y = terrain_height(world_x)
points.append((sx, int(y)))
poly = [(0, HEIGHT)] + points + [(WIDTH, HEIGHT)]
pygame.draw.polygon(screen, DIRT, poly)
# couche d'herbe
grass = points[:]
pygame.draw.lines(screen, GROUND, False, grass, 4)
def draw_background(screen, camera_x):
screen.fill(SKY)
# montagnes lointaines
for layer, color, amp, offset_y, speed in [
(1, (160, 180, 200), 50, 260, 0.2),
(2, (130, 150, 170), 80, 320, 0.35),
]:
pts = []
for x in range(0, WIDTH + 20, 20):
wx = x + camera_x * speed
y = offset_y + amp * math.sin(wx * 0.003 + layer) + 20 * math.sin(wx * 0.01)
pts.append((x, int(y)))
poly = [(0, HEIGHT), *pts, (WIDTH, HEIGHT)]
pygame.draw.polygon(screen, color, poly)
# ----------------------------
# Jeu
# ----------------------------
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("BMX Montagne")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 28)
big_font = pygame.font.SysFont("arial", 52, bold=True)
bike = Bike()
camera_x = 0
running = True
while running:
dt = clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r and not bike.alive:
bike = Bike()
camera_x = 0
keys = pygame.key.get_pressed()
bike.update(keys)
# caméra suit le joueur
target_camera = bike.x - 250
camera_x += (target_camera - camera_x) * 0.08
camera_x = max(0, camera_x)
# dessin
draw_background(screen, camera_x)
draw_terrain(screen, camera_x)
bike.draw(screen, camera_x)
# UI
txt = font.render(f"Distance: {bike.score} m", True, WHITE)
screen.blit(txt, (20, 20))
txt2 = font.render("→ accélérer | ← freiner/reculer | A/D rotation en l'air | R recommencer", True, WHITE)
screen.blit(txt2, (20, 55))
if not bike.alive:
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 120))
screen.blit(overlay, (0, 0))
game_over = big_font.render("GAME OVER", True, WHITE)
restart = font.render("Appuie sur R pour recommencer", True, WHITE)
screen.blit(game_over, (WIDTH // 2 - game_over.get_width() // 2, HEIGHT // 2 - 60))
screen.blit(restart, (WIDTH // 2 - restart.get_width() // 2, HEIGHT // 2 + 10))
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()