Wannabe Mateo

I made a snake game.

You push the buttons to move the snake and try not to die.

 

Also there’s a cool space background just to make things more interesting.

Snake_Space

 

import processing.serial.*;
Serial myPort;

ArrayList<Sphere> spheres;

int xPos = 400;
int yPos = 300;
int Xspeed = 1;
int Yspeed;
int diam = 10;
float []  coordinatesX = {};
float [] coordinatesY = {};
int game = 0;

void setup(){
  size(800,600);
  background(30);
  fill(255);
  String portName = "/dev/tty.usbserial-DA01LJI0";
  myPort = new Serial(this,portName, 9600);
  myPort.bufferUntil('\n');

spheres = new ArrayList<Sphere>();

}
void draw(){
  
  background(30);
  spheres.add(new Sphere());
   for (int i = spheres.size()-1; i>=0; i--) { // manipulate the objects
    Sphere s = spheres.get(i);
    s.update();
    s.render();
    //s.bounds();
    if (s.isDead()) {
      spheres.remove(i);
    }
  }
  
 game = 1;
  if(game == 1){ 
    fill(255); 
    stroke(255);
    xPos = constrain(xPos,0,800);
    yPos = constrain(yPos,0,600);
    xPos += Xspeed;
    yPos += Yspeed;
  

    for (int i = 0; i < coordinatesX.length; i++){
      
      rect(coordinatesX[i],coordinatesY[i],diam,diam);
      
      if ((xPos == coordinatesX[i] && yPos == coordinatesY[i]) || xPos == 0 || xPos == 800 || yPos == 0 || yPos == 600 ){
        game = 2;
  }
  }
  rect(xPos,yPos,diam,diam);
  //'UP' command
 
  coordinatesX = append(coordinatesX,int(xPos));
  coordinatesY = append(coordinatesY,int(yPos));
 
  if(game == 2){
    background(30);
    textSize(20);
    text("Bruh you dead.",200,height/2);
    yPos = 0;
    xPos = 0;
    fill(30);
  }
}
}
void serialEvent(Serial myPort) {
  String input = myPort.readString();
  String[] numbers = split(input, ',');
  float[] values = float(numbers);
  
//'UP' command
   if(values[2] == 1){
    Yspeed= -3;
    Xspeed=0;
  }
//'DOWN' command
    if(values[1] == 1){
      Yspeed = 3;
      Xspeed = 0;
    }
//'LEFT' command
    if(values[3] == 1){
      Yspeed = 0;
      Xspeed = -3;    
    }
    if(values[0] == 1){
      Yspeed = 0;
      Xspeed = 3; 
    }
    myPort.write('x');
  }


class Sphere {
  // variables for the object
  float xPos;
  float yPos;
  float xSpeed;
  float ySpeed;
  float diam;
  float alpha;

  Sphere() {
    alpha = random(250,  255);
    xPos = 400;
    yPos = 300;
    xSpeed = random(-7.,7.);
    ySpeed = random(-7.,7.);
    diam = 100;
    
  }

  boolean isDead() {
    if (alpha <= 0) {
      return true;
    } else {
      return false;
    }
  }
  // declare all my functions
  void render() {
    alpha -=3;
    diam--;
    fill(210, alpha);
    //for(int i = 0; i<100;i = i+5){
    ellipse(xPos, yPos, xSpeed, ySpeed );
   // ellipse(xPos-i,yPos+i,xSpeed,ySpeed);
   // ellipse(xPos+i,yPos-i,xSpeed,ySpeed);
   // ellipse(xPos-i,yPos-i,xSpeed,ySpeed);
   // }
   
   if (mousePressed == true){
    for(int i = 0; i<100;i = i+50){
      ellipse(xPos+i, yPos+i, xSpeed, ySpeed );
       ellipse(xPos-i,yPos+i,xSpeed,ySpeed);
      ellipse(xPos+i,yPos-i,xSpeed,ySpeed);
       ellipse(xPos-i,yPos-i,xSpeed,ySpeed);
    }
   }
     
  }

  void update() {
    xPos-=xSpeed;
    yPos-=ySpeed;  
    
      
    
      
      
    
  }

  void bounds() {
    if (xPos >= width || xPos <= 0) {
      xSpeed = xSpeed * -1;
    }
    if (yPos >= height || yPos <= 0) {
      ySpeed = ySpeed * -1;
    }
  }

}

This is the code for Arduino:

void setup() {
  // put your setup code here, to run once:
pinMode(2,INPUT); //GREEN
pinMode(3,INPUT); //YELLOW
pinMode(5,INPUT); //RED
pinMode(7,INPUT); //BLUE


Serial.begin(9600);
Serial.println("0,0,0,0");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
 int processing = Serial.read();

 Serial.print(digitalRead(2));
 Serial.print(",");
 delay(1);

 Serial.print(digitalRead(3));
 Serial.print(",");
 delay(1);

 Serial.print(digitalRead(5));
 Serial.print(",");
 delay(1);

 Serial.println(digitalRead(7));
 delay(1);

}
}

 

 

Leave a Reply