Skip to content

Instantly share code, notes, and snippets.

@udiboy1209
Created February 8, 2017 14:23
Show Gist options
  • Save udiboy1209/3a23e83669b51dfc15584ca104c10952 to your computer and use it in GitHub Desktop.
Save udiboy1209/3a23e83669b51dfc15584ca104c10952 to your computer and use it in GitHub Desktop.
Modified kivent example 14
from kivy.app import App
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivent_core.systems.gamesystem import GameSystem
from kivent_core.managers.resource_managers import texture_manager
from os.path import dirname, join, abspath
from kivent_maps import map_utils
from kivent_maps.map_system import MapSystem
Window.size = (640, 640)
def get_asset_path(asset, asset_loc):
return join(dirname(dirname(abspath(__file__))), asset_loc, asset)
class TestGame(Widget):
def __init__(self, **kwargs):
super(TestGame, self).__init__(**kwargs)
# Args required for Renderer init
map_render_args = {
'zones': ['general'],
'frame_count': 2,
'gameview': 'camera1',
'shader_source': get_asset_path('positionshader.glsl', 'assets/glsl')
}
# Args for AnimationSystem init
map_anim_args = {
'zones': ['general'],
}
# Args for PolyRenderer init
map_poly_args = {
'zones': ['general'],
'frame_count': 2,
'gameview': 'camera1',
'shader_source': 'poscolorshader.glsl'
}
# Initialise systems for 4 map layers and get the renderer and
# animator names
self.map_layers, self.map_layer_animators = \
map_utils.load_map_systems(4, self.gameworld,
map_render_args, map_anim_args, map_poly_args)
# Set the camera1 render order to render lower layers first
self.camera1.render_system_order = reversed(self.map_layers)
# Init gameworld with all the systems
self.gameworld.init_gameworld(
['position', 'color', 'camera1', 'tile_map']
+ self.map_layers
+ self.map_layer_animators,
callback=self.init_game)
def init_game(self):
self.setup_states()
self.setup_tile_map()
self.set_state()
def setup_tile_map(self):
# The map file to load
# Change to hexagonal/isometric/isometric_staggered.tmx for other maps
filename = get_asset_path('isometric.tmx','assets/maps')
map_manager = self.gameworld.managers['map_manager']
# Load TMX data and create a TileMap from it
map_name = map_utils.parse_tmx(filename, self.gameworld)
# Initialise each tile as an entity in the gameworld
map_utils.init_entities_from_map(map_manager.maps[map_name],
self.gameworld.init_entity)
self.tilemap = map_manager.maps[map_name]
def setup_states(self):
# We want renderers to be added and unpaused
# and animators to be unpaused
self.gameworld.add_state(state_name='main',
systems_added=self.map_layers,
systems_unpaused=self.map_layer_animators + self.map_layers)
def set_state(self):
self.gameworld.state = 'main'
def screen_touched(self,event):
x,y = event.pos
x -= self.pos[0]
y -= self.pos[1]
print('Tile %d,%d clicked' % self.tilemap.get_tile_index(x,y))
class DebugPanel(Widget):
fps = StringProperty(None)
def __init__(self, **kwargs):
super(DebugPanel, self).__init__(**kwargs)
Clock.schedule_once(self.update_fps)
def update_fps(self,dt):
self.fps = str(int(Clock.get_fps()))
Clock.schedule_once(self.update_fps, .05)
class YourAppNameApp(App):
pass
if __name__ == '__main__':
YourAppNameApp().run()
#:kivy 1.9.0
#:import get_asset_path __main__.get_asset_path
#:set map_layers ['map_layer%d' % i for i in range(4)]
TestGame:
on_touch_down: self.screen_touched(args[1])
<TestGame>:
gameworld: gameworld
camera1: camera1
GameWorld:
id: gameworld
gamescreenmanager: gamescreenmanager
size_of_gameworld: 250*1024
system_count: 4
zones: {'general': 100000}
PositionSystem2D:
system_id: 'position'
gameworld: gameworld
zones: ['general']
ColorSystem:
system_id: 'color'
gameworld: gameworld
zones: ['general']
MapSystem:
system_id: 'tile_map'
id: tile_map
gameworld: gameworld
zones: ['general']
GameView:
system_id: 'camera1'
gameworld: gameworld
size: root.size
window_size: root.size
pos: root.pos
do_scroll_lock: False
id: camera1
updateable: True
GameScreenManager:
id: gamescreenmanager
size: root.size
pos: root.pos
gameworld: gameworld
<GameScreenManager>:
MainScreen:
id: main_screen
<MainScreen@GameScreen>:
name: 'main'
FloatLayout:
DebugPanel:
size_hint: (.2, .1)
pos_hint: {'x': .225, 'y': .025}
<DebugPanel>:
Label:
pos: root.pos
size: root.size
font_size: root.size[1]*.5
halign: 'center'
valign: 'middle'
color: (1,1,1,1)
text: 'FPS: ' + root.fps if root.fps != None else 'FPS:'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment