-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.py
executable file
·81 lines (68 loc) · 2.63 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
import time
import random
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
bullet_charged = False
bullet_fired = False
bullet_power = 1
bullet_charge_start = None
def __init__(self, screen, owner):
super(Bullet, self).__init__()
self.screen = screen
self.owner = owner
self.bullet_speed = [4, 0]
self.reset()
def handle_event(self, event):
if pygame.KEYDOWN == event.type:
if pygame.K_SPACE == event.key:
self.charge()
elif pygame.KEYUP == event.type:
if pygame.K_SPACE == event.key:
self.fire()
def reset(self):
self.rect = pygame.Rect(0, 0, 5, 5)
self.bullet_charged = False
self.bullet_fired = False
self.bullet_power = 1
self.bullet_charge_start = None
def charge(self):
if not self.bullet_charged and not self.bullet_fired:
self.bullet_charged = True
self.bullet_charge_start = time.time()
def fire(self):
if self.bullet_charged:
self.bullet_fired = True
self.bullet_charged = False
def power_reduce(self, hit):
self.bullet_power -= hit * 2
if self.bullet_power <= 0:
self.reset()
def update(self):
if self.bullet_charged:
power_charged = int((time.time() - self.bullet_charge_start) * 10)
if power_charged > self.bullet_power and self.bullet_power <= 20:
if power_charged > 20:
power_charged = 20
self.bullet_power = power_charged
if self.bullet_fired:
self.rect = self.rect.move(self.bullet_speed)
if self.bullet_speed[0] < 0 and self.rect.left < 0:
self.reset()
if self.bullet_speed[0] > 0 and self.rect.right > self.screen.get_rect().width:
self.reset()
def blit_me(self):
colour = (250, 250, 250)
if self.bullet_power >= 20:
colour = (random.randint(0, 250), random.randint(0, 250), random.randint(0, 250))
if self.bullet_charged:
if self.owner.sprite_direct == -1:
x = self.owner.rect.left - 5
self.bullet_speed[0] = -4
if self.owner.sprite_direct == 1:
x = self.owner.rect.right + 5
self.bullet_speed[0] = 4
y = self.owner.rect.top + 20
self.rect = pygame.draw.circle(self.screen, colour, (x, y), self.bullet_power)
if self.bullet_fired:
self.rect = pygame.draw.circle(self.screen, colour, self.rect.center, self.bullet_power)