-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.py
80 lines (62 loc) · 2.34 KB
/
bullet.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
70
71
72
73
74
75
76
77
78
79
80
import random
import copy
import math
class Bullet(object):
# Model
def __init__(self, cx, cy, angle, speed, power):
# A bullet has a position, a size, a direction, and a speed
self.cx = cx
self.cy = cy
self.r = power
self.angle = angle
self.speed = speed
# View
def draw(self, canvas):
canvas.create_oval(self.cx - self.r, self.cy - self.r,
self.cx + self.r, self.cy + self.r,
fill="red", outline=None)
# Controller
def moveBullet(self):
# Move according to the original trajectory
self.cx += math.cos(math.radians(self.angle))*self.speed
self.cy -= math.sin(math.radians(self.angle))*self.speed
def isOffscreen(self, width, height):
# Check if the bullet has moved fully offscreen
return (self.cx + self.r <= 0 or self.cx - self.r >= width) and \
(self.cy + self.r <= 0 or self.cy - self.r >= height)
class BulletSpawner(object):
def __init__(self,x,y):
self.x = x
self.y = y
def makeBullet(self, screenWidth, screenHeight,level):
side = random.choice(["topbot","leftright"])
if side == "topbot":
x = random.randint(0,screenWidth)
y = random.choice([0,screenHeight])
elif side == "leftright":
x = random.choice([0,screenWidth])
y = random.randint(0,screenHeight)
choices = []
num = 5
for i in range(level+1):
choices.append(num)
num += 5
if choices[-1] > 50:
powerC = []
for i in choices:
if i <= 50:
powerC.append(i)
if i > 50:
choices.append(i+5)
power = random.choice(choices)
speed = random.choice(choices)
if x == 300:
x += .001
if y == 300:
y += .001
angle = math.degrees(math.atan((y *1. - screenHeight/2)/(screenWidth/2 - x)))
if x > 300:
angle = math.degrees(math.atan((x * 1. - screenWidth/2)/(y - screenHeight/2))) - 90
if y > 300:
angle += 180
return Bullet(x, y, angle, speed, power)