Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * from math import cos, sin keys = [] camx = 0.0 tcamx = 0.0 camy = 0.0 tcamy = 0.0 scalex = 1 scaley = 1 class Player: def __init__(self): self.x = 100.0 self.y = 200.0 self.velocity = 19.0 self.vpoint = 0.0 self.pointing = 0.0 def __str__(self): return str([self.x,self.y,self.velocity,self.pointing,self.vpoint]) def vectorToVxy(vel,di): return [cos(di)*vel,sin(di)*vel] def setup(): global car size(500,500) car = Player() def keyPressed(): global keys if keyboard.keyCode not in keys: keys.append(keyboard.keyCode) def keyReleased(): global keys keys.remove(keyboard.keyCode) def hone(var,target,step): if var < step+target and var > target-step and var != target: var = target elif var > target: var -= step elif var < target: var += step return var def exphone(var,target,mult,snap): if var < snap+target and var > target-snap and var != target: var = target else: var += (target-var)/mult return var def toNearest(var,every): if every: temp = float(int(var/every)*every) else: temp = 0.0 return temp def draw(): global car, camx, camy, tcamx, tcamy, scalex, scaley if car.velocity > 10: if car.velocity < 88: scalex = 1/(car.velocity/20+.5) scaley = 1/(car.velocity/20+.5) else: scalex = 1/((88.0/20)+.5) scaley = 1/((88.0/20)+.5) else: scalex = 1 scaley = 1 scale(scalex,scaley) background(255,0,255) rectMode(CENTER) pushMatrix() translate(-250+250*(1/scalex),-250+250*(1/scaley)) translate(-camx,-camy) colorMode(HSB) stroke(0) strokeWeight(100) for i in [-1250,-750,-250,250,750,1250,1750]: for j in [-1250,-750,-250,250,750,1250,1750]: fill(abs((toNearest(camx,500)+i)+(toNearest(camy,500)+j))/50,255,255) rect(toNearest(camx,500)+i,toNearest(camy,500)+j,500,500) colorMode(RGB) noStroke() translate(car.x,car.y) rotate(car.pointing) fill(255) rect(0,0,100,50,10) fill(100) textMode(CENTER,CENTER) textSize(100) popMatrix() if UP in keys: if car.velocity < 10: car.velocity += .1 elif car.velocity > 88: car.velocity += .3 else: car.velocity += .05 else: car.velocity *= 0.8 if 16 in keys and car.velocity > 10: car.velocity = hone(car.velocity,10,.5) if LEFT in keys: if abs(car.vpoint) < PI/30: car.vpoint -= PI/1000 elif RIGHT in keys: if abs(car.vpoint) < PI/30: car.vpoint += PI/1000 else: car.vpoint *= 0.8 car.pointing += car.vpoint temp = vectorToVxy(car.velocity,car.pointing) car.x += temp[0] car.y += temp[1] car.pointing %= TAU if car.velocity < 20: if car.x > camx + 500: tcamx += 500 elif car.x < camx: tcamx -= 500 if car.y > camy + 500: tcamy += 500 elif car.y < camy: tcamy -= 500 tcamx = toNearest(tcamx,500) tcamy = toNearest(tcamy,500) camx = exphone(camx,tcamx,8,1) camy = exphone(camy,tcamy,8,1) else: tcamx = car.x-250 tcamy = car.y-250 camx = exphone(camx,tcamx,3,1) camy = exphone(camy,tcamy,3,1) fill(255) run()
racing.py
( around 130 lines python code )
Published By:
luyfru
Published on
2023-09-21T19:05:15Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here