Start using a Python class to organize the game.
import arcade GAME_TITLE = 'Drawing Example' class MyGame(arcade.Window): def on_draw(self): arcade.set_background_color(arcade.color.WHEAT) arcade.start_render() arcade.draw_text(GAME_TITLE, 200, 300, arcade.color.BLACK, 12) def main(): game = MyGame(600, 600, GAME_TITLE) arcade.run() if __name__ == '__main__': main()
- We make a class for our game, with a class name of
- This class is a “recipe” for the game, not an actual running game
- To make an actual game, we make an instance of the game on line 14
- Our class doesn’t start with an empty slate, it uses
arcade.Windowas a starting point
- This is called subclassing, and
MyGameis a “subclass” of the
arcade.Windowclass (aka superclass, base class)
arcade.Windowis itself a subclass of
pyglet.window.Windowwhich has a LOT of complexity
- But our game doesn’t have to do all that, it is using the base classes as a starting point
- Classes can hold data (called state) and behavior (called methods)
- When a function is part of a class, it is called a method
- It is easy to spot a method in Python: it is a
defwith a first argument of
- What is “self”? A variable hold the instance of the class that we are working on
- In Arcade, when you use a class instead of a function, the Arcade
rules are different. For example,
start_renderbut not a
finish_render. Arcade knows to run
- Put a breakpoint just after making the
gameinstance, run the game under the debugger, and see what data is stored on the instance.
- What is a superclass used for?
- What are the differences between a function and a method?
- What is the difference between a class and an instance?
- In a method, the first argument (
self) hold what value?