Advertisement
here2share

# Tk_pool.py ZZZ

Aug 10th, 2020
1,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. # Tk_pool.py ZZZ
  2.  
  3. import sys, math
  4. from Tkinter import *
  5. import random
  6.  
  7. size = ww, hh = 1200, 640
  8. table = tablewidth, tableheight = 960, 540
  9. tX = 60
  10. tY = 100
  11. tZ = 40
  12. friction = 0.995
  13.  
  14. #creates ball class
  15. class Ball:
  16.     def __init__(self):
  17.         self.x = 0
  18.         self.y = 0
  19.         self.radius = 16
  20.         self.vel = 0.00
  21.         self.pos = [tX+60, tY+tableheight/2-self.radius]
  22.         self.aim = [0, 0]
  23.         self.obj = 0
  24.         self.color = "red"
  25.         self.name = ""
  26.         self.type = ""
  27. 0
  28.  
  29. #Initialises functions to handle vector math
  30. def strike(b,vel):
  31.     vel = min(vel,27.0)
  32.     p = abs(b.x)*1.0/(abs(b.x)+abs(b.y))
  33.     b.x = vel*p
  34.     b.y = vel*(1-p)
  35.  
  36. def vsub(v1, v2):
  37.     return [v1[0]-v2[0], v1[1]-v2[1]]
  38.  
  39. def vlength(b):
  40.     return (b[0]*b[0]+b[1]*b[1])**(0.5)
  41.  
  42. def c(r,g,b):
  43.     return '#{:02x}{:02x}{:02x}'.format(r,g,b)
  44.  
  45. #creates an empty list to store balls, and then creates and adds objects of the Ball class to the list
  46. balls = list()
  47.  
  48. for i in range(16):
  49.     balls.append(Ball())
  50. 0
  51. #initialises 16 objects of the ball class
  52. z = 0
  53. while z < 16:
  54.     if z == 0:
  55.         balls[z].name = "cue"
  56.         balls[z].type = "cue ball"
  57.         balls[z].color = "red"
  58.     if z == 1:
  59.         balls[z].name = "8"
  60.         balls[z].type = "8 ball"
  61.     if (z % 2) == 0 and z != 1 and z != 0:
  62.         balls[z].name = str(z-1)
  63.         balls[z].type = "solid"
  64.     if (z % 2 == 1) and z != 1 and z != 0:
  65.         balls[z].name = str(z-2)
  66.         balls[z].type = "striped"
  67.     z += 1
  68.  
  69. #placeholder values for bug testing
  70. balls[1].pos[0] = 600
  71. balls[1].pos[1] = 480
  72. balls[1].x = 20
  73. balls[1].y = -80
  74. strike(balls[1],3.0)
  75.  
  76. balls[0].pos[0] = 120
  77. balls[0].pos[1] = 540
  78. balls[0].color = "white"
  79. balls[0].x = 800
  80. balls[0].y = 700
  81. strike(balls[0],99.0) #velocity from strike will be lowered by function call
  82.  
  83. root = Tk()
  84. root.withdraw()
  85. top = Toplevel()
  86. top.title('Tk Pool')
  87. Label(top)
  88. canvas = Canvas(top, bg='orange', width=ww, height=hh)
  89. canvas.pack()
  90. #Draws Snooker Table
  91. canvas.create_rectangle((tX-tZ, tY-tZ, tablewidth+tZ, tableheight+tZ), fill=c(0, 128, 0))
  92. canvas.create_rectangle((tX, tY, tablewidth, tableheight), fill=c(0, 255, 0))
  93. z = 0
  94. while z < 16:
  95.     #renders balls
  96.     r = balls[z].radius
  97.     x,y = int(balls[z].pos[0]), int(balls[z].pos[1])
  98.     balls[z].obj = canvas.create_oval((x-r,y-r,x+r,y+r), fill=balls[z].color)
  99.     z += 1
  100. '''
  101. Widget.bind(canvas, "<Button-1>",
  102.                    mouseDown)
  103. Widget.bind(canvas, "<Button1-ButtonRelease>",
  104.                    mouseUp)
  105. '''
  106.  
  107. run = True
  108. while run:
  109.     #checks for collision, and updates speeds
  110.     i = 0
  111.     while i < len(balls):
  112.         j = i+1
  113.         while j < len(balls):
  114.             #finds a normal vector
  115.             iii = balls[i]
  116.             jjj = balls[j]
  117.             n = vsub(iii.pos, jjj.pos)
  118.            
  119.             #finds the length between balls
  120.             length = vlength(n)
  121.             radx2 = jjj.radius+iii.radius
  122.            
  123.             if length and length <= radx2:
  124.                 #reverse straight to contact point
  125.                 cp = length/radx2
  126.                 iii.pos = [iii.pos[0]-iii.x*cp, iii.pos[1]-iii.y*cp]
  127.                 jjj.pos = [jjj.pos[0]-jjj.x*cp, jjj.pos[1]-jjj.y*cp]
  128.                
  129.                 #momentum transfer of x
  130.                 ix = iii.pos[0]
  131.                 jx = jjj.pos[0]
  132.                 x = abs(jx-ix)/radx2
  133.                 iii.x, jjj.x = jjj.x*x, iii.x*x
  134.                
  135.                 #momentum transfer of y
  136.                 iy = iii.pos[1]
  137.                 jy = jjj.pos[1]
  138.                 y = abs(jy-iy)/radx2
  139.                 iii.y, jjj.y = jjj.y*y, iii.y*y
  140.  
  141.             j+=1
  142.         i+=1
  143.  
  144.     i = 0
  145.     while i <= len(balls)-1:
  146.         #checks if balls are going out of bounds and updates speeds with v=-eu
  147.         ball = balls[i]
  148.         x,y = ball.pos
  149.         vel = abs(ball.x) + abs(ball.y)
  150.        
  151.         if vel > 0.009:
  152.             z = ball.pos[0]
  153.             if z > tablewidth-ball.radius:
  154.                 ball.x *= -1
  155.                 ball.pos[0] = tablewidth-ball.radius-2
  156.             elif z < tX+ball.radius:
  157.                 ball.x *= -1
  158.                 ball.pos[0] = tX+ball.radius+2
  159.                
  160.             z = ball.pos[1]
  161.             if z > tableheight-ball.radius:
  162.                 ball.pos[1] = tableheight-ball.radius-2
  163.                 ball.y *= -1
  164.             elif z < tY+ball.radius:
  165.                 ball.pos[1] = tY+ball.radius+2
  166.                 ball.y *= -1
  167.                
  168.             #applies friction to balls speed
  169.             ball.x *= friction
  170.             ball.y *= friction
  171.            
  172.             #updates positions of balls
  173.             ball.pos = [ball.pos[0]+ball.x, ball.pos[1]+ball.y]
  174.         else:
  175.             ball.x = ball.y = 0
  176.  
  177.         #renders balls
  178.         r = ball.radius
  179.         x,y = ball.pos
  180.         canvas.coords(ball.obj, (x-r,y-r,x+r,y+r))
  181.         i+=1
  182.     canvas.update()
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement