| 1 | #!/usr/bin/env python |
|---|
| 2 | import soy |
|---|
| 3 | import blocks |
|---|
| 4 | from time import sleep |
|---|
| 5 | |
|---|
| 6 | sce = soy.scenes.Scene() |
|---|
| 7 | cam = soy.bodies.Camera(sce) |
|---|
| 8 | cam.position = (0.0, 0.0, 15.0) |
|---|
| 9 | lig = soy.bodies.Light(sce) |
|---|
| 10 | lig.position = (-10.0,10.0,2.0) |
|---|
| 11 | |
|---|
| 12 | black = soy.materials.Material() |
|---|
| 13 | black.ambient = soy.colors.black |
|---|
| 14 | black.diffuse = soy.colors.Color('#222') |
|---|
| 15 | black.specular= soy.colors.Color('#222') |
|---|
| 16 | black.shininess = 5.0 |
|---|
| 17 | colors = { |
|---|
| 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 | |
|---|
| 28 | bks = blocks.blocks(sce, colors) |
|---|
| 29 | |
|---|
| 30 | fps = soy.textures.Print() |
|---|
| 31 | |
|---|
| 32 | scr = soy.Screen() |
|---|
| 33 | win = soy.Window(scr, 'CollideBlocks', background=soy.colors.gray) |
|---|
| 34 | pro = soy.widgets.Projector(win, camera=cam) |
|---|
| 35 | can = soy.widgets.Canvas(win, texture=fps) |
|---|
| 36 | |
|---|
| 37 | def wireframeToggle() : |
|---|
| 38 | if cam.wireframe : |
|---|
| 39 | cam.wireframe = False |
|---|
| 40 | else : |
|---|
| 41 | cam.wireframe = True |
|---|
| 42 | |
|---|
| 43 | def fullscreenToggle() : |
|---|
| 44 | if scr.fullscreen : |
|---|
| 45 | scr.fullscreen = None |
|---|
| 46 | else : |
|---|
| 47 | scr.fullscreen = win |
|---|
| 48 | |
|---|
| 49 | def moreLight() : |
|---|
| 50 | lig.diffuse = lig.diffuse + 1.0 |
|---|
| 51 | print lig.diffuse |
|---|
| 52 | |
|---|
| 53 | def lessLight() : |
|---|
| 54 | lig.diffuse = lig.diffuse - 1.0 |
|---|
| 55 | print lig.diffuse |
|---|
| 56 | |
|---|
| 57 | key = soy.controllers.Keyboard(win) |
|---|
| 58 | pearl = bks['Pearl'] |
|---|
| 59 | key['Q'] = soy.actions.Force(pearl, soy.atoms.Vector((-100, 0, 0))) |
|---|
| 60 | key['R'] = soy.actions.Force(pearl, soy.atoms.Vector(( 0, 100, 0))) |
|---|
| 61 | key['S'] = soy.actions.Force(pearl, soy.atoms.Vector(( 100, 0, 0))) |
|---|
| 62 | key['T'] = soy.actions.Force(pearl, soy.atoms.Vector(( 0, -100, 0))) |
|---|
| 63 | key['U'] = soy.actions.Force(pearl, soy.atoms.Vector(( 0, 0, -100))) |
|---|
| 64 | key['V'] = soy.actions.Force(pearl, soy.atoms.Vector(( 0, 0, 100))) |
|---|
| 65 | key['q'] = soy.actions.Quit() |
|---|
| 66 | key[ 1 ] = soy.actions.Quit() # 9 = esc key |
|---|
| 67 | key['f'] = fullscreenToggle |
|---|
| 68 | key['w'] = wireframeToggle |
|---|
| 69 | key['['] = lessLight |
|---|
| 70 | key[']'] = moreLight |
|---|
| 71 | wcn = soy.controllers.Window(win) |
|---|
| 72 | wcn['close'] = soy.actions.Quit() |
|---|
| 73 | |
|---|
| 74 | if __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 |
|---|