-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwork4.py
119 lines (93 loc) · 3.36 KB
/
hwork4.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
lengthofasteroids= int(input())
heightofasteroids= int(input())
distancebetweenspaceshipandasteroids= int(input())
allasteroids=[]
def printthescene():
for m in allasteroids:
for h in m:
print(h, end='')
print()
print(72*'-')
# INITIAL POSITIONS OF ASTEROIDS
for i in range(heightofasteroids):
eachasteroidsegment=[]
for j in range(lengthofasteroids):
eachasteroidsegment.append('*')
allasteroids.append(eachasteroidsegment)
# IMPLEMENTING EMPTY LISTS
for i in range(distancebetweenspaceshipandasteroids):
eachspacesegment=[]
for j in range(lengthofasteroids):
eachspacesegment.append(' ')
allasteroids.append(eachspacesegment)
# SPACESHIP CONTROL LIST
spaceshipcontrollist = []
for i in range(lengthofasteroids):
spaceshipcontrollist.append(' ')
allasteroids.append(spaceshipcontrollist)
# INSERTING THE SPACESHIP INTO THE SPACESHIP CONTROL LIST
if lengthofasteroids%2!=0:
indexofspaceship= (lengthofasteroids//2)
else:
indexofspaceship= (lengthofasteroids//2)-1
if not (heightofasteroids==0 or lengthofasteroids==0):
spaceshipcontrollist[indexofspaceship]='@'
printthescene()
if heightofasteroids==0 or lengthofasteroids==0:
print('YOU WON!')
for i in range(distancebetweenspaceshipandasteroids):
print()
print('@')
print(72*'-')
timer=0
while lengthofasteroids!=0 and heightofasteroids!=0:
# DANGEROUS INPUTS
action= input('Choose your action!')
action=action.lower()
# TIMER IS INCREMENTED BY 1
timer += 1
if action=='exit':
printthescene()
break
if action=='right':
if indexofspaceship<lengthofasteroids-1:
spaceshipcontrollist[indexofspaceship] = ' '
indexofspaceship += 1
spaceshipcontrollist[indexofspaceship] = '@'
if action == 'left':
if indexofspaceship > 0:
spaceshipcontrollist[indexofspaceship]=' '
indexofspaceship -= 1
spaceshipcontrollist[indexofspaceship]='@'
if action=='fire':
heightplusspace = heightofasteroids + distancebetweenspaceshipandasteroids
for i in range(0,heightplusspace):
if allasteroids[heightplusspace-1-i][indexofspaceship]!='*':
allasteroids[heightplusspace-1-i][indexofspaceship] = '|'
printthescene()
allasteroids[heightplusspace-1-i][indexofspaceship]=' '
elif allasteroids[heightplusspace-1-i][indexofspaceship]=='*':
allasteroids[heightplusspace-1-i][indexofspaceship]= ' '
break
# CHECKING IF THE USER HAS WON AFTER THE SHOT
asteroidcounter=0
for eachlist in allasteroids:
for i in eachlist:
if i=='*':
asteroidcounter+=1
if asteroidcounter==0:
print('YOU WON!')
printthescene()
break
# CHECKING WHETHER OR NOT TO SLIDE ASTEROIDS
if timer % 5 == 0 and timer != 0:
newbornlist = [' ' for each in range(lengthofasteroids)]
allasteroids.insert(0, newbornlist)
if allasteroids[-2].count('*') == 0:
allasteroids.pop(-2)
else:
print('GAME OVER')
printthescene()
break
# GAME OVER
printthescene()