racing_2.py

Created by 1epauletteshark

Created on October 25, 2024

4.29 KB


from kandinsky import *
from random import randint
from math import cos, sin, radians
from time import monotonic, sleep
from ion import keydown, KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN

direction = 0
x, y = 35, 35
SCREEN_W, SCREEN_H = 320, 222
FONT_W, FONT_H = 10, 16
SPEED, TURN, FPS = 7, 15, 22
TIME = monotonic()

COLORS = {"b": "black", "w": "white", "r": "red", "o": "orange", "y": "yellow", "g": "green", "l": "blue", "p": "purple", "i": "pink"}
TRACK = [(20, 20, SCREEN_W - 40, 30), (20, 20, 30, SCREEN_H - 50), (20, SCREEN_H - 50, SCREEN_W - 40, 30), (SCREEN_W - 50, 20, 30, SCREEN_H - 50)]

def raytrace(x, y, dist, dir, angle):
    sclr = "grey"
    i = 1
    cos_dir_angle = cos(radians(dir) + angle)
    sin_dir_angle = sin(radians(dir) + angle)
    while sclr != (82, 195, 0) and i < dist:
        sclr = get_pixel(int(x), int(y))
        x += cos_dir_angle
        y += sin_dir_angle
        i += 1
    return i

def render(x, y, sprite, color, direction):
    global SPEED, TIME
    sprites = {"car": "0200b0300b0400b0500b1400b1500b1600b1700b0101r0201r0301b0401b0501b0701r0801r0901r1001r1401b1501b1601b1701b0002r0102r0202r0302r0402b0502b0602r0702r0802r0902r1002r1102r1402b1502b1602b1702b1902r2002r0003o0103r0203r0303r0403r0503r0603r0703r0803r0903r1003r1103r1203r1503r1603r1903r2003r0004o0104r0204r0304r0404r0504r0604r0704r0804r0904b1004b1104r1204r1304r1504r1604r1904r2004r0005r0105r0205r0305r0405r0505r0605r0705r0805b0905b1005b1105b1205r1305r1405r1505r1605r1705r1805r1905r2005r0006r0106r0206r0306r0406r0506r0606r0706r0806b0906b1006b1106b1206r1306r1406r1506r1606r1706r1806r1906r2006r0007r0107r0207r0307r0407r0507r0607r0707r0807b0907b1007b1107b1207r1307r1407r1507r1607r1707r1807r1907r2007r0008o0108r0208r0308r0408r0508r0608r0708r0808r0908b1008b1108r1208r1308r1508r1608r1908r2008r0009o0109r0209r0309r0409r0509r0609r0709r0809r0909r1009r1109r1209r1509r1609r1909r2009r0010r0110r0210r0310r0410b0510b0610r0710r0810r0910r1010r1110r1410b1510b1610b1710b1910r2010r0111r0211r0311b0411b0511b0711r0811r0911r1011r1411b1511b1611b1711b0212b0312b0412b0512b1412b1512b1612b1712b"}
    cos_dir = cos(radians(direction))
    sin_dir = sin(radians(direction))
    for i in range(0, len(sprites[sprite]), 5):
        pixel = sprites[sprite][i:i+5]
        pixel_x = x - 10 + int(pixel[:2])
        pixel_y = y - 6 + int(pixel[2:4])
        pixel_color = COLORS[pixel[4]]
        rotated_x = round((pixel_x - x) * cos_dir - (pixel_y - y) * sin_dir + x)
        rotated_y = round((pixel_x - x) * sin_dir + (pixel_y - y) * cos_dir + y)
        if pixel_color == "red":
            pixel_color = color
        elif pixel_color == "black" and color == "red" and get_pixel(rotated_x, rotated_y) == (82, 195, 0):
            SPEED -= 0.1
        if get_pixel(rotated_x, rotated_y) == (255, 255, 255):
            TIME = monotonic()
        set_pixel(rotated_x, rotated_y, pixel_color)

BOTS = [{"x": x, "y": y, "dir": 0, "col": (randint(1, 254), randint(1, 254), randint(1, 254))} for _ in range(int(input("Combien de Bots? ---> ")))]

while True:
    fill_rect(0, 0, SCREEN_W, SCREEN_H, "green")
    for rect in TRACK:
        fill_rect(rect[0], rect[1], rect[2], rect[3], "gray")
    fill_rect(50, 20, 5, 30, "white")
    SPEED = 7
    render(x, y, "car", "red", direction)
    for bot in BOTS:
        ray = [raytrace(bot["x"], bot["y"], 40, bot["dir"], i - 1) for i in range(3)]
        if ray[0] > ray[2]:
            bot["dir"] -= randint(12, 17)
        elif ray[0] < ray[2]:
            bot["dir"] += randint(12, 17)
        bot["x"] += round(randint(6, 8) * cos(radians(bot["dir"])))
        bot["y"] += round(randint(6, 8) * sin(radians(bot["dir"])))
        render(bot["x"], bot["y"], "car", bot["col"], bot["dir"])
    fill_rect(int(SCREEN_W / 2 - 75), int(SCREEN_H / 2 - 25), 50, 50, (round(255 + SPEED / 7 * (-255)), round(SPEED / 7 * 255), 0))
    draw_string(str(int(monotonic() - TIME)), int(200 - FONT_W / 2 * len(str(int(monotonic() - TIME)))), int(SCREEN_H / 2 - FONT_H / 2), "black", "green")
    if keydown(KEY_RIGHT):
        direction += TURN
    if keydown(KEY_LEFT):
        direction -= TURN
    if keydown(KEY_UP):
        x += round(SPEED * cos(radians(direction)))
        y += round(SPEED * sin(radians(direction)))
    if keydown(KEY_DOWN):
        x -= round(SPEED * cos(radians(direction)))
        y -= round(SPEED * sin(radians(direction)))
    sleep(1 / FPS)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.