racing_game.py

Created by 1epauletteshark

Created on December 06, 2023

2.91 KB


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

direction=0
x=35
y=35
SCREEN_W=320
SCREEN_H=222
FONT_W=10
FONT_H=16
SPEED=7
TURN=15
FPS=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 render(x,y,sprite,direction):
  global SPEED,TIME
  sprites = {"car":"0200b0300b0400b0500b1400b1500b1600b1700b0101r0201r0301b0401b0501b0701r0801r0901r1001r1401b1501b1601b1701b0002r0102r0202r0302r0402b0502b0602r0702r0802r0902r1002r1102r1402b1502b1602b1702b1902r2002r0003o0103r0203r0303r0403r0503r0603r0703r0803r0903r1003r1103r1203r1503r1603r1903r2003r0004o0104r0204r0304r0404r0504r0604r0704r0804r0904b1004b1104r1204r1304r1504r1604r1904r2004r0005r0105r0205r0305r0405r0505r0605r0705r0805b0905b1005b1105b1205r1305r1405r1505r1605r1705r1805r1905r2005r0006r0106r0206r0306r0406r0506r0606r0706r0806b0906b1006b1106b1206r1306r1406r1506r1606r1706r1806r1906r2006r0007r0107r0207r0307r0407r0507r0607r0707r0807b0907b1007b1107b1207r1307r1407r1507r1607r1707r1807r1907r2007r0008o0108r0208r0308r0408r0508r0608r0708r0808r0908b1008b1108r1208r1308r1508r1608r1908r2008r0009o0109r0209r0309r0409r0509r0609r0709r0809r0909r1009r1109r1209r1509r1609r1909r2009r0010r0110r0210r0310r0410b0510b0610r0710r0810r0910r1010r1110r1410b1510b1610b1710b1910r2010r0111r0211r0311b0411b0511b0711r0811r0911r1011r1411b1511b1611b1711b0212b0312b0412b0512b1412b1512b1612b1712b"}
  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(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 == "black" 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)

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",direction)
  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)