-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame_object.py
More file actions
311 lines (255 loc) · 10.4 KB
/
game_object.py
File metadata and controls
311 lines (255 loc) · 10.4 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import pygame
import os
import random
class GameObject(pygame.sprite.Sprite):
"""The class which provides the chared behavior for all objects shown in the game
Attributes:
image -> the image which is displayed on the screen for the object
rect -> the bounding box of the object
original_image -> the inital image of the object
"""
def __init__(self, image):
"""
Set the starting values of the attributes
"""
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.original_image = self.image
def location(self):
"""
Gives the location of the image
Returns:
(x,y) -> a tuple describing the location of the image.
"""
return(self.rect.x, self.rect.y)
def size(self):
"""
Gives the size of the image
Returns:
(#) -> a tuple describing length of one side of the object in pixels.
"""
return(self.rect.size)
def draw(self, screen):
"""
Draws the image associated with the object
Arguments:
screen -> the screen which the object will be drawn on
"""
screen.blit(self.image, self.rect)
def draw_offset(self, screen, x, y):
"""
Draws the image associated with the object, with offset (x,y)
Arguments:
screen -> the screen which the object will be drawn on
x,y -> offset values for x,y in pixels
"""
screen.blit(self.image, self.rect.move(x,y))
def set_position(self, x, y):
"""
A method which sets the position of the object on the screen
Arguments:
x -> the x cordinate describing the location of the image.
y -> the y cordinate describing the location of the image.
"""
self.rect.x = x
self.rect.y = y
def set_transparency(self, value):
"""
A method which sets the transparency of the object on the screen
Arguments:
value -> value of transparency, 0 is transparent 255 is fully colored
"""
self.image = self.original_image.copy()
alpha = value
self.image.fill((255,255,255, alpha),None, pygame.BLEND_RGBA_MULT)
def scale_image(self, new_size):
"""
A method which scales object to a new size
Arguments:
new_size -> new size of the image in pixels
"""
self.image = pygame.transform.scale(self.image, (new_size, new_size))
self.rect.size = (new_size, new_size)
def collision_group(self, group):
"""
A method which checkes collision of the object with specific group
Arguments:
group -> Pygame group.
"""
collision_list = pygame.sprite.spritecollideany(self, group)
return collision_list
def show(self):
"""
Show the object by setting transparency to 255
"""
self.image.set_alpha(255)
def hide(self):
"""
Hide the object by setting transparency to 0
"""
self.image.set_alpha(0)
class Wall(GameObject):
"""A class which describes the walls forming the borders of the labrynth"""
def __init__(self, x, y):
"""
Set the starting values of the attributes
"""
super().__init__(os.getcwd()+"/image/wall.png")
super().set_position(x,y)
self.set_transparency(1)
def scale_image(self, new_size):
"""
Scale the image to a desired size
Arguments:
new_size -> the length in pixels of one side of the scaled image
"""
self.image = pygame.transform.scale(self.image, (new_size, new_size))
self.original_image = pygame.transform.scale(self.original_image, (new_size, new_size))
self.rect.size = (new_size, new_size)
class Player(GameObject):
"""
A class which stores the player data
Attributes:
speed -> the walking speed of the Player
inventory -> a dictionary of boolians storing what items the player has picked up
"""
def __init__(self, x, y):
"""Set the starting values of the attributes"""
super().__init__(os.getcwd()+"/image/player1.png")
super().set_position(x,y)
self.animation_right = []
self.load_images()
self.animation_left = self.rotate_animation_image(180)
self.animation_up = self.rotate_animation_image(270)
self.animation_down = self.rotate_animation_image(90)
self.speed = 1
self.inventory = {}
def add_to_iventory(self,item):
"""
A method which adds an item into the player's inventory
Arguments:
item -> the name of the item picked up
"""
self.inventory[item] = True
def move_player(self, direction):
pass
def rotate_animation_image(self,degree):
"""
Rotates list of images so that player can have different animation for different direction
Arguments:
degree -> angle to rotate the image
"""
images = []
for image in self.animation_right:
images.append(pygame.transform.rotate(image, degree))
return images
def load_images(self):
"""
Loads 5 different images for animation
"""
for i in range(5):
image = pygame.image.load(os.getcwd()+"/image/player"+str(i+1)+".png")
self.animation_right.append(image)
def scale_image(self, new_size):
"""
Scale the image to a desired size
Scales all possible images included in the Player object
Arguments:
new_size -> the length in pixels of one side of the scaled image
"""
self.image = pygame.transform.scale(self.image, (new_size, new_size))
self.rect.size = (new_size, new_size)
for i in range(5):
self.animation_down[i] = pygame.transform.scale(self.animation_down[i], (new_size, new_size))
self.animation_up[i] = pygame.transform.scale(self.animation_up[i], (new_size, new_size))
self.animation_right[i] = pygame.transform.scale(self.animation_right[i], (new_size, new_size))
self.animation_left[i] = pygame.transform.scale(self.animation_left[i], (new_size, new_size))
def set_image(self, direction, step):
"""
Set image of the player according to current situation
Arguments:
direction -> the current direction player is heading
step -> current animation step of the player, maximum value is 8
"""
if step > 4:
step = 8-step
if direction == "north":
self.image = self.animation_up[step]
if direction == "south":
self.image = self.animation_down[step]
if direction == "east":
self.image = self.animation_right[step]
if direction == "west":
self.image = self.animation_left[step]
class Ping(GameObject):
"""
A class which describes the ping object used for querying distance
Ping is 1 x 1 pixel object that exist for short period of time
Attributes:
image -> the pygame surface which stores the position of the ping
rect -> the bounding box of the ping"""
def __init__(self, player):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([1, 1])
self.rect = self.image.get_rect()
self.rect.x = player.rect.center[0]
self.rect.y = player.rect.center[1]
class NPC(GameObject):
"""A class which describes NPCs in the world
Attributes:
directory -> the file folder which stores the NPC data
dialogue -> a list of the dialogue strings of the NPC
key -> a dictionary storing the checkpoint items of the player to advance dialogue
index_position -> a tracker to track what point the character is through their dialogue
map -> GameMap object with all required objects to play the game
view -> View object with required objects to display visuals and play audio """
def __init__(self,x,y, directory):
"""Set the starting values of the attributes"""
super().__init__(os.getcwd()+"/documents/NPCs/" + directory + "/image.png")
super().set_position(x,y)
self.directory = directory
self.dialogue = self.load_file("dialogue")
self.key = self.load_file("keys")
self.index_position = 0
self.set_transparency(1)
def load_file(self,attribute):
"""Pull an atribute from the designated file"""
file = open(os.getcwd()+"/documents/NPCs/" + self.directory + "/" + attribute + ".txt")
return eval(file.read())
def scale_image(self, new_size):
"""
Scale the image to a desired size
Arguments:
new_size -> the length in pixels of one side of the scaled image
"""
self.image = pygame.transform.scale(self.image, (new_size, new_size))
self.original_image = pygame.transform.scale(self.original_image, (new_size, new_size))
self.rect.size = (new_size, new_size)
def update_dialogue(self,audio_engine, view):
"""A method which handels loading the dialogue lines of the NPC"""
#if there is a checkpoint needed for the next line of dialogue
if self.index_position in self.key.keys():
#if the player has achieved the checkpoint
if player.inventory.contains(self.key[self.index_position]):
#print the next position
self.index_position += 1
view.say_and_display(self.dialogue[self.index_position],audio_engine)
#say_line(self.dialogue[self.index_position])
#if the player has not achieved the checkpoint
else:
#repreate the previous line
view.say_and_display(self.dialogue[self.index_position],audio_engine)
#if it is the first or last dialogue line
elif self.index_position == len(self.dialogue)-1:
#say the first line of dialogue
view.say_and_display(self.dialogue[self.index_position],audio_engine)
elif self.index_position == 0:
view.say_and_display(self.dialogue[self.index_position],audio_engine)
self.index_position +=1
#if there is a normal dialogue line
else:
#say the next line of dialogue
view.say_and_display(self.dialogue[self.index_position],audio_engine)
self.index_position += 1