Writing Games with Processing: Moving Around

18. December, 2013

The game would be pretty boring if Platty would just sit there, waiting to be eaten. The player needs to be able to move him around:

void keyPressed() {
  if(key == CODED) {
    if(keyCode == UP) {
      movePlayer(0, -1);
    } else if(keyCode == DOWN) {
      movePlayer(0, 1);
    } else if(keyCode == LEFT) {
      movePlayer(-1, 0);
    } else if(keyCode == RIGHT) {
      movePlayer(1, 0);
  } else if(key == ' ') {
    movePlayer(0, 0);

As you can see, I’m using the cursor keys to move the character. You could as easily use the keys WASD or any other combination but since we don’t need the mouse, the cursor keys felt like a good choice. Also note that the player can sit around and wait using the space bar.

The code to move the character is pretty simple:

void movePlayer(int dx, int dy) {
  px += dx * tileSize;
  py += dy * tileSize;

An alternative would be to always center the screen around the character and move everything else.

Or you can try to move the player one pixel at a time. Change the code to

void movePlayer(int dx, int dy) {
  px += dx;
  py += dy;

and play a bit. How does that feel?

When playing with the game, you might notice a slight problem: You can move outside of the visible screen. We need to check that the new coordinate is valid before moving the character:

void movePlayer(int dx, int dy) {
  dx *= tileSize;
  dy *= tileSize;
  int newX = px + dx;
  int newY = py + dy;
  if(newX >= 0
    && newX < width
    && newY >= 0
    && newY < height
  ) {
    px = newX;
    py = newY;

Nice. We have a player character, we have control, we have enemies. What else do we need? Moving the enemies.

You can find the whole source code here.

Previous: Enemies
First post: Getting Started

Writing Games with Processing: Enemies

17. December, 2013

The next element of our game are the crocodiles. Since we can have several of them, let’s use a class to collect all data necessary to draw them:

class Enemy {
  color c;
  int x, y;
  String name;
  Enemy(String name) {
    this.name = name;
    c = color(222,40,107);
  void draw() {
    ellipse(x + tileSize/2, y + tileSize/2, tileSize, tileSize);
    textAlign(CENTER, TOP);
    text(name, x + tileSize/2, y + tileSize + 2);

As you can see, the code is pretty similar to the drawing Platty, the player character. I’m giving them names to make it easier to distinguish them on the screen and while debugging.

Next, we need a list of enemies and a helper function to easily create enemies:

ArrayList<Enemy> enemies = new ArrayList<Enemy>();
void addEnemy(int x, int y, String name) {
  Enemy enemy = new Enemy(name);
  enemy.x = x;
  enemy.y = y;

To draw them, we just loop over the elements in the list and call the draw() method of each element:

void drawEnemies() {
 for(Enemy e: enemies) {

Next, we add two enemies in the initial setup:

void setup() {
  size(640, 480); //VGA for those old enough to remember
  addEnemy(20, 20, "Kenny");
  addEnemy(600, 20, "Benny");

After adding the new drawEnemies() method to draw(), we’re done:

void draw() {

Let’s see what we’ve got:


You can find the final code here.

Next: Moving around
Previous: Setup and a Simple Player Character
First post: Getting Started

Writing Games with Processing: Setup and a Simple Player Character

16. December, 2013

We have an idea, now we need to write the game. Let’s use a screen size of 640 x 480:

void setup() {
  size(640, 480); //VGA for those old enough to remember

The next item on the task list is to render the game area. Let’s create a nice grass background:

void draw() {
  fill(174, 204, 27);
  rect(0, 0, width, height);

Too boring? Well, we can always add a more complex background later. Right now, we’re in development mode: We want to test some game ideas, quickly prototype something and play. Fancy graphics is our least priority right now.

What else do we need? The player character, Platty, the platypus:

int px = 320, py = 240;

void drawPlatty() {
  rect(px, py, 20, 20);
  textAlign(CENTER, CENTER);
  text("P", px+10, py+10);

void drawBackground() {
  fill(174, 204, 27); // green
  rect(0, 0, width, height); // fill whole screen

void draw() {

That’s it. Again nothing fancy, just the bare minimum.

You will notice that some of the numbers are related but it’s not obvious from the code. At this point, we’ll have to make our first design decision: Can the player move freely (pixel by pixel) or is the movement tile base (as in chess)?

At this stage, both are equally simple to implement. An ego shooter or a jump&run would use fluid movement while a puzzle type game often uses tiles.

Let’s use tiles:

int px = 320, py = 240;
int tileSize = 20;

void drawPlatty() {
  rect(px, py, tileSize, tileSize);
  textAlign(CENTER, CENTER);
  text("P", px+tileSize/2, py+tileSize/2-2);

Much better.

Note how I split tasks into different helper methods to keep the code readable. I could have done the player character rendering inside of the main draw() method. But by moving the code into different methods, I can keep things together that belong together while at the same time documenting my code. drawBackground() is like a comment explaining what the code in the method does. But unlike a comment, it can’t become outdated.

You can find the final version of the code here.

Next: Enemies!

First post in the series: Getting Started

Writing Games with Processing: Getting Started

15. December, 2013

Ever wanted to write your own game? Can’t be too hard, right?

Most games sold today are ego shooters. But the genre is sucked pretty dry. The most innovative games in the last years were simple, surprising and cheap. Examples are BraidFez, Thomas Was Alone. Those games didn’t thrive from multi-million development budgets, they throve from simple ideas. They were great not despite but because of their limitations.

In this series of blog posts, I’ll show you how to develop a very simple game using Processing.

Processing is a simple environment to create amazing computer generated images using a simplified version of Java like the one on the right (code).

Komplexe Methoden –  M.1 Zufall und Rauschen –  M.1.6 Agenten im Raum –  M_1_6_01_TOOL

Komplexe Methoden – M.1 Zufall und Rauschen – M.1.6 Agenten im Raum – M_1_6_01_TOOL

While you download the software, we need an idea.

In my case, the idea came from a brainstorming session organized by Zurich Game Developers at the MechArtLab. Everyone had to buy a Kinder Surprise egg (illegal in the USA, btw.) and create a game with the content. The eggs in my group contained a platypus, a crocodile and a frog.

After a quick brainstorming session we came up with this game idea: “Kribbit, the frog, runs a zoo. A small zoo. A very small zoo. Only a single compound. He has two animals: A poor, lonely platypus and a couple of crocodiles. The crocodiles promised to be nice to the platypus and not to eat him. But every time Kribbit looks the other way, the crocodiles try to eat poor Platty.”

That’s it. Game idea? Check.

What? Too simple for you? Well, complex game = low chance of success.

Next post: Setup and a simple player character.

Selling Used Games? No Way!

4. January, 2013

Sony found a new way to harass customers: They filed a patent for a technology that prevents playing second-hand games.

In a nutshell, the “game playing system” checks whether someone else already played the game on a different system and if so, it refuses to start the game.

Great, isn’t it?

With ideas like that, Sony will surely overcome it’s financial issues, soon – by going bankrupt even faster.

Need more reasons not to buy Sony?

Related articles:

Dealing With Cheating

13. July, 2012

All online games attract cheaters. Most of them try to ban players who cheat but Rockstar Games came up with a better approach: They herd them.

Makes me wonder what took them so long. It would be great if there was a special server for cheaters and people using modded clients. Just imagine how many people will start working on AI problems (identify threats, take cover, shooting at targets, move around in a complex maze).

Create a Better World, One Game at a Time

10. July, 2012

Can you make the world better by playing games?

Sure: Fold enzymes on fold.it. Scientists tried for 15 years to fold a H.I.V related enzyme, fold.it solved it in 15 days.

You know better than the government? Look at social impact bonds.

Want to improve ethical behavior of large corporations? Donate to a convent.

Under the condition that they use it to buy enough shares to submit resolutions at shareholder meetings. And if that doesn’t work, the church has centuries of experience with public shaming. As Sister Nora Nash of the Sisters of St. Francis put it: “We’re not here to put corporations down. We’re here to improve their sense of responsibility.