Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / tofudemo_create_level.pyOpen in CoCalc
Author: William A. Stein
1#! /usr/bin/python -O
2
3# Game Skeleton
4# Copyright (C) 2003-2004 Jean-Baptiste LAMY
5#
6# This program is free software; you can redistribute it and/or modify
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20# Soya gaming tutorial, lesson 1
21# Create the demo level
22
23# A bunch of import
24import sys, os, os.path
25import tofu
26import soya
27import soya.widget as widget
28
29from tofudemo import Level
30
31
32# Inits Soya
33soya.init()
34
35def create_level():
36  """This function creates and saves the game skeleton demo level."""
37
38  # Create a level object
39  level = Level()
40
41  # Separates static and non static parts
42  # This will speed up network games, since only the non static part will be
43  # sent on the network
44  level_static = soya.World(level)
45
46  # Load 3 materials (= textures) for files ./materials{grass|ground|snow}.data
47  grass  = soya.Material.get("grass")
48  ground = soya.Material.get("ground")
49  snow   = soya.Material.get("snow")
50
51  # Creates a landscape, from the heighmap "./images/map.png"
52  # The landscape is in the static part (=level_static), because it won't change along the game.
53  land = soya.Land(level_static)
54  land.y = -35.0
55  land.from_image(soya.Image.get("map.png"))
56
57  # Sets how high is the landscape
58  land.multiply_height(50.0)
59
60  # These values are trade of between quality and speed
61  land.map_size = 8
62  land.scale_factor = 1.5
63  land.texture_factor = 1.0
64
65  # Set the texture on the landscape, according to the height
66  # (i.e. height 0.0 to 15.0 are textured with grass, ...)
67  land.set_material_layer(grass,   0.0,  15.0)
68  land.set_material_layer(ground, 15.0,  25.0)
69  land.set_material_layer(snow,   25.0,  50.0)
70
71  # Loads the shape "./shapes/ferme.data"
72  # This model has been created in Blender
73  house = soya.Shape.get("ferme")
74
75  # Adds 2 houses in the level
76  house1 = soya.Volume(level_static, house)
77  house1.set_xyz(250.0, -7.2, 182.0)
78
79  house2 = soya.Volume(level_static, house)
80  house2.set_xyz(216.0, -11.25, 200.0)
81  house2.rotate_lateral(100.0) # degrees
82
83  # Creates a light in the level, similar to a sun (=a directional light)
84  sun = soya.Light(level_static)
85  sun.directional = 1
86  sun.diffuse = (1.0, 0.8, 0.4, 1.0)
87  sun.rotate_vertical(-45.0)
88
89  # Creates a sky atmosphere, with fog
90  atmosphere = soya.SkyAtmosphere()
91  atmosphere.ambient = (0.3, 0.3, 0.4, 1.0)
92  atmosphere.fog = 1
93  atmosphere.fog_type  = 0
94  atmosphere.fog_start = 40.0
95  atmosphere.fog_end   = 50.0
96  atmosphere.fog_color = atmosphere.bg_color = (0.2, 0.5, 0.7, 1.0)
97  atmosphere.skyplane  = 1
98  atmosphere.sky_color = (1.5, 1.0, 0.8, 1.0)
99
100  # Set the atmosphere to the level
101  level.atmosphere = atmosphere
102
103  # Save the level as "./worlds/level_demo.data" (remember, levels are subclasses of worlds)
104  level_static.filename = level.name = "level_tofudemo_static"
105  level_static.save()
106  level.filename = level.name = "level_tofudemo"
107  level.save()
108
109
110# Now we just display the level
111
112
113
114# This function must be called the first time you run game_skel.
115# Then, you can comment it, since the level has been saved.
116create_level()
117
118# Create the scene (a world with no parent)
119scene = soya.World()
120
121# Loads the level, and put it in the scene
122level = tofu.Level.get("level_tofudemo")
124
125# Creates a camera in the scene
126camera = soya.Camera(scene)
127camera.set_xyz(222.0, 0.0, 230.0)
128
129# Creates a widget group, containing the camera and a label showing the FPS.
130soya.set_root_widget(widget.Group())