root / trunk / pysoy / examples / CollideBlocks.py

Revision 1350, 2.8 kB (checked in by ArcRiley, 4 months ago)

Tickets #898 #915 :

  • added docs to soy.actions.Action .Callback & .Force
  • changed soy.actions.Force to take a Vector as second argument
  • updated examples to new Force API
  • added Id keyword to Locomotive and Rotate classes
  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2import soy
3import blocks
4from time import sleep
5
6sce = soy.scenes.Scene()
7cam = soy.bodies.Camera(sce)
8cam.position = (0.0, 0.0, 15.0)
9lig = soy.bodies.Light(sce)
10lig.position = (-10.0,10.0,2.0)
11
12black = soy.materials.Material()
13black.ambient = soy.colors.black
14black.diffuse = soy.colors.Color('#222')
15black.specular= soy.colors.Color('#222')
16black.shininess = 5.0
17colors = {
18  'Aventurine'     : (soy.materials.aventurine,    black, ( 0, 0, 0)),
19  'Basalt'         : (soy.materials.basalt,        black, ( 4,-1,-4)),
20  'Copper'         : (soy.materials.copper,        black, (-3,-2,-2)),
21  'CopperSulfate'  : (soy.materials.copperSulfate, black, ( 0,-2,-1)),
22  'DarkWood'       : (soy.materials.darkWood,      black, ( 5, 3,-6)),
23  'Pearl'          : (soy.materials.pearl,         black, (-1, 2,-3)),
24  'Rhodonite'      : (soy.materials.rhodonite,     black, (-4, 1,-5)),
25  'VelvetyRed'     : (soy.materials.velvetyRed,    black, ( 3, 0,-8)),
26}
27
28bks = blocks.blocks(sce, colors)
29
30fps = soy.textures.Print()
31
32scr = soy.Screen()
33win = soy.Window(scr, 'CollideBlocks', background=soy.colors.gray)
34pro = soy.widgets.Projector(win, camera=cam)
35can = soy.widgets.Canvas(win, texture=fps)
36
37def wireframeToggle() :
38  if cam.wireframe :
39    cam.wireframe = False
40  else :
41    cam.wireframe = True
42
43def fullscreenToggle() :
44  if scr.fullscreen :
45    scr.fullscreen = None
46  else :
47    scr.fullscreen = win
48
49def moreLight() :
50  lig.diffuse = lig.diffuse + 1.0
51  print lig.diffuse 
52
53def lessLight() :
54  lig.diffuse = lig.diffuse - 1.0
55  print lig.diffuse 
56
57key = soy.controllers.Keyboard(win)
58pearl = bks['Pearl']
59key['Q'] = soy.actions.Force(pearl, soy.atoms.Vector((-100,    0,    0)))
60key['R'] = soy.actions.Force(pearl, soy.atoms.Vector((   0,  100,    0)))
61key['S'] = soy.actions.Force(pearl, soy.atoms.Vector(( 100,    0,    0)))
62key['T'] = soy.actions.Force(pearl, soy.atoms.Vector((   0, -100,    0)))
63key['U'] = soy.actions.Force(pearl, soy.atoms.Vector((   0,    0, -100)))
64key['V'] = soy.actions.Force(pearl, soy.atoms.Vector((   0,    0,  100)))
65key['q'] = soy.actions.Quit()
66key[ 1 ] = soy.actions.Quit() # 9 = esc key
67key['f'] = fullscreenToggle
68key['w'] = wireframeToggle
69key['['] = lessLight
70key[']'] = moreLight
71wcn = soy.controllers.Window(win)
72wcn['close'] = soy.actions.Quit()
73
74if __name__ == '__main__' :
75  while True:
76    sleep(.1)
77    fps.text = '%sfps' % str(int(cam.fps)).zfill(4)
78    for bk in bks :
79      p = bks[bk].position
80      v = bks[bk].velocity
81      v = [v[0], v[1], v[2]]
82      if abs(p[0]) > 5 and ((p[0]>0 and v[0]>0) or (p[0]<0 and v[0]< 0)) : 
83        v[0] = v[0]*-1
84      if abs(p[1]) > 5 and ((p[1]>0 and v[1]>0) or (p[1]<0 and v[1]< 0)) : 
85        v[1] = v[1]*-1
86      if abs(p[2]) > 5 and ((p[2]>0 and v[2]>0) or (p[2]<0 and v[2]< 0)) : 
87        v[2] = v[2]*-1
88      bks[bk].velocity = v
Note: See TracBrowser for help on using the browser.