Dynamics

You can create a board and design tokens on it until now. But these can not move yet.

The act() method

The board and all tokens can be controlled by the method act(). method. This method is called again and again (exactly: All board.speed time units) until the game is finished.

First Token

When you create a token, you can use the decorator @register to add an act() method to the token. to add an act() method to the game field or to your tokens:

Example

 1from miniworldmaker import *
 2
 3board = TiledBoard()
 4board.columns = 20
 5board.rows = 8
 6board.tile_size = 42
 7board.add_background("images/soccer_green.jpg")
 8player = token()
 9player.add_costume("images/player_1.png")
10player.direction = 90
11@player.register
12def act(self):
13    self.move()
14
15board.run()

What happens here?

Lines 12-14: The decorator @player.register binds the act method to the object player.

In a similar way you will later often register reactions to events on objects (e.g. reactions to keyboard or mouse inputs or collision checks). or collision checks).

The keyword self

In the code above you saw that the act method expects the keyword self as a parameter.

All methods that belong to an object always get this keyword as the first paramer.

Then, within the method, attributes and methods of the object itself can be accessed with this keyword.

Example:

This code

@player.register
def act(self):
    self.direction = "right"

is equivalent to this code:

@player.register
def act(self):
    player.direction = "right"

self here refers to the player object where the method was registered.

The frame rate - how often is act() called?

You can set how often act() is called by configuring the board.fps and board.speed attributes.

  • board.fps defines the frame rate. Analogous to a flipbook, where you turn the pages at a fixed speed, the frame rate defines how many times per second the image is redrawn. board.fps has the default value 60, i.e. 60 frames per second are displayed.

  • In the attribute board.frame the current frame is stored. The frames since program start are counted up.

  • board.speed defines how often the program logic (e.g. act) is called per second. A value of 60 means that the act() method is called every 60th frame.

  from miniworldmaker import *

  board = PixelBoard()
  board.size = (120,210)

  @board.register
  def on_setup(self):
      board.fps = 1
      board.speed = 3
      
  @board.register
  def act(self):
      print(board.frame)

  board.run()

The program above has the output:

  3
  6
  9
  12
  15

It is counted up very slowly, because exactly one frame per second is played and every 3. frame (so every 3 seconds) the function act() is called.