problème de contrôle #2354
-
Je veux appuyer sur ZQSD pour diriger une voiture à deux roues et, pour cela, je dois cliquer sur Z puis sur Entrée pour qu’elle avance. Moi, je voudrais que, quand je clique sur Z, ça avance jusqu’à ce que je lâche. Voici mon programme : from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
hub = TechnicHub()
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait
# Initialiser le hub et les moteurs
hub = TechnicHub()
left_motor = Motor(Port.A)
right_motor = Motor(Port.B)
# Vitesse
SPEED = 300
print("Contrôle avec Z (avant), S (arrière), Q (gauche), D (droite). Tape 'x' pour quitter.")
while True:
key = input("Commande (z/s/q/d/x): ").lower()
if key == 'q':
left_motor.run(300)
right_motor.run(300)
elif key == 'd':
left_motor.run(-300)
right_motor.run(-300)
elif key == 'z':
left_motor.run(-300)
right_motor.run(300)
elif key == 's':
left_motor.run(300)
right_motor.run(-300)
elif key == 'x':
break
else:
left_motor.stop()
right_motor.stop()
wait(500)
left_motor.stop()
right_motor.stop()
print("Programme terminé.") |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
It isn't possible to do this using just the terminal in Pybricks Code using this method. It does not know anything about when a key was pressed or when a key was released. It only knows that you typed a letter in the terminal. You would need to write a special program on your computer that can detect the key press separately from the key release. Then use that to send one letter when a key is pressed and a different letter when a key is released to get the two different events. |
Beta Was this translation helpful? Give feedback.
-
Je suis super reconnaissant pour le temps que tu as pris à coder ça pour moi, merci ! |
Beta Was this translation helpful? Give feedback.
It isn't possible to do this using just the terminal in Pybricks Code using this method. It does not know anything about when a key was pressed or when a key was released. It only knows that you typed a letter in the terminal.
You would need to write a special program on your computer that can detect the key press separately from the key release. Then use that to send one letter when a key is pressed and a different letter when a key is released to get the two different events.