""" this version for tree_birds game/toy testing, adding Apples to the tree! """ import random import soya from plant import Plant from effectfood import GrowthApple, FertilizingApple appletypes = [GrowthApple, FertilizingApple] class AppleTree(Plant): def __init__(self, scene): Plant.__init__(self, scene) self.apples = [] def grow(self): if len(self.branches) >= self.branchamount * 5: bidx = len(self.apples) + 1 if bidx <= len(self.branches): p = self.branches[-bidx].end.position() #self.apples.append(Apple(self, p)) self.apples.append(random.choice(appletypes)(self, p)) else: print "Can't grow more apples - out of branches." #would it be interesting to grow a branch here? #or does this way introcude us 'seasons'? else: Plant.grow(self) def revert(self): if self.apples: self.remove(self.apples.pop()) else: Plant.revert(self) class PlantToy(soya.World): from soya.sdlconst import * keybindings = {K_ESCAPE: "soya.IDLER.stop()", K_LEFT: "plant.turn_lateral(-10)", K_RIGHT: "plant.turn_lateral(10)", K_UP: "plant.grow()", K_DOWN: "plant.revert()", K_a: "camera.set_xyz(camera.x, camera.y, camera.z - 0.1)", K_z: "camera.set_xyz(camera.x, camera.y, camera.z + 0.1)", } def __init__(self): #should get plant etc. (current globals) soya.World.__init__(self) soya.set_grab_input(1) def begin_round(self): soya.World.begin_round(self) #is there anything? for event in soya.process_event(): if event[0] == soya.sdlconst.KEYDOWN: try: cmd = self.keybindings[event[1]] except KeyError: pass else: eval(cmd) elif event[0] == soya.sdlconst.MOUSEMOTION: rel_x = event[3] rel_y = event[4] if len(plant.branches) == 0: #freely move trunk end mouse_x = event[1] mouse_y = event[2] mouse_pos = camera.coord2d_to_3d(mouse_x, mouse_y, (plant % camera).z) mouse_pos.convert_to(self) plant.branchfrom.end.x = mouse_pos.x plant.branchfrom.update_info() else: #spin the tree with x movement soya.set_mouse_pos(300, 300) #window middle would be nice soya.process_event() #resets rel mouse plant.turn_lateral(rel_x) #growth control in y axis if rel_y > 0: if plant.grow(): camera.z += 0.05 if rel_y < 0: plant.revert() if camera.z > 2: camera.z -= 0.05 if __name__ == '__main__': soya.init(width = 700, height = 700) scene = PlantToy() plant = AppleTree(scene) #globals (show for scene & plant) light = soya.Light(scene) light.set_xyz(1.0, 0.7, 1.0) light2 = soya.Light(scene) light2.set_xyz(-1.0, 0.9, 0.7) light3 = soya.Light(scene) light3.set_xyz(0.3, 0.8, -0.7) camera = soya.Camera(scene) camera.set_xyz(0.0, 1.2, 2.0) soya.set_root_widget(camera) #Start idler = soya.Idler(scene) idler.idle()