-
Notifications
You must be signed in to change notification settings - Fork 0
/
mooga.py
288 lines (253 loc) · 10.7 KB
/
mooga.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
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
""" Mooga Library provides a simplified interface for 2D game development in Python. """
import pygame
import time
import os
class Color:
""" A library of colors in (R, G, B) format from imgonline. """
black = 0, 0, 0
grey = 128, 128, 128
white_smoke = 245, 245, 245
white = 255, 255, 255
red = 255, 0, 0
salmon = 250, 128, 114
tomato = 255, 99, 71
orange = 255, 165, 0
yellow_green = 154, 205, 50
lime = 0, 255, 0
dark_turquoise = 0, 206, 209
deep_sky_blue = 0, 191, 255
blue = 0, 0, 255
dark_orchid = 153, 50, 204
crimson = 220, 20, 60
burlywood = 222, 184, 135
khaki = 240, 230, 140
medium_sea_green = 60, 179, 113
class Scene:
""" A container class for Character and View management. """
def __init__(self, backgroundColor=(0,0,0)):
self.backgroundColor = backgroundColor
self.currentView = 'default'
self.views = {}
self.views['default'] = {'x':0,'y':0,'width':800,'height':600}
self.characters = []
self.lastUpdateTime = 0
def addView(self, view_name, x, y, width, height):
""" Add a new view to the scene by specifying its name, coordinates and size. """
self.views[view_name] = {'x': x, 'y':y, 'width': width, 'height':height}
def resizeView(self, view_name, width, height):
self.views[view_name] = {'x': self.views[view_name]['x'], 'y': self.views[view_name]['y'], 'width': width, 'height':height}
def moveView(self, view_name, newX, newY):
self.views[view_name] = {'x': newX, 'y': newY, 'width': self.views[view_name]['width'], 'height':self.views[view_name]['height']}
def autoScrollView(self, x, y, milliseconds):
""" Move the current view by x and y every interval of specified milliseconds. """
timed = time.time()*1000
if ( (timed- self.lastUpdateTime)> milliseconds):
self.moveView(self.currentView, self.views[self.currentView]['x']+x, self.views[self.currentView]['y']+y)
self.lastUpdateTime = timed
def viewFollowCharacter(self, character, padding_x, padding_y, smoothing=False, speed=0, milliseconds=0):
target = {'x':character.x-padding_x, 'y':character.y-padding_y}
this_x, this_y = self.views[self.currentView]['x'], self.views[self.currentView]['y']
timed = time.time()*1000
if ( (timed- self.lastUpdateTime)> milliseconds):
if smoothing:
if (this_x > target['x']):
if (this_x - target['x']) < speed:
speed = this_x - target['x']
self.moveView(self.currentView, this_x - speed, this_y)
if (this_x < target['x']):
if (target['x'] - this_x) < speed:
speed = target['x'] - this_x
self.moveView(self.currentView, this_x + speed, this_y)
if (this_y > target['y']):
if (this_y - target['y']) < speed:
speed = this_y - target['y']
self.moveView(self.currentView, this_x, this_y - speed)
if (this_y < target['y']):
if (target['y'] - this_y) < speed:
speed = target['y'] - this_y
self.moveView(self.currentView, this_x, this_y + speed)
else:
self.moveView(self.currentView, character.x-padding_x, character.y-padding_y)
self.lastUpdateTime = timed
def addCharacters(self, new_characters):
""" Add Character objects to the Scene. """
if isinstance(new_characters, list):
for character in new_characters:
self.characters.append(character)
else:
self.characters.append(new_characters)
def setView(self, view):
""" Set the current view of the Scene. """
self.currentView = view
def getView(self):
""" Return the current view of the Scene as a dict. """
return self.views[self.currentView]
def setBackgroundColor(self, color):
""" Set the scene background color as (R, G, B). """
self.backgroundColor = color
def updateScene(self, screen):
""" Draw the objects within the current Scene view to a Pygame Display. """
screen.fill(self.backgroundColor)
for character in self.characters:
bottom = character.y + character.height
right = character.x + character.width
if (bottom < self.views[self.currentView]['y']):
continue
if (character.y > (self.views[self.currentView]['y']+self.views[self.currentView]['height'])):
continue
if (right < self.views[self.currentView]['x']):
continue
if (character.x > (self.views[self.currentView]['x']+self.views[self.currentView]['width'])):
continue
screen.blit(character.getImage(), character.getRect().move(-self.views[self.currentView]['x'],-self.views[self.currentView]['y']))
class Animation:
""" A container class for Pygame Image management. """
def __init__(self, images=None):
self.index = 0
self.frames = []
if (images!=None):
if isinstance(images, list):
for image in images:
self.frames.append(pygame.image.load(image))
else:
if images[-1]=='/':
image_dir = os.listdir(images)
for image in image_dir:
self.frames.append(pygame.image.load(images+image))
else:
self.frames.append(pygame.image.load(images))
def addFrames(self, images):
""" Add frames to the animation. """
if isinstance(images, list):
for image in images:
self.frames.append(pygame.image.load(image))
else:
self.frames.append(pygame.image.load(images))
def takeFrame(self):
""" Return the next frame in the animation sequence. """
index = self.index
if (self.index<len(self.frames)-1):
self.index += 1
else:
self.index = 0
return self.frames[index]
def firstFrame(self):
""" Return the first frame of the animation sequence. """
return self.frames[0]
class Character:
""" A 2D sprite class for handling motion and collisions. """
def __init__(self, animation):
self.animation = animation
self.current_frame = self.animation.firstFrame()
self.rect = self.animation.firstFrame().get_rect()
self.lastUpdateTime = 0
self.lastUpdateAnimTime = 0
self.colliders = []
self.x = self.rect.left
self.y = self.rect.top
self.height = self.rect.height
self.width = self.rect.width
self.xvel = 0
self.yvel = 0
self.xacc = 0
self.yacc = 0
def setSize(self, new_width, new_height):
self.height = new_height
self.width = new_width
def setPosition(self, newX, newY):
self.x = newX
self.y = newY
def addColliders(self, others):
if isinstance(others, list):
for other in others:
self.colliders.append(other)
else:
self.colliders.append(others)
def Collide(self, other):
#Define anchors of self
left = self.x
right = self.x + self.width
top = self.y
bottom = self.y + self.height
#Define anchors of other
otherL = other.x
otherR = other.x + other.width
otherT = other.y
otherB = other.y + other.height
#Left/ Right Collision Testing
if not (top > otherB or bottom < otherT ):
if (self.xvel>0):
if (right == otherL):
self.xvel = 0
if (right < otherL):
if (right+self.xvel > otherL):
self.xvel = otherL - right
if (self.xvel<0):
if (left == otherR):
self.xvel = 0
if (left > otherR):
if (left+self.xvel < otherR):
self.xvel = left - otherR
#Top/ Bottom Collision Testing
if not (left > otherR or right < otherL ):
if (self.yvel>0):
if (bottom == otherT):
self.yvel = 0
if (bottom < otherT):
if (bottom+self.yvel > otherT):
self.yvel = otherT - bottom
if (self.yvel<0):
if (top == otherB):
self.yvel = 0
if (top > otherB):
if (top+self.yvel < otherB):
self.yvel = otherB - top
def checkCollisionByType(self, other, direction='bottom'):
""" Returns True if colliding with an instance of specified Character """
return (isinstance(self.checkCollision(other,direction),type(other)))
def checkCollision(self, other, direction='bottom'):
#Define anchors of self
left = self.x
right = self.x + self.width
top = self.y
bottom = self.y + self.height
#Define anchors of other
otherL = other.x
otherR = other.x + other.width
otherT = other.y
otherB = other.y + other.height
if direction=='left':
if (left == otherR) and (not (top > otherB or bottom < otherT )):
return other
if direction=='right':
if (right == otherL) and (not (top > otherB or bottom < otherT )):
return other
if direction=='top':
if (top == otherB) and (not (left > otherR or right < otherL )):
return other
if direction=='bottom':
if (bottom == otherB) and (not (left > otherR or right < otherL )):
return other
return None
def updateMotion(self):
if (len(self.colliders)>0):
for collider in self.colliders:
self.Collide(collider)
self.x += self.xvel
self.y += self.yvel
self.xvel += self.xacc
self.yvel += self.yacc
def autoUpdateMotion(self, milliseconds):
timed = time.time()*1000
if ( (timed- self.lastUpdateTime)> milliseconds):
self.updateMotion()
self.lastUpdateTime = timed
def autoUpdateFrame(self, milliseconds):
timed = time.time()*1000
if ( (timed- self.lastUpdateAnimTime)> milliseconds):
self.current_frame = self.animation.takeFrame()
self.lastUpdateAnimTime = timed
def getRect(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
def getImage(self):
return self.current_frame