Previous topic

Installation

Next topic

Graphics

This Page

An example gameΒΆ

Let’s start with a simple example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import bacon

kitten = bacon.Image('res/kitten.png')

bacon.window.width = 512
bacon.window.height = 512

class Game(bacon.Game):
    def on_tick(self):
        bacon.clear(0, 0, 0, 1)
        bacon.draw_image(kitten, 0, 0)
        
bacon.run(Game())

Note

This example is in the Bacon source distribution, in examples/quickstart.py.

Reading from the end of the file backwards:

bacon.run(Game())

This instructs Bacon to start running the game; i.e., to show the window, initialize the graphics driver, and start rendering frames and processing events. All events are sent to the Game class you provide, including Game.on_tick(), which is called once per frame:

    def on_tick(self):
        bacon.clear(0, 0, 0, 1)
        bacon.draw_image(kitten, 0, 0)
        

Other events, such as keyboard and mouse input, can be handled by overriding methods on Game.

Typically all your animation, game logic and rendering is performed in on_tick.

Note

TODO build up this example incrementally to have more game in it.