[Tutorial][Community]3.Level 1-3, first game

ok let me level with you, we are going to cover a fair bit in this on we are making the first game
what is the first game? well technically its tennis for 2 but a more popular variation is called PONG

#include "Pokitto.h"
Pokitto::Core game;
	In this tutorial we are using fast screen mode.
	It means screen resolution is 110x88
	playerA's buttons are UP and DOWN
	playerB's buttons are A and B
//setting variables
char ballX = 55;
float ballY = 44;
float ballS = 0;
bool ballV = 1, ballH = 0;
char paddleA = 40;
char paddleB = 40;
int scoreA = 0;
int scoreB = 0;
char screenW = 110;
char screenH = 88;

void resetVal(bool isBallLeft);

int main(){
	game.display.width = screenW;
	game.display.height = screenH;

	while (game.isRunning()) {
    	if (game.update()) {
    		if (game.buttons.repeat(BTN_UP,0) && paddleA > 0){ //if button up is pressed AND paddle is not touching to the edge
    			paddleA--; //then move playerA's paddle to up
    		if (game.buttons.repeat(BTN_DOWN,0) && paddleA < (screenH - 15)){ //if button down is pressed AND paddle is not touching to the edge
    			paddleA++; //then move playerA's paddle to down
			if (game.buttons.repeat(BTN_A,0) && paddleB > 0){ //same as playerA
    		if (game.buttons.repeat(BTN_B,0) && paddleB < (screenH - 15)){ //our paddle's height is 15
			if (ballV) ballY = ballY - ballS; //if bool value ballV is 1 then move ball to up
			else ballY = ballY + ballS;		//if bool value ballV is 0 then move ball to down
			if (ballH) ballX++;	//if bool value ballX is 1 then move ball to right
			else ballX--;		//you know what is going on here
			// first we check is ball touching the upward and downward edge
			if ((ballY < 5) || ((ballY + 5) > screenH)) { // we use ballY + 5 because our balls radius is 5
				ballV = !ballV; //if ball touching edge then change the direction
			// after we check is any player make score
			if (ballX == 4){	// if playerA not hit the ball
				scoreB++;		// increase playerB's score
			if ((ballX + 5) == (screenW - 4)){ // if playerB not hit the ball
				scoreA++;		// same as above
			// paddle-ball collision
			if ((ballX == 10) && (ballY > paddleA) && (ballY < paddleA+15)){
				ballH = !ballH;
				ballS = (((paddleA - ballY) / 7.5) + 1) * 2;
			if ((ballX == (screenW - 10)) && (ballY > paddleB) && (ballY < paddleB+15)){
				ballH = !ballH;
				ballS = (((paddleB - ballY) / 7.5) + 1) * 2;
    		// write score
    		game.display.color = 1;
    		// draw playerA's paddle
    		game.display.color = 2;
    		// draw ball
    		game.display.color = 3;
    		// draw playerB's paddle
    		game.display.color = 4;

void resetVal(bool isBallLeft){
    ballX = 55;
    ballY = 44;
    paddleA = 40;
    paddleB = 40;
    ballS = 0;
    ballV = 1;
    ballH = isBallLeft;

Note: Code updated for newly released pokitto simulator. Also i added color and some float values to make this game more realistic.


Humm. Am I missing something or ball is really not moving?

work in progress, though is you want to take on a challenge you can add to it
the pong version i want to show is the simple boolean version where it just goes diagonal
you cant upgrade to radial math and add a bit more physics but this is going to be meant for beginners so keeping it simple to explain the language

Okay. I’m started to working on playable version :smile:

awsome, this is a wiki post so you can edit the top post , do try and explain whats going on in your code, its meant for biginners

do we iterate on this?
i mean theres a decent example for making a function for the reset values

ballX = 55;		// reset everything to initial position
ballY = 44;
paddleA = 40;
paddleB = 40;
ballV = 0;
ballH = 0;	

could also add the minimal ai ( a simple linear interpolating one )

I don’t because there is no tutorial for functions. You can add if you want :slight_smile:

realy dumb function example

void resetVal(bool isBallLeft){
  ballX = 55;
  ballY = 44;
  paddleA = 40;
  paddleB = 40;
  ballV = 0;
  ballH = isBallLeft;

this is sort of a convention i got though on booleans where it the variable name is a question you know what to answer it and you know its a boolean by is, this is totaly optional but well clean understandable code
on the same topic there also like prefixes you can add like img or spr so you know thats a image or sprite, again those are conventions and people argue over these allot wich are more preferable like start a function with a capital letter or not, this isnt super important wen your working alone but it always happens wen your working in a team. sorry turned into a bit of a rant :stuck_out_tongue_closed_eyes:

ok first time in the simulator and BAM it works
im pretty amazed we did this compleetly blind :grin:
thx nullmember for helping out


That is so cool! Amazing!

1 Like

And ofcourse it works on hardware too!


I was going to add colors but they were already there. Question: why is the ball so slow now?

its only going 1 pixel per frame, so its very dependent on framerate
have you got a way to calculated the framerate yet?

Yep it is set at 20 fps exactly at the moment. The reason is that the fps vs hardware is most reliable at that point

wait someone aded floats to it XD

1 Like

Horizontal speed is same but vertical speed changed. Balls vertical speed changing every paddle-ball collision point. It’s more realistic. Can you try on hardware? If speed is same (I ask because in your video it’s running speeder than simulator) I can change speed of ball and paddles.

well its basicly aratmetic slowing it down, floating points are slower to calculate the intigers,

@adekto @NullMember Keep up the good work. I am going to test sim vs hardware later today with what ever you come up with and then calibrate the sim.

1 Like


I finally got this example to compile and sorted out stuff about screen mode.

But when I run it I get this error:

Any ideas?


Did the game actually run or did it jump straight to that?

And do you have a sample of the code you ran that you could post?