Top-down game template

This is a template I have been using personally to get started quick with a new top-down game without doing all the simple stuff like player movement for the tenth time. I’m posting this in the Beginners section because I think beginners who don’t know much will appreciate the lengthy comments I put into it. Please let me know if I should change, remove, or add anything!

The simplest games only have stationary hazards. My first game using this template(granted, a much sloppier version) just had a points system. You gained points by touching a square, and lost points if you hit the walls. However, there was a constant gravity pushing the player towards a wall, and the gravity changed every time you got a point. It was fun and didn’t need any enemies.

One last thing - “TfGuy44” gets credit for helping me make the whole player movement system smooth and not janky.

/*
Author's Name
Project's Name
Started ______
Last editted ______
*/

int playerWidth = 20;
int playerHeight = 20;
/*
The 500 in this equation is the display screen's 
width, but that variable cannot be used here.
The equation makes the player's x coordinate centered
on the display screen upon startup.
*/
int playerX = 500/2 - playerWidth/2;
/*
The 500 in this equation is the display screen's
height, but that variable cannot be used here.
The equation makes the player's y coordinate centered
on the display screen upon startup.
*/
int playerY = 500/2 - playerHeight/2; 
int playerSpeed = 5;

void setup()
{
  /*
  If either of these variables are changed, the
  respective number above used to center the player
  upon startup should also be changed.
  */
  size(500, 500);
}

void draw()
{
  background(150, 80, 10);
  
  /*
  This creates semi-transparent lines. To ensure that
  the lines are evenly spaced and that none are 
  missing, width divided by lineFrequency should
  equal 0, and the same for height. If those
  conditions are not met, then the final lines may
  not appear, and if they do, then the lines will
  not be evenly spaced. The opacity of the lines is
  controlled by the final variable of stroke,
  minimum 0 and maximum 255.
  */
  stroke(0, 0, 0, 120);
  int lineFrequency = 100;
  for(int i = 1; i < height/lineFrequency;  i ++)
  {
    line(i*lineFrequency, 0, i*lineFrequency, height);
    line(0, i*lineFrequency, width, i*lineFrequency);
  }
  stroke(0, 0, 0, 255);//reset stroke to normal
  
  /*
  kd[] is a boolean array defined below in void(k).
  The exact key for each value of kd[] is simply the
  same value of the ks[] array. These are both found
  above the definition of void k(). Values 0 through 3
  are a, s, d, w respectively, so if those keys are
  being pressed, I simply update the players position
  in the relative direction with +/- playerSpeed.
  */
  if(kd[0]) playerX -= playerSpeed;
  if(kd[1]) playerY += playerSpeed;
  if(kd[2]) playerX += playerSpeed;
  if(kd[3]) playerY -= playerSpeed;
  //If you add more keys to ks[] and kd[], then add the
  //key's function here under the next number for if(kd[x])
  
  /*
  The 4 lines below lock the player on the screen. If you
  want this function removed, simply comment them out.
  */
  if(playerX < 0) playerX = 0;
  if(playerX > width - playerWidth) playerX = width - playerWidth;
  if(playerY < 0) playerY = 0;
  if(playerY > height - playerHeight) playerY = height - playerHeight;
  
  fill(30, 200, 30);
  rect(playerX, playerY, playerWidth, playerHeight);
}

/*
This default processing fuction is called for the first
time every time a new key is pressed down, and it will
continue being called until every single key is let go.
*/
void keyPressed()
{
  k(true);
}

/*
This default processing function is called once every
time a key is let go of, wether it was tapped or held
and then released.
*/
void keyReleased()
{
  k(false);
}

/*
Although only being simple arrays, these two arrays
communicate with each other and are to an extent the
same things. kd[] is what can be used in void draw()
in order to do whatever you want when a certain key
is pressed. ks[] is used in void k() to very quickly
and efficiently know which exact key was pressed down
or released. To track more keys, simply add it to ks[]
using the same syntax and don't forget to add another
false to kd[]. For special keys like ENTER, UP, TAB, etc.,
do not use '' but instead simply type it in all caps. FYI,
the SPACE key is just ' '.
*/
int[] ks = {'a', 's', 'd', 'w'};
boolean[] kd = {false, false, false, false};

/*
If a new key is pressed down, ks[] checks which key it
was which lets kd[] know which value to change to true.
If a key is released, ks[] checks which key it was and
lets kd[] know which value to reset to false. By using
this system instead of just if() statements in void 
keyPressed(), it allows multiple keys to be held down at
the same time with no problems.
*/
void k(boolean b)
{
  for(int i = 0; i < ks.length; i ++)
  {
    if(ks[i] == key || ks[i] == keyCode) kd[i] = b;
  }
}
1 Like

Another diagonal movements example, but w/ 2 ball “players”: :volleyball:

1 Like