-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.py
46 lines (40 loc) · 1.6 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
import pygame
from sys import exit
import math
from settings import *
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, angle, enemy_group, player):
super().__init__()
self.image = pygame.image.load("assets/1.png").convert_alpha()
self.image = pygame.transform.rotozoom(self.image, 0, BULLET_SCALE)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.x = x
self.y = y
self.angle = angle
self.speed = BULLET_SPEED
self.x_vel = math.cos(self.angle * (2*math.pi/360)) * self.speed
self.y_vel = math.sin(self.angle * (2*math.pi/360)) * self.speed
self.bullet_lifetime = BULLET_LIFETIME
self.spawn_time = pygame.time.get_ticks() # gets the specific time that the bullet was created
self.enemy_group = enemy_group # Group of enemies
self.kills = 0
self.player = player
def bullet_movement(self):
self.x += self.x_vel
self.y += self.y_vel
self.rect.x = int(self.x)
self.rect.y = int(self.y)
if pygame.time.get_ticks() - self.spawn_time > self.bullet_lifetime:
self.kill()
def update(self):
self.bullet_movement()
hit_enemies = pygame.sprite.spritecollide(self, self.enemy_group, False)
if hit_enemies:
for enemy in hit_enemies:
if enemy.alive:
enemy.alive = False
enemy.score -= DEATH_PENALTY
self.kills += 1
self.player.kills += 1
self.player.score += KILL_REWARD