Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / game_skel-1.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
7
# it under the terms of the GNU General Public License as published by
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
24
import sys, os, os.path
25
import soya
26
import soya.widget as widget
27
28
# Inits Soya
29
soya.init()
30
31
# Define data path (=where to find models, textures, ...)
32
HERE = os.path.dirname(sys.argv[0])
33
soya.path.append(os.path.join(HERE, "data"))
34
35
class Level(soya.World):
36
"""A game level.
37
Level is a subclass of soya.World.
38
According to the game you are working on, you'll probably want to add
39
attributes and methods to the level class."""
40
41
def create_level():
42
"""This function creates and saves the game skeleton demo level."""
43
44
# Create a level object
45
level = Level()
46
47
# Separates static and non static parts
48
# This will speed up network games, since only the non static part will be
49
# sent on the network
50
level_static = soya.World(level)
51
52
# Load 3 materials (= textures) for files ./materials{grass|ground|snow}.data
53
grass = soya.Material.get("grass")
54
ground = soya.Material.get("ground")
55
snow = soya.Material.get("snow")
56
57
# Creates a landscape, from the heighmap "./images/map.png"
58
# The landscape is in the static part (=level_static), because it won't change along the game.
59
land = soya.Land(level_static)
60
land.y = -35.0
61
land.from_image(soya.Image.get("map.png"))
62
63
# Sets how high is the landscape
64
land.multiply_height(50.0)
65
66
# These values are trade of between quality and speed
67
land.scale_factor = 1.5
68
land.texture_factor = 1.0
69
70
# Set the texture on the landscape, according to the height
71
# (i.e. height 0.0 to 15.0 are textured with grass, ...)
72
land.set_material_layer(grass, 0.0, 15.0)
73
land.set_material_layer(ground, 15.0, 25.0)
74
land.set_material_layer(snow, 25.0, 50.0)
75
76
# Loads the shape "./shapes/ferme.data"
77
# This model has been created in Blender
78
house = soya.Shape.get("ferme")
79
80
# Adds 2 houses in the level
81
house1 = soya.Volume(level_static, house)
82
house1.set_xyz(250.0, -7.2, 182.0)
83
84
house2 = soya.Volume(level_static, house)
85
house2.set_xyz(216.0, -11.25, 200.0)
86
house2.rotate_lateral(100.0) # degrees
87
88
# Creates a light in the level, similar to a sun (=a directional light)
89
sun = soya.Light(level_static)
90
sun.directional = 1
91
sun.diffuse = (1.0, 0.8, 0.4, 1.0)
92
sun.rotate_vertical(-45.0)
93
94
# Creates a sky atmosphere, with fog
95
atmosphere = soya.SkyAtmosphere()
96
atmosphere.ambient = (0.3, 0.3, 0.4, 1.0)
97
atmosphere.fog = 1
98
atmosphere.fog_type = 0
99
atmosphere.fog_start = 40.0
100
atmosphere.fog_end = 50.0
101
atmosphere.fog_color = atmosphere.bg_color = (0.2, 0.5, 0.7, 1.0)
102
atmosphere.skyplane = 1
103
atmosphere.sky_color = (1.5, 1.0, 0.8, 1.0)
104
105
# Set the atmosphere to the level
106
level.atmosphere = atmosphere
107
108
# Save the level as "./worlds/level_demo.data" (remember, levels are subclasses of worlds)
109
level_static.filename = level.name = "level_demo_static"
110
level_static.save()
111
level.filename = level.name = "level_demo"
112
level.save()
113
114
115
# Now we just display the level
116
117
118
119
# This function must be called the first time you run game_skel.
120
# Then, you can comment it, since the level has been saved.
121
create_level()
122
123
# Create the scene (a world with no parent)
124
scene = soya.World()
125
126
# Loads the level, and put it in the scene
127
level = soya.World.get("level_demo")
128
scene.add(level)
129
130
# Creates a camera in the scene
131
camera = soya.Camera(scene)
132
camera.set_xyz(222.0, 0.0, 230.0)
133
134
# Creates a widget group, containing the camera and a label showing the FPS.
135
soya.set_root_widget(widget.Group())
136
soya.root_widget.add(camera)
137
soya.root_widget.add(widget.FPSLabel())
138
139
#soya.render(); soya.screenshot().resize((320, 240)).save(os.path.join(os.path.dirname(sys.argv[0]), "results", os.path.basename(sys.argv[0])[:-3] + ".jpeg"))
140
141
# Creates and run an "idler" (=an object that manage time and regulate FPS)
142
# By default, FPS is locked at 40.
143
soya.Idler(scene).idle()
144