Last active
July 17, 2024 14:04
-
-
Save matiasmicheletto/511b3f55b66fb6476f313fd53b3ea145 to your computer and use it in GitHub Desktop.
Bus race for console :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import os | |
import math | |
buses_names = ["CONDOR", "PLUSMAR", "RAPIDO", "FLECHA"] | |
def clear_console(): | |
os.system('cls' if os.name == 'nt' else 'clear') | |
def print_buses(bus_positions, finish): | |
road = "+" + "-" * finish + "+" | |
print(road) | |
for i, position in enumerate(bus_positions): | |
b_space = finish - position - len(buses_names[i]) - 2 | |
finish_line = " " * b_space + "|" if b_space > 0 else "" | |
bus = "|" + " " * position + "🚍" + buses_names[i] | |
print(bus, end="") | |
print(finish_line) | |
print(road) | |
def bus_race(finish): | |
bus_positions = [0] * len(buses_names) | |
clear_console() | |
while all(position < finish for position in bus_positions): | |
for i in range(len(buses_names)): | |
bus_positions[i] += random.randint(0, 2) | |
print_buses(bus_positions, finish) | |
time.sleep(0.1) | |
clear_console() | |
winners = [i for i, position in enumerate(bus_positions) if position >= finish] | |
msg = "" | |
if len(winners) > 1: | |
msg = "¡Es un empate entre " + " y ".join([buses_names[i] for i in winners]) + "!\n\n" | |
else: | |
msg = "¡El ganador es el " + buses_names[winners[0]] + "!\n\n" | |
print_buses(bus_positions, finish) | |
print("\n\n" + " " * math.floor((finish - len(msg))/2) + msg) | |
bus_race(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment