Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / fullscreen-1.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2001-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
# fullscreen-1: Setting up : displaying a 3D model
20
21
# Soya can also render in fullscreen.
22
# Beware: you MUST write a way to exit the program, e.g. by processing SDL events!
23
24
25
# Imports sys, os modules and the Soya module.
26
27
import sys, os, os.path, soya, soya.sdlconst
28
29
# Inits Soya in 800x600 fullscreen mode (the last argument, 1, stands for fullscreen).
30
# The first argument in the window name, which is only visible in window mode.
31
32
soya.init("Soya fullscreen tutorial", 800, 600, 1)
33
34
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
35
36
37
# The rest of the tutorial is identical to basic-5.py
38
39
scene = soya.World()
40
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
for event in soya.process_event():
52
if event[0] == soya.sdlconst.KEYDOWN:
53
if event[1] == soya.sdlconst.K_UP: self.speed.z = -0.2
54
elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.1
55
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
69
def advance_time(self, proportion):
70
soya.Volume.advance_time(self, proportion)
71
self.add_mul_vector(proportion, self.speed)
72
73
74
class 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
80
def begin_round(self):
81
soya.Volume.begin_round(self)
82
self.look_at(self.previous)
83
if self.distance_to(self.previous) < 1.5: self.speed.z = 0.0
84
else: self.speed.z = -0.2
85
86
def advance_time(self, proportion):
87
soya.Volume.advance_time(self, proportion)
88
self.add_mul_vector(proportion, self.speed)
89
90
91
# Creates a caterpillar head and 10 caterpillar piece of body.
92
93
caterpillar_head = CaterpillarHead(scene)
94
caterpillar_head.rotate_lateral(90.0)
95
96
previous_caterpillar_piece = caterpillar_head
97
for i in range(10):
98
previous_caterpillar_piece = CaterpillarPiece(scene, previous_caterpillar_piece)
99
previous_caterpillar_piece.x = i + 1
100
101
# Creates a light.
102
103
light = soya.Light(scene)
104
light.set_xyz(2.0, 5.0, 0.0)
105
106
# Creates a camera.
107
108
camera = soya.Camera(scene)
109
camera.set_xyz(0.0, 15.0, 15.0)
110
camera.look_at(caterpillar_head)
111
soya.set_root_widget(camera)
112
113
soya.Idler(scene).idle()
114