-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCyberFunk2021.py
356 lines (295 loc) · 12.2 KB
/
CyberFunk2021.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'''
CyberFunk 2021.exe
No OOP and some variables are passed to functions and some are just accessed globally.
Also it runs slow on command prompt. Better on powershell and better still on vscode powershell.
CyberFunk 2021, None Rights Reserved.
'''
import ctypes
import time
import os
import enum
import math
import time
import itertools
import random
#Variables
AlienLimit = 10
StageSize = (60,30)
Score = 0
Level = 1
GameStatus = 1
VBorder = ['| |', '| |','|v|']
HBorder = [[' <',' < ','< '],['> ',' > ',' >']]
SpriteList = []
AlienList = []
FireList = []
AnimationList = []
StageList = [[' ' for _ in range(StageSize[0])] for _ in range(StageSize[1])]
SpriteGraphics = [
[[' | iAi=#='],[' A /8\\ /|W|\\A=+=A^ ^',' A /8\\ /|M|\\A=x=A* *']],
[['\\i/=8="^"','_l_>O<`^`'],['\___//=O=\\\\___/// \\\\A A','\___//O==\\\\___/|| || A A ','\___//=O=\\\\___/\\\\ // M ','\___//==O\\\\___/|| || A A ']],
[['^'],['A'],['*'],list('POWERUP')] ,
[[symb*((2*x + 1)**2) for symb in ' .o0O@' ] for x in range(3)]
# Generates the animation graphics by listcomp
]
#Game Functions
def printStage(StageList,Frame):
# Print the frame into the terminal
Frame = Frame % 3
for _ in range(StageSize[0]+2*len(VBorder)): print('-',end='')
print()
for _ in range((StageSize[0] + 2*len(VBorder))//6 + 1): print(HBorder[0][Frame],end='')
for _ in range((StageSize[0] + 2*len(VBorder))//6): print(HBorder[1][Frame],end='')
print()
for _ in range(StageSize[0]+2*len(VBorder)): print('-',end='')
print()
# Print the StageList contents with their colors.
for x,row in enumerate(StageList):
print(VBorder[(x-Frame)%3],end='')
for pixel in row:
if len(pixel) == 1:
print(pixel, end='')
else:
print(f'\033[0;9{pixel[1]}m{pixel[0]}\033[0m',end='')
print(VBorder[(x-Frame)%3],end='')
print()
for _ in range(StageSize[0]+2*len(VBorder)): print('-',end='')
print()
print(f'Score:{Score}\t\tLevel:{Level}\t\t\tHP:{SpriteList[0][0]["HP"]}')
print()
for _ in range(StageSize[0]+2*len(VBorder)): print('-',end='')
if GameStatus == 0:
print()
print('GameOver')
def clearStage(StageList):
# Clear previous frame
for y in range(StageSize[1]):
for x in range(StageSize[0]):
StageList[y][x] = ' '
def gameLoop(StageList, SpriteList,):
# Run the game processes
Alternate = itertools.cycle(range(30))
while(GameStatus and not ctypes.windll.User32.GetAsyncKeyState(0x42)):
Frame = next(Alternate)
clearStage(StageList)
printSprite(StageList, SpriteList)
os.sys.stdout.flush()
time.sleep(.05)
shipRoutine(SpriteList,Frame)
fireRoutine(SpriteList[2])
alienRoutine(SpriteList,AlienLimit, Frame)
animationRoutine(SpriteList[3])
offscreenCheck(SpriteList)
os.system('cls')
printStage(StageList,Frame)
controller()
#Sprite Functions
class Catg(enum.Enum):
#Enum for sprite categories
SHIP = 0
ALIEN = 1
PROJECTILE = 2
ANIMATION = 3
def newSprite(Cat, HP, Hitbox, Position, Graphics = 0, Costume = 0, Color = 0, Parent = None):
if Cat != Catg.PROJECTILE:
return {'Catg': Cat, 'HP':HP, 'Hitbox':Hitbox, 'Position':Position, 'Graphics':Graphics, 'Costume':Costume, 'Color':Color}
return {'Catg': Cat, 'HP':HP, 'Hitbox':Hitbox, 'Position':[Parent['Position'][0],Parent['Position'][1] + math.ceil(Parent['Hitbox']/2)*(-1 if Parent['Catg'].value == 0 else 1)], 'Parent':Parent, 'Graphics':Graphics, 'Costume':Costume, 'Color':Color}
def printSprite(StageList,SpriteList):
# Print the sprite to the StageList as a tuple (Character,Color)
for CatgList in SpriteList:
for Sprite in CatgList:
for z in range(Sprite['Hitbox']**2):
#y & x are the pseudo 2d pointer to the imaginary 2d graphic list made from the 1d list.
x,y = divmod(z,Sprite['Hitbox'])
#y & xPointer point to the place on StageList which bears the character of sprite
yPointer = Sprite['Position'][1] + y - (Sprite['Hitbox'])//2
xPointer = Sprite['Position'][0] + x - (Sprite['Hitbox'])//2
if(xPointer >= 0 and xPointer < StageSize[0] and yPointer >= 0 and yPointer < StageSize[1]):
StageList[yPointer][xPointer] = (SpriteGraphics[Sprite['Catg'].value][Sprite['Graphics']][Sprite['Costume']][x + y*Sprite['Hitbox']],Sprite['Color'])
def nextCostume(Sprite):
# Cycle through costumes.
if len(SpriteGraphics[Sprite['Catg'].value][Sprite['Graphics']]) > (Sprite['Costume'] + 1):
Sprite['Costume'] += 1
else:
Sprite['Costume'] = 0
def collisionCheck(Sprite1, Sprite2):
# Check if any two sprites are colliding
if abs(Sprite1['Position'][1] - Sprite2['Position'][1])-1 < abs(Sprite1['Hitbox']//2 + Sprite2['Hitbox']//2) and abs(Sprite1['Position'][0] - Sprite2['Position'][0])-1 < abs(Sprite1['Hitbox']//2 + Sprite2['Hitbox']//2):
return True
def offscreenCheck(SpriteList):
# Check if any sprite except ship is vertically offscreen
for CatgList in SpriteList:
for Sprite in CatgList:
if (Sprite['Position'][1] <= 0 or Sprite['Position'][1] >= StageSize[1]) and not (Sprite['Catg'] == Catg.SHIP):
CatgList.remove(Sprite)
def disintegrate(Sprite):
# Remove sprite and animate explosion
newAnimation(Sprite,'Explosion')
if Sprite in SpriteList[Sprite['Catg'].value]:
SpriteList[Sprite['Catg'].value].remove(Sprite)
# Ship functions
def shipRoutine(SpriteList,Frame):
Ship = SpriteList[0][0]
nextCostume(SpriteList[0][0])
#Check if the ship is big, small or dead
if Ship['HP'] < 1:
global GameStatus
GameStatus = 0
elif Ship['HP'] > 4:
Ship['Hitbox'] = 5
Ship['Graphics'] = 1
Ship['Power'] = 2
else:
Ship['Hitbox'] = 3
Ship['Graphics'] = 0
Ship['Costume'] = 0
Ship['Power'] = 1
# Collision check with enemy and projectiles
for index in range(1,3):
for Sprite in SpriteList[index]:
if collisionCheck(Ship, Sprite):
if Sprite['HP'] < 0:
if Ship['HP'] != 5:
newAnimation(Ship,'Damage')
else:
newAnimation(Ship,'Explosion')
if Sprite['Catg'] == Catg.PROJECTILE:
Ship['HP'] += Sprite['HP']
disintegrate(Sprite)
elif not Frame % 3:
Ship['HP'] -= 1
# Shoot
if not Frame % 4:
addFire(SpriteList[2], SpriteList[0][0],-Ship['Power'])
def controller():
# Wait for user input for 1 millisecond then continue with game
state = ctypes.windll.User32.GetAsyncKeyState
pos = SpriteList[0][0]['Position']
Lap = time.perf_counter()
while(time.perf_counter() - Lap) < .001:
if(state(0x41) and pos[0] >= 0):
pos[0] += -2
time.sleep(.001)
if(state(0x53) and pos[1] < StageSize[1]):
pos[1] += 1
time.sleep(.01)
if(state(0x44)and pos[0] < StageSize[0]):
pos[0] += 2
time.sleep(.001)
if(state(0x57) and pos[1] >= 0):
pos[1] -= 1
time.sleep(.01)
#Shot functions
def addFire(FireList, Sprite, HP = -1):
# Deal with enemy projectile/powerup
if Sprite['Catg'] == Catg.ALIEN:
if HP > 0:
Graphics = 3
Colour = 2
else:
Graphics = 2
Colour = 1
FireList.append(newSprite(Catg.PROJECTILE,HP,1,'Position',Graphics,0,Colour,Sprite))
# Deal with ship projectile
else:
if Sprite['HP'] >= 5:
Graphics = 1
else:
Graphics = 0
FireList.append(newSprite(Catg.PROJECTILE,HP,1,'Position',Graphics,0,2,Sprite))
def fireRoutine(FireList):
# Move all projectiles in their respective directions
if FireList:
for Shot in FireList:
nextCostume(Shot)
if Shot['Parent']['Catg'].value == 0:
Shot['Position'][1] -= 1
else:
Shot['Position'][1] += 1
# Alien functions
def spawnAlien(AlienList,AlienLimit):
# Spawn aliens randomly at random position till limit
if len(AlienList) < AlienLimit:
# Randomize alien type
AlienType = random.randint(0,1)
if AlienType == 0:
NewAlien = newSprite(Catg.ALIEN,3,3,[0,1],0,Color=random.choice((0,4,5,6)))
elif AlienType == 1:
NewAlien = newSprite(Catg.ALIEN,5,5,[0,1],1,Color=random.choice((0,4,5,6)))
NewAlien['Position'][0] = random.randint(0,StageSize[0])
# Check that the spawn position is not preoccupied
if not brethrenCollision(NewAlien,AlienList):
AlienList.append(NewAlien)
def brethrenCollision(Alien,AlienList):
isColliding = False
for Brethren in AlienList:
if Brethren != Alien and not isColliding:
isColliding = collisionCheck(Alien,Brethren)
return isColliding
def alienRoutine(SpriteList, AlienLimit, Frame):
global Score
if not random.randint(0,5):
spawnAlien(SpriteList[1], AlienLimit)
# Randomly fire and assign projectile to random alien
if SpriteList[1] and not random.randint(0,4):
addFire(SpriteList[2], SpriteList[1][random.randint(0,len(SpriteList[1]) - 1)])
for Alien in SpriteList[1]:
if not Frame%2:
randomHStep(Alien)
if not Frame % 30:
VStep(Alien)
if not Frame % 3 and collisionCheck(Alien,SpriteList[0][0]):
newAnimation(Alien,'Damage')
Alien['HP'] -= 1
if Alien['HP'] < 1:
if not random.randint(0,4):
addFire(SpriteList[2],Alien,1)
disintegrate(Alien)
Score += 10
# Check collision and do collision and score stuff
for Shot in SpriteList[2]:
if Shot['Parent']['Catg'] != Catg.ALIEN:
if collisionCheck(Alien, Shot):
newAnimation(Alien,'Damage')
Alien['HP'] += Shot['HP']
disintegrate(Shot)
Score += 1
if Alien['HP'] < 1:
if not random.randint(0,4):
addFire(SpriteList[2],Alien,1)
disintegrate(Alien)
Score += 10
if not Frame % 4: nextCostume(Alien)
def randomHStep(Alien):
#If the alien doesnt collide with others then move randomly
Step = random.randint(-1,1)
if (Alien['Position'][0] + Step) > 0 and (Alien['Position'][0] + Step) < StageSize[0]:
Alien['Position'][0] += Step
if brethrenCollision(Alien,SpriteList[1]):
Alien['Position'][0] -= Step
def VStep(Alien):
# Step down
if Alien['Position'][1] < StageSize[1]:
Alien['Position'][1] += 1
#Animation functions
def newAnimation(Sprite,type):
# Spawn a short lived sprite of highest priority with no routines except costume cycle
if type == 'Explosion':
SpriteList[3].append(newSprite(Catg.ANIMATION,6,Sprite['Hitbox'],Sprite['Position'],int((Sprite['Hitbox']-1)/2),Color=3))
elif type == 'Damage':
SpriteList[3].append(newSprite(Sprite['Catg'],2,Sprite['Hitbox'],Sprite['Position'],Sprite['Graphics'],Color=1))
def animationRoutine(AnimationList):
# Animate the animation sprite
for Animation in AnimationList:
nextCostume(Animation)
Animation['HP'] -= 1
if Animation['HP'] == 0:
AnimationList.remove(Animation)
def main():
SpriteList.append([newSprite(Catg.SHIP,3,5,[25,18],1,0,2)])
SpriteList.append(AlienList)
SpriteList.append(FireList)
SpriteList.append(AnimationList)
gameLoop(StageList,SpriteList)
if __name__ == '__main__':
main()