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 * def setup(): global selection, sustain, width, height, sust, th, tw selection = None sustain = 0 sust = 0 height = 500 width = 500 th = 500 tw = 500 size(width,height) class Block: instances = [] def __init__(self,x,y,sizex,sizey,text='',tags=[],ox=0,oy=0): self.x = x self.y = y self.sizex = sizex self.sizey = sizey self.text = text self.tags = tags self.ox = ox self.oy = oy Block.instances.append(self) Conglom(0,0,[self]) def __str__(self): return self.text class Conglom: instances = [] def __init__(self,vx,vy,inside=[],nomove=False,tempno=False): self.inside = inside self.vx = vx self.vy = vy self.nomove = nomove self.tempno = tempno Conglom.instances.append(self) def mousePressed(): global selection k = selection for j in Conglom.instances: for i in j.inside: if mouse.x >= i.x and mouse.x <= i.x+i.sizex and mouse.y >= i.y and mouse.y <= i.y+i.sizey: selection = i if k == selection: if mouse.button == 39:#keyboard.keyCode == 17 and keyboard.keyPressed: selection = Block(mouse.x-(25),mouse.y-(25),50,50,'') elif selection: if not touching(mouse.x,mouse.y,selection.x,selection.y,0,0,selection.sizex,selection.sizey): selection = None def mouseDragged(): if selection: if touching(mouse.x,mouse.y,selection.x,selection.y,0,0,selection.sizex,selection.sizey): if keyboard.keyCode != RIGHT or not keyboard.keyPressed: if 'held' not in selection.tags: selection.tags.append('held') if keyboard.keyCode == RIGHT and keyboard.keyPressed: for check in Conglom.instances: if selection in check.inside: Conglom.instances[Conglom.instances.index(check)].tempno = True break selection.sizex += mouse.x - mouse.px selection.sizey += mouse.y - mouse.py selection.sizex = abs(selection.sizex) selection.sizey = abs(selection.sizey) def mouseReleased(): for i in Block.instances: if 'held' in i.tags: i.tags.remove('held') def keyPressed(): global selection, sustain, sust, tw, th if selection: if keyboard.keyCode == 18 and 'held' in selection.tags and sustain >= 0: sustain += 5 elif sustain >= 0: sustain = 0 elif sustain >= 0: sustain = 0 if not selection: if keyboard.keyCode in [UP,DOWN,LEFT,RIGHT] and sust >= 0: sust += 5 else: sust = 0 else: sust = 0 done = 0 if keyboard.keyCode not in [16,UP,DOWN,LEFT,RIGHT,17,18] and selection: selection.text += keyboard.key if keyboard.keyCode == LEFT and selection: selection.text = selection.text[0:len(selection.text)-1] if keyboard.keyCode == 18 and selection and sustain >= 0: for j in Conglom.instances: for i in j.inside: if selection in j.inside: break if touching(i.x,i.y,selection.x,selection.y,i.sizex,i.sizey,selection.sizex,selection.sizey): for check in Conglom.instances: if selection in check.inside: Conglom.instances[Conglom.instances.index(check)].inside = Conglom.instances[Conglom.instances.index(check)].inside + j.inside Conglom.instances.remove(j) done = 1 break if done: break if keyboard.keyCode == UP and selection: for check in Conglom.instances: if selection in check.inside: if Conglom.instances[Conglom.instances.index(check)].nomove: Conglom.instances[Conglom.instances.index(check)].nomove = False else: Conglom.instances[Conglom.instances.index(check)].nomove = True break if keyboard.keyCode == DOWN and selection: for check in Conglom.instances: if selection in check.inside: Conglom.instances[Conglom.instances.index(check)].inside.remove(selection) Block.instances.remove(selection) selection = None break for check in Conglom.instances: if selection in check.inside: temp = Conglom.instances[Conglom.instances.index(check)] if keyboard.keyCode == 18 and selection and sustain > 200: for i in temp.inside: vectorx = (i.x-mouse.x)/10 vectory = (i.y-mouse.y)/10 Conglom(vectorx,vectory,[i]) Conglom.instances.remove(temp) sustain = -1 if keyboard.keyCode == 17 and selection and len(temp.inside) > 1: Conglom(0,0,[selection],temp.nomove) temp.inside.remove(selection) if not selection and sust > 200: if keyboard.keyCode == RIGHT: tw += 100 elif keyboard.keyCode == LEFT: tw -= 100 elif keyboard.keyCode == UP: th -= 100 elif keyboard.keyCode == DOWN: th += 100 sust = 0 def keyReleased(): global sustain, sust for j in Conglom.instances: j.tempno = False sustain = 0 sust = 0 def outline(texta,px,py,weight=2): fill(0); for x in range(weight): for y in range(weight): text(texta, px-x+1,py-y+1) fill(255); text(texta, px,py); def touching(x1,y1,x2,y2,sx1,sy1,sx2,sy2): if x1+sx1 >= x2 and x1 <= x2+sx2 and y1+sy1 >= y2 and y1 <= y2+sy2: return True else: return False 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(num,mult): num = int((float(num)/float(mult))+.5)*mult return num def draw(): global width, height, tw, th width = exphone(width,tw,5,1) height = exphone(height,th,5,1) size(width,height) if width <= 1: tw = 100 if height <= 100: th = 200 background(220-(hone(sust,0,30)/3),220,220) strokeWeight(5) stroke(0) fill(255) rect(-25,height-100,width+50,150) textSize(20) for j in Conglom.instances: stroke(0) fill(255) done = 0 for i in j.inside: if 'held' in i.tags and i == selection: for i in j.inside: j.vx = mouse.x - mouse.px j.vy = mouse.y - mouse.py done = 1 if done: break if not j.tempno: for al in j.inside: al.x += j.vx al.y += j.vy j.vy += 0.5 if j.nomove or j.tempno: j.vx = 0 j.vy = 0 bounced = 0 for i in j.inside: if i.y > height-100-i.sizey: save = i.y i.y = height-100-i.sizey save = save - i.y for al in j.inside: if i == al: continue al.y -= save bounced = 1 #break #i.y = hone(i.vy,0,1) if bounced: j.vy *= -1 j.vy += 5 j.vx *= 0.93 for i in j.inside: if i.x > width-i.sizex: j.vx *= -0.9 save = i.x i.x = width-i.sizex save = save - i.x for al in j.inside: if i == al: continue al.x -= save break for i in j.inside: if i.x < 0: j.vx *= -0.7 save = i.x i.x = 0 for al in j.inside: if i == al: continue al.x -= save break if j.nomove: stroke(100) for i in j.inside: if i == selection: fill(220+(sustain/2),255-(sustain),220) rect(i.x,i.y,i.sizex,i.sizey) fill(0) textAlign(CENTER,CENTER) outline(i.text,i.x+(i.sizex/2),i.y+(i.sizey/2),5) run()
bouncynotes.py
( around 258 lines python code )
Published By:
luyfru
Published on
2023-09-21T19:11:11Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here