forked from mattboan/Galtron
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbullet.py
108 lines (86 loc) · 3.62 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import math
import pygame as pg
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired from the ship"""
def __init__(self, setting, screen, ship, traj, damage, charge=0):
"""Create a bullet object at the ships current position"""
super(Bullet, self).__init__()
self.screen = screen
# decide the trajectory of bullets
self.traj = traj
# To specify the bullet is ultimate
self.isUltimate = False
# load the bullet image and set its rect attribute
self.image = pg.image.load('gfx/pBullet.png')
if (self.traj == 1):
self.image = pg.transform.rotate(self.image, 315)
elif (self.traj == 2):
self.image = pg.transform.rotate(self.image, 45)
self.rect = self.image.get_rect()
if (charge != 0):
bulletSize = (self.rect.width * (charge + 1), self.rect.height * (charge + 1))
self.image = pg.transform.scale(self.image, bulletSize)
self.rect = self.image.get_rect()
# Create a bullet rect at (0,0)
##self.rect = pg.Rect(0, 0, setting.bulletWidth, setting.bulletHeight)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
# store the bullets position as a decimal value
self.x = float(self.rect.centerx)
self.y = float(self.rect.y)
self.setting = setting
# damage of basic bullet (default : 1)
self.damage = damage
def update(self):
"""Move the bullet -y up the screen"""
# update the decimal position of the bullet
bulletSpeed = self.setting.bulletSpeed
if (self.traj == 0):
self.y -= 1.5 * bulletSpeed
elif (self.traj == 1):
self.x += 0.5 * bulletSpeed
self.y -= 0.5 * 2.0 * bulletSpeed
elif (self.traj == 2):
self.x -= 0.5 * bulletSpeed
self.y -= 0.5 * 2.0 * bulletSpeed
else:
self.x -= 0.8 * math.sin(0.05 * self.y)
self.y -= 0.8 * bulletSpeed
# Update the rect position899
self.rect.centerx = self.x
self.rect.y = self.y
def drawBullet(self):
"""Draw the bullet to the screen"""
# pg.draw.rect(self.screen, self.color, self.rect)
self.screen.blit(self.image, self.rect)
class SpecialBullet(Sprite):
"""A class to manage special bullet which can be fired only by the ultimate"""
def __init__(self, setting, screen, pos, damage):
"""Create the bullet object at the some position"""
super(SpecialBullet, self).__init__()
self.screen = screen
# To specify the bullet is ultimate
self.isUltimate = True
# load the bullet image and set its rect attribute
self.image = pg.image.load('gfx/sBullet.png')
self.rect = self.image.get_rect()
# Create a bullet rect at (0,0)
##self.rect = pg.Rect(0, 0, setting.bulletWidth, setting.bulletHeight)
self.rect.centerx = pos[0]
self.rect.top = pos[1]
# store the bullets position as a decimal value
self.y = float(self.rect.y)
self.setting = setting
# damage of SpecialBullet
self.damage = damage * 10
def update(self):
"""Move the bullet -y up the screen"""
# update the decimal position of the bullet
bulletSpeed = self.setting.bulletSpeed
self.y -= bulletSpeed
self.rect.y = self.y
def drawBullet(self):
"""Draw the bullet to the screen"""
# pg.draw.rect(self.screen, self.color, self.rect)
self.screen.blit(self.image, self.rect)