Dru89

Untitled

Mar 12th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. import kivy
  5. from kivy.app import App
  6. from kivy.properties import ObjectProperty, NumericProperty
  7. from kivy.core.window import Window
  8. from kivy.uix.image import Image
  9. from kivy.uix.widget import Widget
  10. from kivy.factory import Factory
  11. from kivy.config import Config
  12.  
  13. class TextureAtlas(object):
  14.     def __init__(self, filename, rows=1, columns=1, total=None):
  15.         try:
  16.             self._image = Image(source=filename)
  17.         except Exception, message:
  18.             print message
  19.         else:
  20.             self._rows = rows
  21.             self._columns = columns
  22.             self._total = total if total is not None else rows * columns
  23.            
  24.             self._image_width = self._texture.width / columns
  25.             self._image_height = self._texture.height / rows
  26.    
  27.     def get_image_by_radians(self, radians):
  28.         print radians
  29.         index = round(self._total * ((radians % (2 * math.pi)) / (2 * math.pi)))
  30.         return self.get_image(index)
  31.    
  32.     def get_image(self, index):
  33.         index = int(index) % self._total
  34.         row, col = index / self._columns, index % self._columns
  35.         return self.texture.get_region(col * self._image_width,
  36.                                         row * self._image_height,
  37.                                         self._image_width,
  38.                                         self._image_height)
  39.    
  40.     @property
  41.     def image(self): return self._image
  42.    
  43.     @property
  44.     def texture(self): return self._image.texture
  45.  
  46.     @property
  47.     def width(self): return self._image_width
  48.    
  49.     @property
  50.     def height(self): return self._image_height
  51.  
  52.     @property
  53.     def size(self): return (self.width, self.height)
  54.  
  55. class Blaster(Widget):
  56.     THRUST_KEYCODES = [pygame.K_UP, pygame.K_w]
  57.     ROTATE_RIGHT_KEYCODES = [pygame.K_RIGHT, pygame.K_d]
  58.     ROTATE_LEFT_KEYCODES = [pygame.K_LEFT, pygame.K_a]
  59.    
  60.     atlas = TextureAtlas('blasteroid1.png', 4, 4, 16)
  61.     rotational_position = NumericProperty(0)
  62.     def __init__(self, *args, **kwargs):
  63.         super(Blaster, self).__init__(*args, **kwargs)
  64.         self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
  65.         self._keyboard.bind(on_key_down=self._keydown)
  66.        
  67.     def _keyboard_closed(self):
  68.         """This method is called whenever the keyboard is closed."""
  69.        
  70.     def _keydown(self, keyboard, keycode, text, modifiers):
  71.         handled = False
  72.         if keycode[0] in self.ROTATE_RIGHT_KEYCODES:
  73.             handled = True
  74.             self.rotational_position += 0.05
  75.             self.canvas.ask_update()
  76.         elif keycode[0] in self.ROTATE_LEFT_KEYCODES:
  77.             handled = True
  78.             self.rotational_position -= 0.05
  79.             self.canvas.ask_update()
  80.         return handled
  81.  
  82. class BlasteroidsGame(Widget):
  83.     blaster = ObjectProperty(None)
  84.  
  85. Factory.register("Blaster", Blaster)
  86. Factory.register("BlasteroidsGame", BlasteroidsGame)
  87.  
  88. class BlasteroidsApp(App):
  89.     def build(self):
  90.         return BlasteroidsGame()
  91.        
  92. if __name__ in ('__android__', '__main__'):
  93.     BlasteroidsApp().run()
  94.  
  95.  
  96. ---------------------------
  97.  
  98. #:kivy 1.0.9
  99.  
  100. <Blaster>:
  101.     size: self.atlas.size
  102.     canvas:
  103.         Rectangle:
  104.             id: image
  105.             texture: self.atlas.get_image_by_radians(self.rotational_position)
  106.             pos: self.pos
  107.             size: self.size
  108.  
  109. <BlasteroidsGame>:
  110.     blaster: blasteroids_blaster
  111.     Label:
  112.         text: str(root.blaster.rotational_position)
  113.         pos: root.center
  114.     Blaster:
  115.         id: blasteroids_blaster
  116.         center: self.parent.center
Advertisement
Add Comment
Please, Sign In to add comment