Processing meets Arduino project

For the Processing meets Arduino assignment I’ve decided to expand on my jumping ball game project and add a joystick to it. Thus, instead of being controlled by arrows on keyboard, the ball is now controlled by the joystick. This was fairly simple, since the ball’s movement is only controlled on the x axis, however, it took me a little to get used to figure out the adjustments I had to do in terms of screen width/canvas width so that the ball moves just like I want it to.

Another thing that was a sort of an easy fix but took a little while to figure out was how to restart the game on button press rather than restarting the whole processing sketch. The first idea was to restart the port connection on button press, but then an error would pop up and freeze the whole computer, because reestablishing the port connection would not work. The second thing I tried was rerunning the setup void (on button press) again once the game is over, however, that would crash processing and freeze the computer as well. Then I got lucky because Pierre walked into the room and suggested something very simple that I have not thought of for some reason. His idea was to create a gameRunning Boolean that would start the initialize void and the draw void. Thus, rather than restarting the whole setup void, I would just have a gameRunning Boolean on button press, and then depending on that the initialize and draw would either run or not, which will then be connected to the gameOver Boolean, and together, if game is over and game is not running the button press would restart the initialize and draw loops to start the game again.

As some improvements from the last time I’ve presented the game, I’ve added a couple of things. First of all, the platforms are now fading in rather than just suddenly appearing out of nowhere. Then I’ve also added the “Welcome” screen (I forgot to change ‘lol’ from when I was trying out if it works, and now I think it is just a part of the project), and the “Game Over” screen.

Here is the new code:

 

Game Sketch

Player p;
ArrayList platforms, platformsDanger;
boolean upPressed, leftPressed, downPressed, rightPressed;
int score, fallCount;
boolean gameOver;
int Ypos;
int Xpos;
int Sel;
boolean gameRunning;
int led=0;
int led2=0;

// to control a ball with a joystick
import processing.serial.*;
Serial myPort;

void setup()
{
 Xpos = 0;
 Ypos=0;
 Sel=1;
 size(480, 640);
 frameRate(60);
 //initializeDanger();
 ellipseMode(CORNER);

// for joystick
 printArray(Serial.list());
 String portname=Serial.list()[2];
 println(portname);
 myPort = new Serial(this, portname, 9600);
 myPort.clear();
 myPort.bufferUntil('\n');
}

void initialize()
{
 if (Sel==0) {
 gameRunning = true;
 score = 0;
 fallCount = 0;
 gameOver = false;
 p = new Player(width/2, height/2);
 platforms = new ArrayList();
 platforms.add(new MovingPlatform(20, 80, 70, 8, false));
 platforms.add(new Platform(width/2, height/2, 100, 8, false));
 platforms.add(new Platform((int)random(40, 500), 320, (int)random(50, 120), 8, false));
 platforms.add(new Platform((int)random(40, 500), 220, (int)random(50, 120), 8, false));
 platforms.add(new Platform((int)random(40, 500), 120, (int)random(50, 120), 8, false));
 platforms.add(new Platform((int)random(40, 500), 20, (int)random(50, 120), 8, false));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),20,20, true));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),30,30, true));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),45,45, true));
 } else { 
 background(255);
 fill(0);
 textSize(50);
 textAlign(CENTER);
 if (gameOver == false) { 
 text("lol", width/2, height/2);
 textSize(25);
 text("press to start", width/2, 500);
 
 } else if (gameOver == true) {
 text("You Lost", width/2, height/2);
 textSize(25);
 text("press to restart", width/2, 500);
 }
 }
}

//void initializeDanger(){
// platformsDanger = new ArrayList();
// platforms.add(new MovingPlatform(20,80,20,20, true));
// platforms.add(new MovingPlatform(20,80,30,30, true));
// platforms.add(new MovingPlatform(20,80,25,25, true));

//}

void draw() { 
 if (gameRunning ==true && gameOver == false) {
 led = 1;
 led2=0;
 } else if (gameOver == true) {
 led=0;
 led2=1;
 } else {
 led2=1;
 led = 0;
 }
 if (gameRunning == true) {
 //println(score);
 background(255);
 fill(0, 10, 153, 204);
 textSize(12);
 text("score", 15, 15);
 text(score, 60, 15);
 //println(platforms.size());
 for (int i=0; i<platforms.size(); i++)
 {

p.collide((Platform)platforms.get(i));
 ((Platform)platforms.get(i)).display();
 ((Platform)platforms.get(i)).move();
 //if(i<3){
 // p.collide((Platform)platformsDanger.get(i));
 // ((Platform)platformsDanger.get(i)).display();
 // ((Platform)platformsDanger.get(i)).move();
 //}
 }
 p.display(); 
 p.move();

adjustViewport();
 cleanUp();
 seedNewPlatforms();
 if (platformsBelow() == 0) gameOver = true;
 if (gameOver) fallCount++;
 if (fallCount > 3 ) initialize();
 } else { 
 initialize();
 }
}

int platformsBelow()
{
 int count = 0;
 for (int i=0; i<platforms.size(); i++)
 {
 if (((Platform)platforms.get(i)).y >= p.y) count++;
 }
 return count;
}


void adjustViewport()
{
 // above midpoint
 float overHeight = height * 0.5 - p.y;
 if (overHeight > 0) {
 p.y += overHeight;
 for (int i=0; i<platforms.size(); i++)
 {
 ((Platform)platforms.get(i)).y += overHeight;
 }
 score += overHeight;
 }
 // falling
 float underHeight = p.y - (height-p.h-4);
 if (underHeight > 0)
 {
 p.y -= underHeight;
 for (int i=0; i<platforms.size(); i++)
 {
 ((Platform)platforms.get(i)).y -= underHeight;
 }
 } 
 //above midpoint danger
 //for(int i=0; i<platformsDanger.size(); i++)
 // {
 // ((Platform)platformsDanger.get(i)).y += overHeight;
 // }
 // score += overHeight;

// falling
 //underHeight = p.y - (height-p.h-4);
 //if(underHeight > 0){
 // p.y -= underHeight;
 // for(int i=0; i<platformsDanger.size(); i++)
 // {
 // ((Platform)platformsDanger.get(i)).y -= underHeight;
 // }
 //}
}

void cleanUp()
{
 for (int i=platforms.size()-1; i>=0; i--) {
 // scrolled off the bottom
 if (((Platform)platforms.get(i)).y > height) {
 platforms.remove(i);
 }
 }
 //for(int i=platformsDanger.size()-1; i>=0; i--){
 // // scrolled off the bottom
 // if(((Platform)platformsDanger.get(i)).y > height){
 // platformsDanger.remove(i);
 // }
 //}
}

void seedNewPlatforms()
{
 if (platforms.size() < 9)
 {
 float randomizer = random(0, 10);

if (score < 1250) {
 if (randomizer < 3) {

platforms.add(new MovingPlatform((int)random(10, width-80), -10, 70, 8, false));
 } 
 //else if (randomizer < 4) {
 // platforms.add(new MovingPlatform((int)random(20,400),-10,30,30, true));
 // platforms.add(new MovingPlatform((int)random(20,400),-10,30,30, true));
 //}
 // else if (randomizer < 1) {
 // platforms.add(new MovingPlatform((int)random(20,400),-10,45,45, true));
 //} else {
 platforms.add(new Platform((int)random(20, 400), -10, (int)random(50, 120), 8, false));
 } else if (score < 500) {
 if (randomizer < 3) {
 platforms.add(new MovingPlatform((int)random(10, width-80), 300, 70, 8, false));
 } else {
 platforms.add(new Platform((int)random(20, 400), 300, (int)random(50, 120), 8, false));
 }
 } else { 
 if (randomizer < 9) {
 platforms.add(new MovingPlatform((int)random(20, 400), -10, 30, 30, true));
 } else {
 platforms.add(new MovingPlatform((int)random(10, width-80), 300, 70, 8, false));
 }
 }
 }
}

//if(platformsDanger.size() < 3)
//{
// platforms.add(new MovingPlatform((int)random(10,width-80),-10,20,20,true));
//}


void keyPressed()
{
 if (keyCode == UP) upPressed = true;
 if (keyCode == LEFT) leftPressed = true;
 if (keyCode == DOWN) downPressed = true;
 if (keyCode == RIGHT) rightPressed = true;
}

void keyReleased()
{
 if (keyCode == UP) upPressed = false;
 if (keyCode == DOWN) downPressed = false;
 if (keyCode == LEFT) leftPressed = false;
 if (keyCode == RIGHT) rightPressed = false;
}


void serialEvent(Serial myPort) {
 String s=myPort.readStringUntil('\n');
 s=trim(s);
 if (s!=null) {
 //println(s);
 int values[]=int(split(s, ','));
 if (values.length==3) {
 //Xpos=(int)map(values[0],0,1023,0, width);
 Xpos=(int)values[0];
 Ypos=(int)map(values[1], 0, 1023, 0, height);
 Sel=values[2];
 println("POS:");
 println(Xpos);
 println("ENDPOS");
 println("SEL "+Sel);
 
 myPort.write(led+","+led2+"\n");
 //println(Ypos);
 }
 }
}

 

Platform

class Platform {
 float x, y, w, h;
 float xvel; 
 boolean danger;
 float alpha;
 Platform(int x_, int y_, int w_, int h_, boolean d)
 {
 x = x_;
 y = y_;
 w = w_;
 h = h_;
 danger = d;

alpha = 0;

println("NEW PLATFORM:");
 println("Y: " + str(y));
 }

void display()
 {
 if (danger == false) {
 fill(0, 0, 0, alpha);
 } else {
 fill(255, 0, 0, alpha);
 }
 noStroke();
 rect(x, y, w, h);


if (alpha < 255) {
 alpha+=4f;
 }
 }

void move()
 {


 x += xvel;
 y += 0;
 }
}

class MovingPlatform extends Platform
{
 static final float speed = 0.9;

MovingPlatform(int x, int y, int w, int h, boolean d)
 {
 super(x, y, w, h, d);
 xvel = speed;
 }

void move()
 {
 super.move();
 if ( (x+w > width - 10) || (x < 10) )
 {
 xvel *= -1;
 }
 }
}

Player

class Player
{
 float gravity = 0.14;
 float bounceVel = 9;
 float maxYVel = 13;
 float maxXVel = 3;

float x, y, xVel, yVel;
 int w, h;
 Player(int x, int y)
 {
 w = h = 20;
 this.x = x;
 this.y = y;
 }

void display()
 {
 fill(0, 0, 240);
 ellipse(x, y, w, h);
 }

void move()
 {
 x += xVel;
 y += yVel;

if (x > width-w) x = 0;
 if (x < 0) x = width-w;

//horizontal
 // for arrows
 //if (!gameOver){
 // if (leftPressed) xVel -= 0.05;
 // else if (rightPressed) xVel += 0.05;
 // else
 // {
 // if (xVel > 0) xVel -= 0.03;
 // else xVel += 0.03;
 // }
 //}
 //for joystick
 if (!gameOver) {
 if (Xpos<500) xVel -= 0.05;
 else if (Xpos>503) xVel += 0.05;
 else
 {
 if (xVel > 0) xVel -= 0.03;
 else xVel += 0.03;
 }
 }
 if (abs(xVel) < 0.01) xVel = 0;
 xVel = min(maxXVel, xVel);
 xVel = max(-maxXVel, xVel);

// vertical
 yVel += gravity;
 yVel = min(maxYVel, yVel);
 yVel = max(-maxYVel, yVel);
 }

void collide(Platform plat) {


 if (x < plat.x + plat.w &&
 x + w > plat.x &&
 y < plat.y + plat.h &&
 y + h > plat.y)
 {
 if (plat.danger == false) {
 if (yVel > 0) {
 yVel = -bounceVel;
 }
 } else {
 if (plat.danger == true) {
 gameOver = true;
 gameRunning = false;
 Sel = 1;
 }
 // game over
 }
 }
 }
}

 

Here is how it looks 🙂