Arduino + Processing = Lit

For this week’s assignment, I added a physical controller in the form of a joystick to my Snake game. The joystick had 3 outputs (analog x-coordinate, analog y-coordinate, and digital click) so it was a bit difficult to get everything right in the beginning.

I ran into some more issues when trying to connect the controller output to the processing game code: the joystick values were just not being updated into the game. I tried printing out the values generated by the joystick and they were correct. Honestly had no clue why the snake just wouldn’t turn. Oh stubborn snake. Upon closer inspection, there seemed to be a 2-ish second delay between the commencement of the game and the updating of the value generated by the joystick into the game. I added a 2 second delay to the beginning of the game and everything seems to work now. Mashallah

Here is a video demonstration of my game:

Here is my code:

import processing.serial.*;

Serial myPort;
//int[] dataIn = new int[2]; // a list to hold data from the serial ports

ArrayList<Integer> positionsX = new ArrayList<Integer>();
ArrayList<Integer> positionsY = new ArrayList<Integer>();

int dimSquare = 40;
int xPos = 0;
int yPos = 0;
int poo = 0;
int internalInput = -1;
int dim = 800;
int positionX = 5;
int positionY = 5;
int foodX = 15;
int foodY = 15;
int [] dirX = {1,-1,0,0};
int [] dirY = {0,0,1,-1};
int direction = 0;
int speedOfGame = 10;
boolean gameover = false;

class gameBoard{
gameBoard(){}

void drawLines(){
noFill();
stroke(0);
strokeWeight(1);
for(int i = 0; i < dim; i++){
line(dimSquare*i, 0, dimSquare*i, dim);
line(0, dimSquare*i, dim, dimSquare*i);
}
}
}

class snake{
snake(){
positionX = positionX*dimSquare;
positionY = positionY*dimSquare;
}

void drawSnake(){
fill(0,255,0);
noStroke();
for(int i = 0; i < positionsX.size() ; i++){
rect(positionsX.get(i)*dimSquare, positionsY.get(i)*dimSquare, dimSquare, dimSquare);
}
if(frameCount % speedOfGame == 0){
positionsX.add(0, positionsX.get(0) + dirX[direction]);
positionsY.add(0, positionsY.get(0) + dirY[direction]);
//positionsX.add(0, dirX[direction]);
//positionsY.add(0, dirY[direction]);
positionsX.remove(positionsX.size() – 1);
positionsY.remove(positionsY.size() – 1);
}
}

void growSnake(){
positionsX.add(1, positionsX.get(0) + dirX[direction]);
positionsY.add(1, positionsY.get(0) + dirY[direction]);
}
}

class food{
food(){
}

void drawFood(){
fill(255, 0, 0);
rect(foodX*dimSquare, foodY*dimSquare, dimSquare, dimSquare);
}
}

gameBoard a;
snake b;
food c;

void setup(){
size(802, 802);
printArray(Serial.list());
String portname=Serial.list()[8];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil(‘\n’);
a = new gameBoard();
b = new snake();
c = new food();
positionsX.add(5);
positionsY.add(5);
delay(2000);
}

void draw(){

if(gameover == false){
background(255);
//delay(30);
//a.drawLines();
b.drawSnake();
c.drawFood();
if(positionsX.get(0) == foodX && positionsY.get(0) == foodY){
foodX = (int)random(0,20);
foodY = (int)random(0,20);
b.growSnake();
}
if(positionsX.get(0) < 0 || positionsY.get(0) < 0 || positionsX.get(0) >= 20 || positionsY.get(0) >= 20){
gameover = true;
fill(0);
textSize(30);
textAlign(CENTER);
text(“GAME OVER.”,width/2,height/2);
}
if(positionsX.size() == 20){
gameover = true;
fill(0);
textSize(30);
textAlign(CENTER);
text(“YOU WINNNNN.”,width/2,height/2);
}
}

if(xPos == 1023){
internalInput = 0;
}
if(xPos == 0){
internalInput = 1;
}
if(yPos == 1023){
internalInput = 2;
}
if(yPos == 0){
internalInput = 3;
}
if(internalInput != -1){
direction = internalInput;
}

//println(xPos);
//println(yPos);

}

void serialEvent(Serial myPort){
String temp=myPort.readStringUntil(‘\n’);
temp=trim(s);
if (temp != null){
int values[]=int(split(temp,’,’));
if (values.length==2){
xPos=(int)(values[0]);
yPos=(int)(values[1]);
}
}
println(xPos);
}

Leave a Reply

Your email address will not be published. Required fields are marked *