Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / traveling-camera-1.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
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
29import sys, os, os.path, soya, soya.sdlconst
30
31soya.init()
32soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
33
34# Creates a scene.
35
36scene = soya.World()
37
38
39# The CaterpillarHead class is very similar to the CaterpillarHead class of the previous
40# lesson.
41
43  def __init__(self, parent):
45    self.speed                  = soya.Vector(self, 0.0, 0.0, 0.0)
46    self.rotation_lateral_speed = 0.0
47    self.solid = 0
48
49  def begin_round(self):
50    soya.Volume.begin_round(self)
51
52    for event in soya.process_event():
53      if event[0] == soya.sdlconst.KEYDOWN:
54        if   event[1] == soya.sdlconst.K_UP:     self.speed.z = -0.2
55        elif event[1] == soya.sdlconst.K_DOWN:   self.speed.z =  0.1
56        elif event[1] == soya.sdlconst.K_LEFT:   self.rotation_lateral_speed =  10.0
57        elif event[1] == soya.sdlconst.K_RIGHT:  self.rotation_lateral_speed = -10.0
58        elif event[1] == soya.sdlconst.K_q:      soya.IDLER.stop()
59        elif event[1] == soya.sdlconst.K_ESCAPE: soya.IDLER.stop()
60
61      if event[0] == soya.sdlconst.KEYUP:
62        if   event[1] == soya.sdlconst.K_UP:     self.speed.z = 0.0
63        elif event[1] == soya.sdlconst.K_DOWN:   self.speed.z = 0.0
64        elif event[1] == soya.sdlconst.K_LEFT:   self.rotation_lateral_speed = 0.0
65        elif event[1] == soya.sdlconst.K_RIGHT:  self.rotation_lateral_speed = 0.0
66
67    self.rotate_lateral(self.rotation_lateral_speed)
68
72
73
74class CaterpillarPiece(soya.Volume):
75  def __init__(self, parent, previous):
76    soya.Volume.__init__(self, parent, soya.Shape.get("caterpillar"))
77    self.previous = previous
78    self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
79    self.solid = 0
80
81  def begin_round(self):
82    soya.Volume.begin_round(self)
83    self.look_at(self.previous)
84    if self.distance_to(self.previous) < 1.5: self.speed.z =  0.0
85    else:                                     self.speed.z = -0.2
86
90
91
94
96for i in range(10):
97  previous_caterpillar_piece = CaterpillarPiece(scene, previous_caterpillar_piece)
98  previous_caterpillar_piece.x = i + 1
99
100# Creates a light.
101
102light = soya.Light(scene)
103light.set_xyz(2.0, 5.0, 0.0)
104
105# Creates a camera.
106
107camera = soya.TravelingCamera(scene)
108
110#traveling = soya.ThirdPersonTraveling(soya.Point(caterpillar_head, 0.0, 1.0, 0.0))
111traveling.distance = 15.0
112#traveling.smooth_move     = 1
113traveling.smooth_rotation = 0
114#traveling.direction = soya.Vector(camera, 0.0, 1.0, 2.0)
115#traveling.incline_as = None
116