Add import to pyworlds to avoid errors.
[pyworlds.git] / tests / soya-game-skel1.py
blob57a4fd3d20ae141ddbf17688e120ee49a2754beb
1 #!/usr/bin/python
2 # Game Skeleton
3 # Copyright (C) 2003-2004 Jean-Baptiste LAMY
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # Soya gaming tutorial, lesson 1
20 # Create the demo level
22 # A bunch of import
23 import sys, os, os.path
24 import soya
25 import soya.widget as widget
27 # Inits Soya
28 soya.init()
30 # Define data path (=where to find models, textures, ...)
31 HERE = os.path.dirname(sys.argv[0])
32 soya.path.append(os.path.join(HERE, "data"))
34 class Level(soya.World):
35 """A game level.
36 Level is a subclass of soya.World.
37 According to the game you are working on, you'll probably want to add
38 attributes and methods to the level class."""
40 def create_level():
41 """This function creates and saves the game skeleton demo level."""
43 # Create a level object
44 level = Level()
46 # Separates static and non static parts
47 # This will speed up network games, since only the non static part will be
48 # sent on the network
49 level_static = soya.World(level)
51 # Load 3 materials (= textures) for files ./materials{grass|ground|snow}.data
52 grass = soya.Material.get("grass")
53 ground = soya.Material.get("ground")
54 snow = soya.Material.get("snow")
56 # Creates a terrain, from the heighmap "./images/map.png"
57 # The terrain is in the static part (=level_static), because it won't change along the game.
58 terrain = soya.Terrain(level_static)
59 terrain.y = -35.0
60 terrain.from_image(soya.Image.get("map.png"))
62 # Sets how high is the terrain
63 terrain.multiply_height(50.0)
65 # These values are trade of between quality and speed
66 terrain.scale_factor = 1.5
67 terrain.texture_factor = 1.5
69 # Set the texture on the terrain, according to the height
70 # (i.e. height 0.0 to 15.0 are textured with grass, ...)
71 terrain.set_material_layer(grass, 0.0, 15.0)
72 terrain.set_material_layer(ground, 15.0, 30.0)
73 terrain.set_material_layer(snow, 30.0, 50.0)
75 # Loads the model "./models/ferme.data"
76 # This model has been created in Blender
77 house = soya.Model.get("ferme")
79 # Adds 2 houses in the level
80 house1 = soya.Body(level_static, house)
81 house1.set_xyz(250.0, -7.2, 182.0)
83 house2 = soya.Body(level_static, house)
84 house2.set_xyz(216.0, -11.25, 200.0)
85 house2.rotate_y(100.0) # degrees
87 # Creates a light in the level, similar to a sun (=a directional light)
88 sun = soya.Light(level_static)
89 sun.directional = 1
90 sun.diffuse = (1.0, 0.8, 0.4, 1.0)
91 sun.rotate_x(-45.0)
93 # Creates a sky atmosphere, with fog
94 atmosphere = soya.SkyAtmosphere()
95 atmosphere.ambient = (0.3, 0.3, 0.4, 1.0)
96 atmosphere.fog = 1
97 atmosphere.fog_type = 0
98 atmosphere.fog_start = 50.0
99 atmosphere.fog_end = 100.0
100 atmosphere.fog_color = atmosphere.bg_color = (0.2, 0.5, 0.7, 1.0)
101 atmosphere.skyplane = 1
102 atmosphere.sky_color = (1.5, 1.0, 0.8, 1.0)
104 # Set the atmosphere to the level
105 level.atmosphere = atmosphere
107 # Save the level as "./worlds/level_demo.data" (remember, levels are subclasses of worlds)
108 level_static.filename = level.name = "level_demo_static"
109 level_static.save()
110 level.filename = level.name = "level_demo"
111 level.save()
114 # Now we just display the level
118 # This function must be called the first time you run game_skel.
119 # Then, you can comment it, since the level has been saved.
120 create_level()
122 # Create the scene (a world with no parent)
123 scene = soya.World()
125 # Loads the level, and put it in the scene
126 level = soya.World.get("level_demo")
127 scene.add(level)
129 # Creates a camera in the scene
130 camera = soya.Camera(scene)
131 camera.set_xyz(222.0, 0.0, 230.0)
132 camera.front = 1
133 camera.back = 100
135 # Creates a widget group, containing the camera and a label showing the FPS.
136 soya.set_root_widget(widget.Group())
137 soya.root_widget.add(camera)
138 soya.root_widget.add(widget.FPSLabel())
140 #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"))
142 # Creates and run an "main_loop" (=an object that manage time and regulate FPS)
143 # By default, FPS is locked at 40.
144 soya.MainLoop(scene).main_loop()