-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFireball.py
116 lines (102 loc) · 5.1 KB
/
Fireball.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
__author__ = 'Erilyth'
import pygame
import random
import math
from OnBoard import OnBoard
'''
This class defines all our fireballs.
A fireball inherits from the OnBoard class since we will use it as an inanimate object on our board.
Each fireball can check for collisions in order to decide when to turn and when they hit a player.
'''
class Fireball(OnBoard):
def __init__(self, raw_image, position, index,speed):
super(Fireball,self).__init__(raw_image, position)
#Set the fireball direction randomly
self.__direction = int(math.floor(random.random() * 100)) % 2
self.index = index
self.wallsBelow = []
self.laddersBelow = []
#The newly spawned fireball is not falling
self.__fall = 0
#The speed of a fireball is set
self.__speed = speed
#Update the image of a fireball
def updateImage(self, raw_image):
self.image = raw_image
self.image = pygame.transform.scale(self.image, (15, 15))
#Getters and Setters for some private variables
def getSpeed(self):
return self.__speed
def setSpeed(self,speed):
self.__speed = speed
def getFall(self):
return self.__fall
def getDirection(self):
return self.__direction
#Moves the fireball in the required direction
def continuousUpdate(self, wallGroup, ladderGroup):
#The fireball is falling
if self.__fall == 1:
#We move the fireball downwards with speed of self.__speed
self.update(self.image, "V", self.__speed)
if self.checkCollision(wallGroup, "V"):
#We have collided with a wall below, so the fireball can stop falling
self.__fall = 0
#Set the direction randomly
self.__direction = int(math.floor(random.random() * 100)) % 2
else:
#While we are on the ladder, we use a probability of 4/20 to make the fireball start falling
if self.checkCollision(ladderGroup, "V") and len(self.checkCollision(wallGroup, "V")) == 0:
randVal = int(math.floor(random.random() * 100)) % 20
if randVal < 15:
self.__fall = 0
else:
self.__fall = 1
#We are at the edge of the floor so the fireball starts falling
if len(self.checkCollision(ladderGroup, "V")) == 0 and len(self.checkCollision(wallGroup, "V")) == 0:
self.__fall = 1
#We are moving right, so update the fireball image to the right
if self.__direction == 0:
self.update(pygame.image.load('Assets/fireballright.png'), "H", self.__speed)
#When we hit a wall, we change direction
if self.checkCollision(wallGroup, "H"):
self.__direction = 1
self.update(self.image, "H", -self.__speed)
#We are moving left, so update the fireball image to the left
else:
self.update(pygame.image.load('Assets/fireballleft.png'), "H", -self.__speed)
#When we hit a wall, we change direction
if self.checkCollision(wallGroup, "H"):
self.__direction = 0
self.update(self.image, "H", self.__speed)
#Move the fireball in the required direction with the required value and also set the image of the fireball
def update(self, raw_image, direction, value):
if direction == "H":
self.setPosition((self.getPosition()[0] + value, self.getPosition()[1]))
self.image = raw_image
self.image = pygame.transform.scale(self.image, (15, 15))
if direction == "V":
self.setPosition((self.getPosition()[0], self.getPosition()[1] + value))
self.rect.center = self.getPosition()
'''
We check for collisions in the direction in which we are moving if the parameter direction is "H".
The way we do this is move a little forward in the direction in which we are moving, then check for collisions then move back to the original location
We check for collisions below the fireball if the parameter direction is "V"
We do this by moving down a little, then check for collisions then move back up to the original location
'''
def checkCollision(self, colliderGroup, direction):
if direction == "H":
if self.__direction == 0:
self.update(self.image, "H", self.__speed) # Right collision
if self.__direction == 1:
self.update(self.image, "H", -self.__speed) # Left collision
Colliders = pygame.sprite.spritecollide(self, colliderGroup, False)
if self.__direction == 0:
self.update(self.image, "H", -self.__speed) # Right collision
if self.__direction == 1:
self.update(self.image, "H", self.__speed) # Left collision
else:
self.update(self.image, "V", self.__speed) # Bottom collision
Colliders = pygame.sprite.spritecollide(self, colliderGroup, False)
self.update(self.image, "V", -self.__speed)
return Colliders