-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
275 lines (220 loc) · 7.09 KB
/
classes.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
from procedural_generation import *
from non_original_work import *
from other import *
import math
class Player:
def __init__(self, w, h, data):
self.width = w
self.height = h
startcell = self.getStartCell(data,1)
#Player location
self.row = startcell[0]-1
self.col = startcell[1]
self.maxCol = data.visibleCols//2 + 1
self.minCol = 1
self.minRow = 5
self.maxRow = data.visibleRows - 5
self.startRow = None
#movement booleans
self.jumping = False
self.falling = True
self.inDescent = False
self.climbing = False
self.inUpDownBlock = False
self.inUpLeft = False
self.onTopOfLadder = False
#vertical scrolling values
self.lowerBound = None
self.upperBound = None
#jump constants
self.maxJump = 4
self.baseMaxJump = 3
#powerups
self.hasPowerUp = None
self.invincible = False
def getStartCell(self, data, col):
for row in range(data.firstVisibleRow + data.visibleRows//2,\
data.firstVisibleRow+data.visibleRows):
if data.grid[row][col] == True:
return (row, col)
return(12, 12)
def moveHorizontal(self, dx,grid,data, multiplayer=False):
self.col += dx
if self.checkForCollison(grid, data.firstVisibleCol, data):
self.col -= dx
if self.inUpDownBlock != True and self.inUpLeft != True and data.scroll==True:
return self.scrollHorizontal(dx, data, multiplayer)
return False
def getBounds(self):
x0 = self.col*self.width
y0 = self.row*self.height
return x0, y0, x0+self.width, y0+self.height
def checkForCollison(self, grid, col, data):
try:
if grid[self.row][self.col + col] == True or grid[self.row][self.col + col] == "gravel":
self.inDescent = False
return True
elif grid[self.row][self.col + col] == "ladder":
self.falling = False
self.climbing = True
#prevents player from escaping the ladder upwards
elif grid[self.row+1][self.col + col] == "ladder":
self.onTopOfLadder = True
self.scrollVertical(-1, data)
self.inDescent = False
else:
self.onTopOfLadder = False
#jumpPowerUp
if grid[self.row][self.col + col] == "jumpPower" and self.hasPowerUp == None:
grid[self.row][self.col+col] = False
self.hasPowerUp = "jump"
self.maxJump += 1
#shield
if grid[self.row][self.col + col] == "shield" and self.hasPowerUp == None:
grid[self.row][self.col+col] = False
self.hasPowerUp = "shield"
self.invincible = True
return False
except IndexError:
data.gameOver = True
def scrollHorizontal(self, dx, data, multiplayer):
if self.col < self.minCol and data.firstVisibleCol > 0:
self.col = self.minCol
data.firstVisibleCol -= 1
if multiplayer == False:
for background in data.backgrounds:
background.scroll(data, -1, 0)
return True
elif self.col < self.minCol:
self.col = self.minCol
elif self.col > self.maxCol:
self.col = self.maxCol
data.firstVisibleCol += 1
if multiplayer == False:
for background in data.backgrounds:
background.scroll(data, 1, 0)
for obj in data.flyingObjects:
obj.move(data)
return True
return False
def moveVertical(self,data,dy):
self.row += dy
if self.checkForCollison(data.grid, data.firstVisibleCol, data):
self.row -= dy
def moveUpDownRow(self, data, dy):
if data.grid[self.row+1][self.col+data.firstVisibleCol] == True or\
data.grid[self.row+2][self.col+data.firstVisibleCol] == True:
if dy == 1: return None
self.row += dy
if self.climbing:
self.scrollVertical(dy, data)
def scrollVertical(self, dy, data):
data.firstVisibleRow += dy
if data.firstVisibleRow < self.lowerBound or\
data.firstVisibleRow+data.visibleRows > self.upperBound:
data.firstVisibleRow -= dy
else:
for flyingObject in data.flyingObjects:
flyingObject.cy -= (data.cellHeight*dy)
def getMaxJumpRow(self, grid,data):
if self.startRow == None:
self.startRow = 1000
return self.startRow - self.maxJump
def startJump(self):
self.startRow = self.row
self.jumping = True
self.falling = False
self.climbing = False
def jump(self, data):
self.moveVertical(data, -1)
if self.row <= self.getMaxJumpRow(data.grid, data) or self.falling == True:
self.jumping = False
self.falling = True
self.inDescent = True
def fall(self, data):
if self.jumping == False:
self.moveVertical(data, 1)
def checkForGravel(self, data):
for i in range(4):
if not self.climbing:
try:
if data.grid[self.row][self.col + i] == "gravel":
return True
elif data.grid[self.row][self.col - i] == "gravel":
return True
except IndexError:
continue
def canvasGetBounds(self, data):
x0, y0, x1,y1 = self.getBounds()
y0 = y0 - (data.firstVisibleRow * data.cellHeight)
y1 = y0 + self.height
return x0, y0, x1, y1
def getCanvasCenter(self,data):
x0, y0, x1,y1 = self.canvasGetBounds(data)
cx = (x0+x1)/2
cy = (y0+y1)/2
return cx, cy
def reversePowerUp(self):
if self.hasPowerUp == "jump":
self.maxJump -= 1
if self.hasPowerUp == "shield":
self.invincible = False
def isOnLand(self, data):
try:
if data.grid[self.row+1][self.col+data.firstVisibleCol] == True:
return True
except IndexError:
data.gameOver = True
return False
def checkForObstacle(self, data):
try:
if data.grid[self.row][self.col + data.firstVisibleCol] == "obstacle":
if not self.invincible:
data.gameOver = True
except IndexError:
data.gameOver = True
def checkIfWon(self, data):
if data.grid[self.row][self.col+data.firstVisibleCol] == "flag":
data.won = True
class Background:
def __init__(self, img, data, cx, cy, width):
self.width = width
self.height = int(data.height)
self.img = img
self.background = self.createImage(data)
self.cx = cx
self.cy = cy
def createImage(self,data):
return createImage(data, self.width, self.height, self.img)
def draw(self, canvas):
canvas.create_image(self.cx, self.cy, image=self.background)
def drawShift(self, canvas, data):
canvas.create_image(self.cx+data.shiftAmount, self.cy, image=self.background)
def scroll(self, data, dx, dy):
self.cx += ((data.firstVisibleCol*data.cellWidth)/50)*dx*-1
self.cy += ((data.firstVisibleRow*data.cellHeight)/50)*dy*-1
def needNewBackground(self, data):
if self.cx + self.width/2 < self.width:
cx = self.cx + self.width
cy = self.cy
data.backgrounds.insert(0, Background(data.backgroundImage, data, cx, cy, self.width))
class FlyingObject:
def __init__(self,data, player):
self.width = int(data.cellWidth*1.5)
self.height = int(data.cellHeight*1.5)
self.cx = data.width + self.width
self.cy = random.randint(self.height//2, data.height-(self.height//2))
self.speed = data.objSpd
self.player = player
def draw(self, canvas,data):
canvas.create_image(self.cx, self.cy, image=data.flyingObjectImage)
def drawShift(self, canvas, data):
canvas.create_image(self.cx+data.shiftAmount, self.cy, image=data.flyingObjectImage)
def move(self, data):
self.cx -= self.speed
self.collision(self.player, data)
def collision(self,player,data):
cx, cy = player.getCanvasCenter(data)
if abs(self.cx - cx) <= self.width//2 and abs(self.cy-cy) <= self.height//2:
if not player.invincible:
data.gameOver = True