What do you like about Bfactory classes

 

The two program parts just written could also have been developed by two working groups. The next task is to bring these pieces together, which is not always easy. But it's like this one Uniform program style and the code largely decoupled, this makes "merging" the code much easier

As a new functionality, the halving of the fruits when they meet with an arrow is also to be incorporated. We have already prepared this because the fruits have two sprite images, one for the whole and one for the halved fruit.

As you know, collisions between actors are detected by a collision event. To do this, you specify for each actor who are the possible collision partners. Think about the following: If you generate an arrow when you shoot it, then all the fruits that are currently present are collision partners.

But don't forget that as the arrow moves, new fruits will also appear. Therefore, when creating a fruit, you have to define all existing arrows (maybe there is only one) as collision partners.

In JGameGrid you can pass a whole list of actors as collision partners to addCollisionActors (). With getActors (class) you get a list of all actors of the given class that you can transfer.


 


 

from gamegrid import * from random import randint, random import math # ---------- class Fruit ------------------------ classFruit (Actor): def __init __ (self, spriteImg, vx): Actor .__ init __ (self, True, spriteImg, 2) self.vx = vx self.vy = 0 self.isSliced ​​= False def reset (self): # Called when Fruit is added to GameGrid self.px = self.getX () self.py = self.getY () def act (self): self.movePhysically () self.turn (10) def movePhysically (self): self.dt = 0.002 * getSimulationPeriod () self.vy = self.vy + g * self.dt self.px = self.px + self.vx * self.dt self.py = self.py + self.vy * self.dt self. setLocation (Location (int (self.px), int (self.py))) self.cleanUp () def cleanUp (self): ifnot self.isInGrid (): self.removeSelf () def sliceFruit (self): ifnot self .isSliced: self.isSliced ​​= True self.show (1) def collide (self, actor1, actor2): actor1.sliceFruit () return 0 # ------ class Melon ----------- classMelon (Fruit): def __init __ (self, vx): Fruit .__ init __ (self, "sprites / melon.gif", vx) # ------ class Orange ----------- classOrange (Fruit): def __init __ (self, vx): Fruit .__ init __ (self, "sprites / orange.gif", vx) # ------ class Strawberry ----------- classStrawberry (Fruit): def __init __ (self, vx): Fruit. __init __ (self, "sprites / strawberry.gif", vx) # ------------------- class FruitFactory -------------- ----- classFruitFactory (Actor): myCapacity = 0 myFruitFactory = None nbGenerated = 0 @staticmethod def create (capacity, slowDown): if FruitFactory.myFruitFactory == None: FruitFactory.myCapacity = capacity FruitFactory.myFruitFactory = FruitFactory () FruitFactory .myFruitFactory.setSlowDown (slowDown) return FruitFactory.myFruitFactory def act (self): self.createRandomFruit () def createRandomFruit (self): if FruitFactory.nbGenerated == FruitFactory.myCapacity: print "Factory expired" return vx = - (random () * 20 + 30) r = randint (0, 2) if r = = 0: fruit = Melon (vx) elif r == 1: fruit = Orange (vx) else: fruit = Strawberry (vx) FruitFactory.nbGenerated + = 1 y = int (random () * screenHeight / 2) addActorNoRefresh (fruit , Location (screenWidth-50, y), 180) # for a new fruit, the collision partners are all existing darts fruit.addCollisionActors (toArrayList (getActors (Dart))) # ------------ ------- class Crossbow ----------------------- classCrossbow (Actor): def __init __ (self): Actor .__ init __ (self, True , "sprites / crossbow.gif", 2) # ------ class Dart ---------------- classDart (Actor): def __init __ (self, speed): Actor .__ init __ (self, True, "sprites / dart.gif") self.sp eed = speed self.dt = 0.005 * getSimulationPeriod () # Called when actor is added to GameGriddef reset (self): self.px = self.getX () self.py = self.getY () dx = math.cos (math .radians (self.getDirectionStart ())) self.vx = self.speed * dx dy = math.sin (math.radians (self.getDirectionStart ())) self.vy = self.speed * dy def act (self) : self.vy = self.vy + g * self.dt self.px = self.px + self.vx * self.dt self.py = self.py + self.vy * self.dt self.setLocation (Location ( int (self.px), int (self.py))) self.setDirection (math.degrees (math.atan2 (self.vy, self.vx))) ifnot self.isInGrid (): self.removeSelf () crossbow .show (0) # Load crossbowdef collide (self, actor1, actor2): actor2.sliceFruit () return 0 # ------ End of class definitions --------------- ----- def keyCallback (e): code = e.getKeyCode () if code == KeyEvent.VK_UP: cross bow.setDirection (crossbow.getDirection () - 5) elif code == KeyEvent.VK_DOWN: crossbow.setDirection (crossbow.getDirection () + 5) elif code == KeyEvent.VK_SPACE: if crossbow.getIdVisible () == 1: # Wait until crossbow is loadedreturn crossbow.show (1) # crossbow is released dart = Dart (100) addActorNoRefresh (dart, crossbow.getLocation (), crossbow.getDirection ()) # for a new dart, the collision partners are all existing fruits dart.addCollisionActors (toArrayList (getActors (Fruit))) FACTORY_CAPACITY = 20 FACTORY_SLOWDOWN = 35 screenWidth = 600 screenHeight = 400 g = 9.81 makeGameGrid (screenWidth, cursor screenHeight, 1, False up, keyPressed = keyCallback.) setTit to target, Space to shoot. ") setBgColor (makeColor (" skyblue ")) factory = FruitFactory.create (FACTORY_CAPACITY, FACTORY_SLOWDOWN) addActor (factory, Location (0, 0)) # needed to run act () crossbow = Crossbow ( ) addActor (crossbow, Location (80, 320)) setSimulationPeriod (30) doRun () show ()
Mark the program code