Skip to content

A2b - Using Objects

We'll start by looking at the general structure of an op_aracade game file.

code

In this example:

  • self refers to the class we're in, so Level_1
  • Anything indented under on_create will happen as soon as the level starts
  • Anything indented under on_step will happen on every frame refresh; roughly 60 times per second

Run update.bat to download the template file for this assignment (it will go into Local\Unit 2, and be called a2.py). Have a look through the file and run it. It is structured in the same way seen above, but in each section we do things such as:

  • Create objects
  • Set object properties
  • Call object methods

At this point, we'll summarize some of the main attributes and methods you will be using on both levels and sprites. Further details are provided in the binders (or this PDF version)

Attributes and Methods for Views

A level is a type of View. In this assignment, you will preceed each of these with: self.

Attributes

  • background_color (set this to something starting with cc.Colors.)

Methods

  • clear() - clears the background. You'll see this under on_step. Don't change it!
  • draw_grid() - draws a grid over the scene for ease of development. Feel free to comment or remove this line when you're done so the grid goes away.

Attributes and Methods of Sprites

In this assignment, you will preceed each of this with: self.name_of_sprite. (replacing name_of_sprite with the actual variable name you chose for the sprite).

Attributes

  • alpha - A value between 0 and 255 indicating the transparency of the sprite, where 0 is completely invisible and 255 is completely opaque.
  • angle - The angle of rotation of the sprite.
  • bottom - The bottom y position of the sprite.
  • change_x - The sprite's speed along the x axis.
  • change_y - The sprite's speed along the y axis.
  • color - The color tint of the sprite. Any colour format compatible with the arcade game engine is acceptable, but it is easiest to pick a color from cc.Colors.
  • height - The height of the sprite
  • left - The leftmost x position of the sprite.
  • right - The rightmost x position of the sprite.
  • scale - The scale factor of the entire sprite.
  • scale_x - The scale factor along the x axis.
  • scale_y - The scale factor along the y axis.
  • top - The highest y position of the sprite.
  • width - The height of the sprite
  • x - The centre x position of the sprite
  • y - The centre y position of the sprite

Methods

  • bind_top_down_directional_keys() - Sets the arrow keys to control the movement of the sprite. You'll want to call this method on your player. If you want to change the keys that are used, you can use the optional arguments such as: bind_top_down_directional_keys(up=cc.Keys.W, down=cc.Keys.S)
  • enable_physics() - When called on a sprite, it will be prevented from walking through walls (among other things we'll discuss later)

Sprites have many more methods which we'll see in the coming assignments. In this assignment, you won't really be using sprite methods beyond what is already there.

The Increment Operator

There is one additional small concept introduced in this assignment. Under on_step, you will see this line:

self.bee.angle += 1

The += is called the increment operator. It will cause the attribute self.bee.angle to increment by 1. It is a shorthand for this:

self.bee.angle = self.bee.angle + 1

Free free to use this operator in your code.

Your Task

Extend/modify the file provided to you. At minimum, you should:

  • Create at least one additional sprite
  • Set at least five new attributes on sprites, at least one of which should be under on_step so it happens on every frame refresh.