From fa32d9eee7eb74ba046b31da91fe5e5580d59cb0 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 15 Apr 2013 14:40:27 +0200 Subject: [PATCH] Simple keyboard based maze navigation --- erdslangetjie/__main__.py | 55 +++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index 9802657..816ec79 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -1,10 +1,12 @@ import sys import os import kivy +import pygame kivy.require('1.6.0') from kivy.app import App +from kivy.core.window import Window from kivy.uix.widget import Widget from kivy.uix.floatlayout import FloatLayout from kivy.graphics import Color, Rectangle @@ -13,6 +15,8 @@ from erdslangetjie.data import filepath, load from erdslangetjie.level import Level from erdslangetjie.player import ThePlayer, Nemesis +TILE_SIZE = 24 + class GameWindow(FloatLayout): @@ -30,40 +34,69 @@ class GameWindow(FloatLayout): self.level_obj = Level() self.level_obj.load(level_data) level_data.close() + self.tiles = {} self.player = ThePlayer() self.level_obj.load_tiles() if not self.level_obj.enter_pos: raise RuntimeError('No entry point') + self.player_tile = None self.player.pos = self.level_obj.enter_pos + self.keyboard = Window.request_keyboard(self._closed, self) + self.keyboard.bind(on_key_down=self._on_key_down) def build(self): self.clear_widgets() + self.tiles = {} tiles = self.level_obj.get_tiles() - tile_size = 24 bx, by = 0, 0 for tile_line in tiles: bx = 0 for tile in tile_line: - node = Widget(size=(tile_size, tile_size), pos=(bx, by)) + node = Widget(size=(TILE_SIZE, TILE_SIZE), pos=(bx, by)) with node.canvas: Color(1, 1, 1) Rectangle(pos=node.pos, size=node.size, texture=tile.texture) self.add_widget(node) - bx += tile_size - by += tile_size - - player_pos = (self.player.pos[0] * tile_size, - self.player.pos[1] * tile_size) - player_tile = Widget(size=(tile_size, tile_size), + self.tiles[(bx, by)] = node + bx += TILE_SIZE + by += TILE_SIZE + + self.draw_player() + + def draw_player(self): + if self.player_tile: + self.remove_widget(self.player_tile) + player_pos = (self.player.pos[0] * TILE_SIZE, + self.player.pos[1] * TILE_SIZE) + self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE), pos=player_pos) - with player_tile.canvas: + with self.player_tile.canvas: Color(1, 1, 1) - Rectangle(pos=player_pos, size=player_tile.size, + Rectangle(pos=player_pos, size=self.player_tile.size, texture=self.player.sprite.texture) - self.add_widget(player_tile) + self.add_widget(self.player_tile) + + def _closed(self): + self.keyboard.unbind(on_key_down=self._on_key_down) + + def _on_key_down(self, keyboard, keycode, text, modifiers): + # FIXME - likely portablity issues + direction = None + if keycode[0] == pygame.K_UP: + direction = (0, 1) + elif keycode[0] == pygame.K_DOWN: + direction = (0, -1) + elif keycode[0] == pygame.K_LEFT: + direction = (-1, 0) + elif keycode[0] == pygame.K_RIGHT: + direction = (1, 0) + if direction: + if self.player.can_move(direction, self.level_obj): + self.player.move(direction) + self.draw_player() class GameApp(App): -- 2.34.1