-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weapon.py
123 lines (99 loc) · 4.7 KB
/
Weapon.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
import math
import Constants as cs
import random
from pygame.sprite import Group
class Weapons():
def __init__(self,image ,teer_image):
self.original_image = image
self.angle = 0
self.image = pygame.transform.rotate(self.original_image,self.angle)
self.teer_image = teer_image
self.rect = self.image.get_rect()
self.fired = False
self.last_shot = pygame.time.get_ticks()
def update(self,player):
shot_cooldown = 300
teer = None
self.rect.center = player.rect.center
pos = pygame.mouse.get_pos()
x_dist = pos[0] - self.rect.centerx
y_dist = -(pos[1] - self.rect.centery) # -ve denn pygame y cordinates increase downstream
self.angle = math.degrees(math.atan2(y_dist,x_dist))
# Get mouse click
if pygame.mouse.get_pressed()[0] and self.fired == False and (pygame.time.get_ticks() - self.last_shot) >= shot_cooldown:
teer = Teer(self.teer_image, self.rect.centerx,self.rect.centery,self.angle)
self.fired = True
self.last_shot = pygame.time.get_ticks()
# reset mouse click
if pygame.mouse.get_pressed()[0] == False:
self.fired = False
return teer
def draw(self,surface):
self.image = pygame.transform.rotate(self.original_image,self.angle)
surface.blit(self.image,((self.rect.centerx - int(self.image.get_width()/2)),self.rect.centery - int(self.image.get_height()/2)))
class Teer(pygame.sprite.Sprite):
def __init__(self,image,x,y,angle):
pygame.sprite.Sprite.__init__(self)
self.original_image = image
self.angle = angle
self.image = pygame.transform.rotate(self.original_image, self.angle - 90)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# Calculate the vertical and horizontal speed on the angle
self.dx = math.cos(math.radians(self.angle)) * cs.Teer_speed
self.dy = -(math.sin(math.radians(self.angle)) * cs.Teer_speed)
def update(self,screen_scroll, obstacle_tile, enemy_list):
# reset variable
damage = 0
damage_pos = None
# reposition based on speed
self.rect.x += screen_scroll[0] + self.dx
self.rect.y += screen_scroll[1] + self.dy
# Check for collision b/w teer and tile walls
for obst in obstacle_tile:
if obst[1].colliderect(self.rect):
self.kill()
# check isf arrow gone out of the screen
if self.rect.right < 0 or self.rect.left > cs.Screen_Width or self.rect.bottom < 0 or self.rect.top > cs.Screen_Hight:
self.kill()
# check collision between teer and enemy
for enemy in enemy_list:
if enemy.rect.colliderect(self.rect) and enemy.alive:
damage = 10 + random.randint(-5,5)
damage_pos = enemy.rect
enemy.health -= damage
enemy.hit = True
self.kill()
break
return damage , damage_pos
def draw(self,surface):
surface.blit(self.image,((self.rect.centerx - int(self.image.get_width()/2)),self.rect.centery - int(self.image.get_height()/2)))
class Fireball(pygame.sprite.Sprite):
def __init__(self, image, x, y, target_x, target_y):
pygame.sprite.Sprite.__init__(self)
self.original_image = image
x_dist = target_x - x
y_dist = -(target_y - y)
self.angle = math.degrees(math.atan2(y_dist, x_dist))
self.image = pygame.transform.rotate(self.original_image, self.angle - 90)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# Calculate the vertical and horizontal speed on the angle
self.dx = math.cos(math.radians(self.angle)) * cs.Fireball_Speed
self.dy = -(math.sin(math.radians(self.angle)) * cs.Fireball_Speed)
def update(self,screen_scroll, player):
# reposition based on speed
self.rect.x += screen_scroll[0] + self.dx
self.rect.y += screen_scroll[1] + self.dy
# check isf fireball gone out of the screen
if self.rect.right < 0 or self.rect.left > cs.Screen_Width or self.rect.bottom < 0 or self.rect.top > cs.Screen_Hight:
self.kill()
# check collision between fireball and player
if player.rect.colliderect(self.rect) and player.hit == False:
player.hit = True
player.last_hit = pygame.time.get_ticks()
player.health -= 10
self.kill()
def draw(self,surface):
surface.blit(self.image,((self.rect.centerx - int(self.image.get_width()/2)),self.rect.centery - int(self.image.get_height()/2)))