-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcherryChase.py
More file actions
298 lines (238 loc) · 8.24 KB
/
cherryChase.py
File metadata and controls
298 lines (238 loc) · 8.24 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
from random import randrange
import pygame
import numpy as np
pygame.init()
_gameWidth = 500
_gameHeight = 500
win = pygame.display.set_mode((_gameWidth, _gameHeight))
pygame.display.set_caption("Cherry Chase")
_grid = np.zeros(250000).reshape(500, 500)
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
blue = (0, 0, 100)
_playerX = 0
_playerY = 0
_health = 100
_blockage = 1
_cherries = []
_cherryWidth = 10
_cherryHeight = 10
def removeOldPlayer(playerX, playerY, playerWidth, playerHeight):
pygame.draw.rect(win, (0, 0, 0), (playerX, playerY,
playerWidth, playerHeight))
def collisionCheck():
global _cherries
global _playerX
global _playerY
global _cherryWidth
global _cherryHeight
if len(_cherries) > 0:
for cherry in _cherries:
cherryX = cherry[0]
cherryXLowerBound = cherryX - (_cherryWidth // 2)
cherryXUpperBound = cherryX + _cherryWidth
xCollision = _playerX < cherryXUpperBound and _playerX >= cherryXLowerBound
cherryY = cherry[1]
cherryYLowerBound = cherryY - (_cherryHeight // 2)
cherryYUpperBound = cherryY + _cherryHeight
yCollision = _playerY < cherryYUpperBound and _playerY >= cherryYLowerBound
if xCollision and yCollision:
pygame.draw.rect(
win, (0, 0, 0), (cherryX, cherryY, _cherryWidth, _cherryHeight))
_cherries.remove(cherry)
return True
return False
def spawnCherry():
global _cherries
cherryWidth = 10
cherryHeight = 10
cherryX = randrange(_gameWidth-cherryWidth)
cherryY = randrange(_gameHeight-cherryHeight-10)+10
if validCherry(cherryX, cherryY, cherryWidth, cherryHeight):
pygame.draw.rect(win, red, (cherryX, cherryY,
cherryWidth, cherryHeight))
_cherries.append((cherryX, cherryY))
else:
spawnCherry()
def validCherry(cherryX, cherryY, cherryWidth, cherryHeight):
for i in range(cherryWidth):
for j in range(cherryHeight):
if _grid[cherryX + i][cherryY + j] != 0:
return False
return True
def addItemToGrid(x, y, width, height, item):
for i in range(width):
if x + i < _gameWidth:
for j in range(height):
if y + j < _gameHeight:
_grid[x+i][y+j] = item
def spawnBlockage():
x = 0
y = 10
for i in range(1000):
randomBlockageType = randrange(0, 25)
if randomBlockageType > 0 and randomBlockageType < 5:
spawnLeftCornerBlockage(x, y)
if randomBlockageType > 5 and randomBlockageType < 10:
spawnRightCornerBlockage(x, y)
if randomBlockageType > 10 and randomBlockageType < 15:
spawnLongVerticalLineBlockage(x, y)
if randomBlockageType > 15 and randomBlockageType < 21:
spawnLongHorizontalLineBlockage(x, y)
# leaving a gap from 21 -> 25
x += 50
if x >= _gameWidth:
x = 0
y += 50
if y >= _gameHeight:
return
def spawnVerticalLineBlockage(x, y):
global _blockage
width = 10
height = 30
pygame.draw.rect(win, blue, (x, y, width, height))
addItemToGrid(x, y, width, height, _blockage)
def spawnLongVerticalLineBlockage(x, y):
spawnVerticalLineBlockage(x, y)
spawnVerticalLineBlockage(x, y + 30)
def spawnHorizontalLineBlockage(x, y):
global _blockage
width = 30
height = 10
pygame.draw.rect(win, blue, (x, y, width, height))
addItemToGrid(x, y, width, height, _blockage)
def spawnLongHorizontalLineBlockage(x, y):
spawnHorizontalLineBlockage(x, y)
spawnHorizontalLineBlockage(x + 30, y)
def spawnLeftCornerBlockage(x, y):
random = randrange(0, 2)
if random == 0:
spawnTopLeftCornerBlockage(x, y)
if random == 1:
spawnBottomLeftCornerBlockage(x, y)
def spawnTopLeftCornerBlockage(x, y):
random = randrange(0, 100)
if random > 0 and random < 20:
spawnHorizontalLineBlockage(x, y)
spawnVerticalLineBlockage(x, y)
if random > 20 and random < 60:
spawnLongHorizontalLineBlockage(x, y)
spawnVerticalLineBlockage(x, y)
if random > 60 and random < 100:
spawnHorizontalLineBlockage(x, y)
spawnLongVerticalLineBlockage(x, y)
def spawnBottomLeftCornerBlockage(x, y):
random = randrange(0, 100)
if random > 0 and random < 20:
spawnHorizontalLineBlockage(x, y + 20)
spawnVerticalLineBlockage(x, y)
if random > 20 and random < 60:
spawnLongHorizontalLineBlockage(x, y + 20)
spawnVerticalLineBlockage(x, y)
if random > 60 and random < 100:
spawnHorizontalLineBlockage(x, y + 50)
spawnLongVerticalLineBlockage(x, y)
def spawnRightCornerBlockage(x, y):
random = randrange(0, 2)
if random == 0:
spawnTopRightCornerBlockage(x, y)
if random == 1:
spawnBottomRightCornerBlockage(x, y)
def spawnTopRightCornerBlockage(x, y):
random = randrange(0, 100)
if random > 0 and random < 20:
spawnHorizontalLineBlockage(x, y)
spawnVerticalLineBlockage(x + 20, y)
if random > 20 and random < 60:
spawnLongHorizontalLineBlockage(x, y)
spawnVerticalLineBlockage(x + 50, y)
if random > 60 and random < 100:
spawnHorizontalLineBlockage(x, y)
spawnLongVerticalLineBlockage(x + 20, y)
def spawnBottomRightCornerBlockage(x, y):
random = randrange(0, 100)
if random > 0 and random < 20:
spawnHorizontalLineBlockage(x, y + 20)
spawnVerticalLineBlockage(x + 20, y)
if random > 20 and random < 60:
spawnLongHorizontalLineBlockage(x, y + 20)
spawnVerticalLineBlockage(x + 50, y)
if random > 60 and random < 100:
spawnHorizontalLineBlockage(x, y + 50)
spawnLongVerticalLineBlockage(x + 20, y)
def validMove(playerX, playerY):
global _grid
return _grid[playerX][playerY] == 0
def checkPlayerMovement(playerWidth, playerHeight):
global _playerX
global _playerY
global _health
step = 5
cherryEaten = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if validMove(_playerX, _playerY - step):
if _playerY > step:
_playerY -= step
cherryEaten = collisionCheck()
if keys[pygame.K_DOWN]:
if validMove(_playerX, _playerY + step):
if _playerY < _gameHeight - playerHeight:
_playerY += step
cherryEaten = collisionCheck()
if keys[pygame.K_LEFT]:
if validMove(_playerX - step, _playerY):
if _playerX > step:
_playerX -= step
cherryEaten = collisionCheck()
if keys[pygame.K_RIGHT]:
if validMove(_playerX + step, _playerY):
if _playerX < _gameWidth - playerWidth:
_playerX += step
cherryEaten = collisionCheck()
if cherryEaten:
if _health < 75:
_health += 25
else:
_health = 100
# speed = speed * 1.1, if this is enabled we need to speed up food drop. Else impossible
pygame.draw.rect(win, (255, 255, 255), (_playerX,
_playerY, playerWidth, playerHeight))
def drawHealthBar():
global _health
_health -= 0.25
healthBarWidth = 100
pygame.draw.rect(
win, red, (_gameWidth - healthBarWidth, 0, healthBarWidth, 10))
pygame.draw.rect(win, green, (_gameWidth - healthBarWidth, 0, _health, 10))
def endGameCheck():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
return True
def gameLoop():
run = True
global _playerX
global _playerY
global _health
playerWidth = 5
playerHeight = 5
foodDropWait = 150
foodDropCountdown = 0
spawnBlockage()
while run:
pygame.time.wait(50)
drawHealthBar()
run = endGameCheck()
if foodDropCountdown <= 1:
foodDropCountdown = foodDropWait
spawnCherry()
foodDropCountdown -= 1
removeOldPlayer(_playerX, _playerY, playerWidth, playerHeight)
checkPlayerMovement(playerWidth, playerHeight)
# draw updates
pygame.display.update()
gameLoop()
pygame.quit()
quit()