Class Animation

Re-create the earlier “kinda not-really” animation section, this time with a class.

Code

import arcade


class MyGame(arcade.Window):
    def __init__(self, width, height, title, bg_color):
        super().__init__(width, height, title)
        arcade.set_background_color(bg_color)
        self.width = width
        self.height = height
        self.title = title

    def on_draw(self):
        arcade.start_render()
        x = self.width / 2
        y = self.height / 2
        arcade.draw_text(self.title, x, y, arcade.color.BLACK, 12)


def main():
    game1 = MyGame(600, 600, 'Drawing Example', arcade.color.WHEAT)
    arcade.run()


if __name__ == '__main__':
    main()

Analysis

  1. This time we pass another value into the constructor and store it on the instance.
  2. We are now storing width and height on the instance.
  3. Why? We are doing a calculation in on_draw. We want to draw the text in the middle of the screen. width and height were passed into the constructor but to be used in another method, need to be stored on the instance self.

What Is on_draw?

Arcade makes it easy to do games. You write a method named on_draw and Arcade will call that method to do all the drawing.

Exercises

  1. Put the text a third of the way from the top.