Tweak window size
[erdslangetjie.git] / erdslangetjie / __main__.py
1 import sys
2 import os
3 import kivy
4 import pygame
5
6 kivy.require('1.6.0')
7
8 from kivy.app import App
9 from kivy.core.window import Window
10 from kivy.uix.widget import Widget
11 from kivy.uix.floatlayout import FloatLayout
12 from kivy.graphics import Color, Rectangle
13
14 from erdslangetjie.data import filepath, load
15 from erdslangetjie.level import Level
16 from erdslangetjie.player import ThePlayer, Nemesis
17
18 TILE_SIZE = 24
19
20
21 class GameWindow(FloatLayout):
22
23     def __init__(self, level):
24         super(GameWindow, self).__init__(size=(648, 480))
25         if os.path.exists(filepath(level)):
26             level_data = load(level)
27             self.level = level
28         elif os.path.exists(filepath('levels/' + level)):
29             level_data = load('levels/' + level)
30             self.level = 'levels/' + level
31         else:
32             print 'Unable to find %s - treating this as a new level' % level
33             sys.exit(1)
34         self.level_obj = Level()
35         self.level_obj.load(level_data)
36         level_data.close()
37         self.tiles = {}
38
39         self.player = ThePlayer()
40         self.level_obj.load_tiles()
41         if not self.level_obj.enter_pos:
42             raise RuntimeError('No entry point')
43         self.player_tile = None
44
45         self.player.pos = self.level_obj.enter_pos
46         self.keyboard = Window.request_keyboard(self._closed, self)
47         self.keyboard.bind(on_key_down=self._on_key_down)
48
49     def build(self):
50         self.clear_widgets()
51         self.tiles = {}
52         tiles = self.level_obj.get_tiles()
53         bx, by = 0, 0
54         for tile_line in tiles:
55             bx = 0
56             for tile in tile_line:
57                 node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=(bx, by))
58                 with node.canvas:
59                     Color(1, 1, 1)
60                     Rectangle(pos=node.pos, size=node.size,
61                             texture=tile.texture)
62                 self.add_widget(node)
63                 self.tiles[(bx, by)] = node
64                 bx += TILE_SIZE
65             by += TILE_SIZE
66
67         self.draw_player()
68
69     def draw_player(self):
70         if self.player_tile:
71             self.remove_widget(self.player_tile)
72         player_pos = (self.player.pos[0] * TILE_SIZE,
73                 self.player.pos[1] * TILE_SIZE)
74         self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
75                 pos=player_pos)
76         with self.player_tile.canvas:
77             Color(1, 1, 1)
78             Rectangle(pos=player_pos, size=self.player_tile.size,
79                     texture=self.player.sprite.texture)
80         self.add_widget(self.player_tile)
81
82     def _closed(self):
83         self.keyboard.unbind(on_key_down=self._on_key_down)
84
85     def _on_key_down(self, keyboard, keycode, text, modifiers):
86         # FIXME - likely portablity issues
87         direction = None
88         if keycode[0] == pygame.K_UP:
89             direction = (0, 1)
90         elif keycode[0] == pygame.K_DOWN:
91             direction = (0, -1)
92         elif keycode[0] == pygame.K_LEFT:
93             direction = (-1, 0)
94         elif keycode[0] == pygame.K_RIGHT:
95             direction = (1, 0)
96         if direction:
97             if self.player.can_move(direction, self.level_obj):
98                 self.player.move(direction)
99                 self.draw_player()
100
101
102 class GameApp(App):
103
104     def __init__(self):
105         self.level = 'level1.txt'
106         super(GameApp, self).__init__()
107
108     def build(self):
109         game = GameWindow(self.level)
110         game.build()
111         return game
112
113
114 def main():
115     """ Erdslangetjie, a maze game of eluding nemesis
116     """
117     GameApp().run()