racing2.py

Created by 1epauletteshark

Created on October 28, 2024

3.84 KB


from kandinsky import *
from random import *
from math import *
from time import *
from ion import *

W,H=320,222
FW,FH=10,16
SPEED=7
TURN=15
FPS=22
onLine=False
l_onLine=False

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

def text(text,x=W/2,y=H/2):
  text=str(text)
  draw_string(text,int(x-len(text)*FW/2),int(y-FH/2),"black","green")

def raytrace(x,y,dist,dir,angle):
  ray=[]
  sclr="grey"
  i=1
  while sclr!=(82,195,0) and i<dist:
    sclr=get_pixel(int(x),int(y))
    #set_pixel(int(x),int(y),"blue")
    x+=cos(radians(dir)+angle)
    y+=sin(radians(dir)+angle)
    i+=1
  return i
    

def render(x,y,color,direction):
  global SPEED,TIME
  sprites={"car":"0200b0300b0400b0500b1400b1500b1600b1700b0101r0201r0301b0401b0501b0701r0801r0901r1001r1401b1501b1601b1701b0002r0102r0202r0302r0402b0502b0602r0702r0802r0902r1002r1102r1402b1502b1602b1702b1902r2002r0003o0103r0203r0303r0403r0503r0603r0703r0803r0903r1003r1103r1203r1503r1603r1903r2003r0004o0104r0204r0304r0404r0504r0604r0704r0804r0904b1004b1104r1204r1304r1504r1604r1904r2004r0005r0105r0205r0305r0405r0505r0605r0705r0805b0905b1005b1105b1205r1305r1405r1505r1605r1705r1805r1905r2005r0006r0106r0206r0306r0406r0506r0606r0706r0806b0906b1006b1106b1206r1306r1406r1506r1606r1706r1806r1906r2006r0007r0107r0207r0307r0407r0507r0607r0707r0807b0907b1007b1107b1207r1307r1407r1507r1607r1707r1807r1907r2007r0008o0108r0208r0308r0408r0508r0608r0708r0808r0908b1008b1108r1208r1308r1508r1608r1908r2008r0009o0109r0209r0309r0409r0509r0609r0709r0809r0909r1009r1109r1209r1509r1609r1909r2009r0010r0110r0210r0310r0410b0510b0610r0710r0810r0910r1010r1110r1410b1510b1610b1710b1910r2010r0111r0211r0311b0411b0511b0711r0811r0911r1011r1411b1511b1611b1711b0212b0312b0412b0512b1412b1512b1612b1712b"}
  for i in range(0,len(sprites["car"]),5):
    pixel=sprites["car"][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(radians(direction))-(pixel_y-y)*sin(radians(direction))+x)
    rotated_y=round((pixel_x-x)*sin(radians(direction))+(pixel_y-y)*cos(radians(direction))+y)
    if pixel_color=="red":
      pixel_color=color
    if color=="red":
      if pixel_color=="black" and get_pixel(rotated_x,rotated_y)==(82,195,0): SPEED-=0.1
    set_pixel(rotated_x, rotated_y, pixel_color)

while True:
  direction=0
  x,y=35,35
  #botn=int(input("Combien de Bots? ---> "))
  botn=2
  BOTS=[{"x":x,"y":y,"dir":0,"col":(randint(1,254),randint(1,254),randint(1,254))} for i in range(botn)]
  laps=3
  #sleep(0.5)
  while laps>0:
    fill_rect(0,0,W,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
    for bot in BOTS:
      ray1=raytrace(bot["x"],bot["y"],40,bot["dir"],-1)
      ray2=raytrace(bot["x"],bot["y"],40,bot["dir"],1)
      if ray1>ray2:   bot["dir"]-=randint(12,17)
      elif ray1<ray2: 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"],bot["col"],bot["dir"])
    if get_pixel(x,y)==(255,255,255): onLine=True
    else: onLine=False
    if l_onLine==False and onLine==True: laps-=1
    l_onLine=onLine
    render(x,y,"red",direction)
    fill_rect(int(W/2-75),int(H/2-25),50,50,(round(255+SPEED/7*(-255)),round(SPEED/7*255),0))
    text(str(laps)+" laps",x=(135+280)/2)
    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.