Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/game.py
blob: 428525a9187759930fba5cc0bce57e05b9953b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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()