-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
80 lines (73 loc) · 1.93 KB
/
game.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
import random
import signal,copy,sys,time
from board import *
from player import *
from obstacle import *
from getchunix import *
from alarmexception import *
from controls import *
from bomb import *
getch = GetchUnix()
def alarmHandler(signum, frame):
raise AlarmException
def input_to(timeout = 1):
signal.signal(signal.SIGALRM, alarmHandler)
signal.setitimer(signal.ITIMER_REAL,timeout)
t0 = time.time()
try:
text = getch()
signal.alarm(0)
t1 = time.time()
while (t1 - t0 < 0.5):
t1 = time.time()
return text
except AlarmException:
print(end='')
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''
def main():
print("Press any key to start game")
level = 1
score = 0
t0 = time.time()
while(1):
villan = []
hero = Hero()
board_obj = Board()
wall = Wall()
wall.fabricate(board_obj)
brick = []
bomb = Bomb(level)
brick_cnt = 35 - 3 * level
for i in range(brick_cnt):
brick.append(Brick())
brick[i].fabricate(board_obj,wall)
for i in range (bomb._villan_cnt):
villan.append(Villan(board_obj,brick,wall))
while(hero._lives and bomb._villan_cnt):
move = input_to()
controls(move,hero,board_obj,bomb)
t1 = time.time()
if(t1 - t0 > 1):
t0 = time.time()
for i in range (bomb._villan_cnt):
villan[i].motion(board_obj,bomb,hero)
board_obj.bombDraw(hero,bomb,villan)
t2 = time.time()
if(t2 - bomb._plant_time > 1):
bomb._plant_time = time.time()
if(bomb._positionX != -1):
bomb._time -= 1
bomb._upperShape=[bomb._boundary,bomb._time,bomb._time,bomb._boundary]
bomb._lowerShape=[bomb._boundary,bomb._time,bomb._time,bomb._boundary]
if(bomb._time == -1):
bomb.blast(board_obj,hero,villan)
blast = 1
board_obj.playerDraw(hero)
board_obj.draw()
total_score = score + hero._score
print("Score: " , total_score , " Lives: " , hero._lives , " Level: " , level)
level += 1
score += hero._score
if __name__ == '__main__':
main()