Advertisement
Guest User

KivEventTut3Mod

a guest
Apr 27th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.widget import Widget
  3. from kivy.clock import Clock
  4. from kivy.core.window import Window
  5. import kivent
  6. from random import randint
  7. from math import radians
  8.  
  9.  
  10. class TestGame(Widget):
  11.     def __init__(self, **kwargs):
  12.         super(TestGame, self).__init__(**kwargs)
  13.         Clock.schedule_once(self.init_game)
  14.         self.asteroids = []
  15.  
  16.     def init_game(self, dt):
  17.         self.setup_map()
  18.         self.setup_states()
  19.         self.set_state()
  20.         self.draw_some_stuff()
  21.         Clock.schedule_interval(self.update, 0)
  22.  
  23.     def draw_some_stuff(self):
  24.         size = Window.size
  25.         for x in range(50):
  26.             pos = (randint(0, size[0]), randint(0, size[1]))
  27.             self.create_asteroid(pos)
  28.  
  29.     def create_asteroid(self, pos):
  30.         x_vel = randint(-100, 100)
  31.         y_vel = randint(-100, 100)
  32.         angle = radians(randint(-360, 360))
  33.         angular_velocity = radians(randint(-150, -150))
  34.         shape_dict = {'inner_radius': 0, 'outer_radius': 32,
  35.             'mass': 50, 'offset': (0, 0)}
  36.         col_shape = {'shape_type': 'circle', 'elasticity': .5,
  37.             'collision_type': 1, 'shape_info': shape_dict, 'friction': 1.0}
  38.         col_shapes = [col_shape]
  39.         physics_component = {'main_shape': 'circle',
  40.             'velocity': (x_vel, y_vel),
  41.             'position': pos, 'angle': angle,
  42.             'angular_velocity': angular_velocity,
  43.             'vel_limit': 250,
  44.             'ang_vel_limit': radians(200),
  45.             'mass': 50, 'col_shapes': col_shapes}
  46.         create_component_dict = {'physics': physics_component,
  47.             'physics_renderer': {'texture': 'asteroid1', 'size': (64 , 64)},
  48.             'position': pos, 'rotate': 0}
  49.         component_order = ['position', 'rotate',
  50.             'physics', 'physics_renderer']
  51.         asteroidID = self.gameworld.init_entity(create_component_dict, component_order)
  52.         self.asteroids.append(asteroidID)
  53.         return asteroidID
  54.  
  55.     def setup_map(self):
  56.         gameworld = self.gameworld
  57.         gameworld.currentmap = gameworld.systems['map']
  58.  
  59.     def on_touch_move(self, touch):
  60.         pos = (touch.x, touch.y)
  61.         self.create_asteroid(pos)
  62.         for aid in self.asteroids:
  63.           apos = self.gameworld.entities[aid].position
  64.           dvecx = touch.x-apos.x
  65.           dvecy = touch.y-apos.y
  66.           self.gameworld.entities[aid].physics.body.apply_impulse((dvecx,dvecy))
  67.         print len(self.gameworld.entities)
  68.     def on_touch_down(self, touch):
  69.         pos = (touch.x, touch.y)
  70.         self.create_asteroid(pos)
  71.     def update(self, dt):
  72.         self.gameworld.update(dt)
  73.  
  74.     def setup_states(self):
  75.         self.gameworld.add_state(state_name='main',
  76.             systems_added=['renderer', 'physics_renderer'],
  77.             systems_removed=[], systems_paused=[],
  78.             systems_unpaused=['renderer', 'physics_renderer'],
  79.             screenmanager_screen='main')
  80.  
  81.     def set_state(self):
  82.         self.gameworld.state = 'main'
  83.  
  84.  
  85. class YourAppNameApp(App):
  86.     def build(self):
  87.         Window.clearcolor = (0, 0, 0, 1.)
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     YourAppNameApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement