Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_pool.py ZZZ
- import sys, math
- from Tkinter import *
- import random
- size = ww, hh = 1200, 640
- table = tablewidth, tableheight = 960, 540
- tX = 60
- tY = 100
- tZ = 40
- friction = 0.995
- #creates ball class
- class Ball:
- def __init__(self):
- self.x = 0
- self.y = 0
- self.radius = 16
- self.vel = 0.00
- self.pos = [tX+60, tY+tableheight/2-self.radius]
- self.aim = [0, 0]
- self.obj = 0
- self.color = "red"
- self.name = ""
- self.type = ""
- 0
- #Initialises functions to handle vector math
- def strike(b,vel):
- vel = min(vel,27.0)
- p = abs(b.x)*1.0/(abs(b.x)+abs(b.y))
- b.x = vel*p
- b.y = vel*(1-p)
- def vsub(v1, v2):
- return [v1[0]-v2[0], v1[1]-v2[1]]
- def vlength(b):
- return (b[0]*b[0]+b[1]*b[1])**(0.5)
- def c(r,g,b):
- return '#{:02x}{:02x}{:02x}'.format(r,g,b)
- #creates an empty list to store balls, and then creates and adds objects of the Ball class to the list
- balls = list()
- for i in range(16):
- balls.append(Ball())
- 0
- #initialises 16 objects of the ball class
- z = 0
- while z < 16:
- if z == 0:
- balls[z].name = "cue"
- balls[z].type = "cue ball"
- balls[z].color = "red"
- if z == 1:
- balls[z].name = "8"
- balls[z].type = "8 ball"
- if (z % 2) == 0 and z != 1 and z != 0:
- balls[z].name = str(z-1)
- balls[z].type = "solid"
- if (z % 2 == 1) and z != 1 and z != 0:
- balls[z].name = str(z-2)
- balls[z].type = "striped"
- z += 1
- #placeholder values for bug testing
- balls[1].pos[0] = 600
- balls[1].pos[1] = 480
- balls[1].x = 20
- balls[1].y = -80
- strike(balls[1],3.0)
- balls[0].pos[0] = 120
- balls[0].pos[1] = 540
- balls[0].color = "white"
- balls[0].x = 800
- balls[0].y = 700
- strike(balls[0],99.0) #velocity from strike will be lowered by function call
- root = Tk()
- root.withdraw()
- top = Toplevel()
- top.title('Tk Pool')
- Label(top)
- canvas = Canvas(top, bg='orange', width=ww, height=hh)
- canvas.pack()
- #Draws Snooker Table
- canvas.create_rectangle((tX-tZ, tY-tZ, tablewidth+tZ, tableheight+tZ), fill=c(0, 128, 0))
- canvas.create_rectangle((tX, tY, tablewidth, tableheight), fill=c(0, 255, 0))
- z = 0
- while z < 16:
- #renders balls
- r = balls[z].radius
- x,y = int(balls[z].pos[0]), int(balls[z].pos[1])
- balls[z].obj = canvas.create_oval((x-r,y-r,x+r,y+r), fill=balls[z].color)
- z += 1
- '''
- Widget.bind(canvas, "<Button-1>",
- mouseDown)
- Widget.bind(canvas, "<Button1-ButtonRelease>",
- mouseUp)
- '''
- run = True
- while run:
- #checks for collision, and updates speeds
- i = 0
- while i < len(balls):
- j = i+1
- while j < len(balls):
- #finds a normal vector
- iii = balls[i]
- jjj = balls[j]
- n = vsub(iii.pos, jjj.pos)
- #finds the length between balls
- length = vlength(n)
- radx2 = jjj.radius+iii.radius
- if length and length <= radx2:
- #reverse straight to contact point
- cp = length/radx2
- iii.pos = [iii.pos[0]-iii.x*cp, iii.pos[1]-iii.y*cp]
- jjj.pos = [jjj.pos[0]-jjj.x*cp, jjj.pos[1]-jjj.y*cp]
- #momentum transfer of x
- ix = iii.pos[0]
- jx = jjj.pos[0]
- x = abs(jx-ix)/radx2
- iii.x, jjj.x = jjj.x*x, iii.x*x
- #momentum transfer of y
- iy = iii.pos[1]
- jy = jjj.pos[1]
- y = abs(jy-iy)/radx2
- iii.y, jjj.y = jjj.y*y, iii.y*y
- j+=1
- i+=1
- i = 0
- while i <= len(balls)-1:
- #checks if balls are going out of bounds and updates speeds with v=-eu
- ball = balls[i]
- x,y = ball.pos
- vel = abs(ball.x) + abs(ball.y)
- if vel > 0.009:
- z = ball.pos[0]
- if z > tablewidth-ball.radius:
- ball.x *= -1
- ball.pos[0] = tablewidth-ball.radius-2
- elif z < tX+ball.radius:
- ball.x *= -1
- ball.pos[0] = tX+ball.radius+2
- z = ball.pos[1]
- if z > tableheight-ball.radius:
- ball.pos[1] = tableheight-ball.radius-2
- ball.y *= -1
- elif z < tY+ball.radius:
- ball.pos[1] = tY+ball.radius+2
- ball.y *= -1
- #applies friction to balls speed
- ball.x *= friction
- ball.y *= friction
- #updates positions of balls
- ball.pos = [ball.pos[0]+ball.x, ball.pos[1]+ball.y]
- else:
- ball.x = ball.y = 0
- #renders balls
- r = ball.radius
- x,y = ball.pos
- canvas.coords(ball.obj, (x-r,y-r,x+r,y+r))
- i+=1
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement