many things already done - pushing for saving. Needs to crete fighting; then make everything better

This commit is contained in:
Arnas Udovicius 2014-02-03 18:45:06 +02:00
parent 6cc59c3c38
commit f5b3ba083f
12 changed files with 1556 additions and 0 deletions

6
__main__.py Normal file
View file

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from gui import main
main()

9
countries/lt Normal file
View file

@ -0,0 +1,9 @@
Lietuva
56
255
5
[XIX]
images/ru/xix_pst.png
images/ru/xix_ark.png
images/ru/xix_ptr.png

9
countries/ru Normal file
View file

@ -0,0 +1,9 @@
Rusija
255
12
56
[XIX]
images/ru/xix_pst.png
images/ru/xix_ark.png
images/ru/xix_ptr.png

1114
gui.py Normal file

File diff suppressed because it is too large Load diff

409
guilib.py Normal file
View file

@ -0,0 +1,409 @@
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import json
import os
import re
import time
from math import fabs
HAND_CURSOR_STRING = (
" XX ",
" X..X ",
" X..X ",
" X..X ",
" X..XXXXX ",
" X..X..X.XX ",
" XX X..X..X.X.X ",
"X..XX.........X ",
"X...X.........X ",
" X.....X.X.X..X ",
" X....X.X.X..X ",
" X....X.X.X.X ",
" X...X.X.X.X ",
" X.......X ",
" X....X.X ",
" XXXXX XX ")
CURSOR_HAND, MASK = pygame.cursors.compile(HAND_CURSOR_STRING, black='X',
white='.', xor='o')
def mygt(screen, x, y, x_sz, y_sz, txt, gs):
"""Drawing button."""
x_m, y_m = pygame.mouse.get_pos()
# if mouse is under button
if (x_m < (x + x_sz) and x_m > x) and (y_m < (y + y_sz) and y_m > y):
pygame.draw.rect(screen, (200, 160, 60), (x, y, x_sz, y_sz), 0)
pygame.mouse.set_cursor((16, 16), (8, 1), CURSOR_HAND, MASK)
gs.register_button(x, y, x_sz, y_sz)
else:
pygame.draw.rect(screen, (255, 200, 80), (x, y, x_sz, y_sz), 0)
if not gs.check_button(x, y, x_sz, y_sz):
pygame.mouse.set_cursor(*pygame.cursors.arrow)
pygame.draw.rect(screen, (0, 0, 0), (x, y, x_sz, y_sz), 2)
fonts = pygame.font.Font(None, 24)
a = fonts.render(txt, True, (0, 0, 0))
screen.blit(a, (x + 8, y + (y_sz / 2) - 8))
class GameSettings:
"""Object to save all game parameters during game."""
een = True # Game loop
stage = 'start' # display stage of game (start, map, settings etc.)
turn = 0 # Player starts
fullscreen = False # Fullscreen is off
countries = {} # Countries
maps = {} # Maps
ages = ['XIII', 'XIX']
log = [] # Game log
GREEN = (0, 180, 0)
RED = (180, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
COLORS = [GREEN, RED, BLUE, BLACK]
PRINT = 0
LAST_POINT = 0
POINT = 0
HOME = os.path.expanduser("~")
HOME = os.path.join(HOME, '.rc_maumataskis')
try:
f = open(HOME, 'r')
HOME = f.readline()
f.close()
try:
HOME = HOME[:-1]
os.listdir(HOME)
except OSError:
HOME = os.path.expanduser("~")
HOME = os.path.join(HOME, '.rc_maumataskis')
f = open(HOME, 'w')
ent = '\n'
f.write(os.path.expanduser("~"))
f.write(ent)
f.close()
HOME = os.path.expanduser("~")
except IOError:
f = open(HOME, 'w')
ent = '\n'
f.write(os.path.expanduser("~"))
f.write(ent)
f.close()
HOME = os.path.expanduser("~")
# FIXME: rewrite opt and flagai
opt = {'kursor_pos' : 0, 't_raidie' : '', 'zaid' : '',
's_dir_cur' : HOME, 's_dir_pos' : 0, 'HOME' : HOME} # varies options
flagai = {'SAVE_DIR' : 0, 'new_dir' : 0, 'KNIST_DIR' : 0, 'new_file' : 0,
'new_file_err' : 0, 'load_err' : 0, 'SAUGOM' : 0, 'KRAUNAM' : 0,
'PRINTINAM' : 0, 'KNIST_OK' : 0} # varies flags
buttons_stack = {}
errors = []
editor = {
'in_progress': False,
'word': '',
'cursor': 0
}
settings = {
'age' : 'XIX',
'map' : '',
'country' : ''
}
map_editor = {
'tool' : 'border',
'action' : None,
'x': None,
'y': None,
'tmp' : None,
'country' : None
}
map_editor_tools = {
'border': u'Sienos',
'undo': u'Atšaukti',
'capital': u'Sostinė',
'countries': u'Šalys',
'connect': u'Ribojasi'
}
new_map = {
'borders': [],
'capitals': [],
'countries': {}
}
game = {
'age': '',
'map': '',
'users': [],
'turn': 0,
'stage': '',
'fight_from': None,
'fight_to': None,
'fight_pst': 0,
'fight_ark': 0,
'fight_ptr': 0
}
fight = {
'fields': [],
'units': [],
'selected_unit': None,
'selected_field': None,
'selected_pos': None,
'action': 'looking'
}
def __init__(self):
self.load_countries()
self.load_maps()
self.fight['fields'] = []
for i in range(54):
self.fight['fields'].append([])
for j in range(9):
self.fight['fields'][i].append('')
def get_stage(self):
return self.stage
def set_stage(self, stage):
self.stage = stage
def load_countries(self):
countries = os.listdir('countries')
for country_short in countries:
country = Country(country_short)
f = open('countries/'+country_short, 'r')
country.name = f.readline()[0:-1]
r = int(f.readline())
g = int(f.readline())
b = int(f.readline())
country.color = (r, g, b)
# reading players by ages
ages = {}
age = None
line = f.readline()
while line:
line = line[0:-1]
b = re.match('\[(\w+)\]', line)
if b:
age = b.groups()[0]
ages[age] = []
line = f.readline()
continue
if line:
ages[age].append(line)
line = f.readline()
f.close()
country.ages = ages
self.add_country(country_short, country)
def add_country(self, country_short, country):
self.countries[country_short] = country
def get_country(self, short_name):
if short_name in self.countries:
return self.countries[short_name]
def get_country_land_from_capital(self, x, y):
for country in self.game['map']:
for land in self.game['map'][country]:
if x - 10 < land['capital'][0] < x + 10 and y - 10 < land['capital'][1] < y + 10:
return country, land
return None, None
def save_new_map(self):
# prepare content
structure ={}
for country in self.new_map['countries']:
structure[country] = []
country_data = self.get_country(country)
print(country_data)
for capital in self.new_map['countries'][country]:
border = self.get_capital_border(self.new_map['borders'], capital)
if border:
structure[country].append({
'capital': capital,
'border': border,
'color': country_data.color
})
filename = self.editor['word'].replace(' ', '_')+'.ia.map'
f = open('maps/'+filename, 'w')
f.write(self.editor['word']+"\n")
f.write(self.settings['age']+"\n")
f.write(json.dumps(structure)+"\n")
f.close()
self.maps = {}
self.load_maps()
def load_maps(self):
maps = os.listdir('maps')
for filename in maps:
map_ = Map()
f = open('maps/'+filename, 'r')
map_.name = f.readline()[0:-1]
map_.age = f.readline()[0:-1]
map_.file = 'maps/'+filename
map_.map = json.loads(f.readline()[0:-1])
f.close()
self.add_map(map_.name, map_)
def add_map(self, map_name, map_object):
self.maps[map_name] = map_object
def clear_menu(self):
"""Clear menu and set flag."""
self.menu = False
self.menu_flag = 0
self.buttons_stack = {} # Make empty for renew situation
def register_button(self, x, y, x_sz, y_sz):
self.buttons_stack['%i_%i_%i_%i' % (x, y, x_sz, y_sz)] = True
def check_button(self, x, y, x_sz, y_sz):
self.buttons_stack['%i_%i_%i_%i' % (x, y, x_sz, y_sz)] = False
return True in self.buttons_stack.values()
def clear_log(text=None):
self.log = []
if text:
self.log.append(text)
def add_log(text):
self.log.insert(0, text)
def quit_game(self):
"""Quit aplication."""
self.een = 0
def get_extremums(self, border):
"""Returns extremums of border."""
min_x = min([p[0] for p in border])
max_x = max([p[0] for p in border])
min_y = min([p[1] for p in border])
max_y = max([p[1] for p in border])
return min_x, max_x, min_y, max_y
def extend_border(self, border):
extended = [border[0]]
pos_old = None
tmp_border = border
tmp_border.append(border[0])
for pos in tmp_border:
if pos_old:
if not pos[1] - pos_old[1] == 0:
k = (pos[0] - pos_old[0]) * 1. / (pos[1] - pos_old[1])
c = -1 * (pos[1] * k - pos[0])
if (pos[1] > pos_old[1]):
step = 1
else:
step = -1
for y in range(pos_old[1], pos[1], step):
x_ = int(y * k + c)
if (extended[-1][0] <= x_):
step_x = 1
else:
step_x = -1
for x in range(extended[-1][0], x_, step_x):
extended.append((x, y))
else:
if (pos[0] > pos_old[0]):
step = 1
else:
step = -1
for x in range(pos_old[0], pos[0], step):
extended.append((x, pos[1]))
pos_old = pos
return extended
def get_capital_border(self, borders, capital):
for border in borders:
min_x, max_x, min_y, max_y = self.get_extremums(border)
FOUND = False
border_extended = self.extend_border(border)
if min_x < capital[0] < max_x and min_y < capital[1] < max_y:
# this algoritm counts how many time it passes contur
# border. If it is < 0 and is odd it meens what point is
# inside contur
y = capital[1]
count = 0
while y > min_y - 1:
y -= 1
for p in border_extended:
if p[0] == capital[0] and p[1] == y:
count += 1
print(count)
if count > 0 and count % 2 == 1:
return border
class Country:
"""Main class for country."""
def __init__(self, short_name):
self.short_name = short_name
self.ages = {}
self.name = ''
self.color = None
class Map:
"""Main class for map."""
def __init__(self):
self.name = ''
self.age = ''
self.file = ''
self.map = None

BIN
images/menu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
images/ru/xix_ark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B

BIN
images/ru/xix_pst.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 615 B

BIN
images/ru/xix_ptr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 610 B

3
maps/daugiau.ia.map Normal file
View file

@ -0,0 +1,3 @@
daugiau
XIX
{"ru": [{"color": [255, 12, 56], "border": [[715, 329], [641, 457], [626, 548], [648, 565], [663, 593], [683, 648], [703, 684], [706, 700], [715, 719], [753, 718], [791, 717], [833, 718], [896, 716], [973, 717], [1105, 715], [1119, 715], [1114, 625], [1117, 15], [1114, 12], [1114, 13], [805, 20], [847, 148], [902, 176], [913, 201], [921, 213], [922, 238], [910, 272], [872, 280], [863, 288], [842, 295], [841, 290], [821, 331], [763, 312], [717, 331], [715, 329], [715, 329], [715, 329]], "capital": [1021, 191]}, {"color": [255, 12, 56], "border": [[788, 251], [766, 263], [730, 261], [717, 261], [704, 260], [689, 256], [679, 251], [669, 247], [662, 243], [657, 242], [642, 253], [641, 301], [659, 323], [711, 328], [761, 310], [792, 320], [818, 331], [837, 288], [803, 270], [789, 257], [785, 251], [799, 233], [793, 245], [784, 251], [806, 269], [834, 284], [847, 289], [849, 291], [909, 271], [919, 215], [897, 172], [853, 152], [812, 149], [777, 152], [750, 158], [738, 159], [728, 168], [726, 174], [723, 183], [723, 184], [723, 202], [736, 207], [761, 207], [777, 212], [791, 224], [796, 227], [797, 235], [798, 237], [788, 251], [788, 251], [788, 251], [788, 251], [788, 251], [788, 251]], "capital": [678, 277]}], "lt": [{"color": [56, 255, 5], "border": [[788, 251], [766, 263], [730, 261], [717, 261], [704, 260], [689, 256], [679, 251], [669, 247], [662, 243], [657, 242], [642, 253], [641, 301], [659, 323], [711, 328], [761, 310], [792, 320], [818, 331], [837, 288], [803, 270], [789, 257], [785, 251], [799, 233], [793, 245], [784, 251], [806, 269], [834, 284], [847, 289], [849, 291], [909, 271], [919, 215], [897, 172], [853, 152], [812, 149], [777, 152], [750, 158], [738, 159], [728, 168], [726, 174], [723, 183], [723, 184], [723, 202], [736, 207], [761, 207], [777, 212], [791, 224], [796, 227], [797, 235], [798, 237], [788, 251], [788, 251], [788, 251], [788, 251], [788, 251], [788, 251]], "capital": [799, 181]}, {"color": [56, 255, 5], "border": [[633, 283], [574, 283], [541, 274], [485, 241], [462, 214], [464, 162], [455, 136], [454, 76], [547, 57], [593, 69], [618, 108], [668, 155], [674, 157], [643, 188], [639, 231], [660, 244], [641, 255], [643, 288], [633, 283], [633, 283], [633, 283], [633, 283], [633, 283]], "capital": [513, 118]}]}

3
maps/figut.ia.map Normal file
View file

@ -0,0 +1,3 @@
figut
XIX
{"ru": [{"color": [255, 12, 56], "border": [[381, 148], [251, 323], [392, 443], [562, 281], [383, 148], [381, 148], [381, 148], [381, 148]], "capital": [395, 240]}], "lt": [{"color": [56, 255, 5], "border": [[750, 82], [713, 195], [832, 271], [967, 207], [804, 199], [768, 160], [976, 115], [750, 82], [750, 82]], "capital": [837, 229]}, {"color": [56, 255, 5], "border": [[634, 305], [629, 598], [936, 591], [940, 320], [634, 306], [634, 305], [634, 305]], "capital": [755, 470]}]}

3
maps/lt_lv_ru.ia.map Normal file
View file

@ -0,0 +1,3 @@
lt lv ru
XIX
{"ru": [{"color": [255, 12, 56], "border": [[281, 508], [277, 610], [514, 604], [504, 571], [408, 511], [286, 506], [281, 508]], "capital": [319, 533]}, {"color": [255, 12, 56], "border": [[285, 366], [266, 201], [322, 108], [499, 203], [656, 161], [735, 45], [931, 67], [878, 210], [825, 315], [758, 394], [732, 351], [646, 344], [569, 287], [503, 363], [287, 373], [287, 364], [285, 366], [285, 366]], "capital": [600, 185]}], "lt": [{"color": [56, 255, 5], "border": [[505, 362], [284, 373], [261, 450], [282, 506], [410, 512], [505, 570], [513, 604], [606, 633], [606, 685], [747, 673], [787, 584], [817, 596], [861, 556], [794, 545], [772, 478], [854, 430], [826, 313], [756, 396], [733, 351], [645, 345], [568, 289], [506, 362], [505, 362], [505, 362], [505, 362]], "capital": [687, 547]}]}