-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
724 lines (596 loc) · 27.2 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import curses
import random
import time
# curses must be installed
# Utility functions not associated with a class
def point_in_rect(rx1, ry1, rx2, ry2, x, y):
"""
Given a rectangle from bottom-left (rx1, ry1) to upper-right (rx2, ry2),
see if point (x, y) is inside of it
"""
if (x > (rx1) and x < (rx2 - 1) and
y > (ry1) and y < (ry2 - 1)) :
return True
else :
return False
def get_rect_from_center(x, y, w, h):
return ((int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)))
# renders the variables of the current world
class Renderer:
def __init__(self):
pass
def render(self, state):
window = state.window
stats = state.stats
debug_info = state.debug_info
world = state.world
player = state.world.player
monsters = state.world.monsters
trees = state.world.trees
bullet = state.world.bullet
window.clear()
window.box()
world._render(state)
stats._render(state)
debug_info._render(state)
player._render(state)
for i in range(len(monsters)):
monsters[i]._render(state)
for i in range(len(trees)):
trees[i]._render(state)
if bullet:
bullet._render(state)
window.refresh()
# keeps track of the screen pos of the object
class ScreenPos:
def __init__(self, x, y):
self.x = x
self.y = y
# keeps track of the world pos of the object
class WorldPos:
def __init__(self, x, y):
self.x = x
self.y = y
# passed into every object class to keep track of the world position
class WorldObject:
def __init__(self, start_x, start_y):
self.start_x = start_x
self.start_y = start_y
self.pos = WorldPos(start_x, start_y)
@property
def x(self):
return self.pos.x
@x.setter
def x(self, x):
self.pos.x = x
@property
def y(self):
return self.pos.y
@y.setter
def y(self, y):
self.pos.y = y
# keeps track of player position in the world
class Player(WorldObject):
def __init__(self, start_x, start_y):
super().__init__(start_x, start_y)
self.start_x = start_x
self.start_y = start_y
def _render(self, state):
camera = state.camera
if camera.is_visible(self.pos):
screen_pos = state.camera.to_screen_space(self.pos)
state.window.addch(screen_pos.y, screen_pos.x, 'A')
else:
return
# keeps track of the walls for the world
class Wall(WorldObject):
def __init__(self, start_x, start_y):
super().__init__(start_x, start_y)
self.start_x = start_x
self.start_y = start_y
# keeps track of the game world in a 2d grid
class World:
def __init__(self, player_start_x, player_start_y, max_x, max_y, world_x_compare_window, world_y_compare_window, window_max_x, window_max_y):
self.common_tools = CommonTools()
self.player = Player(player_start_x, player_start_y)
self.bullet = None
self.max_x = max_x
self.max_y = max_y
self.window_max_x = window_max_x
self.window_max_y = window_max_y
self.world_x_compare_window = int(world_x_compare_window)
self.world_y_compare_window = int(world_y_compare_window)
self.trees = []
for i in range(30):
tree = Tree(player_start_x, player_start_y)
while(not self.common_tools.object_clash(tree, self.player.x, self.player.y)):
tree.x = random.randint(-self.world_x_compare_window, self.world_x_compare_window + self.window_max_x)
tree.y = random.randint(-self.world_y_compare_window, self.world_y_compare_window + self.window_max_y)
self.trees.append(tree)
self.monsters = []
for i in range(3):
monster = Monster(player_start_x, player_start_y)
while(not self.common_tools.object_clash(monster, self.player.x, self.player.y)):
monster.x = random.randint(-self.world_x_compare_window, self.world_x_compare_window + self.window_max_x)
monster.y = random.randint(-self.world_y_compare_window, self.world_y_compare_window + self.window_max_y)
for i in range(len(self.trees)):
while(not self.common_tools.object_clash(monster, self.trees[i].x, self.trees[i].y)):
monster.x = random.randint(-self.world_x_compare_window, self.world_x_compare_window + self.window_max_x)
monster.y = random.randint(-self.world_y_compare_window, self.world_y_compare_window + self.window_max_y)
self.monsters.append(monster)
self.left_wall_middle = Wall(-self.world_x_compare_window - 1, player_start_y)
self.left_wall_top = Wall(-self.world_x_compare_window - 1, -self.world_y_compare_window - 1)
self.left_wall_bottom = Wall(-self.world_x_compare_window - 1, self.window_max_y + self.world_y_compare_window + 1)
self.right_wall_middle = Wall(self.window_max_x + self.world_x_compare_window + 1, player_start_y)
self.right_wall_top = Wall(self.window_max_x + self.world_x_compare_window + 1, -self.world_y_compare_window - 1)
self.right_wall_bottom = Wall(self.window_max_x + self.world_x_compare_window + 1, self.window_max_y + self.world_y_compare_window + 1)
self.bottom_wall_middle = Wall(player_start_x, self.window_max_y + self.world_y_compare_window + 1)
self.bottom_wall_middle_right = Wall(player_start_x + 4, self.window_max_y + self.world_y_compare_window + 1)
self.bottom_wall_middle_left = Wall(player_start_x - 4, self.window_max_y + self.world_y_compare_window + 1)
self.bottom_wall_right = Wall(self.window_max_x + self.world_x_compare_window + 1, self.window_max_y + self.world_y_compare_window + 1)
self.bottom_wall_left = Wall(-self.world_x_compare_window - 1, self.window_max_y + self.world_y_compare_window + 1)
self.top_wall_middle = Wall(player_start_x, -self.world_y_compare_window - 1)
self.top_wall_middle_right = Wall(player_start_x + 4, -self.world_y_compare_window - 1)
self.top_wall_middle_left = Wall(player_start_x - 4, -self.world_y_compare_window - 1)
self.top_wall_right = Wall(self.window_max_x + self.world_x_compare_window + 1, -self.world_y_compare_window - 1)
self.top_wall_left = Wall(-self.world_x_compare_window - 1, -self.world_y_compare_window - 1)
def _render(self, state):
camera = state.camera
# left wall
if camera.is_visible(self.left_wall_top.pos):
screen_pos = state.camera.to_screen_space(self.left_wall_top.pos)
for i in range(camera.height - 2):
if (screen_pos.y + i < camera.height - 1 and screen_pos.x > 0):
state.window.addch(screen_pos.y + i, screen_pos.x, '*')
elif camera.is_visible(self.left_wall_bottom.pos):
screen_pos = state.camera.to_screen_space(self.left_wall_bottom.pos)
for i in range(camera.height - 2):
if (screen_pos.y > i and screen_pos.x > 0):
state.window.addch(1 + i, screen_pos.x, '*')
elif camera.is_visible(self.left_wall_middle.pos):
screen_pos = state.camera.to_screen_space(self.left_wall_middle.pos)
for i in range(camera.height - 2):
if (screen_pos.x > 0):
state.window.addch(1 + i, screen_pos.x, '*')
# right wall
if camera.is_visible(self.right_wall_top.pos):
screen_pos = state.camera.to_screen_space(self.right_wall_top.pos)
for i in range(camera.height - 2):
if (screen_pos.y + i < camera.height - 1 and screen_pos.x < self.window_max_x - 1):
state.window.addch(screen_pos.y + i, screen_pos.x, '*')
elif camera.is_visible(self.right_wall_bottom.pos):
screen_pos = state.camera.to_screen_space(self.right_wall_bottom.pos)
for i in range(camera.height - 2):
if (screen_pos.y > i and screen_pos.x < self.window_max_x - 1):
state.window.addch(1 + i, screen_pos.x, '*')
elif camera.is_visible(self.right_wall_middle.pos):
screen_pos = state.camera.to_screen_space(self.right_wall_middle.pos)
for i in range(camera.height - 2):
if (screen_pos.x < self.window_max_x - 1):
state.window.addch(1 + i, screen_pos.x, '*')
# bottom wall
if camera.is_visible(self.bottom_wall_right.pos):
screen_pos = state.camera.to_screen_space(self.bottom_wall_right.pos)
for i in range(camera.width - 2):
if (1 + i < screen_pos.x and screen_pos.y < self.window_max_y - 1):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.bottom_wall_left.pos):
screen_pos = state.camera.to_screen_space(self.bottom_wall_left.pos)
for i in range(camera.width - 2):
if (screen_pos.x + i < (camera.width - 2) and screen_pos.y < self.window_max_y - 1):
state.window.addch(screen_pos.y, screen_pos.x + i, '*')
elif camera.is_visible(self.bottom_wall_middle.pos):
screen_pos = state.camera.to_screen_space(self.bottom_wall_middle.pos)
for i in range(camera.width - 2):
if (screen_pos.y < self.window_max_y - 1):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.bottom_wall_middle_right.pos):
screen_pos = state.camera.to_screen_space(self.bottom_wall_middle_right.pos)
for i in range(camera.width - 2):
if (screen_pos.y < self.window_max_y - 1):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.bottom_wall_middle_left.pos):
screen_pos = state.camera.to_screen_space(self.bottom_wall_middle_left.pos)
for i in range(camera.width - 2):
if (screen_pos.y < self.window_max_y - 1):
state.window.addch(screen_pos.y, 1 + i, '*')
# top wall
if camera.is_visible(self.top_wall_right.pos):
screen_pos = state.camera.to_screen_space(self.top_wall_right.pos)
for i in range(camera.width - 2):
if (1 + i < screen_pos.x and screen_pos.y > 0):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.top_wall_left.pos):
screen_pos = state.camera.to_screen_space(self.top_wall_left.pos)
for i in range(camera.width - 2):
if (screen_pos.x + i < (camera.width - 2) and screen_pos.y > 0):
state.window.addch(screen_pos.y, screen_pos.x + i, '*')
elif camera.is_visible(self.top_wall_middle.pos):
screen_pos = state.camera.to_screen_space(self.top_wall_middle.pos)
for i in range(camera.width - 2):
if (screen_pos.y > 0):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.top_wall_middle_right.pos):
screen_pos = state.camera.to_screen_space(self.top_wall_middle_right.pos)
for i in range(camera.width - 2):
if (screen_pos.y > 0):
state.window.addch(screen_pos.y, 1 + i, '*')
elif camera.is_visible(self.top_wall_middle_left.pos):
screen_pos = state.camera.to_screen_space(self.top_wall_middle_left.pos)
for i in range(camera.width - 2):
if (screen_pos.y > 0):
state.window.addch(screen_pos.y, 1 + i, '*')
else:
return
# keeps track of the current 'camera' position over the 2d world in world space
class Camera:
def __init__(self, start_x, start_y, width, height):
# indicates how large our 'view port' is
self.width = width
self.height = height
self.start_x = start_x
self.start_y = start_y
self.x = start_x
self.y = start_y
def is_visible(self, world_pos):
# rectangle in screen_space around camera first
rect = get_rect_from_center(self.x, self.y, self.width, self.height)
# determine if the world_pos is in that rectangle
return point_in_rect(rect[0][0], rect[0][1], rect[1][0], rect[1][1], world_pos.x, world_pos.y)
def to_screen_space(self, world_pos):
# finds the coordinates of the object in the screen space
offSet_x = self.x - world_pos.x
offSet_y = self.y - world_pos.y
# center of the screen
half_x = int(self.width / 2)
half_y = int(self.height / 2)
return ScreenPos(half_x - offSet_x, half_y - offSet_y)
# commands that the user can use in the game
class GameCommands:
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
SHOOT = 5
TOGGLE_DEBUG_INFO = 6
QUIT = 7
CAMERA_RIGHT = 8
CAMERA_LEFT = 9
CAMERA_UP = 10
CAMERA_DOWN = 11
def __init__(self): pass
def on_up(self, state):
player = state.world.player
camera = state.camera
monsters = state.world.monsters
trees = state.world.trees
common = state.world.common_tools
if (player.y > (-state.world_y_compare_window)):
for i in range(len(trees)):
if (not common.object_clash(trees[i], player.x, (player.y - 1))):
return
camera.y -= 1
player.y -= 1
for i in range(len(monsters)):
if (camera.is_visible(monsters[i]) and (monsters[i].x == player.x)):
if (monsters[i].y == player.y and monsters[i].x == player.x):
return
elif (monsters[i].y > player.y):
for j in range(len(trees)):
if ((trees[j].y == monsters[i].y - 1 and trees[j].x == monsters[i].x)):
return
monsters[i].y -= 1
else:
for k in range(len(trees)):
if ((trees[k].y == monsters[i].y + 1 and trees[k].x == monsters[i].x)):
return
monsters[i].y += 1
def on_left(self, state):
player = state.world.player
camera = state.camera
monsters = state.world.monsters
trees = state.world.trees
common = state.world.common_tools
if (player.x > (-state.world_x_compare_window)):
for i in range(len(trees)):
if (not common.object_clash(trees[i], (player.x - 1), player.y)):
return
camera.x -= 1
player.x -= 1
for i in range(len(monsters)):
if (camera.is_visible(monsters[i]) and (monsters[i].y == player.y)):
if (monsters[i].y == player.y and monsters[i].x == player.x):
return
elif (monsters[i].x > player.x):
for j in range(len(trees)):
if (trees[j].x == monsters[i].x - 1 and trees[j].y == monsters[i].y):
return
monsters[i].x -= 1
else:
for k in range(len(trees)):
if (trees[k].x == monsters[i].x + 1 and trees[k].y == monsters[i].y):
return
monsters[i].x += 1
def on_down(self, state):
camera = state.camera
player = state.world.player
monsters = state.world.monsters
trees = state.world.trees
common = state.world.common_tools
if (player.y < (state.world_max_y - state.world_y_compare_window)):
for i in range(len(trees)):
if (not common.object_clash(trees[i], player.x, (player.y + 1))):
return
camera.y += 1
player.y += 1
for i in range(len(monsters)):
if (camera.is_visible(monsters[i]) and (monsters[i].x == player.x)):
if (monsters[i].y == player.y and monsters[i].x == player.x):
return
elif (monsters[i].y > player.y):
for j in range(len(trees)):
if (trees[j].y == monsters[i].y - 1 and trees[j].x == monsters[i].x):
return
monsters[i].y -= 1
else:
for k in range(len(trees)):
if (trees[k].y == monsters[i].y + 1 and trees[k].x == monsters[i].x):
return
monsters[i].y += 1
def on_right(self, state):
camera = state.camera
player = state.world.player
monsters = state.world.monsters
trees = state.world.trees
common = state.world.common_tools
if (player.x < (state.world_max_x - state.world_x_compare_window)):
for i in range(len(trees)):
if (not common.object_clash(trees[i], (player.x + 1), player.y)):
return
camera.x += 1
player.x += 1
for i in range(len(monsters)):
if (camera.is_visible(monsters[i]) and (monsters[i].y == player.y)):
if (monsters[i].y == player.y and monsters[i].x == player.x):
return
elif (monsters[i].x > player.x):
for j in range(len(trees)):
if (trees[j].x == monsters[i].x - 1 and trees[j].y == monsters[i].y):
return
monsters[i].x -= 1
else:
for k in range(len(trees)):
if (trees[k].x == monsters[i].x + 1 and trees[k].y == monsters[i].y):
return
monsters[i].x += 1
def on_shoot(self, state):
world = state.world
player = state.world.player
world.bullet = Bullet(player.x, player.y)
def toggle_debug_info(self, state):
state.debug_info.show = not state.debug_info.show
def quit(self, state):
state.quit = True
def camera_right(self, state):
state.camera.x += 1
def camera_left(self, state):
state.camera.x -= 1
def camera_up(self, state):
state.camera.y -= 1
def camera_down(self, state):
state.camera.y += 1
# keeps track of variables that change frequently due to user choice, like user coordinate and etc.
class GameState:
def __init__(self, window_max_y, window_max_x, world_max_y, world_max_x):
self.quit = False
self.start = 0
default_start_x = int(window_max_x / 2)
default_start_y = int(window_max_y / 2)
self.window_max_x = window_max_x
self.window_max_y = window_max_y
self.world_max_x = world_max_x
self.world_max_y = world_max_y
self.world_x_compare_window = (world_max_x - window_max_x)/2
self.world_y_compare_window = (world_max_y - window_max_y)/2
self.world = World(default_start_x, default_start_y, world_max_x, world_max_y, self.world_x_compare_window, self.world_y_compare_window, window_max_x, window_max_y)
self.window = None
self.stats = Statistics()
self.renderer = Renderer()
self.camera = Camera(int(window_max_x / 2), int(window_max_y / 2), window_max_x, window_max_y)
self.debug_info = DebugInfo()
self.game_commands = GameCommands()
self.commands = {
ord('w') : GameCommands.UP,
ord('a') : GameCommands.LEFT,
ord('s') : GameCommands.DOWN,
ord('d') : GameCommands.RIGHT,
ord(' ') : GameCommands.SHOOT,
ord('m') : GameCommands.TOGGLE_DEBUG_INFO,
ord('q') : GameCommands.QUIT,
ord('k') : GameCommands.CAMERA_RIGHT,
ord('h') : GameCommands.CAMERA_LEFT,
ord('u') : GameCommands.CAMERA_UP,
ord('j') : GameCommands.CAMERA_DOWN,
}
self.command_handlers = {
GameCommands.UP : self.game_commands.on_up,
GameCommands.LEFT : self.game_commands.on_left,
GameCommands.DOWN : self.game_commands.on_down,
GameCommands.RIGHT : self.game_commands.on_right,
GameCommands.SHOOT : self.game_commands.on_shoot,
GameCommands.TOGGLE_DEBUG_INFO : self.game_commands.toggle_debug_info,
GameCommands.QUIT : self.game_commands.quit,
GameCommands.CAMERA_RIGHT : self.game_commands.camera_right,
GameCommands.CAMERA_LEFT : self.game_commands.camera_left,
GameCommands.CAMERA_UP : self.game_commands.camera_up,
GameCommands.CAMERA_DOWN: self.game_commands.camera_down,
}
self.pending_commands = list()
def run(self):
while not self.quit:
player = self.world.player
monsters = self.world.monsters
for i in range(len(monsters)):
if (monsters[i].x == player.x and monsters[i].y == player.y):
curses.flash()
self.stats.lives_state(self, self.renderer, i)
self.read_input()
self.process_game_moves()
self.run_bullet_manager()
self.renderer.render(self)
def read_input(self):
c = self.window.getch()
command_id = self.commands.get(c)
if not command_id:
return # error handling later! (this key was invalid)
self.pending_commands.append(command_id)
def process_game_moves(self):
for command_id in self.pending_commands:
command_handler = self.command_handlers.get(command_id)
if not command_handler:
continue # error handling later! (this command SHOUlD have been there!)
command_handler(self)
self.pending_commands.clear()
def run_bullet_manager(self):
if not self.world.bullet:
return
self.world.bullet.simulate(self)
if self.world.bullet.shots_remaining < 0:
self.world.bullet = None
# keeps track of the user posititon in the world
class DebugInfo():
def __init__(self):
self.show = False
def _render(self, state):
player = state.world.player
if self.show:
state.window.addstr(0, 17, f"wpos:{player.y}, {player.x}")
# stats of the user in the game
class Statistics:
def __init__(self):
self.score = 0
self.health = 3
def incr_score(self):
self.score += 1
def decr_health(self):
self.health -= 1
def lives_state(self, state, renderer, monsterNo):
window = state.window
monsters = state.world.monsters
common = state.world.common_tools
self.decr_health()
if (self.health != 0):
common.random_area_on_map(monsters[monsterNo], state)
renderer.render(state)
if (self.health == 0):
renderer.render(state)
window.addstr(2, 10, f"YOU LOSE!")
window.refresh()
time.sleep(1.5)
exit()
def _render(self, state):
window = state.window
window.addstr(0, 9, f"Score:{self.score}")
window.addstr(0, 1, f"Lives:{self.health}")
window.addstr(state.window_max_y - 1, 1, "Monster may dodge bullet!")
# bullet operations of the user
class Bullet(WorldObject):
def __init__(self, start_x, start_y):
super().__init__(start_x, start_y)
self.x_count = 1
self.shots_remaining = 3
self.last_sim_time = time.time()
self.frame_count = .1
def _render(self, state):
camera = state.camera
if camera.is_visible(self.pos):
screen_pos = state.camera.to_screen_space(self.pos)
state.window.addch(screen_pos.y, screen_pos.x, 'a')
else:
return
def simulate(self, state):
world = state.world
stats = state.stats
monsters = state.world.monsters
common = state.world.common_tools
now = time.time()
if (now - self.last_sim_time) <= .5:
return
temp_x = (self.start_x + self.x_count)
if ((temp_x) < (world.max_x - 1)):
self.x = temp_x
for i in range(len(monsters)):
if (monsters[i].y == self.y and monsters[i].x == self.x):
common.random_area_on_map(monsters[i], state)
curses.flash()
stats.incr_score()
self.x_count += 1
self.shots_remaining -= 1
self.last_sim_time = now
# monster variable in the game
class Monster(WorldObject):
def __init__(self, start_x, start_y):
super().__init__(start_x, start_y)
# renders the monster on the screen
def _render(self, state):
common = state.world.common_tools
common.render_object(self, state, 'T')
# tree variable in the game
class Tree(WorldObject):
def __init__(self, start_x, start_y):
super().__init__(start_x, start_y)
def _render(self, state):
common = state.world.common_tools
common.render_object(self, state, '@')
# tools used over and over again mostly in object classes
class CommonTools:
def __init__(self):
pass
def object_clash(self, currObject, otherObject_x, otherObject_y):
if(currObject.x == otherObject_x and currObject.y == otherObject_y):
return False
return True
def render_object(self, object, state, char):
camera = state.camera
if camera.is_visible(object.pos):
screen_pos = state.camera.to_screen_space(object.pos)
state.window.addch(screen_pos.y, screen_pos.x, char)
else:
return
def random_area_on_map(self, object, state):
world = state.world
player = state.world.player
camera = state.camera
x_boundary = world.world_x_compare_window + camera.width
y_boundary = world.world_y_compare_window + camera.height
object.y = random.randint(-world.world_y_compare_window, y_boundary)
object.x = random.randint(-world.world_x_compare_window, x_boundary)
if (object.y == player.y):
object.y = random.randint(-world.world_y_compare_window, y_boundary)
while (object.y == player.y):
object.y = random.randint(-world.world_y_compare_window, y_boundary)
elif (object.x == player.x):
object.x = random.randint(-world.world_x_compare_window, x_boundary)
while (object.x == player.x):
object.x = random.randint(-world.world_x_compare_window, x_boundary)
# main operations of actually calling the classes and such for the game to execute
def main():
curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
window_width = 40
window_height = 10
world_width = 80
world_height = 12
game_state = GameState(window_height, window_width, world_height, world_width)
game_state.window = curses.newwin(window_height, window_width)
game_state.window.nodelay(True)
render = Renderer()
render.render(game_state)
game_state.run()
curses.nocbreak()
curses.echo()
curses.endwin()
if __name__ == "__main__":
main()