This repository has been archived by the owner on Dec 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AppEngine.py
285 lines (216 loc) · 7.25 KB
/
AppEngine.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
# Coded by: Rohan Bansal, 2018
# https://github.com/Rohan-Bansal
import contextlib, sys, os
with contextlib.redirect_stdout(None):
import pygame
from pygame.locals import *
pygame.init()
sprites = []
texts = []
black = (0, 0, 0)
white = (255, 255, 255)
gray = (128, 128, 128)
red = (255, 0, 0)
orange = (255, 140, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
indigo = (128, 0, 128)
violet = (238, 130, 238)
gameDisplay = ""
clock = pygame.time.Clock()
def set_window(title, widthInput, heightInput):
global gameDisplay, disp_width, disp_height
gameDisplay = pygame.display.set_mode((widthInput, heightInput))
pygame.display.set_caption(title)
pygame.display.flip()
def disp_width():
return pygame.display.get_surface().get_size()[0]
def disp_height():
return pygame.display.get_surface().get_size()[1]
class keyboard():
def __init__(self):
self.activeKeys = pygame.key.get_pressed()
def singlePress(self, key):
foundKeyPressed = False
for iterEvent in pygame.event.get():
if iterEvent.type == pygame.KEYDOWN and iterEvent.key == key:
foundKeyPressed = True
return foundKeyPressed
class mouse():
def __init__(self):
self.update()
def update(self):
self.x = pygame.mouse.get_pos()[0]
self.y = pygame.mouse.get_pos()[1]
def hoverSprite(self, sprite):
mouseX = pygame.mouse.get_pos()[0]
mouseY = pygame.mouse.get_pos()[1]
if sprite.edgeLeft < mouseX < sprite.edgeRight:
if sprite.edgeTop < mouseY < sprite.edgeBottom:
return True
return False
def mouseClicked(self):
for iterEvent in pygame.event.get():
if iterEvent.type == pygame.MOUSEBUTTONDOWN:
return True
return False
def spriteClicked(self, sprite):
if self.hoverSprite(sprite):
for iterEvent in pygame.event.get():
if iterEvent.type == pygame.MOUSEBUTTONDOWN:
return True
return False
class text():
def __init__(self, string, sizeInput, colorInput, xInput, yInput):
self.textFont = pygame.font.Font("freesansbold.ttf", sizeInput)
self.textSurface = self.textFont.render(string, True, colorInput)
self.textString = string
self.size = sizeInput
self.color = colorInput
self.x = xInput
self.y = yInput
self.visible = True
texts.append(self)
def hide(self):
self.visible = False
def show(self):
self.visible = True
def update(self):
if self.visible == True:
gameDisplay.blit(self.textSurface, (self.x, self.y))
def changeSize(self, size):
self.size = size
self.textFont = pygame.font.Font("freesansbold.ttf", size)
def changeText(self, stringInput, colorInput):
self.textString = stringInput
self.color = colorInput
self.textSurface = self.textFont.render(stringInput, True, colorInput)
def changeFont(self, font):
self.textFont = pygame.font.Font(font, self.size)
self.textSurface = self.textFont.render(self.textString, True, self.color)
def destroy(self):
texts.remove(self)
del(self)
class sprite():
def __init__(self, image, xInput, yInput, id_, **background):
if "background" in background:
self.backG = True
else:
self.backG = False
if isinstance(image, str):
self.main = pygame.image.load(os.path.dirname(os.path.abspath(__file__)) + "/" + image)
self.isStr = True
else:
self.main = image
self.isStr = False
self.x = xInput
self.y = yInput
self.visible = True
self.id_ = id_
self.width = self.main.get_rect().size[0]
self.height = self.main.get_rect().size[1]
self.rect = self.main.get_rect()
self.edgeLeft = self.x
self.edgeRight = self.x + self.width
self.edgeTop = self.y
self.edgeBottom = self.y + self.height
self.HP = 0
self.HPtext = text("", 2, black, 0, 0)
self.inventory = []
self.description = ""
sprites.append(self)
def setHP(self, number):
self.HP = number
def modifyImage(self, image):
if self.isStr == True:
self.main = pygame.image.load(os.path.dirname(os.path.abspath(__file__)) + "/" + image)
else:
self.main = image
def hide(self):
self.visible = False
def show(self):
self.visible = True
def moveToFront(self):
sprites.remove(self)
sprites.append(self)
def update(self):
self.edgeLeft = self.x
self.edgeRight = self.x + self.width
self.edgeTop = self.y
self.edgeBottom = self.y + self.height
if self.visible == True:
if self.backG == False:
gameDisplay.blit(self.main, (self.x,self.y))
def drawHealthText(self, x, y, size, color, value):
self.HPtext.x = x
self.HPtext.y = y
self.HPtext.changeSize(size)
self.HPtext.changeText(value, color)
def inBorder(self):
if self.edgeLeft > 0:
if self.edgeRight < disp_width():
return True
return False
def collide(self, otherSprite):
if otherSprite == None:
return False
if otherSprite.edgeLeft <= self.edgeLeft <= otherSprite.edgeRight or otherSprite.edgeLeft <= self.edgeRight <= otherSprite.edgeRight:
if otherSprite.edgeTop <= self.edgeTop <= otherSprite.edgeBottom or otherSprite.edgeTop <= self.edgeBottom <= otherSprite.edgeBottom:
return True
else:
return False
def destroy(self):
self.HPtext.destroy()
self.main = None
self.rect = None
sprites.remove(self)
del(self)
def moveUp(self, speed):
self.y -= speed
def moveDown(self, speed):
self.y += speed
def moveRight(self, speed):
self.x += speed
def moveLeft(self, speed):
self.x -= speed
class audio():
def __init__(self):
self.song = ""
self.SFX = ""
pygame.mixer.init()
def playMusic(self, song, loopParam): # 0 (once), -1 (indefinite)
self.song = song
pygame.mixer.music.load(song)
pygame.mixer.music.play(loopParam)
def QueueMusic(self, song):
pygame.mixer.music.queue(song)
def stopMusic(self):
self.song = ""
pygame.mixer.music.stop()
def playSound(self, sound):
self.SFX = pygame.mixer.Sound(sound)
self.SFX.play()
self.SFX = ""
kb = keyboard()
ms = mouse()
au = audio()
def quitClicked():
for iterEvent in pygame.event.get():
if iterEvent.type == pygame.QUIT:
quit()
exit()
def gameLoop(background):
if(isinstance(background, tuple)):
gameDisplay.fill(background)
else:
gameDisplay.blit(background.main, (0, 0))
quitClicked()
for sprite in sprites:
sprite.update()
for text in texts:
text.update()
kb.activeKeys = pygame.key.get_pressed()
ms.update()
pygame.display.update()
clock.tick(60)