Skip to content

Commit

Permalink
Development documentation added in help/development.py. Rename run.py…
Browse files Browse the repository at this point in the history
… -> run_game.py
  • Loading branch information
Smart Viking committed Dec 20, 2012
1 parent 3dcb01d commit 0b87180
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 43 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@ Knights

A puzzle game where you must fill a board using only knight movements. It's pretty much a clone of the game troyis. If you want, you can watch this [gameplay video](https://www.youtube.com/watch?v=v_faK8K-Pzk) which shows how you can download and play.

Run the game with the file `run.py`.
Run the game with the file `run_game.py`.

Tested on Python 2.6, 2.7 and 3.2 on Ubuntu. So the game should be Python 3 compitable.
Also tested on Windows 7 with Python 2.7, no problems except that the font wasn't available. This doesn't really matter because the game worked fine, but I might include fonts with the game in the future.
Expand All @@ -12,6 +12,8 @@ The game requires pygame. The debian package name is python-pygame.

The settings.yaml file is full of settings you can tinker with, if you want, you can change the game drastically through that file.

The file help/help.html contains the rules of the game. The file help/development.html contains some documentation about how the game works.

License
--
This program is licensed under the GNU GPL.
Expand Down
64 changes: 64 additions & 0 deletions help/development.html
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Help for development</title>
</head>
<body>
<h1>Development help:</h1>

<section id="rules">
<p>
Here I'll write a few things about how the game was developed in case anyone wants to modify it.
</p>

<h2>General</h2>
<p>
The game is supposed to work in both python2.7 and python 3. It's GNU GPLv3 licensed.
</p>

<h2>Game structure</h2>
<p>
The main loop is contained in the Game class, inside knights/game.py.

The variable self.mode inside Game, will be an instance of the "current mode". For a class to be a mode, it has to have certain features so that Game can ducktype on it.

Here's a list as of writing this file:
</p>
<ul>
<li>Game.mode.name</li>
<p class="dochelp">The name of the mode, is used in Game.handle_button_clicks</p>
<li>Game.mode.update</li>
<p class="dochelp">Is called once per loop, used to do whatever</p>
<li>Game.mode.background</li>
<p class="dochelp">This is the surface of the mode</p>
<li>Game.mode.changed</li>
<p class="dochelp">A boolean. Game.blit will only blit Game.mode.background on to Game.screen if Game.mode.changed is True. This is to improve performance.</p>
<li>Game.mode.mouseup AND Game.mode.mousedown</li>
<p class="dochelp">These methods are called in Game.events, along with the location of the mouse button. A mode has to have them, the mode can do whatever with them.</p>
</ul>

<h2>Settings</h2>
<p>All the settings come from the file settings.yaml. They are loaded by yaml in the file knights/common.py.
Btw, PyYAML is included with the game for portability. It's located in the yaml directory and you shouldn't have to worry about it.<br /> Anyway, the settings variable in common.py is imported to knights/settings.py. Here the settings are split up into more variables. Each of the relevant parts of the game, will import any settings from this file. These are the *only uppercase variables* used in the project, so it should be clear that they are settings when you know this.</p>

<h2>Game logic</h2>

<p>The game logic of the gameboard resides in knights/gameboard_logic.py. <br />The function generate_board is important, it generates a list of tuples of coordinates within some gameboard size, until the amount of tuples reaches some threshold. This function is called in Gameboard.populate_board, and the tuples inside the list will become the coordinates of *open* fields, that is, those fields that the knight must visit to win the game.<br /><br /> Gameboard.board will be a list of instances of the Field class, each field, or coordinate, has one instance. Gameboard.print_board is just a function to print the board in a nice way.<br /> Gameboard.game_over will return if the game is over. Gameboard.move_knight will try to move the knight to another coordinate, it will return false if it is an invalid move. This is an important function.
<br />
It will probably be useful to see how Gameboard is used inside knights/cli.py, which is a command line version of the game. You can run the game with the file run_cli.py. It's not a fun way to play but it's very useful. It's only python 2 compatible at this moment.</p>

<h2>Pygame board representation</h2>
<p>The file knights/gameboard.py is all about how the gameboard is represented in pygame.<br /> The class Board inherits from the class Gameboard from gameboard_logic.py.<br /> The names are a little confusing, sorry about that.

Board.pygame_field is a list of instances of PygameField
</p>


<h2>Ehm</h2>
<p>There are other stuff I could write about, like how the menu-type modes inherit from Menu in knights/menu.py, but I don't have time. Good luck!</p>
</section>

<a href="help.html">Back to game rules</a>
</body>
</html>
27 changes: 3 additions & 24 deletions help/help.html
@@ -1,31 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Help!</title>
<style type="text/css">
body {
line-height: 140%;
}
h1, h2 {
color: darkred;
}
#rules {
width: 700px;
margin: 30px;
}
.beige {
background-color: #EAB26E;
padding: 2px;
}
.green {
background-color: #64C961;
padding: 2px;
}
.blue {
background-color: #3A67BA;
padding: 2px;
}
</style>
</head>
<body>
<h1>Game Rules:</h1>
Expand All @@ -41,5 +18,7 @@ <h2>Timesave game</h2>

<p>Timesave game is similar to normal game, but you start with very little time. When a level ends, the time you have left is passed on to the next level, so the success of the game depends on how fast you did previous levels. Other than that, it's the same as normal game; when time runs out, you lose.</p>
</section>

<a href="development.html">Help For Development</a>
</body>
</html>
26 changes: 26 additions & 0 deletions help/style.css
@@ -0,0 +1,26 @@
body {
line-height: 140%;
}
h1, h2 {
color: darkred;
}
#rules {
width: 700px;
margin: 30px;
}
.beige {
background-color: #EAB26E;
padding: 2px;
}
.green {
background-color: #64C961;
padding: 2px;
}
.blue {
background-color: #3A67BA;
padding: 2px;
}
.dochelp {
color: #444;
margin-left: 10px;
}
41 changes: 23 additions & 18 deletions knights/cli.py
@@ -1,3 +1,4 @@
# -*- Encoding: utf-8 -*-
"""
Copyright (C) 2012 Mattias Ugelvik
Expand Down Expand Up @@ -25,28 +26,31 @@ def game():
if size == '': exit()
gameboard = Gameboard(int(size))

play_d = {"\x1b[B\x1b[D":(-1,2),
"\x1b[B\x1b[C":(1,2),
"\x1b[A\x1b[D":(-1,-2),
"\x1b[A\x1b[C":(1,-2),
"\x1b[C\x1b[A":(2,-1),
"\x1b[C\x1b[B":(2,1),
"\x1b[D\x1b[A":(-2,-1),
"\x1b[D\x1b[B":(-2,1),
play_d = {"dl":(-1,2),
"dr":(1,2),
"ul":(-1,-2),
"ur":(1,-2),
"ru":(2,-1),
"rd":(2,1),
"lu":(-2,-1),
"ld":(-2,1),
"ng":"ng"}
# Those are just escape sequences for the arrow keys in the terminal or
# something stupid like that, I don't even care I just copy-pasted. I mean
# why should I care, we're all gonna die and then it doesn't matter.
# Aliens won't care a byte. Not even a bit.
# Prefix = \x1b[
# A = Up
# B = Down
# C = Right
# D = Left

"""
d = Down
l = Left
r = Right
u = Up
dl = Down-Left | ↓↓←
rd = Right-Down | →→↓
ul = Up-Left | ↑↑←
This is how you tell the game where you want to move. The first char is worth 2 moves, the second 1, together they can form knight movements.
"""

while 1:
print "\n" * 50
print "\n" * 5
gameboard.print_board()

if gameboard.game_over():
Expand All @@ -56,6 +60,7 @@ def game():
try:
new_move = play_d[raw_input()]
except KeyError:
print "errar"
new_move = (-1000,-1000)
if new_move == "ng":
game()
Expand Down
6 changes: 6 additions & 0 deletions run_cli.py
@@ -0,0 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
from knights.cli import game

print("This file is most useful for development, the game itself is the run_game.py file.")
game()
File renamed without changes.

0 comments on commit 0b87180

Please sign in to comment.