Skip to content

Commit

Permalink
Merge pull request #21 from Ben-Ryder/develop
Browse files Browse the repository at this point in the history
v1.1 - Changing data save format to JSON.
  • Loading branch information
ben-ryder authored Sep 11, 2021
2 parents e1b1229 + 0ddf72f commit 2d6e7ca
Show file tree
Hide file tree
Showing 31 changed files with 392 additions and 242 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The game was inspired by [Polytopia](http://midjiwan.com/polytopia.html) and [Ci
To download executables see [releases](https://github.com/Ben-Ryder/Conqueror-of-Empires/releases).

***
**v1.0.1**: Release version 1.0.1 brings a proper fix for game crashes where the `saved` directory doesn't exist.
**v1.1**: Release version 1.1 changes the data save format to JSON.
***

## Playing the Game
Expand Down
4 changes: 1 addition & 3 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Ben-Ryder 2019

import sys
import subprocess
import paths
Expand All @@ -12,7 +10,7 @@
version = subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE).stdout.decode("utf-8")
assert version != ""
except Exception: # seems to be so dependent on system and versions, easier to do a catch all
version = "v1.0" # git not installed, or older lib version, so revert to hardcoded version
version = "v1.1" # git not installed, or older lib version, so revert to hardcoded version


# configuration for pygame.display
Expand Down
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# Ben-Ryder 2019

import logging
logging.basicConfig(filename='main.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
Expand Down
4 changes: 0 additions & 4 deletions paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Ben-Ryder 2019

import os

dataPath = os.getcwd() + os.sep + "data" + os.sep
Expand All @@ -17,5 +15,3 @@
uiPath = imagePath + "UI" + os.sep # general, throughout sections.
uiMenuPath = imagePath + "menu" + os.sep
uiGamePath = imagePath + "game" + os.sep


2 changes: 0 additions & 2 deletions project/background.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Ben-Ryder 2019

import pygame
import random

Expand Down
2 changes: 0 additions & 2 deletions project/control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# Ben-Ryder 2019

from project.control.controller import *
2 changes: 0 additions & 2 deletions project/control/controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Ben-Ryder 2019

import constants
import paths

Expand Down
36 changes: 26 additions & 10 deletions project/data.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
# Ben-Ryder 2019
# Currently using PICKLE as method of file save
# Currently using JSON as method of file save

import pickle
import json
import os

import constants

def save(game, filename):
with open(filename, "wb") as file:
pickle.dump(game, file)

def save(data, filename):
with open(filename + ".json", "w") as file:
json.dump(data, file, indent=2)


def load(filename):
with open(filename, "rb") as file:
game = pickle.load(file)
return game
with open(filename + ".json", "r") as file:
data = json.load(file)
return data


def delete(filename):
os.remove(filename)
os.remove(filename + ".json")


def check_exists(filename):
return os.path.isfile(filename)


def load_map_format(map_file):
with open(map_file, "r") as file:
grid = file.read().split("\n")
grid = [i.replace(",", "") for i in grid]

# Converting for referencing as [row][col] as split by "/n" gives [col][row]
new_grid = []
for row in range(constants.MAP_SIZE[0]):
new_grid.append([])
for col in grid:
new_grid[len(new_grid) - 1].append(col[row])

return new_grid
2 changes: 0 additions & 2 deletions project/game/calculations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Ben-Ryder 2019
# Currently a direct subtraction of attack from health


def apply_attack(attacker, defender):
defender.health -= attacker.attack

Expand Down
10 changes: 5 additions & 5 deletions project/game/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Ben-Ryder 2019

import pygame

import project.game.model as model
import project.game.gui as gui
import project.menus.leaderboard as leaderboard
import project.data as data
Expand All @@ -15,8 +14,9 @@ def __init__(self, display, game_reference):
self.display = display
self.game_reference = game_reference

# General Game Setup
self.game_model = data.load(paths.gamePath + self.game_reference)
# Game Model Setup
save_data = data.load(paths.gamePath + self.game_reference)
self.game_model = model.Model(save_data)

# View + GUI Setup
self.GUI = gui.GameGui(self, self.display, self.game_model)
Expand All @@ -39,7 +39,7 @@ def quit(self):
self.save()

def save(self):
data.save(self.game_model, paths.gamePath + self.game_reference)
data.save(self.game_model.get_save_data(), paths.gamePath + self.game_reference)

def get_state(self):
return self.state
42 changes: 22 additions & 20 deletions project/game/gui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Ben-Ryder 2019
import pygame
import time

Expand Down Expand Up @@ -56,7 +55,7 @@ def __init__(self, control, display, model):
self.passive_guis = [] # acts as normal list

# Welcome Message
if self.model_link.current_player.get_turn() == 0:
if self.model_link.get_current_player().get_turn() == 0:
self.launch_welcome_message()

def run(self):
Expand Down Expand Up @@ -171,8 +170,9 @@ def launch_welcome_message(self):
self.persistent_guis.append(WelcomeMessage(self))

def next_turn_message(self):
next_player = self.model_link.get_player(self.model_link.get_next_player())
self.persistent_guis.append(NextTurnMessage(self, "Next Turn",
["Get ready %s!" % self.model_link.get_next_player().get_name(),
["Get ready %s!" % next_player.get_name(),
"It's your turn up next."]))

def game_over_message(self):
Expand All @@ -199,13 +199,13 @@ def next_turn(self):
self.save()

def save(self):
self.model_link.current_player.set_camera_focus(self.camera.get_position())
self.model_link.get_current_player().set_camera_focus(self.camera.get_position())
self.control_link.save()

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

def end_game(self): # natural end, with a winner not quit.
self.state = "ended"
Expand Down Expand Up @@ -341,7 +341,7 @@ def check_clicked(self, mouse_x, mouse_y):
def draw(self, display):
self.background_panel.draw(display)

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

Expand Down Expand Up @@ -784,19 +784,20 @@ def __init__(self, model):
self.player_values_panel.rect[0] + 180, 2)

def update_player(self):
self.current_player_name_text.change_text(self.model_link.current_player.get_name() + "'s turn")
current_player = self.model_link.get_current_player()
self.current_player_name_text.change_text(current_player.get_name() + "'s turn")
self.topleft_panel.reset_width(self.name_padding + self.current_player_name_text.get_rect()[2])
self.current_turn_text.change_text(str(self.model_link.current_player.get_turn()))
self.current_turn_text.change_text(str(current_player.get_turn()))

self.update_player_score()
self.update_player_ap()
self.update_player_score(current_player)
self.update_player_ap(current_player)

def update_player_score(self):
self.current_score_text.change_text("{:,}".format(self.model_link.current_player.get_score()))
def update_player_score(self, player):
self.current_score_text.change_text("{:,}".format(player.get_score()))

def update_player_ap(self):
ap_gain = " (+" + str(self.model_link.current_player.get_turn_ap()) + ")"
self.current_ap_text.change_text(str(self.model_link.current_player.get_ap()) + ap_gain)
def update_player_ap(self, player):
ap_gain = " (+" + str(player.get_turn_ap()) + ")"
self.current_ap_text.change_text(str(player.get_ap()) + ap_gain)

def draw(self, display):
self.topleft_panel.draw(display)
Expand Down Expand Up @@ -933,7 +934,8 @@ def __init__(self, model):
rect = [x + self.padding, y + self.padding,
self.tile_size - self.padding*2, self.tile_size - self.padding*2]
if tile.current_holder is not None:
colour = tile.current_holder.get_colour()
player = self.model_link.get_player(tile.current_holder)
colour = player.get_colour()
self.tiles.append(MiniMapTile(rect, constants.COLOURS[colour]))
else:
self.tiles.append(MiniMapTile(rect, (200, 200, 200)))
Expand All @@ -957,15 +959,15 @@ def refresh(self):
self.__init__(self.model_link)

def is_visible(self):
return self.model_link.current_player.get_minimap_status()
return self.model_link.get_current_player().get_minimap_status()

def handle_click(self):
if self.is_visible():
if self.hide_button.check_clicked():
self.model_link.current_player.set_minimap_status(False)
self.model_link.get_current_player().set_minimap_status(False)
else:
if self.show_button.check_clicked():
self.model_link.current_player.set_minimap_status(True)
self.model_link.get_current_player().set_minimap_status(True)

def draw(self, display):
if self.is_visible():
Expand Down
2 changes: 0 additions & 2 deletions project/game/isometric.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Ben-Ryder 2019

import constants


Expand Down
Loading

0 comments on commit 2d6e7ca

Please sign in to comment.