Make Your Own Arduboy Game – Make Pong From Scratch!

Or Operator

We already talked about using the and operator. It’s a way we connect two comparisons inside of an ifstatement. To illustrate it, look at this code:

if( a == 5 and b > 20 ) {
	c = 0;
}

c will only be set to 0 if a is equal to 5 and b is greater than 20.

There are some other operators that we can use instead of the and operator. One of them is the or operator. The or operator can be used to check if one of a few comparisons are true. Check out this code:

if( a == 5 or b > 20 ) {
	c = 20;
}

In the above example, c will be set to 20 if either a is equal to 5 or b is greater than 20.

Keep the or operator in mind. We’ll use it soon!

Modulo Operation

Do you know the answer to 12 ÷ 6? The answer is 2! That was easy, right? Okay, what about 23 ÷ 5? The answer is either 4.6 or 4 with a remainder of 3. Which is the correct answer??

If you asked the Arduboy what 23 ÷ 5 was, the answer would be 4! That seems wrong! Where did the remainder go?? We need give the Arduboy another instruction to figure out the remainder.

Remember when I said that computers have to be very precise when given instructions? When computers were young, people tried to come up with the best way to do devision. There were a lot of ideas, but the best way seemed to be introducing the modulo operator.

To get the remainer, we ask the Arduboy for the answer of 23 % 5. It will output 3! If you asked 12 % 6, you would get 0!

Why is this important? Well, we can use this to figure out more complex math, but there’s something special in game development that we can use this for.

Anytime you use two numbers with the modulo operator, the answer will be somewhere between 0 and the second number. a % b will never be a number more than b.

Keep this in mind and we’ll do something really fun with it later!

7. Adjusting The AI

Okay, so, the computer player is way too good. We’ll never be able to win. One thing we can do to stop it from being so good is to only allow it to move some of the time. When will we allow it to move its paddle? Well, for starters, we could let it move its paddle only whenever the ball is close to the right side of the screen!

Find these lines of code:

if(bally < computery) {
	computery = computery - 1;
}
if(bally + ballsize > computery + paddleheight) {
	computery = computery + 1;
}

These are the lines of code that move the computer’s paddle. Let’s put an if statement around them that checks if the ball’s horizontal position (ballx) is close to the right-side of the screen. I put 115.

if(ballx > 115) {
	if(bally < computery) {
		computery = computery - 1;
	}
	if(bally + ballsize > computery + paddleheight) {
		computery = computery + 1;
	}
}

Test it out and think about it for a second. Notice how the computer’s paddle doesn’t always move like it used it? It’s a little more precise right now, but only moves whenever the ball is close to scoring.

But, we need to add more variance… More randomness… We need to make it so that the game is different every time you play. I KNOW! Let’s let the computer paddle move when the ball is close or let the computer paddle move when the computer picks a random number.

Back in Part 5 of this series, I showed you how to pick a random number. I’m going to explain to you how that works, now.

First of all, we need to know about the rand() function. This will give us a random number, but the number can really be anything. It can be 2,000, or 9 or -200. There’s really no control over it. HOWEVER, we can use the modulo operator that we learned about earlier!! Take a look at this code and think about what this will output!!

rand() % 20

Well, we’ll randomly pick a number, then divide it by 20, then only have the remainder left. It doesn’t matter what number is randomly picked, if we just looked at the remainder, the remainder will be some number between 0 and 20. Which number? I don’t know. Isn’t that awesome?! :laughing:

That little line of code will essentially pick a random number between 0 and 20!

Let’s adjust that if statement from above!

if(ballx > 115 or rand() % 20 == 1) {

This means that if ballx is greater than 115, or a randomly-picked number is equal to 1, then we move the comptuer’s paddle. Basically, the computer’s paddle will randomly move 1/20th of the time.

Here’s the full code so far!

//Jonathan Holmes (crait)
//December 7th, 2016
//A simple Pong clone

#include <Arduboy.h>
Arduboy arduboy;

//Variables declared here
int gamestate = 0;
int justpressed = 0;
int ballx = 62;
int bally = 0;
int ballsize = 4;
int ballright = 1;
int balldown = 1;
int paddlewidth = 4;
int paddleheight = 9;
int playerx = 0;
int playery = 0;
int computerx = 127 - paddlewidth;
int computery = 0;

void setup() {
	arduboy.begin();
	//Seed the random number generator
	srand(7/8);
	//Set the game to 60 frames per second
	arduboy.setFrameRate(60);
	arduboy.clear();
}

void loop() {
	//Prevent the Arduboy from running too fast
	if(!arduboy.nextFrame()) {
		return;
	}
	arduboy.clear();
	//Game code here
	switch( gamestate ) {
		case 0:
			//Title screen
			arduboy.setCursor(0, 0);
			arduboy.print("Title Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 1;
			}
			break;
		case 1:
			//Gameplay screen
			arduboy.setCursor(0, 0);
			arduboy.print("Gameplay");
			//Draw the ball
			arduboy.fillRect(ballx, bally, ballsize, ballsize, WHITE);
			//Move the ball right
			if(ballright == 1) {
				ballx = ballx + 1;
			}
			//Move the ball left
			if(ballright == -1) {
				ballx = ballx - 1;
			}
			//Reflect the ball off of the left side of the screen
			if(ballx == 0) {
				ballright = 1;
			}
			//Reflect the ball off of the right side of the screen
			if(ballx + ballsize == 127) {
				ballright = -1;
			}
			//Move the ball down
			if(balldown == 1) {
				bally = bally + 1;
			}
			//Move the ball up
			if(balldown == -1) {
				bally = bally - 1;
			}
			//Reflect the ball off of the top of the screen
			if(bally == 0) {
				balldown = 1;
			}
			//Reflect the ball off of the bottom of the screen
			if(bally + ballsize == 63) {
				balldown = -1;
			}
			//Draw the player's paddle
			arduboy.fillRect(playerx, playery, paddlewidth, paddleheight, WHITE);
			//If the player presses Up and the paddle is not touching the top of the screen, move the paddle up
			if(arduboy.pressed(UP_BUTTON) and playery > 0) {
				playery = playery - 1;
			}
			//If the player presses down and the paddle is not touching the bottom of the screen, move the paddle down
			if(arduboy.pressed(DOWN_BUTTON) and playery + paddleheight < 63) {
				playery = playery + 1;
			}
			//Draw the computer's paddle
			arduboy.fillRect(computerx, computery, paddlewidth, paddleheight, WHITE);
			//If the ball is close to the edge of the screen or if a random number out of 20 is equal to 1
			if(ballx > 115 or rand() % 20 == 1) {
				//If the ball is higher than the computer's paddle, move the computer's paddle up
				if(bally < computery) {
					computery = computery - 1;
				}
				//If the bottom of the ball is lower than the bottom of the computer's paddle, move the comptuer's paddle down
				if(bally + ballsize > computery + paddleheight) {
					computery = computery + 1;
				}
			}



			//If the ball makes contact with the player's paddle, bounce it back to the right
			if(ballx == playerx + paddlewidth and playery < bally + ballsize and playery + paddleheight > bally) {
				ballright = 1;
			}
			//If the ball makes contact with the computer's paddle, bounce it back to the left
			if(ballx + ballsize == computerx and computery < bally + ballsize and computery + paddleheight > bally) {
				ballright = -1;
			}
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 2;
			}
			break;
		case 2:
			//Win screen
			arduboy.setCursor(0, 0);
			arduboy.print("Win Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 3;
			}
			break;
		case 3:
			//Game over screen
			arduboy.setCursor(0, 0);
			arduboy.print("Game Over Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 0;
			}
			break;
	}
	//Check if the button is being held down
	if(arduboy.notPressed(A_BUTTON)) {
		justpressed = 0;
	}
	arduboy.display();
}

8. Scoring

Since we’re going to introduce scoring, we need a way for the ball to actually be scored. This means we need to remove some of our old bouncing code.

if(ballx == 0) {
	ballright = 1;
}
if(ballx + ballsize == 127) {
	ballright = -1;
}

Find those lines and remove them. This way, the ball will continue to move beyond the screen. In fact, we’ll check to see if the ball has moved off of the screen in order to give points!

Feel free to test the code out once you remove that code. :laughing:

To keep track of the scores, let’s declare some variables at the top of the sketch.

int playerscore = 0;
int computerscore = 0;

Now, look for the code in case 1 that displayed the text, “Gameplay.” Instead of just displaying that text, let’s modify it to display the player’s and computer’s scores.

arduboy.setCursor(20, 0);
arduboy.print(playerscore);

arduboy.setCursor(101, 0);
arduboy.print(computerscore);

Next, we need a way to actually score! Since the code now allows for the ball to move off of the screen, let’s check if it’s off the screen and if it is, then give someone a point, and then move it back into the middle of the screen.

Here’s how we can check if the ball is off of the screen.

if(ballx < -10) {

}
if(ballx > 130) {

}

To change the points, and put it back into the middle of the screen, we simply change the appropriate variables…

if(ballx < -10) {
	ballx = 63;
	computerscore = computerscore + 1;
}
if(ballx > 130) {
	ballx = 63;
	playerscore = playerscore + 1;
}

I really suggest testing the game out, so far. Here’s the code that I have:

//Jonathan Holmes (crait)
//December 7th, 2016
//A simple Pong clone

#include <Arduboy.h>
Arduboy arduboy;

//Variables declared here
int gamestate = 0;
int justpressed = 0;
int ballx = 62;
int bally = 0;
int ballsize = 4;
int ballright = 1;
int balldown = 1;
int paddlewidth = 4;
int paddleheight = 9;
int playerx = 0;
int playery = 0;
int computerx = 127 - paddlewidth;
int computery = 0;
int playerscore = 0;
int computerscore = 0;

void setup() {
	arduboy.begin();
	//Seed the random number generator
	srand(7/8);
	//Set the game to 60 frames per second
	arduboy.setFrameRate(60);
	arduboy.clear();
}

void loop() {
	//Prevent the Arduboy from running too fast
	if(!arduboy.nextFrame()) {
		return;
	}
	arduboy.clear();
	//Game code here
	switch( gamestate ) {
		case 0:
			//Title screen
			arduboy.setCursor(0, 0);
			arduboy.print("Title Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 1;
			}
			break;
		case 1:
			//Gameplay screen
			//Display the player's score
			arduboy.setCursor(20, 0);
			arduboy.print(playerscore);
			//Display the computer's score
			arduboy.setCursor(101, 0);
			arduboy.print(computerscore);
			//Draw the ball
			arduboy.fillRect(ballx, bally, ballsize, ballsize, WHITE);
			//Move the ball right
			if(ballright == 1) {
				ballx = ballx + 1;
			}
			//Move the ball left
			if(ballright == -1) {
				ballx = ballx - 1;
			}
			//Move the ball down
			if(balldown == 1) {
				bally = bally + 1;
			}
			//Move the ball up
			if(balldown == -1) {
				bally = bally - 1;
			}
			//Reflect the ball off of the top of the screen
			if(bally == 0) {
				balldown = 1;
			}
			//Reflect the ball off of the bottom of the screen
			if(bally + ballsize == 63) {
				balldown = -1;
			}
			//Draw the player's paddle
			arduboy.fillRect(playerx, playery, paddlewidth, paddleheight, WHITE);
			//If the player presses Up and the paddle is not touching the top of the screen, move the paddle up
			if(arduboy.pressed(UP_BUTTON) and playery > 0) {
				playery = playery - 1;
			}
			//If the player presses down and the paddle is not touching the bottom of the screen, move the paddle down
			if(arduboy.pressed(DOWN_BUTTON) and playery + paddleheight < 63) {
				playery = playery + 1;
			}
			//Draw the computer's paddle
			arduboy.fillRect(computerx, computery, paddlewidth, paddleheight, WHITE);
			//If the ball is close to the edge of the screen or if a random number out of 20 is equal to 1
			if(ballx > 115 or rand() % 20 == 1) {
				//If the ball is higher than the computer's paddle, move the computer's paddle up
				if(bally < computery) {
					computery = computery - 1;
				}
				//If the bottom of the ball is lower than the bottom of the computer's paddle, move the comptuer's paddle down
				if(bally + ballsize > computery + paddleheight) {
					computery = computery + 1;
				}
			}
			//If the ball moves off of the screen to the left...
			if(ballx < -10) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the computer a point
				computerscore = computerscore + 1;
			}
			//If the ball moves off of the screen to the right....
			if(ballx > 130) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the player a point
				playerscore = playerscore + 1;
			}

			//If the ball makes contact with the player's paddle, bounce it back to the right
			if(ballx == playerx + paddlewidth and playery < bally + ballsize and playery + paddleheight > bally) {
				ballright = 1;
			}
			//If the ball makes contact with the computer's paddle, bounce it back to the left
			if(ballx + ballsize == computerx and computery < bally + ballsize and computery + paddleheight > bally) {
				ballright = -1;
			}
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 2;
			}
			break;
		case 2:
			//Win screen
			arduboy.setCursor(0, 0);
			arduboy.print("Win Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 3;
			}
			break;
		case 3:
			//Game over screen
			arduboy.setCursor(0, 0);
			arduboy.print("Game Over Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 0;
			}
			break;
	}
	//Check if the button is being held down
	if(arduboy.notPressed(A_BUTTON)) {
		justpressed = 0;
	}
	arduboy.display();
}

Awesome, huh? It’s pretty much a finished game at this point! We just need to clean it up, some!

One thing we need to do is check if someone wins! Let’s do a check to see if someone has 5 points. If they do, then change the gamestate variable to the appropriate value.

if(playerscore == 5) {
	gamestate = 2;
}
if(computerscore == 5) {
	gamestate = 3;
}

Hmm… Maybe we should change the code inside of case 2 for pushing the A button. Instead of taking you to gamestate = 3, maybe we should use gamestate = 0 so that it takes you to the title screen of the game. Why would we want to show the win screen, then game over screen right afterwards, right?

Here’s the completed code so far:

//Jonathan Holmes (crait)
//December 7th, 2016
//A simple Pong clone

#include <Arduboy.h>
Arduboy arduboy;

//Variables declared here
int gamestate = 0;
int justpressed = 0;
int ballx = 62;
int bally = 0;
int ballsize = 4;
int ballright = 1;
int balldown = 1;
int paddlewidth = 4;
int paddleheight = 9;
int playerx = 0;
int playery = 0;
int computerx = 127 - paddlewidth;
int computery = 0;
int playerscore = 0;
int computerscore = 0;

void setup() {
	arduboy.begin();
	//Seed the random number generator
	srand(7/8);
	//Set the game to 60 frames per second
	arduboy.setFrameRate(60);
	arduboy.clear();
}

void loop() {
	//Prevent the Arduboy from running too fast
	if(!arduboy.nextFrame()) {
		return;
	}
	arduboy.clear();
	//Game code here
	switch( gamestate ) {
		case 0:
			//Title screen
			arduboy.setCursor(0, 0);
			arduboy.print("Title Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 1;
			}
			break;
		case 1:
			//Gameplay screen
			//Display the player's score
			arduboy.setCursor(20, 0);
			arduboy.print(playerscore);
			//Display the computer's score
			arduboy.setCursor(101, 0);
			arduboy.print(computerscore);
			//Draw the ball
			arduboy.fillRect(ballx, bally, ballsize, ballsize, WHITE);
			//Move the ball right
			if(ballright == 1) {
				ballx = ballx + 1;
			}
			//Move the ball left
			if(ballright == -1) {
				ballx = ballx - 1;
			}
			//Move the ball down
			if(balldown == 1) {
				bally = bally + 1;
			}
			//Move the ball up
			if(balldown == -1) {
				bally = bally - 1;
			}
			//Reflect the ball off of the top of the screen
			if(bally == 0) {
				balldown = 1;
			}
			//Reflect the ball off of the bottom of the screen
			if(bally + ballsize == 63) {
				balldown = -1;
			}
			//Draw the player's paddle
			arduboy.fillRect(playerx, playery, paddlewidth, paddleheight, WHITE);
			//If the player presses Up and the paddle is not touching the top of the screen, move the paddle up
			if(arduboy.pressed(UP_BUTTON) and playery > 0) {
				playery = playery - 1;
			}
			//If the player presses down and the paddle is not touching the bottom of the screen, move the paddle down
			if(arduboy.pressed(DOWN_BUTTON) and playery + paddleheight < 63) {
				playery = playery + 1;
			}
			//Draw the computer's paddle
			arduboy.fillRect(computerx, computery, paddlewidth, paddleheight, WHITE);
			//If the ball is close to the edge of the screen or if a random number out of 20 is equal to 1
			if(ballx > 115 or rand() % 20 == 1) {
				//If the ball is higher than the computer's paddle, move the computer's paddle up
				if(bally < computery) {
					computery = computery - 1;
				}
				//If the bottom of the ball is lower than the bottom of the computer's paddle, move the comptuer's paddle down
				if(bally + ballsize > computery + paddleheight) {
					computery = computery + 1;
				}
			}
			//If the ball moves off of the screen to the left...
			if(ballx < -10) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the computer a point
				computerscore = computerscore + 1;
			}
			//If the ball moves off of the screen to the right....
			if(ballx > 130) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the player a point
				playerscore = playerscore + 1;
			}
			//Check if the player wins
			if(playerscore == 5) {
				gamestate = 2;
			}
			//Check if the computer wins
			if(computerscore == 5) {
				gamestate = 3;
			}



			//If the ball makes contact with the player's paddle, bounce it back to the right
			if(ballx == playerx + paddlewidth and playery < bally + ballsize and playery + paddleheight > bally) {
				ballright = 1;
			}
			//If the ball makes contact with the computer's paddle, bounce it back to the left
			if(ballx + ballsize == computerx and computery < bally + ballsize and computery + paddleheight > bally) {
				ballright = -1;
			}
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 2;
			}
			break;
		case 2:
			//Win screen
			arduboy.setCursor(0, 0);
			arduboy.print("Win Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 0;
			}
			break;
		case 3:
			//Game over screen
			arduboy.setCursor(0, 0);
			arduboy.print("Game Over Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 0;
			}
			break;
	}
	//Check if the button is being held down
	if(arduboy.notPressed(A_BUTTON)) {
		justpressed = 0;
	}
	arduboy.display();
}

9. Reset Function

If you tested this out, you’re going to notice that once you win and try to play again, you won’t really be able to. The reason this happens is that the Arduboy remembers the score from the last match! It starts the new match of Pong, checks the scores, and realizes someone has already won, then shows you either the win screen or game over screen.

What we need to do is reset the game’s variables… ballxplayerscore, and computerscore.

Scroll all the way down to case 2 and case 3. Inside of both of them, there’s a check to see if we push the A button. This is a great place to put the code to reset our variables since pushing A is supposed to reset the game to the very beginning.

So, inside of the button checks of case 2 and case 3, put this code:

ballx = 63;
playerscore = 0;
computerscore = 0;

Actually! I got an idea! Instead of writing the same code multiple times, I should teach you about writing functions to save you time!

You already kinda know what functions are, right? They’re instructions that the computer follows. One example of this is arduboy.clear(), which will erase everything on the screen!

Well, one cool thing about programming is that you can write your own functions! It’s really helpful! Sometimes, you’ll have code that you want to use over and over again, but maybe you don’t want to type over and over again…

Scroll all the way up… After you declare your variables, let’s declare a function. :heart: We’ll call it resetgame(). Type this out:

void resetgame() {

}

Inside of the braces, we’ll be able to put the instructions that are contained within the resetgame() function. Let’s update it with the code we used a moment ago!

void resetgame() {
	ballx = 63;
	playerscore = 0;
	computerscore = 0;
}

There we go! Any time we want to run those instructions, instead of typing out all 3 of those lines, we can save time by typing resetgame() instead!

Back down near the bottom, add the resetgame() function where you change gamestate to 0! :slight_smile:

If you got confused, here’s the full code!

//Jonathan Holmes (crait)
//December 7th, 2016
//A simple Pong clone

#include <Arduboy.h>
Arduboy arduboy;

//Variables declared here
int gamestate = 0;
int justpressed = 0;
int ballx = 62;
int bally = 0;
int ballsize = 4;
int ballright = 1;
int balldown = 1;
int paddlewidth = 4;
int paddleheight = 9;
int playerx = 0;
int playery = 0;
int computerx = 127 - paddlewidth;
int computery = 0;
int playerscore = 0;
int computerscore = 0;

void resetgame() {
	ballx = 63;
	playerscore = 0;
	computerscore = 0;
}

void setup() {
	arduboy.begin();
	//Seed the random number generator
	srand(7/8);
	//Set the game to 60 frames per second
	arduboy.setFrameRate(60);
	arduboy.clear();
}

void loop() {
	//Prevent the Arduboy from running too fast
	if(!arduboy.nextFrame()) {
		return;
	}
	arduboy.clear();
	//Game code here
	switch( gamestate ) {
		case 0:
			//Title screen
			arduboy.setCursor(0, 0);
			arduboy.print("Title Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 1;
			}
			break;
		case 1:
			//Gameplay screen
			//Display the player's score
			arduboy.setCursor(20, 0);
			arduboy.print(playerscore);
			//Display the computer's score
			arduboy.setCursor(101, 0);
			arduboy.print(computerscore);
			//Draw the ball
			arduboy.fillRect(ballx, bally, ballsize, ballsize, WHITE);
			//Move the ball right
			if(ballright == 1) {
				ballx = ballx + 1;
			}
			//Move the ball left
			if(ballright == -1) {
				ballx = ballx - 1;
			}
			//Move the ball down
			if(balldown == 1) {
				bally = bally + 1;
			}
			//Move the ball up
			if(balldown == -1) {
				bally = bally - 1;
			}
			//Reflect the ball off of the top of the screen
			if(bally == 0) {
				balldown = 1;
			}
			//Reflect the ball off of the bottom of the screen
			if(bally + ballsize == 63) {
				balldown = -1;
			}
			//Draw the player's paddle
			arduboy.fillRect(playerx, playery, paddlewidth, paddleheight, WHITE);
			//If the player presses Up and the paddle is not touching the top of the screen, move the paddle up
			if(arduboy.pressed(UP_BUTTON) and playery > 0) {
				playery = playery - 1;
			}
			//If the player presses down and the paddle is not touching the bottom of the screen, move the paddle down
			if(arduboy.pressed(DOWN_BUTTON) and playery + paddleheight < 63) {
				playery = playery + 1;
			}
			//Draw the computer's paddle
			arduboy.fillRect(computerx, computery, paddlewidth, paddleheight, WHITE);
			//If the ball is close to the edge of the screen or if a random number out of 20 is equal to 1
			if(ballx > 115 or rand() % 20 == 1) {
				//If the ball is higher than the computer's paddle, move the computer's paddle up
				if(bally < computery) {
					computery = computery - 1;
				}
				//If the bottom of the ball is lower than the bottom of the computer's paddle, move the comptuer's paddle down
				if(bally + ballsize > computery + paddleheight) {
					computery = computery + 1;
				}
			}
			//If the ball moves off of the screen to the left...
			if(ballx < -10) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the computer a point
				computerscore = computerscore + 1;
			}
			//If the ball moves off of the screen to the right....
			if(ballx > 130) {
				//Move the ball back to the middle of the screen
				ballx = 63;
				//Give the player a point
				playerscore = playerscore + 1;
			}
			//Check if the player wins
			if(playerscore == 5) {
				gamestate = 2;
			}
			//Check if the computer wins
			if(computerscore == 5) {
				gamestate = 3;
			}


			//If the ball makes contact with the player's paddle, bounce it back to the right
			if(ballx == playerx + paddlewidth and playery < bally + ballsize and playery + paddleheight > bally) {
				ballright = 1;
			}
			//If the ball makes contact with the computer's paddle, bounce it back to the left
			if(ballx + ballsize == computerx and computery < bally + ballsize and computery + paddleheight > bally) {
				ballright = -1;
			}
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				gamestate = 2;
			}
			break;
		case 2:
			//Win screen
			arduboy.setCursor(0, 0);
			arduboy.print("Win Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				resetgame();
				gamestate = 0;
			}
			break;
		case 3:
			//Game over screen
			arduboy.setCursor(0, 0);
			arduboy.print("Game Over Screen");
			//Change the gamestate
			if(arduboy.pressed(A_BUTTON) and justpressed == 0) {
				justpressed = 1;
				resetgame();
				gamestate = 0;
			}
			break;
	}
	//Check if the button is being held down
	if(arduboy.notPressed(A_BUTTON)) {
		justpressed = 0;
	}
	arduboy.display();
}

Wow!

Give yourself a pat on the back! Good job with this one! It was a long tutorial, but if you made it this far, you really deserve some kind of prize! :slight_smile: You are definitely ready to start experimenting and making your own game!

Next Tutorial

The next tutorial will be a bit shorter. I’ll teach you some cool stuff that I purposely skipped before. A lot more features that will make programming a little easier for you. We’ll use that stuff to upgrade this Pong game to have more features, too!

This tutorial is copied and abridged from the original by Jonathan Holmes.

Credits

I wrote this tutorial in order to give back to the programming community that taught me to get into it about 10 years ago. If you’d like to follow me on Twitter, please do so at http://www.twitter.com/crait34 . I’d greatly appreciate that. :smile: