Author: ArcRiley
Date: 2007-07-08 18:24:19 -0400 (Sun, 08 Jul 2007)
New Revision: 433

Added:
   games/blockdropper/
   games/blockdropper/blocks.py
   games/blockdropper/controller.py
   games/blockdropper/core.py
   games/blockdropper/well.py
Log:
This was built for Soya3D, needs to be upgraded for PySoy


Added: games/blockdropper/blocks.py
===================================================================
--- games/blockdropper/blocks.py                                (rev 0)
+++ games/blockdropper/blocks.py        2007-07-08 22:24:19 UTC (rev 433)
@@ -0,0 +1,73 @@
+# This should only have to be run once.
+
+import soya
+import os, os.path, sys
+soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
+
+class BlockWorld(soya.World) :
+  def __init__(self, diffuse, specular) :
+    soya.World.__init__(self)
+    material = soya.Material()
+    material.shininess = 15.0
+    material.separate_specular = True
+    material.diffuse = diffuse
+    material.specular = specular
+    Vert = VertexGroup()
+
+    # Create Faces
+    # 2 Halves
+    for a in (1.0, -1.0) :
+      # 3 Sides in each half
+      for b in (0, 1, 2) :
+        # 4 Quarters in each side
+        for c in (1.0, -1.0) :
+          for d in (1.0, -1.0) :
+            points = (Vert(self, b, (.500*a, .000*c, .000*d)), # 0
+                      Vert(self, b, (.500*a, .000*c, .450*d)), # 1
+                      Vert(self, b, (.500*a, .450*c, .450*d)), # 2
+                      Vert(self, b, (.500*a, .450*c, .000*d)), # 3
+                      Vert(self, b, (.485*a, .485*c, .000*d)), # 4
+                      Vert(self, b, (.485*a, .485*c, .450*d)), # 5
+                      Vert(self, b, (.475*a, .475*c, .475*d)), # 6
+                      Vert(self, b, (.485*a, .450*c, .485*d)), # 7
+                      Vert(self, b, (.485*a, .000*c, .485*d))) # 8
+            faces = ([points[0], points[1], points[2]],
+                     [points[0], points[2], points[3]],
+                     [points[2], points[4], points[3]],
+                     [points[2], points[5], points[4]],
+                     [points[2], points[6], points[5]],
+                     [points[2], points[7], points[6]],
+                     [points[2], points[8], points[7]],
+                     [points[2], points[1], points[8]])
+            for face in faces :
+              if a*c*d==1 :
+                face.reverse()
+              f = soya.Face(self, face, material)
+              f.smooth_lit = True
+
+class VertexGroup :
+  def __init__(self) :
+    self.verts = {}
+
+  def __call__(self, parent, shift, coord) :
+    if   shift == 1 : coord = (coord[1], coord[2], coord[0])
+    elif shift == 2 : coord = (coord[2], coord[0], coord[1]) 
+    if not self.verts.has_key(coord) :
+      self.verts[coord] = soya.Vertex(parent, coord[0], coord[1], coord[2])
+    return self.verts[coord]
+
+
+colors = {
+  'aqua' : ((.2, .9, .9, 1.0), (.4, 1.0, 1.0, 1.0)),
+  'teal' : ((.2, .4, 1.0, 1.0), (.4, .6, 1.0, 1.0)),
+  'rose' : ((1.0, .2, .4, 1.0), (1.0, .4, .6, 1.0)),
+  'jade' : ((.2, 1.0, .3, 1.0), (1.0, .4, .6, 1.0)),
+  'gold' : ((1.0, .8, .1, 1.0), (1.0, .9, .2, 1.0)),
+  'grey' : ((.7,  .7, .7, 1.0), (1.0,1.0,1.0, 1.0)),
+  'plum' : ((.9, .2, .9, 1.0), (1.0, .4, 1.0, 1.0)),
+  'pink' : ((.9, .6, .6, 1.0), (1.0, .8,  .8, 1.0)),
+}
+blocks = {}
+
+for color in colors :
+  blocks[color] = BlockWorld(colors[color][0], colors[color][1]).shapify()

Added: games/blockdropper/controller.py
===================================================================
--- games/blockdropper/controller.py                            (rev 0)
+++ games/blockdropper/controller.py    2007-07-08 22:24:19 UTC (rev 433)
@@ -0,0 +1,39 @@
+import soya
+import soya.sdlconst as sdl
+
+class Controller :
+  def __init__(self) :
+    self.keys = {sdl.K_LEFT  : 'left' , sdl.K_RIGHT : 'right',
+                 sdl.K_UP    : 'up'   , sdl.K_DOWN  : 'down' ,
+                 sdl.K_SPACE : 'space', sdl.K_RETURN: 'enter',
+                 sdl.K_q     : 'exit' , sdl.K_ESCAPE: 'exit' ,
+                 sdl.K_LSHIFT: 'shift', sdl.K_RSHIFT: 'shift',
+                 sdl.K_LCTRL : 'ctrl' , sdl.K_RCTRL : 'ctrl' }
+
+    self.down = {'left'  : False, 'right' : False,
+                 'up'    : False, 'down'  : False,
+                 'space' : False, 'enter' : False,
+                 'shift' : False, 'ctrl'  : False}
+
+    self.acts = {}
+
+  def begin_round(self) :
+    newdown = {}
+    events = soya.process_event()
+    for event in events :
+      if event[0] == sdl.KEYDOWN and self.keys.has_key(event[1]) :
+        key = self.keys[event[1]]
+        self.down[key] = True
+        newdown[key] = True
+      if event[0] == sdl.KEYUP and self.keys.has_key(event[1]) :
+        key = self.keys[event[1]]
+        if newdown.has_key(key) and self.acts.has_key(key) :
+          self.acts[key](self.down[key])          
+        self.down[key] = False
+    for act in self.acts :
+      if self.down[act] :
+        self.acts[act](self.down[act])      
+
+
+#left
+#self.turn_incline(-.5)

Added: games/blockdropper/core.py
===================================================================
--- games/blockdropper/core.py                          (rev 0)
+++ games/blockdropper/core.py  2007-07-08 22:24:19 UTC (rev 433)
@@ -0,0 +1,82 @@
+# Blockdropper core.py
+# Copyright (C) 2005 Arc Riley
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import sys, os, os.path, soya, soya.widget
+import blocks, controller, well
+
+class RootWidget(soya.widget.Group) :
+  def __init__(self) :
+    soya.widget.Group.__init__(self)
+    self.controller = controller.Controller()
+    
+  def widget_begin_round(self) :
+    self.controller.begin_round()
+    soya.widget.Group.widget_begin_round(self)
+
+soya.init("BlockDropper", 512, 600, False, False)
+soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
+soya.set_root_widget(RootWidget())
+scene = soya.World()
+
+
+class cubic(soya.World) :
+  def begin_round(self) :
+    self.rotate_lateral(1.0)
+    self.rotate_vertical(.5)
+
+mywell = well.TroWell(scene)
+cubes = cubic(scene)
+cubes.z = 50.0
+
+cube1 = soya.Volume(cubes, blocks.blocks['teal'])
+cube1.set_xyz(-.5, -.5, -.5)
+cube2 = soya.Volume(cubes, blocks.blocks['rose'])
+cube2.set_xyz(.5, -.5, -.5)
+cube3 = soya.Volume(cubes, blocks.blocks['jade'])
+cube3.set_xyz(.5, .5, -.5)
+cube4 = soya.Volume(cubes, blocks.blocks['gold'])
+cube4.set_xyz(.5, .5, .5)
+
+# Creates a light in the scene (same convention: the first argument of the
+# constructor is the parent) and moves it to (1.5, 2.0, 0.2).
+
+light = soya.Light(scene)
+light.set_xyz(0.5, 0.0, 2.0)
+light.directional = 1
+#light.diffuse = (1.0, 0.8, 0.4, 1.0)
+#light.rotate_vertical(-45.0)
+
+# Creates a camera in the scene and moves it to z = 5.0. The camera looks in 
the
+# -Z direction, so, in this case, towards the cube.
+#
+# When relevant, Soya always considers the X direction to be on the right,
+# the Y direction to be on the top and the -Z to be the front.
+# (Using -Z for front seems odd, but using Z for front makes all coordinate 
systems
+# indirect, which is a mathematical nightmare !)
+
+camera = soya.Camera(scene)
+camera.z = 2.0
+camera.fov = 90.0
+
+# Say to Soya that the camera is what we want to be rendered.
+
+soya.root_widget.add(camera)
+
+#soya.render(); 
soya.screenshot().save(os.path.join(os.path.dirname(sys.argv[0]), "results", 
os.path.basename(sys.argv[0])[:-3] + ".jpeg"))
+
+soya.Idler(scene).idle()
+#soya.render()

Added: games/blockdropper/well.py
===================================================================
--- games/blockdropper/well.py                          (rev 0)
+++ games/blockdropper/well.py  2007-07-08 22:24:19 UTC (rev 433)
@@ -0,0 +1,364 @@
+# Blockdropper :: Well Classes (well.py)
+# Copyright (C) 2006 Arc Riley
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import soya
+import blocks
+import random
+
+class BaseWell(soya.World) :
+  def __init__(self, parent, size) :
+    soya.World.__init__(self, parent)
+    self.size = size
+    self.newblocks()
+    self.field = soya.World(self)
+    self.field.set_xyz((size[0]/-2)+.5,
+                       (size[1]/-2)+.5,
+                       (size[2]/-2)+.5)
+    self.active = soya.World(self.field)
+    self.newactive()
+    self.dropdelay = 40
+    self.dropcount = self.dropdelay
+    self.generate_lines()
+    self.x = -3.0
+    self.y = -3.0
+    self.z = -10.0
+    self.turn_vertical(-60.0)
+    self.turn_incline(40.0)
+    self.controller = soya.root_widget.controller
+    self.controller.acts['left'] = self.move_left
+    self.controller.acts['right'] = self.move_right
+    self.controller.acts['up'] = self.move_up
+    self.controller.acts['down'] = self.move_down
+    self.controller.acts['space'] = self.move_drop
+
+  def begin_round(self) :
+    soya.World.begin_round(self)
+    self.dropcount -= 1
+    if self.dropcount == 0 :
+      self.dropcount = self.dropdelay
+      self.move_drop(1.0)
+
+  def move_left(self, p) :
+    if self.controller.down['shift'] :
+      #self.turn_incline(-2. * p)
+      self.active.rotate_incline(-90.0)
+      if self.collide() :
+        self.active.rotate_incline(90.0)
+    else :
+      self.active.x -= 1.0
+      if self.collide() :
+        self.active.x += 1.0
+    self.controller.down['left'] = False
+
+  def move_right(self, p) :
+    if self.controller.down['shift'] :
+      #self.turn_incline(2. * p)
+      self.active.rotate_incline(90.0)
+      if self.collide() :
+        self.active.rotate_incline(-90.0)
+    else :
+      self.active.x += 1.0
+      if self.collide() :
+        self.active.x -= 1.0
+    self.controller.down['right'] = False
+
+  def move_down(self, p) :
+    if self.controller.down['shift'] :
+      #self.turn_vertical(-2. * p)
+      self.active.rotate_vertical(90.0)
+      if self.collide() :
+        self.active.rotate_vertical(-90.0)
+    else :
+      self.active.y -= 1.0
+      if self.collide() :
+        self.active.y += 1.0
+    self.controller.down['down'] = False
+
+  def move_up(self, p) :
+    if self.controller.down['shift'] :
+      #self.turn_vertical(2. * p)
+      self.active.rotate_vertical(-90.0)
+      if self.collide() :
+        self.active.rotate_vertical(90.0)
+    else :
+      self.active.y += 1.0
+      if self.collide() :
+        self.active.y -= 1.0
+    self.controller.down['up'] = False
+
+  def move_drop(self, p) :
+    self.active.z -= 1.0
+    if self.collide() :
+      self.active.z += 1.0
+      self.addblocks()
+      self.newactive()
+      self.controller.down['space'] = False
+  
+
+  def getblockpos(self, block) :
+    p = block.position()
+    p.convert_to(self.field)
+    return (int(round(p.x)), int(round(p.y)), int(round(p.z)))
+
+  def collide(self) :
+    for block in self.active.children :
+      x, y, z = self.getblockpos(block)
+      if x < 0 or x >= self.size[0] :
+        return True
+      if y < 0 or y >= self.size[1] :
+        return True
+      if z < 0 :
+        return True
+      if z >= self.size[2] :
+        continue
+      if self.blocks[z][y][x] :
+        return True
+
+  def addblocks(self) :
+    added = []
+    while True :
+      if not self.active.children :
+        break
+      block = self.active.children[0]
+      x, y, z = self.getblockpos(block)
+      self.active.remove(block)
+      if z < self.size[2] :
+        added.append((x,y,z))
+        self.blocks[z][y][x] = block
+        self.field.add(block)
+        block.set_xyz(float(x), float(y), float(z))
+    self.checknew(added)
+    
+  def newactive(self) :
+    self.getnew()
+    if self.collide() :
+      print 'game over'
+      while True :
+        if not self.active.children :
+          break
+        self.active.remove(self.active.children[0])
+
+  def newblocks(self) :
+    self.blocks = []
+    for z in range(self.size[2]) :
+      zlist = []
+      for y in range(self.size[1]) :
+        zlist.append([None]*self.size[0])
+      self.blocks.append(zlist)
+
+  def generate_lines(self) :
+    sizex, sizey, sizez = self.size
+    field = self.field
+    material = soya.Material()
+    for z in range(sizez+1) :
+      #soya.Face(field, (soya.Vertex(field, -.5, -.5, z-.5),
+      #                 soya.Vertex(field, sizex-.5, -.5, z-.5)))
+      soya.Face(field, (soya.Vertex(field, -.5, sizey-.5, z-.5),
+                       soya.Vertex(field, sizex-.5, sizey-.5, z-.5)))
+      #soya.Face(field, (soya.Vertex(field, -.5, -.5, z-.5),
+      #                 soya.Vertex(field, -.5, sizey-.5, z-.5)))
+      soya.Face(field, (soya.Vertex(field, sizex-.5, -.5, z-.5),
+                       soya.Vertex(field, sizex-.5, sizey-.5, z-.5)))
+    for y in range(sizey+1) :
+      #soya.Face(field, (soya.Vertex(field, -.5, y-.5, -.5),
+      #                 soya.Vertex(field, -.5, y-.5, sizez-.5)))
+      soya.Face(field, (soya.Vertex(field, sizex-.5, y-.5, -.5),
+                       soya.Vertex(field, sizex-.5, y-.5, sizez-.5)))
+    for x in range(sizex+1) :
+      #soya.Face(field, (soya.Vertex(field, x-.5, -.5, -.5),
+      #                 soya.Vertex(field, x-.5, -.5, sizez-.5)))
+      soya.Face(field, (soya.Vertex(field, x-.5, sizey-.5, -.5),
+                       soya.Vertex(field, x-.5, sizey-.5, sizez-.5)))
+
+
+class TroWell(BaseWell) :
+  poses = (((-.5, 0.5, 0.5),
+            (0.5, 0.5, 0.5),
+            (1.5, 0.5, 0.5)),
+           ((-.5, -.5, -.5),
+            (0.5, -.5, -.5),
+            (0.5, 0.5, -.5)))
+
+  colors = ('teal', 'gold', 'rose')
+
+  def __init__(self, parent) :
+    BaseWell.__init__(self, parent, (5, 3, 12))
+
+  def getnew(self) :
+    blockid = int(random.random()*2)
+    for pose in self.poses[blockid] :
+      blockcolor = int(random.random()*3)
+      cube = soya.Volume(self.active, blocks.blocks[self.colors[blockcolor]])
+      cube.set_xyz(pose[0], pose[1], pose[2])
+    children = self.active.children
+    children[0].connected = [children[1]]
+    children[1].connected = [children[0], children[2]]
+    children[2].connected = [children[1]]
+    self.active.set_xyz(1.5, 0.5, 11.5)
+
+  def checknew(self, new) :
+    matches = {}
+    for n in new :
+      thisblock = self.blocks[n[2]][n[1]][n[0]].shape
+      #
+      # Check by X
+      xmatch = []
+      for x in range(self.size[0]) :
+        if self.blocks[n[2]][n[1]][x] and \
+           self.blocks[n[2]][n[1]][x].shape == thisblock :
+          if not xmatch or x == xmatch[-1]+1 :
+            xmatch.append(x)
+          else :
+            if len(xmatch) < 3 :
+              xmatch = [x]
+      if len(xmatch) > 2 :
+        for x in xmatch :
+          matches[(x, n[1], n[2])] = True
+      #
+      # Check by Y
+      ymatch = []
+      for y in range(self.size[1]) :
+        if self.blocks[n[2]][y][n[0]] and \
+           self.blocks[n[2]][y][n[0]].shape == thisblock :
+          if not ymatch or y == ymatch[-1]+1 :
+            ymatch.append(y)
+          else :
+            if len(ymatch) < 3 :
+              ymatch = [y]
+      if len(ymatch) > 2 :
+        for y in ymatch :
+          matches[(n[0], y, n[2])] = True
+      #
+      # Check by Z
+      zmatch = []
+      for z in range(self.size[2]) :
+        if self.blocks[z][n[1]][n[0]] and \
+           self.blocks[z][n[1]][n[0]].shape == thisblock :
+          if not zmatch or z == zmatch[-1]+1 :
+            zmatch.append(z)
+          else :
+            if len(zmatch) < 3 :
+              zmatch = [z]
+      if len(zmatch) > 2 :
+        for z in zmatch :
+          matches[(n[0], n[1], z)] = True
+    # 
+    # Remove matches
+    while True :
+      if not matches :
+        break
+      x, y, z = matches.keys()[0]
+      matches.pop((x, y, z))
+      block = self.blocks[z][y][x]
+      for neighbor in block.connected :
+        neighbor.connected.pop(neighbor.connected.index(block))
+      self.field.remove(block)
+      self.blocks[z][y][x] = None      
+      while True :
+        z += 1
+        if z == self.size[2] or not self.blocks[z][y][x] :
+          break
+        self.blocks[z-1][y][x] = self.blocks[z][y][x]
+        self.blocks[z][y][x].z = round(self.blocks[z][y][x].z - 1)
+        self.blocks[z][y][x] = None
+        if matches.has_key((x,y,z)) :
+          matches.pop((x,y,z))
+          matches[(x, y, z-1)] = True
+        
+
+class TetraWell(BaseWell) :
+  poses = (((-.5, -.5, -.5), # 0
+            (0.5, -.5, -.5),
+            (0.5, 0.5, -.5),
+            (0.5, 0.5, 0.5)),
+           ((-.5, -.5, 0.5), # 1
+            (0.5, -.5, 0.5),
+            (0.5, 0.5, 0.5),
+            (1.5, 0.5, 0.5)),
+           ((-.5, -.5, 0.5), # 2
+            (0.5, -.5, 0.5),
+            (0.5, 0.5, 0.5),
+            (1.5, -.5, 0.5)),
+           ((-1.5,-.5, 0.5), # 3
+            (-.5, -.5, 0.5),
+            (0.5, -.5, 0.5),
+            (0.5, 0.5, 0.5)),
+           ((-.5, 0.5, -.5), # 4
+            (-.5, 0.5, 0.5),
+            (0.5, 0.5, 0.5),
+            (0.5, 0.5, -.5)),
+           ((-1.5,0.5, 0.5), # 5
+            (-.5, 0.5, 0.5),
+            (0.5, 0.5, 0.5),
+            (1.5, 0.5, 0.5)),
+           ((-.5, -.5, -.5), # 6
+            (0.5, -.5, -.5),
+            (0.5, 0.5, -.5),
+            (0.5, -.5, 0.5)),
+           ((0.5, -.5, -.5), # 7
+            (-.5, -.5, -.5),
+            (-.5, 0.5, -.5),
+            (-.5, 0.5, 0.5)))
+
+  colors = ('teal', 'jade', # 0, 1
+            'gold', 'plum', # 2, 3
+            'grey', 'aqua', # 4, 5
+            'pink', 'rose') # 6, 7
+
+  def __init__(self, parent) :
+    BaseWell.__init__(self, parent, (6, 4, 16))
+
+  def getnew(self) :
+    r = int(random.random()*4) + int(random.random()*5)
+    for pose in self.poses[r] :
+      cube = soya.Volume(self.active, blocks.blocks[self.colors[r]])
+      cube.set_xyz(pose[0], pose[1], pose[2])
+    self.active.set_xyz(2.5, 1.5, 15.5)
+
+  def checknew(self, new) :
+    # Which planes are the new blocks in?
+    newz = {}
+    for n in new :
+      newz[n[2]] = True
+    # Check any planes which new blocks are part of
+    complete = []
+    for z in newz :
+      full = True
+      for y in self.blocks[z] :
+        for x in y :
+          if not x :
+            full = False
+      if full :
+        complete.append(z)
+    complete.sort()
+    complete.reverse()
+    for z in complete :
+      for y in self.blocks[z] :
+        for x in y :
+          self.field.remove(x)
+      self.blocks.pop(z)
+      for upz in range(z, self.size[2]-1) :
+        for y in self.blocks[upz] :
+          for x in y :
+            if x :
+              x.z = round(x.z - 1.0)      
+      zlist = []
+      for y in range(self.size[1]) :
+        zlist.append([None]*self.size[0])
+      self.blocks.append(zlist)
+    
+    

_______________________________________________
PySoy-SVN mailing list
[email protected]
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to