Skip to content

Commit 365040f

Browse files
committed
Merge branch 'dev' into GH-2_computer-players
# Conflicts: # project/game/model.py
2 parents 50854d2 + fb9f9f8 commit 365040f

File tree

14 files changed

+413
-185
lines changed

14 files changed

+413
-185
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Custom Ignores
2+
data/saved
3+
14
# Pycharm IDE
25
.idea
36

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "docs"]
2+
path = docs
3+
url = https://github.com/Ben-Ryder/Conqueror-of-Empires.wiki.git

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Conqueror of Empires is a turn-based strategy game where up to 4 local players b
44
The game was inspired by [Polytopia](http://midjiwan.com/polytopia.html) and [Civilisation](https://civilization.com/) and made using python and pygame.
55
To download executables see [releases](https://github.com/Ben-Ryder/Conqueror-of-Empires/releases).
66

7+
***
8+
**v1.1**: Release version 1.1 changes the data save format to JSON.
9+
***
10+
711
## Playing the Game
812
#### From Source:
913
Run the `main.py` file from the repository root.

constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
version = subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE).stdout.decode("utf-8")
1313
assert version != ""
1414
except Exception: # seems to be so dependent on system and versions, easier to do a catch all
15-
version = "v1.0" # git not installed, or older lib version, so revert to hardcoded version
15+
version = "v1.1" # git not installed, or older lib version, so revert to hardcoded version
1616

1717

1818
# configuration for pygame.display

data/saved/.gitignore

-2
This file was deleted.

docs

Submodule docs added at 48926a6

paths.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
dataPath = os.getcwd() + os.sep + "data" + os.sep
66
assetPath = os.getcwd() + os.sep + "assets" + os.sep
77

8-
gamePath = dataPath + os.sep + "saved" + os.sep
9-
mapPath = dataPath + os.sep + "maps" + os.sep
8+
gamePath = dataPath + "saved" + os.sep
9+
mapPath = dataPath + "maps" + os.sep
1010

11-
fontPath = assetPath + os.sep + "fonts" + os.sep
11+
fontPath = assetPath + "fonts" + os.sep
1212
imagePath = assetPath + "images" + os.sep
1313

14-
tilePath = imagePath + os.sep + "tiles" + os.sep
15-
unitPath = imagePath + os.sep + "units" + os.sep
14+
tilePath = imagePath + "tiles" + os.sep
15+
unitPath = imagePath + "units" + os.sep
1616

17-
uiPath = imagePath + os.sep + "UI" + os.sep # general, throughout sections.
18-
uiMenuPath = imagePath + os.sep + "menu" + os.sep
19-
uiGamePath = imagePath + os.sep + "game" + os.sep
17+
uiPath = imagePath + "UI" + os.sep # general, throughout sections.
18+
uiMenuPath = imagePath + "menu" + os.sep
19+
uiGamePath = imagePath + "game" + os.sep
2020

2121

project/data.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
# Ben-Ryder 2019
2-
# Currently using PICKLE as method of file save
2+
# Currently using JSON as method of file save
33

4-
import pickle
4+
import json
55
import os
66

7+
import constants
78

8-
def save(game, filename):
9-
with open(filename, "wb") as file:
10-
pickle.dump(game, file)
9+
10+
def save(data, filename):
11+
with open(filename + ".json", "w") as file:
12+
json.dump(data, file, indent=2)
1113

1214

1315
def load(filename):
14-
with open(filename, "rb") as file:
15-
game = pickle.load(file)
16-
return game
16+
with open(filename + ".json", "r") as file:
17+
data = json.load(file)
18+
return data
1719

1820

1921
def delete(filename):
20-
os.remove(filename)
22+
os.remove(filename + ".json")
2123

2224

2325
def check_exists(filename):
2426
return os.path.isfile(filename)
27+
28+
29+
def load_map_format(map_file):
30+
with open(map_file, "r") as file:
31+
grid = file.read().split("\n")
32+
grid = [i.replace(",", "") for i in grid]
33+
34+
# Converting for referencing as [row][col] as split by "/n" gives [col][row]
35+
new_grid = []
36+
for row in range(constants.MAP_SIZE[0]):
37+
new_grid.append([])
38+
for col in grid:
39+
new_grid[len(new_grid) - 1].append(col[row])
40+
41+
return new_grid

project/game/controller.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pygame
44

5+
import project.game.model as model
56
import project.game.gui as gui
67
import project.menus.leaderboard as leaderboard
78
import project.data as data
@@ -15,8 +16,9 @@ def __init__(self, display, game_reference):
1516
self.display = display
1617
self.game_reference = game_reference
1718

18-
# General Game Setup
19-
self.game_model = data.load(paths.gamePath + self.game_reference)
19+
# Game Model Setup
20+
save_data = data.load(paths.gamePath + self.game_reference)
21+
self.game_model = model.Model(save_data)
2022

2123
# View + GUI Setup
2224
self.GUI = gui.GameGui(self, self.display, self.game_model)
@@ -39,7 +41,7 @@ def quit(self):
3941
self.save()
4042

4143
def save(self):
42-
data.save(self.game_model, paths.gamePath + self.game_reference)
44+
data.save(self.game_model.get_save_data(), paths.gamePath + self.game_reference)
4345

4446
def get_state(self):
4547
return self.state

project/game/gui.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, control, display, model):
5656
self.passive_guis = [] # acts as normal list
5757

5858
# Welcome Message
59-
if self.model_link.current_player.get_turn() == 0:
59+
if self.model_link.get_current_player().get_turn() == 0:
6060
self.launch_welcome_message()
6161

6262
def run(self):
@@ -171,8 +171,9 @@ def launch_welcome_message(self):
171171
self.persistent_guis.append(WelcomeMessage(self))
172172

173173
def next_turn_message(self):
174+
next_player = self.model_link.get_player(self.model_link.get_next_player())
174175
self.persistent_guis.append(NextTurnMessage(self, "Next Turn",
175-
["Get ready %s!" % self.model_link.get_next_player().get_name(),
176+
["Get ready %s!" % next_player.get_name(),
176177
"It's your turn up next."]))
177178

178179
def game_over_message(self):
@@ -204,13 +205,13 @@ def update(self):
204205
self.game_view.world.refresh()
205206

206207
def save(self):
207-
self.model_link.current_player.set_camera_focus(self.camera.get_position())
208+
self.model_link.get_current_player().set_camera_focus(self.camera.get_position())
208209
self.control_link.save()
209210

210211
def update_camera_focus(self):
211-
if self.model_link.current_player.get_camera_focus() != [None, None]: # not set until end of first turn.
212+
if self.model_link.get_current_player().get_camera_focus() != [None, None]: # not set until end of first turn.
212213
# TODO: set initial camera_focus on settlement assignment, so this isn't needed.
213-
self.camera.set_position(self.model_link.current_player.get_camera_focus())
214+
self.camera.set_position(self.model_link.get_current_player().get_camera_focus())
214215

215216
def end_game(self): # natural end, with a winner not quit.
216217
self.state = "ended"
@@ -346,7 +347,7 @@ def check_clicked(self, mouse_x, mouse_y):
346347
def draw(self, display):
347348
self.background_panel.draw(display)
348349

349-
pygame.draw.ellipse(display, constants.COLOURS[self.model_link.current_player.get_colour()],
350+
pygame.draw.ellipse(display, constants.COLOURS[self.model_link.get_current_player().get_colour()],
350351
[self.x + 10, self.y + 5, 15, 15])
351352
self.city_name_text.draw(display)
352353

@@ -788,19 +789,20 @@ def __init__(self, model):
788789
self.player_values_panel.rect[0] + 180, 2)
789790

790791
def update_player(self):
791-
self.current_player_name_text.change_text(self.model_link.current_player.get_name() + "'s turn")
792+
current_player = self.model_link.get_current_player()
793+
self.current_player_name_text.change_text(current_player.get_name() + "'s turn")
792794
self.topleft_panel.reset_width(self.name_padding + self.current_player_name_text.get_rect()[2])
793-
self.current_turn_text.change_text(str(self.model_link.current_player.get_turn()))
795+
self.current_turn_text.change_text(str(current_player.get_turn()))
794796

795-
self.update_player_score()
796-
self.update_player_ap()
797+
self.update_player_score(current_player)
798+
self.update_player_ap(current_player)
797799

798-
def update_player_score(self):
799-
self.current_score_text.change_text("{:,}".format(self.model_link.current_player.get_score()))
800+
def update_player_score(self, player):
801+
self.current_score_text.change_text("{:,}".format(player.get_score()))
800802

801-
def update_player_ap(self):
802-
ap_gain = " (+" + str(self.model_link.current_player.get_turn_ap()) + ")"
803-
self.current_ap_text.change_text(str(self.model_link.current_player.get_ap()) + ap_gain)
803+
def update_player_ap(self, player):
804+
ap_gain = " (+" + str(player.get_turn_ap()) + ")"
805+
self.current_ap_text.change_text(str(player.get_ap()) + ap_gain)
804806

805807
def draw(self, display):
806808
self.topleft_panel.draw(display)
@@ -937,7 +939,8 @@ def __init__(self, model):
937939
rect = [x + self.padding, y + self.padding,
938940
self.tile_size - self.padding*2, self.tile_size - self.padding*2]
939941
if tile.current_holder is not None:
940-
colour = tile.current_holder.get_colour()
942+
player = self.model_link.get_player(tile.current_holder)
943+
colour = player.get_colour()
941944
self.tiles.append(MiniMapTile(rect, constants.COLOURS[colour]))
942945
else:
943946
self.tiles.append(MiniMapTile(rect, (200, 200, 200)))
@@ -961,15 +964,15 @@ def refresh(self):
961964
self.__init__(self.model_link)
962965

963966
def is_visible(self):
964-
return self.model_link.current_player.get_minimap_status()
967+
return self.model_link.get_current_player().get_minimap_status()
965968

966969
def handle_click(self):
967970
if self.is_visible():
968971
if self.hide_button.check_clicked():
969-
self.model_link.current_player.set_minimap_status(False)
972+
self.model_link.get_current_player().set_minimap_status(False)
970973
else:
971974
if self.show_button.check_clicked():
972-
self.model_link.current_player.set_minimap_status(True)
975+
self.model_link.get_current_player().set_minimap_status(True)
973976

974977
def draw(self, display):
975978
if self.is_visible():

0 commit comments

Comments
 (0)