-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
166 lines (134 loc) · 3.75 KB
/
main.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
import curses, random, time
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.curs_set(False)
stdscr.nodelay(True)
obs = '.'
player = 'x'
food_ch = '*'
food_num = 10
score = 0
enemy_ch = 'E'
enemy_num = 3
maxl = curses.LINES - 1
maxc = curses.COLS - 1
world = []
food = []
enemy = []
# making sure player cannot go outside the screen
def in_range(a, min, max):
if a > max:
return max
if a < min:
return min
else:
return a
# getting two random values that doesn't overlap
def random_place():
i = random.randint(0, maxl)
j = random.randint(0, maxc)
while world[i][j] != ' ':
i = random.randint(0, maxl)
j = random.randint(0, maxc)
return in_range(i, 0, maxl), in_range(j, 0, maxc)
def init():
global player_c, player_l
for i in range(maxl + 1):
world.append([])
for j in range(maxc + 1):
world[i].append(' ' if random.random() > 0.03 else obs)
for i in range(food_num):
foodl, foodc = random_place()
fooda = random.randint(1000, 10000)
food.append((foodl, foodc, fooda))
for i in range(enemy_num):
enemyl, enemyc = random_place()
enemy.append((enemyl, enemyc))
player_l, player_c = random_place()
def game_over():
global playing
stdscr.clear()
massage = 'Game Over'
stdscr.addstr(maxl // 2, (maxc - len(massage)) // 2, 'Game Over!')
stdscr.refresh()
time.sleep(3)
playing = False
def draw():
for i in range(maxl):
for j in range(maxc):
stdscr.addch(i, j, world[i][j])
for f in food:
fl, fc, fa = f
stdscr.addch(fl, fc, food_ch)
for e in enemy:
el, ec = e
stdscr.addch(el, ec, enemy_ch)
stdscr.addch(player_l, player_c, player)
stdscr.addstr(1, 1, f"Score: {score}")
stdscr.refresh()
# moving function
def move(c):
global player_l, player_c
if c == 'w' and world[player_l - 1][player_c] != obs:
player_l -= 1
elif c == 's' and world[player_l + 1][player_c] != obs:
player_l += 1
elif c == 'a' and world[player_l][player_c - 1] != obs:
player_c -= 1
elif c == 'd' and world[player_l][player_c + 1] != obs:
player_c += 1
player_l = in_range(player_l, 0, maxl - 1)
player_c = in_range(player_c, 0, maxc - 1)
# eating food and keeping score
def check_food():
global score
for i in range(len(food)):
fl, fc, fa = food[i]
if player_l == fl and player_c == fc:
score += 10
new_fl, new_fc = random_place()
new_fa = random.randint(10000, 100000)
food[i] = (new_fl, new_fc, new_fa)
elif fa == 0:
new_fl, new_fc = random_place()
new_fa = random.randint(10000, 100000)
food[i] = (new_fl, new_fc, new_fa)
else:
fa -= 1
food[i] = (fl, fc, fa)
def enemy_attack():
global playing
for i, e in enumerate(enemy):
el, ec = e
if player_l == el and player_c == ec and not testing:
game_over()
elif ec < player_c and random.random() > 0.999:
ec += 1
elif ec > player_c and random.random() > 0.999:
ec -= 1
elif el < player_l and random.random() > 0.999:
el += 1
elif el > player_l and random.random() > 0.999:
el -= 1
el = in_range(el, 0, maxl)
ec = in_range(ec, 0, maxc)
enemy[i] = (el, ec)
init()
playing = True
testing = False
# main loop
while playing:
try:
c = stdscr.getkey()
except:
c = ''
if c in 'awds':
move(c)
elif c.lower() == 'q':
playing = False
enemy_attack()
check_food()
draw()
curses.endwin()