# Untitled

unknown
python
2 months ago
3.0 kB
1
Indexable
Never
```import pygame
import math
import random

# Initialisation de Pygame
pygame.init()

# Constantes
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
TRACK_COLOR, KART_COLOR, ENEMY_COLOR = (0x20, 0x20, 0x20), (0, 0, 255), (255, 0, 0)

# Initialisation de l'écran
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Karting Game")

# Classe pour les karts ennemis
class EnemyKart:
def __init__(self):
self.x = random.randint(0, SCREEN_WIDTH)
self.y = random.randint(0, SCREEN_HEIGHT)
self.speed = random.uniform(1, 3)
self.angle = 0

def move_towards(self, target_x, target_y):
direction = math.atan2(target_y - self.y, target_x - self.x)
self.x += self.speed * math.cos(direction)
self.y += self.speed * math.sin(direction)
self.angle = math.degrees(direction)

def draw(self):
draw_kart(self.x, self.y, self.angle, ENEMY_COLOR)

# Fonctions auxiliaires
def draw_kart(x, y, angle, color):
pygame.draw.polygon(screen, color, [
(x + 10 * math.cos(math.radians(angle)), y + 10 * math.sin(math.radians(angle))),
(x + 10 * math.cos(math.radians(angle + 120)), y + 10 * math.sin(math.radians(angle + 120))),
(x + 10 * math.cos(math.radians(angle + 240)), y + 10 * math.sin(math.radians(angle + 240)))
])

# Variables du jeu
player_kart_x, player_kart_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
player_kart_angle = 0
player_kart_speed = 0
turn_speed = 5
acceleration = 0.2
max_speed = 5
friction = 0.05

# Création des ennemis
enemies = [EnemyKart() for _ in range(5)]

# Boucle de jeu
running = True
clock = pygame.time.Clock()
while running:
# Gestion des événements
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Contrôles du kart du joueur
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_kart_angle += turn_speed
if keys[pygame.K_RIGHT]:
player_kart_angle -= turn_speed
if keys[pygame.K_UP]:
player_kart_speed += acceleration
if keys[pygame.K_DOWN]:
player_kart_speed -= acceleration

# Mise à jour de la position et de la vitesse du kart du joueur
player_kart_speed = max(-max_speed, min(max_speed, player_kart_speed))
player_kart_speed *= (1 - friction)
player_kart_x += player_kart_speed * math.cos(math.radians(player_kart_angle))
player_kart_y += player_kart_speed * math.sin(math.radians(player_kart_angle))

# Mise à jour des ennemis
for enemy in enemies:
enemy.move_towards(player_kart_x, player_kart_y)

# Dessin
screen.fill(TRACK_COLOR)
draw_kart(player_kart_x, player_kart_y, player_kart_angle, KART_COLOR)
for enemy in enemies:
enemy.draw()

# Mise à jour de l'écran
pygame.display.flip()
clock.tick(60)

pygame.quit()
```
Leave a Comment