Voici un code de base complet en Python avec pygame qui simule un petit robot équipé de capteurs virtuels :
Comprendre comment un robot peut lire et interpréter des capteurs dans son environnement.
Installe Pygame si nécessaire :
pip install pygame
import pygame
import random
import math
# Initialisation
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simulation robot avec capteurs")
clock = pygame.time.Clock()
# Robot
robot = pygame.Rect(100, 100, 40, 40)
robot_speed = 2
robot_angle = 0 # direction en degrés
# Obstacles
obstacles = [
pygame.Rect(300, 200, 100, 50),
pygame.Rect(600, 400, 50, 150)
]
# Sources de chaleur (détectables par IR)
heat_sources = [
pygame.Rect(700, 100, 20, 20),
pygame.Rect(150, 500, 20, 20)
]
font = pygame.font.SysFont(None, 24)
def get_temperature():
return round(random.uniform(18, 28), 1)
def get_velocity():
return robot_speed # constant ici, mais peut varier
def get_distance_to_obstacle():
"""Renvoie la distance jusqu’au 1er obstacle en face"""
ray_length = 150
for i in range(ray_length):
x = robot.centerx + i * math.cos(math.radians(robot_angle))
y = robot.centery + i * math.sin(math.radians(robot_angle))
point = pygame.Rect(x, y, 2, 2)
if any(point.colliderect(obs) for obs in obstacles):
return i
return ray_length
def detect_infrared():
"""Détection d'une source de chaleur proche"""
for heat in heat_sources:
dist = math.hypot(robot.centerx - heat.centerx, robot.centery - heat.centery)
if dist < 100:
return True
return False
def draw_environment():
screen.fill((30, 30, 30))
# Obstacles
for obs in obstacles:
pygame.draw.rect(screen, (150, 150, 150), obs)
# Sources de chaleur
for heat in heat_sources:
pygame.draw.rect(screen, (255, 0, 0), heat)
# Robot
pygame.draw.rect(screen, (0, 200, 255), robot)
# Capteur distance : rayon
dist = get_distance_to_obstacle()
end_x = robot.centerx + dist * math.cos(math.radians(robot_angle))
end_y = robot.centery + dist * math.sin(math.radians(robot_angle))
pygame.draw.line(screen, (0, 255, 0), robot.center, (end_x, end_y), 2)
# Données capteurs
temp = get_temperature()
vel = get_velocity()
ir = detect_infrared()
capteurs = [
f"🌡 Température : {temp} °C",
f"🚀 Vitesse : {vel} px/s",
f"📏 Distance obstacle : {dist} px",
f"🔥 Infrarouge : {'Oui' if ir else 'Non'}"
]
for i, txt in enumerate(capteurs):
img = font.render(txt, True, (255, 255, 255))
screen.blit(img, (10, 10 + i * 20))
running = True
while running:
clock.tick(60)
keys = pygame.key.get_pressed()
# Déplacement
if keys[pygame.K_LEFT]:
robot_angle -= 3
if keys[pygame.K_RIGHT]:
robot_angle += 3
if keys[pygame.K_UP]:
robot.x += robot_speed * math.cos(math.radians(robot_angle))
robot.y += robot_speed * math.sin(math.radians(robot_angle))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
draw_environment()
pygame.display.flip()
pygame.quit()
| Élément | Description |
|---|---|
robot_angle |
Contrôle l’orientation du robot |
get_temperature() |
Simule une température entre 18 et 28°C |
get_velocity() |
Renvoie une vitesse constante |
get_distance_to_obstacle() |
Simule un capteur ultrason ou LIDAR avec un rayon jusqu’à 150 px |
detect_infrared() |
Détecte une source de chaleur à moins de 100 px |
pygame.draw.line() |
Affiche visuellement le rayon du capteur de distance |