diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..fe803eb --- /dev/null +++ b/__main__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- + +from gui import main + +main() + diff --git a/countries/lt b/countries/lt new file mode 100644 index 0000000..4db3a53 --- /dev/null +++ b/countries/lt @@ -0,0 +1,9 @@ +Lietuva +56 +255 +5 +[XIX] +images/ru/xix_pst.png +images/ru/xix_ark.png +images/ru/xix_ptr.png + diff --git a/countries/ru b/countries/ru new file mode 100644 index 0000000..4d2efbb --- /dev/null +++ b/countries/ru @@ -0,0 +1,9 @@ +Rusija +255 +12 +56 +[XIX] +images/ru/xix_pst.png +images/ru/xix_ark.png +images/ru/xix_ptr.png + diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..5c05a68 --- /dev/null +++ b/gui.py @@ -0,0 +1,1114 @@ +# -*- coding: utf-8 -*- + +from guilib import * +import pygame +from pygame.locals import * + + +def main(): + pygame.init() + + # Window + display_flags = DOUBLEBUF #FULLSCREEN #DOUBLEBUF + width, height = 1366, 768 + + pygame.display.set_mode((width, height), display_flags) + pygame.display.set_caption(u'Imperiju amzius!') + screen = pygame.display.get_surface() + + gs = GameSettings() # main game settings + + while gs.een: + stage = gs.get_stage() + eval(stage+'(screen, gs)') + pygame.display.flip() + + + +def start(screen, gs): + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + txt = u'Imperijų amžius!' + fonts = pygame.font.Font(None, 72) + a = fonts.render(txt, True, (0, 0, 0)) + screen.blit(a, (470, 30)) + + img = pygame.image.load("images/menu.png") + screen.blit(img, (383, 110)) + + mygt(screen, 575, 350, 215, 50, u'Pradėti', gs) + mygt(screen, 575, 410, 215, 50, u'Užkrauti', gs) + mygt(screen, 575, 470, 215, 50, u'Nustatymai', gs) + mygt(screen, 575, 530, 215, 50, u'Šalys', gs) + mygt(screen, 575, 590, 215, 50, u'Žemėlapiai', gs) + mygt(screen, 575, 650, 215, 50, u'Išeiti', gs) + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 575 and x_m < 790): + if (y_m > 350 and y_m < 400): + gs.set_stage('new') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 410 and y_m < 460): + gs.set_stage('load') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 470 and y_m < 520): + gs.set_stage('settings') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 530 and y_m < 580): + gs.set_stage('countries') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 590 and y_m < 640): + gs.set_stage('maps') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 650 and y_m < 700): + gs.set_stage('exit') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + + +def exit(screen, gs): + gs.quit_game() + + + +def countries(screen, gs): + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + txt1 = u'Imperijų amžius!' + fonts = pygame.font.Font(None, 72) + a = fonts.render(txt1, True, (0, 0, 0)) + screen.blit(a, (470, 30)) + + mygt(screen, 1140, 30, 215, 50, u'Atgal', gs) + mygt(screen, 1140, 90, 215, 50, u'Nauja šalis', gs) + + start_y = 100 + age = 'XIX' + for short in gs.countries: + country = gs.countries[short] + + fonts = pygame.font.Font(None, 36) + a = fonts.render(country.name, True, (0, 0, 0)) + screen.blit(a, (30, start_y)) + + pygame.draw.rect(screen, country.color, (200, start_y, 20, 20), 0) + + start_x = 400 + for img in country.ages[age]: + img_load = pygame.image.load(img) + screen.blit(img_load, (start_x, start_y)) + start_x += 150 + + mygt(screen, 800, start_y, 215, 50, u'Keisti', gs) + + start_y += 100 + + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): + gs.set_stage('start') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('new_country') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + +def maps(screen, gs): + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + txt1 = u'Imperijų amžius!' + fonts = pygame.font.Font(None, 72) + a = fonts.render(txt1, True, (0, 0, 0)) + screen.blit(a, (470, 30)) + + mygt(screen, 1140, 30, 215, 50, u'Atgal', gs) + mygt(screen, 1140, 90, 215, 50, u'Naujas žemėlapis', gs) + + start_y = 100 + age = 'XIX' + for map_name in gs.maps: + fonts = pygame.font.Font(None, 36) + a = fonts.render(map_name, True, (0, 0, 0)) + screen.blit(a, (30, start_y)) + + mygt(screen, 800, start_y, 215, 50, u'Keisti', gs) + + start_y += 100 + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): + gs.set_stage('start') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('new_map') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + + +def new_map(screen, gs): + fonts18 = pygame.font.Font(None, 18) + fonts24 = pygame.font.Font(None, 24) + + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + mygt(screen, 1140, 30, 215, 50, u'Atgal', gs) + mygt(screen, 1140, 90, 215, 50, u'Saugoti', gs) + + pygame.draw.rect(screen, (250, 250, 250), (10, 10, 1120, 720), 0) + for border in gs.new_map['borders']: + pos_old = None + for pos in border: + if (pos_old): + pygame.draw.aaline(screen, (0, 0, 0), + (pos_old[0], pos_old[1]), + (pos[0], pos[1]), 1) + pos_old = pos + + if gs.map_editor['tool'] == 'border' and gs.map_editor['action'] == 'stroke': + pos_old = None + for pos in gs.map_editor['tmp']: + if (pos_old): + pygame.draw.aaline(screen, (0, 0, 0), + (pos_old[0], pos_old[1]), + (pos[0], pos[1]), 1) + pos_old = pos + + for pos in gs.new_map['capitals']: + pygame.draw.circle(screen, (0, 0, 0), pos, 10) + + for country_short in gs.new_map['countries']: + country = gs.get_country(country_short) + for i, land in enumerate(gs.new_map['countries'][country_short]): + a = fonts18.render('%i: %s' % (i, country.name), True, country.color) + screen.blit(a, (land[0]+12, land[1]+12)) + + + # editor tools + start_y = 160 + for tool in gs.map_editor_tools: + color = (0, 0, 0) + if tool == gs.map_editor['tool']: + color = (255, 0, 0) + + a = fonts24.render(gs.map_editor_tools[tool], True, color) + screen.blit(a, (1140, start_y)) + start_y += 30 + + start_y = 350 + for country_sh in gs.countries: + country = gs.countries[country_sh] + color = (0, 0, 0) + if country_sh == gs.map_editor['country']: + color = (255, 0, 0) + + a = fonts18.render(country.name, True, color) + screen.blit(a, (1140, start_y)) + + pygame.draw.rect(screen, country.color, (1230, start_y, 20, 20), 0) + + start_y += 30 + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): + gs.set_stage('maps') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('save_map') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + # tools + if (x_m > 1140 and x_m < 1350): + start_y = 160 + for tool in gs.map_editor_tools: + if (y_m > start_y and y_m < start_y + 30): + if tool == 'undo': + if gs.map_editor['tool'] == 'border': + if len(gs.map_editor['tmp']) > 0: + gs.map_editor['tmp'].pop() + elif len(gs.new_map['borders']) > 0: + gs.new_map['borders'].pop() + elif gs.map_editor['tool'] == 'capital' and len(gs.new_map['capitals']) > 0: + gs.new_map['capitals'].pop() + elif gs.map_editor['tool'] == 'country' and len(gs.new_map['countries']) > 0: + if gs.map_editor['country'] in gs.new_map['countries']: + if len(gs.new_map['countries'][gs.map_editor['country']]) > 0: + gs.new_map['countries'][gs.map_editor['country']].pop() + + else: + gs.map_editor['tool'] = tool + start_y += 30 + + # countries + if (x_m > 1140 and x_m < 1350): + start_y = 350 + for country in gs.countries: + if (y_m > start_y and y_m < start_y + 30): + gs.map_editor['country'] = country + start_y += 30 + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.map_editor['tool'] == 'border' and gs.map_editor['action'] == 'stroke': + gs.map_editor['tmp'].append((x_m, y_m)) + + gs.map_editor['x'] = x_m + gs.map_editor['y'] = y_m + + if gs.map_editor['tool'] == 'border' and not gs.map_editor['action'] == 'stroke': + gs.map_editor['action'] = 'stroke' + gs.map_editor['tmp'] = [] + gs.map_editor['tmp'].append((x_m, y_m)) + gs.map_editor['x'] = x_m + gs.map_editor['y'] = y_m + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.map_editor['tool'] == 'capital': + gs.new_map['capitals'].append((x_m, y_m)) + if gs.map_editor['country']: + if not gs.map_editor['country'] in gs.new_map['countries']: + gs.new_map['countries'][gs.map_editor['country']] = [] + gs.new_map['countries'][gs.map_editor['country']].append((x_m, y_m)) + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.map_editor['tool'] == 'country': + pass # not implemented + + if event.type == MOUSEBUTTONUP and event.button == 3: + x_m, y_m = event.pos + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.map_editor['tool'] == 'border' and gs.map_editor['action'] == 'stroke': + gs.map_editor['action'] = None + gs.new_map['borders'].append(gs.map_editor['tmp']) + gs.map_editor['tmp'] = [] + + if event.type == MOUSEMOTION: + x_m, y_m = event.pos + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.map_editor['tool'] == 'border' and gs.map_editor['action'] == 'stroke': + + pygame.draw.aaline(screen, (0, 0, 0), + (gs.map_editor['x'], gs.map_editor['y']), + (x_m, y_m), 1) + + + +def save_map(screen, gs): + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + txt1 = u'Imperijų amžius!' + fonts = pygame.font.Font(None, 72) + a = fonts.render(txt1, True, (0, 0, 0)) + screen.blit(a, (470, 30)) + + mygt(screen, 1140, 30, 215, 50, u'Atgal', gs) + mygt(screen, 1140, 90, 215, 50, u'Patvirtinti', gs) + + fonts36 = pygame.font.Font(None, 36) + fonts24 = pygame.font.Font(None, 24) + + a = fonts36.render(u'Pavadinimas', True, (0, 0, 0)) + screen.blit(a, (30, 140)) + pygame.draw.rect(screen, (250, 250, 250), (30, 190, 400, 30), 0) + if gs.editor['in_progress']: + editor_word = gs.editor['word'][:gs.editor['cursor']]+'|'+gs.editor['word'][gs.editor['cursor']:] + else: + editor_word = gs.editor['word'] + + a = fonts24.render(editor_word, True, (0, 0, 0)) + screen.blit(a, (35, 195)) + + a = fonts36.render(u'Amžius', True, (0, 0, 0)) + screen.blit(a, (30, 240)) + start_x = 35 + for age in gs.ages: + color = (0, 0, 0) + if age == gs.settings['age']: + color = (255, 0, 0) + + a = fonts24.render(age, True, color) + screen.blit(a, (start_x, 290)) + start_x += 50 + + if len(gs.errors) > 0: + start_y = 400 + for error in gs.errors: + a = fonts36.render(error, True, (255, 0, 0)) + screen.blit(a, (100, start_y)) + start_y += 50 + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): + gs.set_stage('start') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.errors = [] + if len(gs.editor['word']) == 0: + gs.errors.append(u'Nenurodytas žemėlapio pavadinimas!!!') + else: + gs.save_new_map() + gs.set_stage('maps') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + # ages + if (y_m > 290 and y_m < 335): + start_x = 35 + for age in gs.ages: + if (x_m > start_x and x_m < start_x + 50): + gs.settings['age'] = age + start_x += 50 + + if (x_m > 30 and x_m < 430 and y_m > 190 and y_m < 220): + gs.editor['in_progress'] = True + + + if gs.editor['in_progress']: + if event.type == KEYDOWN and event.key in [K_LEFT] and gs.editor['cursor'] > 0: + gs.editor['cursor'] -= 1 + + if event.type == KEYDOWN and event.key in [K_RIGHT] and gs.editor['cursor'] < len(gs.editor['word']): + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_q]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'q'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_w]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'w'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_e]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'e'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_r]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'r'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_t]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'t'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_y]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'y'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_u]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'u'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_i]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'i'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_o]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'o'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_p]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'p'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_a]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'a'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_s]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'s'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_d]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'d'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_f]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'f'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_g]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'g'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_h]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'h'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_j]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'j'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_k]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'k'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_l]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'l'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_z]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'z'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_x]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'x'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_c]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'c'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_v]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'v'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_b]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'b'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_n]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'n'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_m]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'m'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_0]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'0'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_1]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'1'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_2]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'2'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_3]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'3'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_4]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'4'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_5]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'5'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_6]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'6'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_7]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'7'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_8]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'8'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_9]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+'9'+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_DELETE]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+gs.editor['word'][gs.editor['cursor']+1:] + + if event.type == KEYDOWN and event.key in [K_SPACE]: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']]+' '+gs.editor['word'][gs.editor['cursor']:] + gs.editor['cursor'] += 1 + + if event.type == KEYDOWN and event.key in [K_BACKSPACE] and gs.editor['cursor'] > 0: + gs.editor['word'] = gs.editor['word'][:gs.editor['cursor']-1]+gs.editor['word'][gs.editor['cursor']+1:] + gs.editor['cursor'] -= 1 + + +def new(screen, gs): + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + txt1 = u'Imperijų amžius!' + fonts = pygame.font.Font(None, 72) + a = fonts.render(txt1, True, (0, 0, 0)) + screen.blit(a, (470, 30)) + + mygt(screen, 1140, 30, 215, 50, u'Atgal', gs) + mygt(screen, 1140, 90, 215, 50, u'Pradėti', gs) + + fonts36 = pygame.font.Font(None, 36) + fonts24 = pygame.font.Font(None, 24) + + a = fonts36.render(u'Amžius', True, (0, 0, 0)) + screen.blit(a, (30, 140)) + start_x = 35 + for age in gs.ages: + color = (0, 0, 0) + if age == gs.settings['age']: + color = (255, 0, 0) + + a = fonts24.render(age, True, color) + screen.blit(a, (start_x, 190)) + start_x += 50 + + pygame.draw.aaline(screen, (0, 0, 0), (10, 230), (300, 230), 2) + + a = fonts36.render(u'Žemėlapis', True, (0, 0, 0)) + screen.blit(a, (30, 250)) + start_y = 290 + for map_name in gs.maps: + color = (0, 0, 0) + if map_name == gs.settings['map']: + color = (255, 0, 0) + + a = fonts24.render(map_name, True, color) + screen.blit(a, (30, start_y)) + start_y += 30 + + pygame.draw.aaline(screen, (0, 0, 0), (10, 430), (300, 430), 2) + + a = fonts36.render(u'Šalis', True, (0, 0, 0)) + screen.blit(a, (30, 450)) + start_y = 490 + for country_sh in gs.countries: + country = gs.countries[country_sh] + color = (0, 0, 0) + if country_sh == gs.settings['country']: + color = (255, 0, 0) + + a = fonts24.render(country.name, True, color) + screen.blit(a, (30, start_y)) + + pygame.draw.rect(screen, country.color, (200, start_y, 20, 20), 0) + + start_y += 30 + + if len(gs.errors) > 0: + start_y = 400 + for error in gs.errors: + a = fonts36.render(error, True, (255, 0, 0)) + screen.blit(a, (400, start_y)) + start_y += 50 + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): + gs.set_stage('start') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.errors = [] + if not gs.settings['age'] or not gs.settings['country'] or not gs.settings['map']: + gs.errors.append(u'Amžius, žemėlapis ir šalis turi būti pasirinktas!!!') + + else: + gs.game['age'] = gs.settings['age'] + gs.game['map'] = gs.maps[gs.settings['map']].map + gs.game['turn'] += 1 + gs.game['stage'] = 'get' + gs.game['users'].append(gs.settings['country']) #1st user is real + for country in gs.game['map']: + if not country in gs.game['users']: + gs.game['users'].append(country) + + gs.set_stage('play') + + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + # ages + if (y_m > 185 and y_m < 230): + start_x = 35 + for age in gs.ages: + if (x_m > start_x and x_m < start_x + 50): + gs.settings['age'] = age + start_x += 50 + + # maps + if (x_m > 30 and x_m < 290): + start_y = 290 + for map_name in gs.maps: + if (y_m > start_y and y_m < start_y + 30): + gs.settings['map'] = map_name + start_y += 30 + + # countries + if (x_m > 30 and x_m < 290): + start_y = 490 + for country in gs.countries: + if (y_m > start_y and y_m < start_y + 30): + gs.settings['country'] = country + start_y += 30 + + +def play(screen, gs): + if gs.game['stage'] == 'get': + for country in gs.game['map']: + for i, land in enumerate(gs.game['map'][country]): + if not 'army' in land: + gs.game['map'][country][i]['army'] = { + 'pst': 0, + 'ark': 0, + 'ptr': 0 + } + + gs.game['map'][country][i]['army']['pst'] += 20 + gs.game['map'][country][i]['army']['ark'] += 5 + gs.game['map'][country][i]['army']['ptr'] += 1 + + gs.game['stage'] = 0 + + + fonts18 = pygame.font.Font(None, 18) + fonts24 = pygame.font.Font(None, 24) + + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + mygt(screen, 1140, 30, 215, 50, u'Ėjimas baigtas', gs) + mygt(screen, 1140, 90, 215, 50, u'Meniu', gs) + + pygame.draw.rect(screen, (63, 125, 143), (10, 10, 1120, 720), 0) + + for country in gs.game['map']: + for land in gs.game['map'][country]: + pygame.draw.polygon(screen, land['color'], land['border']) + + pos_old = None + for pos in land['border']: + if (pos_old): + pygame.draw.line(screen, (0, 0, 0), + (pos_old[0], pos_old[1]), + (pos[0], pos[1]), 5) + pos_old = pos + + pygame.draw.circle(screen, (0, 0, 0), land['capital'], 10) + + army_text = '%i | %i | %i' % (land['army']['pst'], land['army']['ark'], land['army']['ptr']) + a = fonts18.render(army_text, True, (0, 0, 0)) + screen.blit(a, (land['capital'][0]+12, land['capital'][1]+12)) + + + a = fonts24.render(u'Ėjimas: %i' % gs.game['turn'], True, (0, 0, 0)) + screen.blit(a, (1140, 160)) + + start_y = 200 + for i, country_sh in enumerate(gs.game['users']): + country = gs.countries[country_sh] + color = (0, 0, 0) + if i == gs.game['stage']: + color = (255, 0, 0) + + a = fonts24.render(country.name, True, color) + screen.blit(a, (1140, start_y)) + + pygame.draw.rect(screen, country.color, (1230, start_y, 20, 20), 0) + + start_y += 30 + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): # and gs.game['stage'] == 0): + if (len(gs.game['users']) == gs.game['stage'] + 1): + gs.game['stage'] = 'get' + else: + gs.game['stage'] += 1 + + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('menu') + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + country, land = gs.get_country_land_from_capital(x_m, y_m) + if country and country == gs.game['users'][gs.game['stage']]: + gs.game['fight_from'] = land['capital'] + elif country and gs.game['fight_from']: + gs.game['fight_to'] = land['capital'] + gs.set_stage('prepare_fight') + + if event.type == MOUSEMOTION: + x_m, y_m = event.pos + + if (x_m > 10 and x_m < 1120 and y_m > 10 and y_m < 720): + if gs.game['fight_from']: + pygame.draw.line(screen, (255, 0, 0), (gs.game['fight_from'][0], gs.game['fight_from'][1]), (x_m, y_m), 5) + pygame.draw.aaline(screen, (0, 0, 0), (gs.game['fight_from'][0], gs.game['fight_from'][1]), (x_m, y_m), 1) + + +def prepare_fight(screen, gs): + fonts18 = pygame.font.Font(None, 18) + fonts24 = pygame.font.Font(None, 24) + fonts72 = pygame.font.Font(None, 72) + + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + mygt(screen, 1140, 30, 215, 50, u'Kovoti', gs) + mygt(screen, 1140, 90, 215, 50, u'Nutraukti puolimą', gs) + + country, land = gs.get_country_land_from_capital(gs.game['fight_from'][0], gs.game['fight_from'][1]) + country = gs.get_country(country) + + img = country.ages[gs.settings['age']][0] + img_load = pygame.image.load(img) + screen.blit(img_load, (50, 100)) + + a = fonts72.render(str(land['army']['pst'] - gs.game['fight_pst']), True, (0, 0, 0)) + screen.blit(a, (250, 120)) + + mygt(screen, 400, 120, 50, 50, '<<<', gs) + mygt(screen, 500, 120, 50, 50, '>>>', gs) + + a = fonts72.render(str(gs.game['fight_pst']), True, (0, 0, 0)) + screen.blit(a, (650, 120)) + + img = country.ages[gs.settings['age']][1] + img_load = pygame.image.load(img) + screen.blit(img_load, (50, 250)) + + a = fonts72.render(str(land['army']['ark'] - gs.game['fight_ark']), True, (0, 0, 0)) + screen.blit(a, (250, 270)) + + mygt(screen, 400, 270, 50, 50, '<<<', gs) + mygt(screen, 500, 270, 50, 50, '>>>', gs) + + a = fonts72.render(str(gs.game['fight_ark']), True, (0, 0, 0)) + screen.blit(a, (650, 270)) + + img = country.ages[gs.settings['age']][2] + img_load = pygame.image.load(img) + screen.blit(img_load, (50, 400)) + + a = fonts72.render(str(land['army']['ptr'] - gs.game['fight_ptr']), True, (0, 0, 0)) + screen.blit(a, (250, 420)) + + mygt(screen, 400, 420, 50, 50, '<<<', gs) + mygt(screen, 500, 420, 50, 50, '>>>', gs) + + a = fonts72.render(str(gs.game['fight_ptr']), True, (0, 0, 0)) + screen.blit(a, (650, 420)) + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): # and gs.game['stage'] == 0): + gs.set_stage('fight') + gs.fight['fields'] = [] + for i in range(54): + gs.fight['fields'].append([]) + for j in range(9): + gs.fight['fields'][i].append('') + + gs.fight['units'] = [] + + land['army']['pst'] -= gs.game['fight_pst'] + land['army']['ark'] -= gs.game['fight_ark'] + land['army']['ptr'] -= gs.game['fight_ptr'] + for i in range(gs.game['fight_pst']): + gs.fight['units'].append({ + 'country': country.short_name, + 'color': country.color, + 'type': 'pst', + 'health': 10 + }) + for i in range(gs.game['fight_ark']): + gs.fight['units'].append({ + 'country': country.short_name, + 'color': country.color, + 'type': 'ark', + 'health': 50 + }) + for i in range(gs.game['fight_ptr']): + gs.fight['units'].append({ + 'country': country.short_name, + 'color': country.color, + 'type': 'ptr', + 'health': 100 + }) + + country_b, land_b = gs.get_country_land_from_capital(gs.game['fight_to'][0], gs.game['fight_to'][1]) + for i in range(land_b['army']['pst']): + gs.fight['units'].append({ + 'country': country_b, + 'color': land_b['color'], + 'type': 'pst', + 'health': 10 + }) + for i in range(land_b['army']['ark']): + gs.fight['units'].append({ + 'country': country_b, + 'color': land_b['color'], + 'type': 'ark', + 'health': 50 + }) + for i in range(land_b['army']['ptr']): + gs.fight['units'].append({ + 'country': country_b, + 'color': land_b['color'], + 'type': 'ptr', + 'health': 100 + }) + + a = 0 + b = 0 + for k, unit in enumerate(gs.fight['units']): + if unit['country'] == country.short_name: + i = a / 9 + j = a % 9 + gs.fight['fields'][i][j] = k + a += 1 + else: + i = b / 9 + 1 + j = b % 9 + gs.fight['fields'][-i][j] = k + b += 1 + + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('play') + gs.game['fight_from'] = None + gs.game['fight_to'] = None + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (400 < x_m < 450): + if (120 < y_m < 170 and gs.game['fight_pst'] > 0): + gs.game['fight_pst'] -= 1 + + if (270 < y_m < 320 and gs.game['fight_ark'] > 0): + gs.game['fight_ark'] -= 1 + + if (420 < y_m < 470 and gs.game['fight_ptr'] > 0): + gs.game['fight_ptr'] -= 1 + + + if (500 < x_m < 550): + if (120 < y_m < 170 and gs.game['fight_pst'] < land['army']['pst']): + gs.game['fight_pst'] += 1 + + if (270 < y_m < 320 and gs.game['fight_ark'] < land['army']['ark']): + gs.game['fight_ark'] += 1 + + if (420 < y_m < 470 and gs.game['fight_ptr'] < land['army']['ptr']): + gs.game['fight_ptr'] += 1 + + +def fight(screen, gs): + fonts18 = pygame.font.Font(None, 18) + fonts24 = pygame.font.Font(None, 24) + fonts72 = pygame.font.Font(None, 72) + + bgcolor = pygame.Surface(screen.get_size()) + bgcolor = bgcolor.convert() + bgcolor.fill((235, 235, 155)) + screen.blit(bgcolor, (0, 0)) + + mygt(screen, 1140, 30, 215, 50, u'Ėjimo pabaiga', gs) + mygt(screen, 1140, 90, 215, 50, u'Pabėgti iš mūšio', gs) + + country, land = gs.get_country_land_from_capital(gs.game['fight_from'][0], gs.game['fight_from'][1]) + country = gs.get_country(country) + + country_b, land_b = gs.get_country_land_from_capital(gs.game['fight_to'][0], gs.game['fight_to'][1]) + country_b = gs.get_country(country_b) + + army_text = '%i | %i | %i' % (gs.game['fight_pst'], gs.game['fight_ark'], gs.game['fight_ptr']) + a = fonts72.render(army_text, True, country.color) + screen.blit(a, (100, 30)) + + army_text = '%i | %i | %i' % (land_b['army']['pst'], land_b['army']['ark'], land_b['army']['ptr']) + a = fonts72.render(army_text, True, country_b.color) + screen.blit(a, (800, 30)) + + pygame.draw.rect(screen, (163, 225, 143), (50, 500, 1080, 180), 0) + start_y = 500 + for i in range(10): + pygame.draw.line(screen, (0, 0, 0), (50, start_y), (1130, start_y), 3) + start_y += 20 + + start_x = 50 + for i in range(55): + pygame.draw.line(screen, (0, 0, 0), (start_x, 500), (start_x, 680), 3) + start_x += 20 + + for i in range(54): + for j in range(9): + index = gs.fight['fields'][i][j] + if isinstance(index, int): + unit = gs.fight['units'][index] + symbol = 'P' + if unit['type'] == 'ark': + symbol = 'A' + elif unit['type'] == 'ptr': + symbol = 'C' + + pygame.draw.rect(screen, unit['color'], (52 + i * 20, 502 + j * 20, 17, 17), 0) + a = fonts18.render(symbol, True, (0, 0, 0)) + screen.blit(a, (55 + i * 20, 505 + j * 20)) + + if gs.fight['action'] == 'selected': + selected_unit = gs.fight['units'][gs.fight['selected_unit']] + if selected_unit['type'] in ['pst', 'ptr']: + actions = ['X', 'F', 'G', 'S'] + longness = 80 + else: + actions = ['X', 'F', 'G'] + longness = 60 + + x, y = gs.fight['selected_pos'] + pygame.draw.rect(screen, (255, 255, 255), (x, y, longness, 20), 0) + start_x = x + 5 + for letter in actions: + a = fonts18.render(letter, True, (0, 0, 0)) + screen.blit(a, (start_x, y + 5)) + start_x += 20 + + + + # Inputs + events = pygame.event.get() + for event in events: + if event.type == MOUSEBUTTONUP and event.button == 1: + x_m, y_m = event.pos + + if (x_m > 1140 and x_m < 1355): + if (y_m > 30 and y_m < 80): # and gs.game['stage'] == 0): + gs.set_stage('fight') + land['army']['pst'] -= gs.game['fight_pst'] + land['army']['ark'] -= gs.game['fight_ark'] + land['army']['ptr'] -= gs.game['fight_ptr'] + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if (y_m > 90 and y_m < 130): + gs.set_stage('play') + # we have logic here. If we retriet we lose half of canons, + # 1/5 simple and 1/10 cavalery + land['army']['pst'] += int(gs.game['fight_pst'] * 0.8) + land['army']['ark'] += int(gs.game['fight_ark'] * 0.9) + land['army']['ptr'] += int(gs.game['fight_ptr'] * 0.5) + + gs.game['fight_from'] = None + gs.game['fight_to'] = None + pygame.mouse.set_cursor(*pygame.cursors.arrow) + + if gs.fight['action'] in ['looking']: + for i in range(54): + for j in range(9): + index = gs.fight['fields'][i][j] + if isinstance(index, int): + x = 52 + i * 20 + y = 502 + j * 20 + if x < x_m < x + 17 and y < y_m < y + 17: + pygame.draw.rect(screen, (0, 0, 255), (x, y, 17, 17), 0) + if gs.fight['action'] == 'looking': + gs.fight['selected_unit'] = index + gs.fight['selected_field'] = i, j + gs.fight['selected_pos'] = x + 30, y - 10 + gs.fight['action'] = 'selected' + + if gs.fight['action'] == 'selected': + selected_unit = gs.fight['units'][gs.fight['selected_unit']] + if selected_unit['type'] in ['pst', 'ptr']: + actions = ['X', 'F', 'G', 'S'] + else: + actions = ['X', 'F', 'G'] + + x, y = gs.fight['selected_pos'] + # X - close + if x < x_m < x + 20 and y < y_m < y + 20: + gs.fight['action'] = 'looking' + gs.fight['selected_unit'] = None + gs.fight['selected_field'] = None + gs.fight['selected_pos'] = None + + x += 20 + # F - fortify + if x < x_m < x + 20 and y < y_m < y + 20: + pass + + x += 20 + # G - go + if x < x_m < x + 20 and y < y_m < y + 20: + gs.fight['action'] = 'selected_go' + + x += 20 + # S - shot + if x < x_m < x + 20 and y < y_m < y + 20: + pass + + diff --git a/guilib.py b/guilib.py new file mode 100644 index 0000000..9d81ee4 --- /dev/null +++ b/guilib.py @@ -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 + diff --git a/images/menu.png b/images/menu.png new file mode 100644 index 0000000..59ba4fa Binary files /dev/null and b/images/menu.png differ diff --git a/images/ru/xix_ark.png b/images/ru/xix_ark.png new file mode 100644 index 0000000..e7739a1 Binary files /dev/null and b/images/ru/xix_ark.png differ diff --git a/images/ru/xix_pst.png b/images/ru/xix_pst.png new file mode 100644 index 0000000..780b641 Binary files /dev/null and b/images/ru/xix_pst.png differ diff --git a/images/ru/xix_ptr.png b/images/ru/xix_ptr.png new file mode 100644 index 0000000..a4a57e2 Binary files /dev/null and b/images/ru/xix_ptr.png differ diff --git a/maps/daugiau.ia.map b/maps/daugiau.ia.map new file mode 100644 index 0000000..94e325f --- /dev/null +++ b/maps/daugiau.ia.map @@ -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]}]} diff --git a/maps/figut.ia.map b/maps/figut.ia.map new file mode 100644 index 0000000..42fee18 --- /dev/null +++ b/maps/figut.ia.map @@ -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]}]} diff --git a/maps/lt_lv_ru.ia.map b/maps/lt_lv_ru.ia.map new file mode 100644 index 0000000..948b97a --- /dev/null +++ b/maps/lt_lv_ru.ia.map @@ -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]}]}