Alien snake game 2.0 (Joystick)

For this week, I thought it would be really cool to make my snake game more advanced by using a joystick to control the movement using both Processing and Arduino. For this I used 4 male and 4 female cables and a joystick. I faced different challenges such as the arduino not transferring to processing or only two sides of the joystick working and it was more challenging than I initially thought, but it I got it working in the end!

Arduino:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("0,0");
}

void loop() {
  if(Serial.available()>0){
    char inByte=Serial.read();
    int sensor = analogRead(A0);
    delay(0);
    int sensor2 = analogRead(A1);
    delay(0);
    Serial.print(sensor);
    Serial.print(',');
    Serial.println(sensor2);
  }
}

Processing:

int grid = 20; //How big each grid square will be
import processing.serial.*;
PVector food;
int speed = 10;
boolean dead = true;
int highscore = 0;
Snake snake;
Serial myPort;
int xPos=0;
int yPos=0;

void setup() {
  size(500, 500);
  snake = new Snake();
  food = new PVector();
  newFood();
    String portname=Serial.list()[2];
  println(portname);
  myPort = new Serial(this,portname,9600);
  //myPort.clear();
  //myPort.bufferUntil('\n');
}

void draw() {
  background(0,0,50);
  fill(200,200,200);
  if (!dead) {
    
    if (frameCount % speed == 0) {
      snake.update(xPos,yPos);
    }
    snake.show();
    snake.eat();
    fill(200,50,100);
    rect(food.x, food.y, grid, grid);
    textAlign(LEFT);
    textSize(15);
    fill(255);
    text("Score: " + snake.len, 10, 20);
  } else {
    textSize(25);
    textAlign(CENTER, CENTER);
    text("Alien snake Game\nAre you up for the challenge?\nClick to start" + "\nHighscore: " + highscore, width/2, height/2);
  }
}

void newFood() {
  food.x = floor(random(width));
  food.y = floor(random(height));
  fill( random(255), random(255), random(255), random(255));
  food.x = floor(food.x/grid) * grid;
  food.y = floor(food.y/grid) * grid;
}

void mousePressed() {
  if (dead) {
    snake = new Snake();
    newFood();
    speed = 10;
    dead = false;
  }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    int values[]=int(split(s,','));
    if (values.length==2){
      xPos=(int)map(values[0],0,1023,0, width);
      yPos=(int)map(values[1],0,1023,0, height);
    }
  }
  println(xPos+ " "+yPos);
  myPort.write('0');
}

class Snake {
  PVector pos;
  PVector vel;
  ArrayList<PVector> hist;
  int len;
  int moveX = 0;
  int moveY = 0;

  Snake() {
    pos = new PVector(0, 0);
    vel = new PVector();
    hist = new ArrayList<PVector>();
    len = 0;
  }

  void update(int x, int y) {

    if (x>300 && y<300 && y>200 && snake.moveX != 2) {
      snake.vel.x = -1;
      snake.vel.y = 0;
    } else if (x<200 && y<300 && y>200 && snake.moveX != -1) {
      snake.vel.x = 1;
      snake.vel.y = 0;
    } else if (x<300 && x>200 && y<200 && snake.moveY != 2) {
      snake.vel.y = -1;
      snake.vel.x = 0;
    } else if (x<300 && x>200 && y>300 && snake.moveY != -1) {
      snake.vel.y = 1;
      snake.vel.x = 0;
    }

    hist.add(pos.copy());
    pos.x += vel.x*grid;
    pos.y += vel.y*grid;
    moveX = int(vel.x);
    moveY = int(vel.y);

    pos.x = (pos.x + width) % width;
    pos.y = (pos.y + height) % height;

    if (hist.size() > len) {
      hist.remove(0);
    }

    for (PVector p : hist) {
      if (p.x == pos.x && p.y == pos.y) {
        dead = true;
        if (len > highscore) highscore = len;
      }
    }
  }

  void eat() {
    if (pos.x == food.x && pos.y == food.y) {
      len++;
      if (speed > 5) speed--;
      newFood();
    }
  }

  void show() {
    noStroke();
    fill( random(255), random(255), random(255), random(255));
    ;
    rect(pos.x, pos.y, grid, grid);
    for (PVector p : hist) {
      rect(p.x, p.y, grid, grid);
    }
  }
}
void keyPressed() {
  if (keyCode == LEFT && snake.moveX != 1) {
    snake.vel.x = -1;
    snake.vel.y = 0;
  } else if (keyCode == RIGHT && snake.moveX != -1) {
    snake.vel.x = 1;
    snake.vel.y = 0;
  } else if (keyCode == UP && snake.moveY != 1) {
    snake.vel.y = -1;
    snake.vel.x = 0;
  } else if (keyCode == DOWN && snake.moveY != -1) {
    snake.vel.y = 1;
    snake.vel.x = 0;
  }
}

 

One thought on “Alien snake game 2.0 (Joystick)”

Leave a Reply