| HomeTurtlegraficsGPanelRoboticsGameGrid WebTigerPython |
| Python - Online |
| Deutsch English |
![]()
YOU LEARN HERE... |
to give the actors a dynamic ‘life of their own’. As soon as the doRun() function is called, a simulation cycle starts in which the act() method is called periodically. |
The basic concept of GameGrid is to give the actors a dynamic ‘life of their own’. As soon as the Run button is pressed in the navigation bar or the doRun() function is called, a simulation cycle starts in which the act() method is called periodically. The simulation period (speed) can be changed using the slider.
By clicking on Step, you can run the movement step by step; by clicking on Reset, you can restart the simulation. |
EXAMPLES |
| Example1: The fish should swim forth and back |
Program: # Gg3.py from gamegrid import * # -------------- class Fish ----------------------------- class Fish(Actor): def __init__(self): Actor.__init__(self, "sprites/nemo.gif") def act(self): self.move() if (self.getX()== 9) or (self.getX() == 0): self.turn(180) # ----------------- main --------------------------------- makeGameGrid(10, 10, 60, Color.red, "sprites/reef.gif") nemo = Fish() addActor(nemo, Location(1, 3)) wanda = Fish() addActor(wanda, Location(6, 7)) show() doRun() Examplel 2: Here, too, the fish should swim back and forth and rotate its image by 180° at the edge of the game window.
Program: # Gg3a.py from gamegrid import * # --------------- class Fish ---------------------------- class Fish(Actor): def __init__(self): Actor.__init__(self, "sprites/nemo.gif") def act(self): self.move() if self.getX()== 9: self.turn(180) self.setHorzMirror(True) if self.getX()== 0: self.turn(180) self.setHorzMirror(False) # ------------------- main --------------------------------- makeGameGrid(10, 10, 60, Color.red, "sprites/reef.gif") nemo = Fish() addActor(nemo, Location(1, 3)) show() doRun() Example 3: The fish should pass through all cells
Program: # Gg3b.py from gamegrid import * # --------------- class Fish ---------------------------- class Fish(Actor): def __init__(self): Actor.__init__(self, "sprites/nemo.gif") def act(self): self.move() if self.getX()== 9: self.turn(90) self.setHorzMirror(True) if self.getX()== 0: self.turn(270) self.setHorzMirror(False) # ------------------- main --------------------------------- makeGameGrid(10, 10, 60, Color.red, "sprites/reef.gif") nemo = Fish() addActor(nemo, Location(0, 0)) show() doRun() |
REMEMBER YOU... |
| To move the actors in the game window, use the following methods:
For these methods to be called automatically, they must be in the act() method. |
TO SOLVE BY YOURSELF |
|
![]()