-
Notifications
You must be signed in to change notification settings - Fork 0
/
weapon.py
133 lines (111 loc) · 5.3 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
124
125
126
127
128
129
130
131
132
133
import math
import pygame
import constants as const
import random
class Weapon():
def __init__(self, image, arrow_img):
self.original_img = image
self.angle = 0
self.image = pygame.transform.rotate(self.original_img, self.angle)
self.arrow_img = arrow_img
self.rect = self.image.get_rect()
self.fired = False
self.last_shot = pygame.time.get_ticks()
self.count = 0
def update(self, player):
shot_cooldown = 300
arrow = None
self.rect.center = player.rect.center
pos = pygame.mouse.get_pos()
# pos[0]- x coordinate of mouse, pos[1] - y coordinate
x_dist = pos[0]-self.rect.centerx
# -ve cause pygame y coordinate increases down the screen
y_dist = -(pos[1]-self.rect.centery)
self.angle = math.degrees(math.atan2(y_dist, x_dist))
# get mouseclick
# 0: left key has been pressed
if pygame.mouse.get_pressed()[0]:
self.count += 1
if self.count > 5:
if pygame.mouse.get_pressed()[0] and self.fired == False and (pygame.time.get_ticks()-self.last_shot >= shot_cooldown):
arrow = Arrow(self.arrow_img, self.rect.centerx,
self.rect.centery, self.angle)
self.last_shot = pygame.time.get_ticks()
self.fired = True
# reset mouseclick
if pygame.mouse.get_pressed()[0] == False:
self.fired = False
return arrow
def draw(self, surface):
self.image = pygame.transform.rotate(self.original_img, 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 Arrow(pygame.sprite.Sprite):
def __init__(self, image, x, y, angle):
pygame.sprite.Sprite.__init__(self)
self.original_img = image
self.angle = angle
self.image = pygame.transform.rotate(self.original_img, self.angle-90)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# Calculate the horizontal and vertical speeds based on the angle
self.dx = math.cos(math.radians(self.angle)) * const.ARROW_SPEED
self.dy = -(math.sin(math.radians(self.angle)) * const.ARROW_SPEED)
# -ve because pygame y coordinate increases down the screen
def update(self, screen_scroll, obstacle_tiles, enemy_list):
# reset variables
damage = 0
damage_pos = None
# reposition arrow based on speed and screen scroll
self.rect.x += screen_scroll[0]+self.dx
self.rect.y += screen_scroll[1]+self.dy
# check for collision between arrow and tile walls
for obstacle in obstacle_tiles:
if obstacle[1].colliderect(self.rect):
self.kill()
# check if arrow has gone off screen
if self.rect.right < 0 or self.rect.left > const.SCREEN_WIDTH or self.rect.bottom < 0 or self.rect.top > const.SCREEN_HEIGHT:
self.kill()
# check collision between arrow and enemies
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_img = 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_img, self.angle-90)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# Calculate the horizontal and vertical speeds based on the angle
self.dx = math.cos(math.radians(self.angle)) * const.FIREBALL_SPEED
self.dy = -(math.sin(math.radians(self.angle)) * const.FIREBALL_SPEED)
# -ve because pygame y coordinate increases down the screen
def update(self, screen_scroll, player):
# reposition arrow based on speed and screen scroll
self.rect.x += screen_scroll[0]+self.dx
self.rect.y += screen_scroll[1]+self.dy
# check if fireball has gone off screen
if self.rect.right < 0 or self.rect.left > const.SCREEN_WIDTH or self.rect.bottom < 0 or self.rect.top > const.SCREEN_HEIGHT:
self.kill()
# check collision between player and fireball
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)))