#!/usr/bin/env python import glucosa import gtk import gobject class Game(glucosa.GameArea): def __init__(self): glucosa.GameArea.__init__(self) _events = glucosa.Events(self) _head = Head(_events) self.connect('draw', _head._update) self.add_sprite(_head) class Head(glucosa.Sprite): def __init__(self, _events): _image = glucosa.Image('images/head.png') glucosa.Sprite.__init__(self, _image, 600, 400) _events.connect('key-pressed', self._key_pressed) self._to_rotate = 0 def _key_pressed(self, _event): if _event.is_pressed(_event.K_UP): if self.rotate == 270: self._to_rotate = 360 elif self.rotate == 90: self._to_rotate = 0 elif self.rotate == 180: self._to_rotate = 0 elif _event.is_pressed(_event.K_DOWN): self._to_rotate = 180 elif _event.is_pressed(_event.K_RIGHT): self._to_rotate = 90 elif _event.is_pressed(_event.K_LEFT): self._to_rotate = 270 gobject.timeout_add(45, self.rotate) def rotate(self): _continue = True if self.rotation > self._to_rotate: self.rotation -= 1 elif self.rotation < self._to_rotate: self.rotation += 1 elif self.rotation == self._to_rotate: _continue = False self.emit('update') return _continue def _update(self, widget, event): if self.rotation == 0: self.y -= 2 elif self.rotation == 90: self.x += 2 elif self.rotation == 180: self.y += 2 elif self.rotation == 270: self.x -= 2 x, y, w, h = widget.get_allocation() if self.x < 0: self.x = 0 elif self.x > w: self.x = w elif self.y < 0: self.y = 0 elif self.y > h: self.y = h class Eat(glucosa.Sprite): def __init__(self): _image = glucosa.Image('images/eat.png') glucosa.Sprite.__init__(self, _image, 0, 0) if __name__ == '__main__': w = gtk.Window() w.maximize() w.connect('destroy', gtk.main_quit) w.add(Game()) w.show_all() gtk.main()