MEA Platformer

Coding resources

Table of contents

Guides
  1. Adding maps/levels
  2. Adding sprites
  3. Adding an enemy
Code explanation
  1. The Player and Enemy class
  2. The game loop and main.py file

Guides

Adding a Map

To add a map, simply

  1. go to the maps folder and
  2. create a new file named map[last map number+1]. For example, if the last map was named map2, name your map map3.

Each number represents a block in the world. To see which available blocks you can paint the map with, go to map.py. In the draw function, there should be a list of all the numbers and sprites to paint the map with. See adding sprites to add your own tile to paint the map with.

Adding Sprites + How to add it as a tile

  1. Upload a png image into the img folder
  2. Go to sprites.py and add in this (replace spriteName and fileName with your sprite name and file name):
spriteName = pygame.image.load('img/fileName.png');

Adding the sprite as a tile

  1. In the map.py file, find the draw() function.
  2. Below the two for loops, find the code that looks like this:
if tile == '1':
    display.blit(sprites.dirt, (x * TILE_SIZE, y * TILE_SIZE))
elif tile == '2':
    display.blit(sprites.grass, (x * TILE_SIZE, y * TILE_SIZE))
  1. From sprites.py add in your sprite at the end of the rest of the tile like below, replacing spriteName with the actual spriteName and the lastNumber+1 with 1 + the last number (the number you're assigning the tile to).
elif tile == 'lastNumber+1':
    display.blit(sprites.spriteName, (x * TILE_SIZE, y * TILE_SIZE))

Now you can add the tile in a map using the tile number you assigned it to!

Adding an Enemy

  1. Make sure you added a sprite for your enemy. Make sure the name of the sprite is the name of your enemy.
  2. Go to main.py and scroll down to around line 19. You should see:
player = Player(pygame.Rect(50, 50, sprites.player.get_width(), sprites.player.get_height()))
enemy = Enemy(pygame.Rect(50, 50, sprites.enemy.get_width(), sprites.enemy.get_height())
  1. Add in your own enemy like this (replacing the 3 enemyName references with your actual enemy name):
enemyName = Enemy(pygame.Rect(50, 50, sprites.enemyName.get_width(), sprites.enemyName.get_height())
  1. Go to the game loop. Add the enemy on the display like this (again, replacing the 2 enemyName references):
display.blit(sprites.enemyName, (enemyName.rect.x, enemyName.rect.y))
  1. Below the movement, add this line (replacing the enemyName reference).
enemyName.moveRoutine(pygame, map, player)

Run the code; the enemy should be in there!

Code explanation

The player and enemy class

Prerequisites:

Classes
Functions

The __init__() constructor + player/enemy properties

The player and enemy classes are found in enemy.py and player.py. Shown in the __init__() function, they share the same movement properties, including:

  • rect, the hitbox of the player/enemy
  • air_timer, the amount of time the player/enemy is in the air
  • movement, stores vertical and horizontal movement of the player/enemy. It's an array with two values - [x, y]. The first value, x, represents horizontal movement, and the second value, y, represents vertical movement.
  • direction, horizontal direction of player/enemy. -1 = moving left, 0 = not moving horizontally, 1 = moving right.
  • y_momentum, vertical speed of the enemy/player.

Player functions

  • collides: returns true if the player is colliding the rect passed in as a parameter.
  • move: moves the player based off of the player's direction, collisions, and y_momentum. Called in main.py.
  • getMovementInput: gets WASD input from the keyboard.

Enemy functions

  • moveRoutine: moves enemy in a routine.
  • moveRight, stopMovement, and moveLeft: moves enemy right, left, or stops movement.
  • move: moves the player based off of the enemy's direction, collisions, and y_momentum. Called in main.py.