-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjects.py
More file actions
279 lines (257 loc) · 9.53 KB
/
objects.py
File metadata and controls
279 lines (257 loc) · 9.53 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
import pygame
pygame.init()
# initialize colours
black = (0,0,0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
lblue = (110, 220, 255)
grey = (225, 225, 225)
yellow = (255, 255, 0)
# initialize window zize
windowWidth = 0
windowHeight = 0
floor = 0
# set window width and height
def init():
from temp import width,height,floor
return width,height,floor
windowWidth, windowHeight, floor = init()
# parent class for all graphical objects
class objects:
# what type of object it is
objectType = ""
# its name
name = ""
# its position
x = 0
y = 0
# where it can be clicked on
clickable = False
# whether it is visible
toRender = True
# set the values of all attributes
def init(self, objectType, name, x, y, clickable, toRender):
self.objectType = objectType
self.name = name
self.x = x
self.y = y
self.clickable = clickable
self.toRender = toRender
# skeleton init for making new objects
# def __init__(self,objectType,name,x,y,clickable,toRender):
# self.init(objectType,name,x,y,clickable,toRender)
# rectangles
class rectangle(objects):
# dimensions
width = 0
height = 0
# the colour
colour = ""
# configure attributes
def __init__(self, name, x, y, clickable, toRender, width, height, colour):
self.init("rectangle", name, x, y, clickable, toRender)
self.width = width
self.height = height
self.colour = colour
# entity stamina bars
class staminaBar(objects):
# width based on the stamina the entity still has
bwidth = 0
# width based on the stamina the entity has lost
gwidth = 0
# the height of the bar
height = 0
# the objects to associate with
entity = ""
object = ""
# configure attributes
def __init__(self, name, entity, object):
self.init("staminaBar", name, 0, 0, False, True)
self.entity = entity
self.object = object
# if the bar is the player's, make it thicker
if self.object.name == "player":
self.height = round(windowHeight/40)
# Move the bar to the top left of the screen
self.x = round(windowWidth / 50)
self.y = self.x + round(windowWidth / 30)
else:
self.height = round(windowHeight/60)
self.update()
# update position and widths
def update(self):
# Update position and bar width
# The width for the player's bar is much bigger than that of enemy bars.
if self.object.name == "player":
# set the player's healthBar's width based on window size
entityWidth = round(windowWidth * 2 / 5)
else:
# move the bar to just above the entity's position
self.x = self.object.x
self.y = self.object.y - round(windowHeight / 24) + round(windowHeight / 50)
# get the width of the entity's default frame
entityWidth = pygame.image.load("graphics/" + self.object.folder + "/stand/0" + ".PNG").get_width()
# set the width of the light blue section as a percentage of the max entity stamina and width of the entity
self.bwidth = self.entity.stamina / self.entity.maxStamina * entityWidth
# set width of the grey section
self.gwidth = entityWidth - self.bwidth
# entity health bars
class healthBar(objects):
# width based on the health the entity still has
gwidth = 0
# width based on the health the entity has lost
rwidth = 0
# the height of the bar
height = 0
# the objects to associate with
entity = ""
object = ""
# the max width of the bar
entityWidth = 0
# configure attributes
def __init__(self, name, entity, object):
self.init("healthBar", name, 0, 0, False, True)
self.entity = entity
self.object = object
# if the bar is the player's, make it thicker
if self.object.name == "player":
self.height = round(windowHeight / 30)
# Move the bar to the top left of the screen
self.x = round(windowWidth / 50)
self.y = self.x
# set the player's healthBar's width based on window size
self.entityWidth = round(windowWidth / 2)
else:
# get the width of the entity's default frame
self.entityWidth = pygame.image.load("graphics/" + self.object.folder + "/stand/0" + ".PNG").get_width()
self.height = round(windowHeight / 50)
self.update()
# update position and widths
def update(self):
# Update position and bar widths
if self.object.name != "player":
# move the bar to just above the entity's position
self.x = self.object.x
self.y = self.object.y - round(windowHeight / 24)
# set the width of the green section as a percentage of the max entity health and width of the entity
self.gwidth = self.entity.health / self.entity.maxHealth * self.entityWidth
# set width of the red section
self.rwidth = self.entityWidth - self.gwidth
# Text objects
class text(objects):
# text to write
text = ""
# colour of the text
colour = ""
# whether to antialias the text
antialiasing = True
# the text size
size = 0
# configure attributes
def __init__(self, name, x, y, clickable, toRender, text, colour, antialiasing, size):
self.init("text", name, x, y, clickable, toRender)
self.text = text
self.colour = colour
self.size = size
self.antialiasing = antialiasing
# centre text around coordinates
def centreText(self, centre):
pygame.font.init()
font = pygame.font.SysFont("font", self.size)
textSize = font.size(self.text)
self.x = centre[0] - round(textSize[0] / 2)
self.y = centre[1] - round(textSize[1] / 2)
class killCounter(objects):
entity = None
def __init__(self,name,entity):
self.init("killCounter",name,0,0,False,True)
self.entity = entity
# images
class image(objects):
# the path to the image
file = ""
# configure attributes
def __init__(self, name, x, y, clickable, toRender, file):
self.init("image", name, x, y, clickable, toRender)
self.file = file
#animations. A folder of images to change every frame
class animation(objects):
# the path to the folder name containing the images
folder = ""
# the current image ID
current = 0
# whether the animation is running
state = 1
# the total number of images in the folder
totalStates = 0
# configure attributes
def __init__(self, name, x, y, clickable, toRender, folder, state, totalStates):
self.init("animation", name, x, y, clickable, toRender)
self.folder = folder
self.state = state
self.totalStates = totalStates
# entities. An object that has multiple animations it can switch between
class entity(objects):
# list of states and how many images each state has
states = [["stand", 3], ["walk", 5]]
# the entities folder. contains all of it's graphics
folder = ""
# Player specific - should the player walk after landing a jump?
jumpWalk = [False, "r"]
# current image to render
current = 0
# total number of states in the folder
totalStates = 3
# current state folder to load from
state = "stand"
# previous active state
previous = []
# current entity orientation
face = "r"
# The distance left to knock back after an attack on the entity
knockbackDistance = 0
# The starting knockback distance
knockbackDistanceMax = 0
# The direction to knock back
knockbackFace = "l"
# configure attributes
def __init__(self, name, x, y, clickable, toRender, states, state, folder, face):
self.init("entity", name, x, y, clickable, toRender)
self.states = states
self.state = state
self.folder = folder
self.face = face
self.updateVars()
# change state
def changeState(self, new):
# get the height of the old frame
oldHeight = pygame.image.load(
"graphics/" + self.folder + "/" + self.state + "/" + str(self.current) + ".PNG").get_height()
# get the height of the new frame
newHeight = pygame.image.load("graphics/" + self.folder + "/" + new + "/0.PNG").get_height()
# if the new feet are different to the old, move to compensate
if newHeight < oldHeight:
self.y += oldHeight - newHeight
elif newHeight > oldHeight:
self.y -= newHeight - oldHeight
# add the old state to the previous states array
self.previous.append(self.state)
# if the array holds more than 2 states
if len(self.previous) > 2:
# remove the oldest one
self.previous.pop(0)
# change state
self.state = new
# update attributes
self.updateVars()
# update attributes after a state change etc
def updateVars(self):
# reset the current image
self.current = 0
# find the current state in the list of states
for i in range(len(self.states)):
if self.states[i][0] == self.state:
# get the total number of images
self.totalStates = self.states[i][1]