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, tan, atan2, atan, radians, degrees class Ball: instance = [] cues = [] moving = [] def __init__(self,x,y,number,motion=0,direction=0,px=0,py=0): self.x = x self.y = y self.motion = motion self.direction = direction self.number = number Ball.instance.append(self) if self.number == 0: Ball.cues.append(self) def collide(self): '''m = tan(radians(i.direction)) try: inversem = -1/m except: inversem = 99999999999 leftx = cos(radians(i.direction+90)) rightx = cos(radians(i.direction-90)) lefty = sin(radians(i.direction+90)) righty = sin(radians(i.direction-90))''' for j in Ball.instance: if self is j: continue if ((self.x-j.x)**2+(self.y-j.y)**2)**.5 <= 20: #bad collision if abs(self.motion) > abs(j.motion): j.direction = degrees(atan2(self.py-j.py,self.px-j.px))+ 180 temp = j.motion j.motion = self.motion self.motion = j.motion*(3.0/5) self.direction += (j.direction-self.direction) * -2 elif abs(j.motion) > abs(self.motion): self.direction = degrees(atan2(j.py-self.py,j.px-self.px))+ 180 temp = self.motion self.motion = j.motion j.motion = self.motion*(3.0/5) j.direction += (self.direction-j.direction) * -2 for j in Bumper.ready: if ((self.x-j.x)**2+(self.y-j.y)**2)**.5 <= 30: self.motion = self.motion*(1.2)+3 j.cooldown = 60 class Hole(): instance = [] def __init__(self,x,y): self.x = x self.y = y Hole.instance.append(self) class Bumper(): instance = [] ready = [] def __init__(self,x,y): self.x = x self.y = y self.motion = 0 self.direction = 0 self.px = x self.py = y self.cooldown = 0 Bumper.instance.append(self) Bumper.ready.append(self) def setup(): global b0, b1, b2, b3, b4, b5 size(500,500) b0 = Ball(250,100,0) b1 = Ball(250,150,1) b2 = Ball(250,300,2) b3 = Ball(100,350,3) b4 = Ball(350,300,4) b5 = Ball(200,200,5) def drawHoles(): for i in Hole.instance: fill(0) ellipse(i.x,i.y,30,30) def drawBumper(): for i in Bumper.instance: fill(255,100,255) ellipse(i.x,i.y,i.cooldown/3+40,i.cooldown/3+40) fill(255,100,0) ellipse(i.x,i.y,30,30) def checkBumper(): Bumper.ready = [] fill(0) for i in Bumper.instance: if i.cooldown == 0: Bumper.ready.append(i) else: i.cooldown -= 1 def drawBalls(): #draw cue-ball for i in Ball.instance: if i.number == 0: fill(255) ellipse(i.x,i.y,20,20) else: if i.number > 8: fill(255) ellipse(i.x,i.y,20,20) if i.number in [1,9]: fill(255,255,0) if i.number in [2,10]: fill(60,60,250) if i.number in [3,11]: fill(255,0,0) if i.number in [4,12]: fill(0,0,150) if i.number in [5,13]: fill(255,120,0) if i.number in [6,14]: fill(0,230,100) if i.number in [7,15]: fill(150,0,0) if i.number in [8]: fill(0) if i.number < 9: ellipse(i.x,i.y,20,20) if i.number > 8: beginShape();vertex(i.x-8,i.y+5);vertex(i.x-10,i.y);vertex(i.x-8,i.y-5);vertex(i.x+8,i.y-5);vertex(i.x+10,i.y);vertex(i.x+8,i.y+5);vertex(i.x-8,i.y+5);endShape() fill(0) if i.number in [4,7,8,12,15]: fill(255) text(str(i.number),i.x,i.y) def moveBall(): for i in Ball.instance: i.px = i.x i.py = i.y i.x += cos(radians(i.direction))*i.motion i.y += sin(radians(i.direction))*i.motion if i.motion != 0 and i not in Ball.moving: Ball.moving.append(i) #print i.number, "+" if i.motion > 0: i.motion -= .1 if i.motion < 0: i.motion +=.1 if i.motion > -.1 and i.motion < .1: i.motion = 0 try: Ball.moving.remove(i) #print i.number, "-" except: pass if i.x > 600 or i.x < -100 or i.y > 600 or i.y < -100: if i not in Ball.cues: Ball.instance.remove(i) try: Ball.moving.remove(i) except: pass for j in Hole.instance: if ((i.x-j.x)**2+(i.y-j.y)**2)**.5 <= 10: Ball.instance.remove(i) try: Ball.cues.remove(i) except: pass try: Ball.moving.remove(i) except: pass def mousePressed(): for i in Ball.cues: i.direction = degrees(atan2(i.y-mouse.y,i.x-mouse.x)) + 180 i.motion = ((i.x-mouse.x)**2+(i.y-mouse.y)**2)**.5 /30 + 2 def keyPressed(): if keyboard.key in ['1','2','3','4','5','6','7','8','9']: Ball(mouse.x,mouse.y,int(keyboard.key)) if keyboard.key in ['0']: instcopy = Ball.instance Ball.instance = [] Ball(mouse.x,mouse.y,0) Ball.instance = Ball.instance + instcopy if keyboard.key in ['q','w','e','r','t','y']: if keyboard.key in ['q']: Ball(mouse.x,mouse.y,10) if keyboard.key in ['w']: Ball(mouse.x,mouse.y,11) if keyboard.key in ['e']: Ball(mouse.x,mouse.y,12) if keyboard.key in ['r']: Ball(mouse.x,mouse.y,13) if keyboard.key in ['t']: Ball(mouse.x,mouse.y,14) if keyboard.key in ['y']: Ball(mouse.x,mouse.y,15) if keyboard.key in ['h']: Hole(mouse.x,mouse.y) if keyboard.key in ['b']: Bumper(mouse.x,mouse.y) if keyboard.key == ' ': for i in Ball.instance: i.motion = 0 def draw(): background(0,105,0) textAlign(CENTER,CENTER) moveBall() drawHoles() checkBumper() drawBumper() drawBalls() for i in Ball.moving: i.collide() run()
pool.py
( around 197 lines python code )
Published By:
luyfru
Published on
2023-09-21T19:26:46Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here