Code for changing color of all tiles using function within a class defined

From GeoMod

Jump to: navigation, search

This code is using class for creating set of tiles and also creating some functions within that class. One function from that class will change the color of whole set of tiles.

from visual import *
from random import *

class floor:
    def __init__(self, nx, nz, dx=1, dz=1):
        self.nx=nx          #number of columns
        self.nz=nz          # number of rows
        self.dx=dx          #x dimension of tiles
        self.dz=dz          #z dimension of tiles
        self.tiles=[]
        for i in range(nx):
            for j in range(nz):
                self.tiles.append(box(length=dx,width=dz,
                                      height=0.5,
                                      color=color.green))
                self.tiles[-1].x=i*dx
                self.tiles[-1].z=j*dz
    def translate(self, x_trans, y_trans, z_trans): #Translation function
            for i in self.tiles:
                i.x=i.x-x_trans
                i.z=i.z-z_trans
                print i.pos
    def axes(self, t): #Drawing axes function
            if t == 0:
                xaxis=cylinder(axis=(4,0,0), color=color.red, radius=.05)
                yaxis=cylinder(axis=(0,4,0), color=color.yellow, radius=.05)
                zaxis=cylinder(axis=(0,0,4), color=color.blue, radius=.05)
    def floorcol(self): #Changing the color of floor function
        for i in self.tiles:
            i.color=(uniform(0.3,1),0.4,0.6)
        
# Initializing the parameters
nx=5
lx=4.0
nz=4
lz=4.0

dx=lx/nx    #Unit size of tile in x-direction
dz=lz/nz    #Unit size of tile in z-direction

# Creating tiles using class floor
tileset=floor(nx,nz,dx,dz)

# Calling translate function within the class
tileset.translate((nx-1)*dx/2, 0, (nz-1)*dz/2)

# Calling axes function from within the class
tileset.axes(1)

# Calling function of changing the color of tiles from within the class
tileset.floorcol()

--Ekta 14:54, 26 Sep 2005 (CDT)

Personal tools