~ubuntu-branches/ubuntu/hardy/solarwolf/hardy

« back to all changes in this revision

Viewing changes to code/input.py

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2002-03-29 10:14:11 UTC
  • Revision ID: james.westby@ubuntu.com-20020329101411-oyzguv1je7ln2p38
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"translate pygame events to controls"
 
2
 
 
3
import pygame.joystick
 
4
from pygame.locals import *
 
5
 
 
6
 
 
7
# control constants
 
8
UP        = 1
 
9
DOWN      = 2
 
10
LEFT      = 3
 
11
RIGHT     = 4
 
12
PRESS     = 5
 
13
RELEASE   = 6
 
14
ABORT     = 7
 
15
 
 
16
 
 
17
#translation tables
 
18
keyboard_table = {
 
19
    K_UP: UP,
 
20
    K_DOWN: DOWN,
 
21
    K_LEFT: LEFT,
 
22
    K_RIGHT: RIGHT,
 
23
    K_SPACE: PRESS,
 
24
    K_RETURN: PRESS,
 
25
    K_ESCAPE: ABORT
 
26
}
 
27
 
 
28
 
 
29
joystick = None
 
30
lastjoyx = RELEASE
 
31
lastjoyy = RELEASE
 
32
 
 
33
 
 
34
def init():
 
35
    "init the joystick"
 
36
    global joystick
 
37
    try:
 
38
        num = pygame.joystick.get_count()
 
39
        if num > 0:
 
40
            joystick = pygame.joystick.Joystick(0)
 
41
            joystick.init()
 
42
    except pygame.error:
 
43
        joystick = None
 
44
 
 
45
 
 
46
def joyindex(val):
 
47
    return int(val*1.9)+1
 
48
 
 
49
def translate(event):
 
50
    global lastjoyx, lastjoyy
 
51
    if event.type == KEYDOWN:
 
52
        return keyboard_table.get(event.key, None)
 
53
    elif event.type == KEYUP and event.key in (K_SPACE, K_RETURN):
 
54
        return RELEASE
 
55
 
 
56
    elif event.type == JOYAXISMOTION:
 
57
        if event.axis == 1:
 
58
            joy = (UP,RELEASE,DOWN)[joyindex(event.value)]
 
59
            if joy != lastjoyy:
 
60
                lastjoyy = joy
 
61
                if joy == RELEASE:
 
62
                    joy = (LEFT, RELEASE, RIGHT)[joyindex(joystick.get_axis(0))]
 
63
                    lastjoyx = joy
 
64
                if joy != RELEASE:
 
65
                    return joy
 
66
        elif event.axis == 0:
 
67
            joy = (LEFT,RELEASE,RIGHT)[joyindex(event.value)]
 
68
            if joy != lastjoyx:
 
69
                lastjoyx = joy
 
70
                if joy == RELEASE:
 
71
                    joy = (UP, RELEASE, DOWN)[joyindex(joystick.get_axis(1))]
 
72
                    lastjoyy = joy
 
73
                if joy != RELEASE:
 
74
                    return joy
 
75
 
 
76
    elif event.type == JOYBUTTONDOWN:
 
77
        if event.button == 0:
 
78
            return PRESS
 
79
        elif event.button > 2:
 
80
            return ABORT
 
81
 
 
82
    elif event.type == JOYBUTTONUP and event.button == 0:
 
83
        return RELEASE
 
84