Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / basic-5.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2004 Jean-Baptiste LAMY
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
# basic-5: Event management : a keyboard-controlled caterpillar
20
21
# In this lesson, our caterpillar will obey you !
22
# You'll learn how to use SDL events with Soya.
23
# Use the cursor arrows to control the caterpillar.
24
25
26
# Import the Soya module.
27
# The soya.sdlconst module contains all the SDL constants.
28
29
import sys, os, os.path, soya, soya.sdlconst
30
31
soya.init()
32
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
33
34
# Creates a scene.
35
36
scene = soya.World()
37
38
39
# The CaterpillarHead class is very similar to the CaterpillarHead class of the previous
40
# lesson.
41
42
class CaterpillarHead(soya.Volume):
43
def __init__(self, parent):
44
soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar_head"))
45
self.speed = soya.Vector(self, 0.0, 0.0, 0.0)
46
self.rotation_lateral_speed = 0.0
47
48
def begin_round(self):
49
soya.Volume.begin_round(self)
50
51
# Loops over all Soya / SDL events.
52
# Each event is a tuple ; the first value indicates the event type and the other
53
# values depend on the type. The following event types exist :
54
# - (KEYDOWN, keysym, modifier) where keysym is the key's code (a K_* constant)
55
# and modifier is a flag combining some of the MOD_* constant (to test the presence
56
# of a modifier, do e.g. for left shift: modifier & soya.sdlconst.MOD_LSHIFT).
57
# - (KEYUP, keysym, modifier)
58
# - (MOUSEMOTION, x, y, xrel, yrel) where x and y are the mouse coordinates (in
59
# pixel) ; xrel and yrel are the relative mouse coordinates (the difference since
60
# next mouse motion event).
61
# - (MOUSEBUTTONDOWN, button, x, y) where button is the mouse button number and
62
# x and y are the mouse coordinates. Mouse buttons are :
63
# - 1 : left
64
# - 2 : middle
65
# - 3 : right
66
# - 4 : roll up
67
# - 5 : roll down
68
# - (MOUSEBUTTONUP, button, x, y)
69
# - (JOYAXISMOTION, axis, value) XXX
70
# - (JOYBUTTONDOWN, button) XXX
71
# - (VIDEORESIZE, new_width, new_height)
72
73
for event in soya.process_event():
74
75
# Checks for key down (press) events.
76
77
if event[0] == soya.sdlconst.KEYDOWN:
78
79
# The up and down arrows set the caterpillar speed to a negative or positive value.
80
81
if event[1] == soya.sdlconst.K_UP: self.speed.z = -0.2
82
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.1
83
84
# The left and right arrow modify the rotation speed.
85
86
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 10.0
87
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = -10.0
88
89
# Pressing the escape or 'q' key will exit the idler mainloop, and thus terminate
90
# the program. soya.IDLER.stop() is the right way to end your application, and
91
# causes the Idler.idle() method to return.
92
93
elif event[1] == soya.sdlconst.K_q: soya.IDLER.stop()
94
elif event[1] == soya.sdlconst.K_ESCAPE: soya.IDLER.stop()
95
96
# Checks for key up (release) events.
97
98
elif event[0] == soya.sdlconst.KEYUP:
99
100
# When up or down arrows are released, the speed is set to zero.
101
102
if event[1] == soya.sdlconst.K_UP: self.speed.z = 0.0
103
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.0
104
105
# When left or right arrows are released, the rotation speed is set to zero.
106
107
elif event[1] == soya.sdlconst.K_LEFT: self.rotation_lateral_speed = 0.0
108
elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_lateral_speed = 0.0
109
110
elif event[0] == soya.sdlconst.QUIT:
111
soya.IDLER.stop()
112
113
# Do the rotation
114
115
self.rotate_lateral(self.rotation_lateral_speed)
116
117
def advance_time(self, proportion):
118
soya.Volume.advance_time(self, proportion)
119
self.add_mul_vector(proportion, self.speed)
120
121
122
# CaterpillarPiece hasn't changed since the previous tutorial.
123
124
class CaterpillarPiece(soya.Volume):
125
def __init__(self, parent, previous):
126
soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar"))
127
self.previous = previous
128
self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
129
130
def begin_round(self):
131
soya.Volume.begin_round(self)
132
self.look_at(self.previous)
133
if self.distance_to(self.previous) < 1.5: self.speed.z = 0.0
134
else: self.speed.z = -0.2
135
136
def advance_time(self, proportion):
137
soya.Volume.advance_time(self, proportion)
138
self.add_mul_vector(proportion, self.speed)
139
140
141
# Creates a caterpillar head and 10 caterpillar piece of body.
142
143
caterpillar_head = CaterpillarHead(scene)
144
caterpillar_head.rotate_lateral(90.0)
145
146
previous_caterpillar_piece = caterpillar_head
147
for i in range(10):
148
previous_caterpillar_piece = CaterpillarPiece(scene, previous_caterpillar_piece)
149
previous_caterpillar_piece.x = i + 1
150
151
# Creates a light.
152
153
light = soya.Light(scene)
154
light.set_xyz(2.0, 5.0, 0.0)
155
156
# Creates a camera.
157
158
camera = soya.Camera(scene)
159
camera.set_xyz(0.0, 15.0, 15.0)
160
camera.look_at(caterpillar_head)
161
soya.set_root_widget(camera)
162
163
soya.Idler(scene).idle()
164