-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
167 lines (144 loc) · 6.33 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
import numpy as np
import os
import pygame
import constants
from game import Game
from game_map import GameMap
from player import Player
from player import PlayerState
os.environ["SDL_VIDEO_CENTERED"] = "1"
class MyGame:
def __init__(self, map_number: int):
pygame.init()
self.window = pygame.display.set_mode(constants.window_size)
pygame.display.set_caption("Car Game")
pygame.display.set_icon(pygame.image.load("image/car_game_icon.png"))
self.clock = pygame.time.Clock()
self.running = True
self.position_grid = np.genfromtxt("positions/position"+str(map_number)+".csv", dtype=int, delimiter=",")
self.game = Game(map_number)
self.collision_help = True
self.turn_count = 0
def process_input(self, player: Player):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
break
elif event.key == pygame.K_h:
self.collision_help = not self.collision_help
else:
if constants.default_inputs.get(event.key) is not None:
if not player.collision_speed_check(
self.game.game_map, constants.default_inputs.get(event.key), self.collision_help
):
player.speed += constants.default_inputs.get(event.key)
self.move_player(player)
break
def render(self):
self.window.fill((0, 0, 0))
self.game.draw(self.window, self.turn_count)
if self.collision_help:
pygame.draw.rect(self.window, (200, 200, 200), (constants.window_width - self.game.game_map.width, 0, 30, 30))
for player in self.game.player_list:
if (
player.collision_speed_check(self.game.game_map, np.array([0, 0]), self.collision_help)
and player.state_check(self.game.game_map) != PlayerState.IS_OUT
):
pygame.draw.polygon(
self.window, (200, 50, 50), [(80, 60), (40, 130), (120, 130)]
)
pygame.draw.polygon(
self.window, (255, 255, 255), [(80, 70), (48, 125), (112, 125)]
)
pygame.draw.rect(self.window, (0, 0, 0), (77, 85, 6, 20))
pygame.draw.circle(self.window, (0, 0, 0), (80, 115), 4)
pygame.display.update()
def run(self):
while self.running:
for player in self.game.player_list:
if player.number == self.turn_count:
if player.state_check(self.game.game_map) == PlayerState.IS_OUT:
player.has_played = True
else:
self.game.update(player)
self.render()
self.process_input(player)
if self.end_of_player(player, self.game.game_map):
continue
if not player.movement_validity():
continue
self.turn_count += 1
self.turn_count %= len(self.game.player_list)
self.game.player_state_reset()
self.end_of_game()
def move_player(self, player: Player):
if player.path_checking(self.game.game_map) == 1:
self.game.game_map.modify_tile_list_state(
player.get_walk_coordinates(), 2**player.number
)
player.plays()
elif player.path_checking(self.game.game_map) == 2:
player.is_out()
print("Game ending, processing results...")
elif player.path_checking(self.game.game_map) == 3:
print("Congratulations for reaching the end,", player.name, "!")
player.plays()
def end_of_game(self):
players_out = 0
players_won = 0
for player in self.game.player_list:
if player.state_check(self.game.game_map) == PlayerState.IS_OUT:
players_out += 1
elif player.state_check(self.game.game_map) == PlayerState.HAS_WON:
players_won += 1
if players_out == len(self.game.player_list):
self.running = False
print("Everyone is out, game is over!")
if players_won > 0:
self.running = False
def end_of_player(self, player: Player, game_map: GameMap):
"""Used to determine if the player should be taken out of the game due to impossibility to save himself.
Parameters
----------
game_map: GameMap
the map on which the player is evolving.
:return:
True if the player should be taken out of the game, False if he can still play.
"""
outcomes = []
for i, elem in constants.default_inputs.items():
outcomes.append(player.collision_speed_check(game_map, elem, self.collision_help))
if np.all(outcomes):
player.is_out()
return True
return False
can_start = False
while not can_start:
try:
player_count = int(input("Please chose how much player will play (max 8): "))
if not 0 < player_count < 9:
print("Invalid number. Please try again")
else:
can_start = True
map_number = int(input("On which map do you want to play? (Maps go from 1 to 5) "))
if not 0 < map_number < 6:
print("Not a valid map number, try again!")
can_start = False
except ValueError:
print("That's not a number, try again!")
can_start = False
name_list = []
for a in range(player_count):
name_list.append(input("Player #" + str(a + 1) + ", choose your name: "))
game = MyGame(map_number)
for a in range(player_count):
if a == 0:
game.game.new_player(a, name_list[a], game.position_grid[player_count - 1])
else:
game.game.new_player(a, name_list[a], game.position_grid[a - 1])
game.run()
pygame.quit()