Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring codebase to fix pylint issues. #32

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[MASTER]

# Custom pylint overrides for the project
disable=
too-many-arguments,
too-many-instance-attributes,
too-few-public-methods,
duplicate-code
70 changes: 36 additions & 34 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import sys
import subprocess
import paths
"""
A file to export constants used throughout the application.

This module exports:
VERSION - The game version of the current code.
DISPLAY_NAME - The window title.
DISPLAY_SIZE - The fixed size of the display.

MAP_SIZE - The x by y size of the game maps, in tiles.
TILE_WIDTH - The width of game tiles in px.
TILE_HEIGHT - The height of game tiles in px.

MAP_WIDTH - The total map width, worked out from the MAP_SIZE and TILE_WIDTH.
MAP_HEIGHT - The total map height, worked out from the MAP_SIZE and TILE_WIDTH.
MAP_PADDING - An arbitrary value for padding around the map.

GAME_RECT - The rect value ([x, y, width, height]) of the map, including padding.
ORIGIN - The exact top point of the isometric map.

COLOURS - A dictionary of colours used throughout the application.
FONTS - A dictionary of font information used in teh application.

# Dev Version Text (Tries for git version, if cant get it, revert to version saved here)
try:
if sys.version_info[0] < 3.5:
version = subprocess.check_output(["git", "describe", "--tags"]).strip()
else:
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.1.1" # git not installed, or older lib version, so revert to hardcoded version
UNIT_SPECS - The data for each unit in the game including health, attach, defense etc.
LEVELS - A matrix that manages how city level increases work.
"""

import paths

# Application version. Should always match repository tag.
VERSION = "v1.1.1"

# configuration for pygame.display
DISPLAY_NAME = "Conqueror of Empires"
Expand All @@ -27,11 +43,11 @@
MAP_HEIGHT = TILE_HEIGHT*MAP_SIZE[1]
MAP_PADDING = 20 # space between map and edge of game surface.

width = MAP_WIDTH + MAP_PADDING*2
height = MAP_HEIGHT + TILE_HEIGHT + MAP_PADDING*2
x = -MAP_WIDTH/2
y = -MAP_PADDING
GAME_RECT = [x, y, width, height] # x, y change with scroll anyway
WIDTH = MAP_WIDTH + MAP_PADDING * 2
HEIGHT = MAP_HEIGHT + TILE_HEIGHT + MAP_PADDING * 2
X = -MAP_WIDTH / 2
Y = -MAP_PADDING
GAME_RECT = [X, Y, WIDTH, HEIGHT] # x, y change with scroll anyway

ORIGIN = [GAME_RECT[2]/2 - TILE_HEIGHT + MAP_PADDING, MAP_PADDING] # top map point

Expand Down Expand Up @@ -59,18 +75,6 @@
"small": 12},
"colour": COLOURS["white"]}

# Game Data
TILE_DATA = {
"s": [0, 0, 0],
"w": [0, 0, 0],
"g": [0, 0, 0],
"f": [100, 20, 5],
"m": [10, 100, 20],
"o": [10, 100, 50],
"c": [100, 50, 25], # default of settlement store (level 1)
}


UNIT_SPECS = {
"scout": {
"max_health": 10,
Expand Down Expand Up @@ -124,9 +128,8 @@
},
}

# each item is level, each item in level is sub-level.
# item: len() = number of sub-levels to next level, value is ap cost to reach sub level/ len() = 0 means max level
# city level starts at 1, to reference level must do city_level - 1.
# Levels Matrix
# each list item represents a level, each item within a level list represents a sub-level.
LEVELS = [
[2, 2, 2], # 1 to 2
[2, 2, 2, 2], # 2 to 3
Expand All @@ -135,6 +138,5 @@
[], # 5 is max
]


# Cleanup unneeded to not pollute namespace.
del x, y, width, height, MAP_PADDING
del X, Y, WIDTH, HEIGHT, MAP_PADDING
32 changes: 21 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
#!/usr/bin/env python3

"""
The main entry point for the application.
"""

import logging
logging.basicConfig(filename='main.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
from project import control

try:
# Setting up logging
logging.basicConfig(
filename='main.log',
filemode='w',
format='%(name)s - %(levelname)s - %(message)s'
)

import project.control as control

def main():
controller = control.ApplicationController()
controller.run()
def main():
""" The main function to instantiate and run the application controller. """

controller = control.ApplicationController()
controller.run()

if __name__ == "__main__":
main()

except Exception as e:
logging.exception("caught at main")
raise e # personal choice, still want to see error in IDE
if __name__ == "__main__":
try:
main()
except Exception as error:
logging.exception("caught at main")
raise error # personal choice, still want to see error in IDE
18 changes: 18 additions & 0 deletions paths.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
"""
A constants file to export file paths used in the application.
This includes data and asset paths.

This module exports:
dataPath - The base folder that stores game data such as maps, game saves etc.
assetPath - The base folder that stores game assets.
gamePath - The folder that stores saved games.
mapPath - The folder that stores map files.
fontPath - The folder that stores font files used in the application.
imagePath - The base path that stores image assets used in the application.
tilePath - The folder that stores map tile images.
unitPath - The folder that stores unit images.
uiPath - The folder that stores general images related to the application UI.
uiMenuPath - The folder that stores UI images specifically for the menus.
uiGamePath - The folder that stores UI images specifically for use in the game.
"""

import os

dataPath = os.getcwd() + os.sep + "data" + os.sep
Expand Down
25 changes: 0 additions & 25 deletions project/game/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ class Tile:
def __init__(self, save_data):
self.type = save_data["type"]
self.position = save_data["position"]
# self.wood, self.stone, self.metal = constants.TILE_DATA[tile_type]

def get_save_data(self):
return {
Expand All @@ -303,30 +302,6 @@ def get_type(self):
def get_position(self):
return self.position

# def take_wood(self, amount=1): # defaults, left for future in case decide change.
# if self.wood > 0:
# self.wood = self.wood - amount
# if self.wood < 0:
# self.wood = 0 # ensures resource is fully used, but cant go negative.
# return True
# return False
#
# def take_stone(self, amount=1):
# if self.stone > 0:
# self.stone = self.stone - amount
# if self.stone < 0:
# self.stone = 0
# return True
# return False
#
# def take_metal(self, amount=1):
# if self.metal > 0:
# self.metal = self.metal - amount
# if self.metal < 0:
# self.metal = 0
# return True
# return False


class City:
def __init__(self, model_link, save_data):
Expand Down
2 changes: 1 addition & 1 deletion project/menus/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(self, GUI):
self.rect[0] + 5, self.rect[1] + 130)

self.version = pygame_gui.Text(
constants.version,
constants.VERSION,
constants.FONTS["sizes"]["medium"], constants.FONTS["colour"], constants.FONTS["main"],
self.rect[0] + 5, self.rect[1] + self.rect[3] - 20)

Expand Down
14 changes: 13 additions & 1 deletion pygame_gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
"""
A custom set of general UI elements that rely on external image assets for display.

Classes:
Text
Image
Button
Checkbox
Entry
Panel
TextButton
"""

from pygame_gui.text import *
from pygame_gui.image import *
from pygame_gui.button import *
from pygame_gui.checkbox import *
from pygame_gui.entry import *
from pygame_gui.cursor import *
from pygame_gui.panel import *
from pygame_gui.text_button import *
40 changes: 39 additions & 1 deletion pygame_gui/button.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
"""
A module holding the pygame_gui Button class.

Classes:
Button
"""

import pygame
import pygame_gui.image


class Button:
""" The Button class which changes display when hovered and runs a function when clicked. """
def __init__(self, rest_image, hover_image, x, y):
"""
Parameters:
rest_image - The default image which is used for the button.
hover_image - The image to use when the button is being hovered over.
x - The x position to use for the button, in px.
y - The y position to use for the button, in px
"""

self.rest_image = pygame_gui.Image(rest_image, x, y)
self.hover_image = pygame_gui.Image(hover_image, x, y)
self.rect = self.rest_image.image.get_rect()
Expand All @@ -12,21 +28,43 @@ def __init__(self, rest_image, hover_image, x, y):
self.function = None

def set_function(self, function):
"""
Used to set the function to be run when the button is clicked.

Parameters:
function - Should be callable as a function.
"""

self.function = function

def mouse_over(self):
""" Checks if the current mouse position is within the button's area. """
if self.rect.collidepoint(pygame.mouse.get_pos()):
return True
return False

def check_clicked(self):
"""
Uses Button.mouse_over to determine if the mouse is within the button's area.
If the mouse is within the button then Button.function will be ran if set.

NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
"""

if self.mouse_over():
if self.function is not None:
self.function()
return True
return False

def draw(self, display):
"""
Draws the button to the given pygame display.

Parameters:
display - A pygame.display instance.
"""

if self.mouse_over():
self.hover_image.draw(display)
else:
Expand Down
36 changes: 35 additions & 1 deletion pygame_gui/checkbox.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
"""
A module holding the pygame_gui Checkbox class.

Classes:
Checkbox
"""

import pygame
import pygame_gui.image


class Checkbox:
""" The Checkbox class which when clicked will toggle a Checkbox.active property. """
def __init__(self, rest_image, hover_image, active_image, active_hover_image, x, y):
"""
Parameters:
rest_image - The default image which is used for an in-active checkbox.
hover_image - The hover image to use for an in-active checkbox.
active_image - The image to use when the checkbox is active (checked).
active_hover_image - The hover image to show when the checkbox is active (checked).
x - The x position to use for the checkbox, in px.
y - The y position to use for the checkbox, in px
"""

self.rest_image = pygame_gui.Image(rest_image, x, y)
self.hover_image = pygame_gui.Image(hover_image, x, y)
self.active_image = pygame_gui.Image(active_image, x, y)
Expand All @@ -14,17 +32,33 @@ def __init__(self, rest_image, hover_image, active_image, active_hover_image, x,
self.active = False

def mouse_over(self):
""" Checks if the current mouse position is within the checkboxes area. """

if self.rect.collidepoint(pygame.mouse.get_pos()):
return True
return False

def check_clicked(self):
"""
Uses Checkbox.mouse_over to determine if the mouse is within the checkboxes area.
If the mouse is within the checkbox then Checkbox.active will be set to not Checkbox.active.

NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
"""

if self.mouse_over():
self.active = not self.active
return True
return False

def draw(self, display):
"""
Draws the checkbox to the given pygame display.

Parameters:
display - A pygame.display instance.
"""

if self.mouse_over():
if self.active:
self.active_hover_image.draw(display)
Expand Down
10 changes: 0 additions & 10 deletions pygame_gui/cursor.py

This file was deleted.

Loading