2 # -*- indent-tabs-mode: t -*-
5 # Copyright (C) 2004 Jean-Baptiste LAMY
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # basic-5: Event management : a keyboard-controlled caterpillar
24 # In this lesson, our caterpillar will obey you !
25 # You'll learn how to use SDL events with Soya.
26 # Use the cursor arrows to control the caterpillar.
29 # Import the Soya module.
30 # The soya.sdlconst module contains all the SDL constants.
32 import sys
, os
, os
.path
, soya
, soya
.sdlconst
35 soya
.path
.append(os
.path
.join(os
.path
.dirname(sys
.argv
[0]), "data"))
42 # The CaterpillarHead class is very similar to the CaterpillarHead class of the previous
45 class CaterpillarHead(soya
.Body
):
46 def __init__(self
, parent
):
47 soya
.Body
.__init
__(self
, parent
, soya
.Model
.get("caterpillar_head"))
48 self
.speed
= soya
.Vector(self
, 0.0, 0.0, 0.0)
49 self
.rotation_y_speed
= 0.0
51 def begin_round(self
):
52 soya
.Body
.begin_round(self
)
54 # Loops over all Soya / SDL events.
55 # Each event is a tuple ; the first value indicates the event type and the other
56 # values depend on the type. The following event types exist :
57 # - (KEYDOWN, keysym, modifier) where keysym is the key's code (a K_* constant)
58 # and modifier is a flag combining some of the MOD_* constant (to test the presence
59 # of a modifier, do e.g. for left shift: modifier & soya.sdlconst.MOD_LSHIFT).
60 # - (KEYUP, keysym, modifier)
61 # - (MOUSEMOTION, x, y, xrel, yrel) where x and y are the mouse coordinates (in
62 # pixel) ; xrel and yrel are the relative mouse coordinates (the difference since
63 # next mouse motion event).
64 # - (MOUSEBUTTONDOWN, button, x, y) where button is the mouse button number and
65 # x and y are the mouse coordinates. Mouse buttons are :
71 # - (MOUSEBUTTONUP, button, x, y)
72 # - (JOYAXISMOTION, axis, value) XXX
73 # - (JOYBUTTONDOWN, button) XXX
74 # - (VIDEORESIZE, new_width, new_height)
76 for event
in soya
.process_event():
78 # Checks for key down (press) events.
80 if event
[0] == soya
.sdlconst
.KEYDOWN
:
82 # The up and down arrows set the caterpillar speed to a negative or positive value.
84 if event
[1] == soya
.sdlconst
.K_UP
: self
.speed
.z
= -0.2
85 elif event
[1] == soya
.sdlconst
.K_DOWN
: self
.speed
.z
= 0.1
87 # The left and right arrow modify the rotation speed.
89 elif event
[1] == soya
.sdlconst
.K_LEFT
: self
.rotation_y_speed
= 10.0
90 elif event
[1] == soya
.sdlconst
.K_RIGHT
: self
.rotation_y_speed
= -10.0
92 # Pressing the escape or 'q' key will exit the main_loop mainloop, and thus terminate
93 # the program. soya.MAIN_LOOP.stop() is the right way to end your application, and
94 # causes the MainLoop.main_loop() method to return.
96 elif event
[1] == soya
.sdlconst
.K_q
: soya
.MAIN_LOOP
.stop()
97 elif event
[1] == soya
.sdlconst
.K_ESCAPE
: soya
.MAIN_LOOP
.stop()
99 # Checks for key up (release) events.
101 elif event
[0] == soya
.sdlconst
.KEYUP
:
103 # When up or down arrows are released, the speed is set to zero.
105 if event
[1] == soya
.sdlconst
.K_UP
: self
.speed
.z
= 0.0
106 elif event
[1] == soya
.sdlconst
.K_DOWN
: self
.speed
.z
= 0.0
108 # When left or right arrows are released, the rotation speed is set to zero.
110 elif event
[1] == soya
.sdlconst
.K_LEFT
: self
.rotation_y_speed
= 0.0
111 elif event
[1] == soya
.sdlconst
.K_RIGHT
: self
.rotation_y_speed
= 0.0
113 elif event
[0] == soya
.sdlconst
.QUIT
:
114 soya
.MAIN_LOOP
.stop()
118 self
.rotate_y(self
.rotation_y_speed
)
120 def advance_time(self
, proportion
):
121 soya
.Body
.advance_time(self
, proportion
)
122 self
.add_mul_vector(proportion
, self
.speed
)
125 # CaterpillarPiece hasn't changed since the previous tutorial.
127 class CaterpillarPiece(soya
.Body
):
128 def __init__(self
, parent
, previous
):
129 soya
.Body
.__init
__(self
, parent
, soya
.Model
.get("caterpillar"))
130 self
.previous
= previous
131 self
.speed
= soya
.Vector(self
, 0.0, 0.0, -0.2)
133 def begin_round(self
):
134 soya
.Body
.begin_round(self
)
135 self
.look_at(self
.previous
)
136 if self
.distance_to(self
.previous
) < 1.5: self
.speed
.z
= 0.0
137 else: self
.speed
.z
= -0.2
139 def advance_time(self
, proportion
):
140 soya
.Body
.advance_time(self
, proportion
)
141 self
.add_mul_vector(proportion
, self
.speed
)
144 # Creates a caterpillar head and 10 caterpillar piece of body.
146 caterpillar_head
= CaterpillarHead(scene
)
147 caterpillar_head
.rotate_y(90.0)
149 previous_caterpillar_piece
= caterpillar_head
151 previous_caterpillar_piece
= CaterpillarPiece(scene
, previous_caterpillar_piece
)
152 previous_caterpillar_piece
.x
= i
+ 1
156 light
= soya
.Light(scene
)
157 light
.set_xyz(2.0, 5.0, 0.0)
161 camera
= soya
.Camera(scene
)
162 camera
.set_xyz(0.0, 15.0, 15.0)
163 camera
.look_at(caterpillar_head
)
164 soya
.set_root_widget(camera
)
166 soya
.MainLoop(scene
).main_loop()