justcheckers/justcheckers/app.py

98 lines
3.0 KiB
Python

"""
Main application for justCheckers
:copyright: Copyright 2004-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: GPL v3+
"""
import json
import logging
import os
import sys
from kivy import properties
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
import justcheckers
LOG = logging.getLogger(__name__)
class JustCheckersApp(App):
title = 'justCheckers'
icon = 'justcheckers/images/icon.png'
use_kivy_settings = False
def on_start(self):
log_system_and_game_info()
super(JustCheckersApp, self).on_start()
def build_config(self, config):
config.setdefaults('Theme', {'theme': 'Sunset', 'background': 'Sunset'})
def build_settings(self, settings):
# TODO: Add turn options for music and volume.
game_settings = [{
"type": "options",
"title": "Board Theme",
"section": "Theme",
"key": "theme",
"options": ["Sunset", "Basic"]
}, {
"type": "options",
"title": "Background",
"section": "Theme",
"key": "background",
"options": ["Sunset", "Basic"]
}]
settings.add_json_panel("justCheckers Settings", self.config, data=json.dumps(game_settings))
class GameBoard(GridLayout):
rows = properties.NumericProperty(8)
cols = properties.NumericProperty(8)
def __init__(self, **kwargs):
super(GameBoard, self).__init__(**kwargs)
for row in range(self.rows):
for col in range(self.cols):
source = 'play'
if row % 2 == 0 and col % 2 == 0:
source = 'non_play'
elif row % 2 == 1 and col % 2 == 1:
source = 'non_play'
self.add_widget(GameTile(tile_state=source, coords=[row, col]))
class GameTile(RelativeLayout):
tile_state = properties.StringProperty()
tile_coords = properties.ListProperty()
tile_image = properties.ObjectProperty()
tile_label = properties.ObjectProperty()
def __init__(self, tile_state='non_play', coords=None, **kwargs):
super(GameTile, self).__init__(**kwargs)
self.tile_state = tile_state
self.coords = coords if coords else [-1, -1]
self.tile_label.text = '{}, {}'.format(self.coords[0], self.coords[1])
self.tile_image.source = 'images/{}.png'.format(self.tile_state)
def log_system_and_game_info():
"""Logs information about the game and system running the game."""
Logger.info('justCheckers -- v{}'.format(justcheckers.__version__))
message = 'I am running on {os}.\nMy screen is {height}x{width}'
root_window = App.get_running_app().root_window
os_sys = sys.platform
if sys.platform == 'posix':
os_name, _, _, _, os_arch = os.uname()
os_sys = '{name} {arch}'.format(name=os_name, arch=os_arch)
Logger.info(message.format(os=os_sys, height=root_window.height, width=root_window.width))