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
14 from erdslangetjie.data import filepath, load
15 from erdslangetjie.level import Level
16 from erdslangetjie.player import ThePlayer, Nemesis
21 class GameWindow(FloatLayout):
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)
28 elif os.path.exists(filepath('levels/' + level)):
29 level_data = load('levels/' + level)
30 self.level = 'levels/' + level
32 print 'Unable to find %s - treating this as a new level' % level
34 self.level_obj = Level()
35 self.level_obj.load(level_data)
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
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)
52 tiles = self.level_obj.get_tiles()
54 for tile_line in tiles:
56 for tile in tile_line:
57 node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=(bx, by))
60 Rectangle(pos=node.pos, size=node.size,
63 self.tiles[(bx, by)] = node
69 def draw_player(self):
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),
76 with self.player_tile.canvas:
78 Rectangle(pos=player_pos, size=self.player_tile.size,
79 texture=self.player.sprite.texture)
80 self.add_widget(self.player_tile)
83 self.keyboard.unbind(on_key_down=self._on_key_down)
85 def _on_key_down(self, keyboard, keycode, text, modifiers):
86 # FIXME - likely portablity issues
88 if keycode[0] == pygame.K_UP:
90 elif keycode[0] == pygame.K_DOWN:
92 elif keycode[0] == pygame.K_LEFT:
94 elif keycode[0] == pygame.K_RIGHT:
97 if self.player.can_move(direction, self.level_obj):
98 self.player.move(direction)
105 self.level = 'level1.txt'
106 super(GameApp, self).__init__()
109 game = GameWindow(self.level)
115 """ Erdslangetjie, a maze game of eluding nemesis