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 from random import uniform, choice GRAV = .2 prev = 'hello world' state = 'free' hld = True hldpos = [] onthekeyboard = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'L', 'K', 'J', 'H', 'G', 'F', 'D', 'S', 'A', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '<', '>', '/', '?', ';', "'", ':', '"', '[', ']', '{', '}', '\\', '|', '-', '=', '_', '+', '`', '~'] keys = [] class Particle: instances = [] def __init__(self,x,y,vx,vy,size,life=20): self.size = size self.x = float(x) self.y = float(y) self.vx = float(vx) self.vy = float(vy) self.life = life Particle.instances.append(self) def drawSelf(self): fill(255,float(self.life)*12.75) ellipse(self.x,self.y,self.size,self.size) class Letter: instances = [] def __init__(self,txt,x,y,vx,vy,held=False): self.txt = txt self.x = x self.y = y self.vx = vx self.vy = vy self.held = held self.target = [None,None,None,None] Letter.instances.append(self) 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 setup(): size(500,500) noStroke() textAlign(CENTER) def keyPressed(): global keys, prev, hldpos, state if 10 not in keys and state != 'free': state = 'free' for i in Letter.instances: i.vx = i.target[2] i.vy = i.target[3] if keyboard.keyCode == UP and UP not in keys: hld = True prev = '' Letter(prev,mouse.x,mouse.y,0,0,True) for i in range(5): Particle(mouse.x,mouse.y,uniform(-4,4),uniform(-4,4),uniform(3,10)) if keyboard.keyCode not in keys: keys.append(keyboard.keyCode) if UP in keys: if keyboard.keyCode == LEFT and len(prev) > 0: prev = prev[:len(prev)-1] elif keyboard.keyCode not in [16,17,18,157,37,38,39,40]: prev += keyboard.key elif 10 in keys and len(Letter.instances) > 0: if 81 in keys:#q if len(Letter.instances) > 0: state = 'orbit' if len(Letter.instances) <= 20: k = TAU/len(Letter.instances) temp = 0 for i in Letter.instances: i.target = [50*cos(k*temp)+mouse.x,50*sin(k*temp)+mouse.y,3*cos(k*temp+PI/2),3*sin(k*temp+PI/2)] temp += 1 elif len(Letter.instances) <= 45: k = TAU/20 temp = 0 for i in Letter.instances[:20]: i.target = [50*cos(k*temp)+mouse.x,50*sin(k*temp)+mouse.y,3*cos(k*temp+PI/2),3*sin(k*temp+PI/2)] temp += 1 k = TAU/len(Letter.instances[20:]) temp = 0 for i in Letter.instances[20:]: i.target = [70*cos(k*temp)+mouse.x,70*sin(k*temp)+mouse.y,4*cos(k*temp+PI/2),4*sin(k*temp+PI/2)] temp += 1 else: k = TAU/20 temp = 0 for i in Letter.instances[:20]: i.target = [50*cos(k*temp)+mouse.x,50*sin(k*temp)+mouse.y,3*cos(k*temp+PI/2),3*sin(k*temp+PI/2)] temp += 1 k = TAU/25 temp = 0 for i in Letter.instances[20:45]: i.target = [70*cos(k*temp)+mouse.x,70*sin(k*temp)+mouse.y,4*cos(k*temp+PI/2),4*sin(k*temp+PI/2)] temp += 1 tempamt = len(Letter.instances[20:]) tempcurr = 45 temprounds = 0 tempspd = .3 while tempamt >= 30: k = TAU/30 temp = 0 tempamt -= 30 temprounds += 1 for i in Letter.instances[tempcurr:tempcurr+30]: i.target = [(70+20*temprounds)*cos(k*temp)+mouse.x,(70+20*temprounds)*sin(k*temp)+mouse.y,(4.5+tempspd*temprounds)*cos(k*temp+PI/2),(4.5+tempspd*temprounds)*sin(k*temp+PI/2)] temp += 1 tempcurr += 30 tempcurr -= 30 if len(Letter.instances[tempcurr:]) > 0: k = TAU/len(Letter.instances[tempcurr:]) temp = 0 for i in Letter.instances[tempcurr:]: i.target = [(70+20*temprounds)*cos(k*temp)+mouse.x,(70+20*temprounds)*sin(k*temp)+mouse.y,(4.5+tempspd*temprounds)*cos(k*temp+PI/2),(4.5+tempspd*temprounds)*sin(k*temp+PI/2)] temp += 1 elif 87 in keys: temp = 1 state = 'gridcorner' while temp**2 < len(Letter.instances): temp += 1 tempcurr = 0 for i in Letter.instances: i.target = [(tempcurr%temp)*10+10,(tempcurr/temp)*10+10,0,0] tempcurr += 1 elif 69 in keys: temp = 1 state = 'grid' while temp**2 < len(Letter.instances): temp += 1 tempx = 500/temp tempcurr = 0 for i in Letter.instances: i.target = [(tempcurr%temp)*tempx+15,(tempcurr/temp)*tempx+20,0,0] tempcurr += 1 elif 82 in keys: state = 'pause' for i in Letter.instances: i.target = [i.x,i.y,0,0] elif 84 in keys: state = 'outwards' tempamt = len(Letter.instances) temp = 0 temp2 = 15 tempcurr = 0 while tempamt > 12: temp += 1 tempamt -= 12 for i in range(temp): if i == 0: k = TAU/(12+tempamt) tempamt2 = 0 for j in Letter.instances[:12+tempamt]: tempamt2 += 1 j.target = [(temp2*i+temp2)*cos(k*tempamt2)+mouse.x,(temp2*i+temp2)*sin(k*tempamt2)+mouse.y,0,0] tempcurr = 12+tempamt else: k = TAU/12 tempamt2 = 0 for j in Letter.instances[tempcurr:tempcurr+12]: tempamt2 += 1 j.target = [(temp2*i+temp2)*cos(k*tempamt2)+mouse.x,(temp2*i+temp2)*sin(k*tempamt2)+mouse.y,0,0] tempcurr += 12 elif keyboard.keyCode == RIGHT: for i in Letter.instances: if len(i.txt) > 1: for j in range(int(len(i.txt)*1.5)): Particle(i.x,i.y,uniform(-4,4),uniform(-4,4),uniform(3,10)) k = textWidth(i.txt) for j in range(len(i.txt)): if i.txt[j] == ' ': continue Letter(i.txt[j],(i.x+(k/len(i.txt))*j)-k/2,i.y,i.vx,i.vy) Letter.instances.remove(i) elif keyboard.keyCode == DOWN: k = [uniform(0,500),uniform(0,500)] if LEFT in keys: temp = (((mouse.x-k[0])**2+(mouse.y-k[1])**2)**.5)/4000 temp = [(k[0]-mouse.x)*temp,(k[1]-mouse.y)*temp] Letter(prev,k[0],k[1],-1*temp[1],temp[0]) else: Letter(prev,k[0],k[1],0,0)#uniform(-4,4),uniform(-4,4)) for i in range(5): Particle(k[0],k[1],uniform(-4,4),uniform(-4,4),uniform(3,10)) elif keyboard.keyCode == 32: for i in range(15): Letter(choice(onthekeyboard),mouse.x,mouse.y,uniform(-5,5),uniform(-5,5)) Particle(mouse.x,mouse.y,uniform(-10,10),uniform(-10,10),uniform(5,15)) elif keyboard.keyCode not in [10,16,17,18,157,37,38,39,40]: k = [uniform(0,500),uniform(0,500)] if LEFT in keys: temp = (((mouse.x-k[0])**2+(mouse.y-k[1])**2)**.5)/4000 temp = [(k[0]-mouse.x)*temp,(k[1]-mouse.y)*temp] Letter(keyboard.key,k[0],k[1],-1*temp[1],temp[0]) else: Letter(keyboard.key,k[0],k[1],0,0)#uniform(-4,4),uniform(-4,4)) for i in range(5): Particle(k[0],k[1],uniform(-4,4),uniform(-4,4),uniform(3,10)) def keyReleased(): global keys, hld if keyboard.keyCode in keys: keys.remove(keyboard.keyCode) if keyboard.keyCode == UP: hld = False for i in Letter.instances: if i.held: i.held = False for j in range(8): Particle(i.x,i.y,uniform(-4,4),uniform(-4,4),uniform(3,10)) def draw(): background(0) fill(255) for i in Letter.instances: if not i.held and state == 'free': i.x += i.vx i.y += i.vy if i.x > 550 or i.x < -50 or i.y > 550 or i.y < -50: Letter.instances.remove(i) distance = ((mouse.x-i.x)**2+(mouse.y-i.y)**2)**.5 intensity = GRAV/(distance+1) i.vx -= (i.x-mouse.x)*intensity i.vy -= (i.y-mouse.y)*intensity elif not i.held: i.x = exphone(i.x,i.target[0],5,5) i.y = exphone(i.y,i.target[1],5,5) else: i.txt = prev text(i.txt,i.x,i.y) for i in Particle.instances: i.x += i.vx i.y += i.vy i.vx *= .9 i.vy *= .9 if i.life <= 0: Particle.instances.remove(i) i.life -= 1 i.drawSelf() run()
vortext.py
( around 235 lines python code )
Published By:
luyfru
Published on
2023-09-21T18:51:41Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here