-
Notifications
You must be signed in to change notification settings - Fork 0
/
balltypes.py
69 lines (56 loc) · 2.82 KB
/
balltypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from graphics import *
from math import *
class Ball:
def __init__(self, params, win):
# Unpacks the dictionary input and sets all fields equal to their initial values
self.acc = params["acc"]
self.vel = params["vel"]
self.pos = params["pos"]
self.prevPos = params["prevPos"]
self.color = params["color"]
self.radius = params["radius"]
self.win = win
self.canClick = True
self.circle = Circle(Point(self.pos[0], self.pos[1]), self.radius)
self.circle.setFill(self.color)
self.circle.draw(self.win)
def move(self, x, y): # The Ball move function uses the circle move function from Graphics.py
self.circle.move(x, y)
def click(self):
if (self.canClick):
self.canClick = False # So you can only click once
v2 = self.vel[0] ** 2 + self.vel[1] ** 2
if (self.color == "yellow"):
if (v2 == 0):
self.vel[0] = 0
else:
self.vel[0] += 5*(self.vel[0]/sqrt(v2))
self.vel[1] += 5*(self.vel[1]/sqrt(v2))
if (self.color == "blue"): # Creates two new balls, one above one below, and returns them to be added to the balls array
return [Ball({"acc" : self.acc, "vel" : [self.vel[0] + 1*self.vel[1]/sqrt(v2), self.vel[1] - 1*self.vel[0]/sqrt(v2)], "pos" : self.pos, "prevPos" : self.pos, "color" : "blue", "radius": 6}, self.win),
Ball({"acc" : self.acc, "vel" : [self.vel[0] - 1*self.vel[1]/sqrt(v2), self.vel[1] + 1*self.vel[0]/sqrt(v2)], "pos" : self.pos, "prevPos" : self.pos, "color" : "blue", "radius": 6}, self.win)]
if(self.color == color_rgb(192,192,192)):
#self.vel[1] = -self.vel[1]
newBall = [Ball({"acc" : [0, self.acc[1]], "vel" : [0,0], "pos" : self.pos, "prevPos" : self.pos, "color" : color_rgb(192,192,192), "radius": 6}, self.win)]
self.vel[0] *= 0.6
self.vel[1] -= 3
return newBall
def removeBall(self):
self.circle.undraw()
class Block():
def __init__(self, params, win):
# Unpacks the dictionary input and sets all fields equal to their initial values
self.acc = params["acc"]
self.vel = params["vel"]
self.pos = params["pos"]
self.pos2 = params["pos2"]
self.prevPos = params["prevPos"]
self.color = params["color"]
self.win = win
self.rect = Rectangle(Point(self.pos[0], self.pos[1]), Point(self.pos2[0], self.pos2[1]))
self.rect.setFill(self.color)
self.rect.draw(self.win)
def move(self, x, y): # The Block move function uses the rect move function from Graphics.py
self.rect.move(x, y)
def undraw(self):
self.rect.undraw()