Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- import kivy
- from kivy.app import App
- from kivy.properties import ObjectProperty, NumericProperty
- from kivy.core.window import Window
- from kivy.uix.image import Image
- from kivy.uix.widget import Widget
- from kivy.factory import Factory
- from kivy.config import Config
- class TextureAtlas(object):
- def __init__(self, filename, rows=1, columns=1, total=None):
- try:
- self._image = Image(source=filename)
- except Exception, message:
- print message
- else:
- self._rows = rows
- self._columns = columns
- self._total = total if total is not None else rows * columns
- self._image_width = self._texture.width / columns
- self._image_height = self._texture.height / rows
- def get_image_by_radians(self, radians):
- print radians
- index = round(self._total * ((radians % (2 * math.pi)) / (2 * math.pi)))
- return self.get_image(index)
- def get_image(self, index):
- index = int(index) % self._total
- row, col = index / self._columns, index % self._columns
- return self.texture.get_region(col * self._image_width,
- row * self._image_height,
- self._image_width,
- self._image_height)
- @property
- def image(self): return self._image
- @property
- def texture(self): return self._image.texture
- @property
- def width(self): return self._image_width
- @property
- def height(self): return self._image_height
- @property
- def size(self): return (self.width, self.height)
- class Blaster(Widget):
- THRUST_KEYCODES = [pygame.K_UP, pygame.K_w]
- ROTATE_RIGHT_KEYCODES = [pygame.K_RIGHT, pygame.K_d]
- ROTATE_LEFT_KEYCODES = [pygame.K_LEFT, pygame.K_a]
- atlas = TextureAtlas('blasteroid1.png', 4, 4, 16)
- rotational_position = NumericProperty(0)
- def __init__(self, *args, **kwargs):
- super(Blaster, self).__init__(*args, **kwargs)
- self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
- self._keyboard.bind(on_key_down=self._keydown)
- def _keyboard_closed(self):
- """This method is called whenever the keyboard is closed."""
- def _keydown(self, keyboard, keycode, text, modifiers):
- handled = False
- if keycode[0] in self.ROTATE_RIGHT_KEYCODES:
- handled = True
- self.rotational_position += 0.05
- self.canvas.ask_update()
- elif keycode[0] in self.ROTATE_LEFT_KEYCODES:
- handled = True
- self.rotational_position -= 0.05
- self.canvas.ask_update()
- return handled
- class BlasteroidsGame(Widget):
- blaster = ObjectProperty(None)
- Factory.register("Blaster", Blaster)
- Factory.register("BlasteroidsGame", BlasteroidsGame)
- class BlasteroidsApp(App):
- def build(self):
- return BlasteroidsGame()
- if __name__ in ('__android__', '__main__'):
- BlasteroidsApp().run()
- ---------------------------
- #:kivy 1.0.9
- <Blaster>:
- size: self.atlas.size
- canvas:
- Rectangle:
- id: image
- texture: self.atlas.get_image_by_radians(self.rotational_position)
- pos: self.pos
- size: self.size
- <BlasteroidsGame>:
- blaster: blasteroids_blaster
- Label:
- text: str(root.blaster.rotational_position)
- pos: root.center
- Blaster:
- id: blasteroids_blaster
- center: self.parent.center
Advertisement
Add Comment
Please, Sign In to add comment