//Snake Game in Processing 3.5.3 (www.processing.org)
import processing.sound.*;
import processing.serial.*;
Serial joyStick;
int joyStickCommand=RIGHT;
boolean joyStickEnabled=false;
int pixW = 50;
ArrayList<bodypart> snake;
float trem = 0;
int keycode = RIGHT;
boolean press = false;
int prevcode = 0;
int foodX;
int foodY;
float root = 2;
int menu = 0;
long sndtimer = 0;
int ease = 2000;
boolean start = false;
boolean pause = false;
TriOsc tri, tri2;
SawOsc saw;
boolean sound = true;
PFont font;
void foodCoordinates(int recursion)
{
foodX = int(random(width/pixW));
foodY = int(random(height/pixW));
for (bodypart part : snake)
{
if (part.m_x == foodX && part.m_y == foodY)
{
if (recursion != 0) foodCoordinates(recursion - 1);
break;
}
}
}
void init()
{
snake.clear();
snake.add(new bodypart(0, int(height/2/pixW)));
snake.add(new bodypart(1, int(height/2/pixW)));
snake.add(new bodypart(2, int(height/2/pixW)));
snake.add(new bodypart(3, int(height/2/pixW)));
}
void stopsnd()
{
tri.stop();
tri2.stop();
saw.stop();
}
void reinit()
{
snake = new ArrayList<bodypart>();
init();
foodCoordinates(1000);
root = 2;
}
void setup()
{
if (Serial.list().length > 1) joyStick = new Serial(this, Serial.list()[1], 38400);
font = createFont("Courier New", 50);
textFont(font);
fullScreen();
noCursor();
background(0);
textSize(300);
fill(0, 255, 0);
textAlign(CENTER, CENTER);
text("SNAKE", width/2, height/6.5);
textSize(50);
text("1. EASY\n2. MEDIUM\n3. HARD\n4. HARDCORE\nJ - TOGGLE EXTERNAL JOYSTICK\nS - TOGGLE SOUND\nE - EXIT", width/2, 2*height/3);
snake = new ArrayList<bodypart>();
init();
tri = new TriOsc(this);
tri2 = new TriOsc(this);
saw = new SawOsc(this);
tri.freq(440);
tri2.freq(880);
saw.freq(50);
foodX = int(random(width/pixW));
foodY = int(random(height/pixW));
sndtimer = 100;
pause = false;
stroke(0);
}
void draw()
{
if (start)
{
if (!joyStickEnabled)
{
if (keyCode > 36 && keyCode < 41)
{
if (abs(keyCode-prevcode) != 2) keycode = keyCode;
if (keyPressed)
{
if (!press)
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
press = true;
} else {
press = false;
}
}
} else {
if (abs(joyStickCommand-prevcode) != 2) keycode = joyStickCommand;
}
if (keyPressed && (key == 'p' || key == 'P'))
if (pause)
{
background(0);
textSize(300);
text("PAUSE", width/2, height/2);
}
if (!pause && trem > (trem = millis()%(ease/(root+2))))
{
bodypart test = new bodypart(0, 0);
prevcode = keycode;
switch(keycode)
{
case UP:
test = snake.get(snake.size()-1).up();
break;
case DOWN:
test = snake.get(snake.size()-1).down();
break;
case LEFT:
test = snake.get(snake.size()-1).left();
break;
case RIGHT:
test = snake.get(snake.size()-1).right();
break;
default:
break;
}
if (test.m_x < 0 || test.m_x >= width/pixW || test.m_y < 0 || test.m_y >= height/pixW)
{
menu = 1;
start = false;
test = new bodypart(0, 0);
if (sound) {
stopsnd();
saw.play();
sndtimer = millis() + 200;
}
} else
{
boolean crash = false;
for (bodypart part : snake)
{
if (part.m_x == test.m_x && part.m_y == test.m_y)
{
crash = true;
break;
}
}
if (!crash)
{
snake.add(test);
if (test.m_x == foodX && test.m_y == foodY)
{
foodCoordinates(1000);
root = sqrt(snake.size());
if (sound)
{
stopsnd();
tri2.play();
sndtimer = millis() + 100;
}
} else {
snake.remove(0);
}
} else {
menu = 1;
start = false;
if (sound) {
stopsnd();
saw.play();
sndtimer = millis() + 100;
}
}
}
}
background(0);
fill(255, 0, 0);
rect(foodX*pixW, foodY*pixW, pixW, pixW);
fill(0, 255, 0);
for (bodypart part : snake)
{
part.update();
}
} else
{
switch(menu)
{
case 0:
if (!keyPressed) press=false;
else
switch(key)
{
case '1':
ease = 2000;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
break;
case '2':
ease = 1000;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
break;
case '3':
ease = 600;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
break;
case '4':
ease = 300;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
break;
case 'J':
case 'j':
if (keyPressed)
{
if (!press)
{
joyStickEnabled = !joyStickEnabled;
stopsnd();
(joyStickEnabled ? tri2 : tri).play();
sndtimer = millis() + 100;
}
press = true;
} else {
press = false;
}
break;
case 'S':
case 's':
if (keyPressed)
{
if (!press)
{
sound = !sound;
stopsnd();
(sound ? tri2 : tri).play();
sndtimer = millis() + 100;
}
press = true;
}
break;
case 'E':
case 'e':
exit();
default:
break;
}
break;
case 1:
keycode = RIGHT;
background(0);
textSize(150);
fill(255, 0, 0);
text("GAME OVER", width/2, height/4);
textSize(35);
text("YOUR SCORE: " + (snake.size()-4) + "\n\n\n\n\n\n\n", width/2, height/1.5);
fill(0, 255, 0);
text("\n1. EASY\n2. MEDIUM\n3. HARD\n4. HARDCORE\nJ - TOGGLE EXTERNAL JOYSTICK\nS - TOGGLE SOUND\nE - EXIT", width/2, height/1.5);
if (!keyPressed) press=false;
else
switch(key)
{
case '1':
ease = 2000;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
reinit();
break;
case '2':
ease = 1000;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
reinit();
break;
case '3':
ease = 600;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
reinit();
break;
case '4':
ease = 300;
start = true;
if (sound) {
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
reinit();
break;
case 'J':
case 'j':
if (keyPressed)
{
if (!press)
{
joyStickEnabled = !joyStickEnabled;
stopsnd();
(joyStickEnabled ? tri2 : tri).play();
sndtimer = millis() + 100;
}
press = true;
} else {
press = false;
}
break;
case 'S':
case 's':
if (keyPressed)
{
if (!press)
{
sound = !sound;
stopsnd();
(sound ? tri2 : tri).play();
sndtimer = millis() + 100;
}
press = true;
}
break;
case 'E':
case 'e':
exit();
default:
break;
}
break;
default:
break;
}
}
if (sndtimer < millis()) stopsnd();
}
class bodypart
{
public int m_x, m_y;
bodypart(int x, int y)
{
m_x = x;
m_y = y;
}
void update()
{
rect(m_x*pixW, m_y*pixW, pixW, pixW);
}
bodypart up()
{
return new bodypart(m_x, m_y-1);
}
bodypart down()
{
return new bodypart(m_x, m_y+1);
}
bodypart left()
{
return new bodypart(m_x-1, m_y);
}
bodypart right()
{
return new bodypart(m_x+1, m_y);
}
}
float vx, vy, but;
boolean pausebuf = true;
void serialEvent (Serial joyStick) {
String inString = joyStick.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
int inputs[] = int(split(inString, ','));
if (inputs.length == 3) {
vy = inputs[0];
vx = inputs[1];
but = inputs[2];
if (start)
{
if (but == 0)
{
if (pausebuf) pause = !pause;
pausebuf = false;
} else {
pausebuf = true;
}
if (vy < 400)
{
if (joyStickCommand != DOWN && sound)
{
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
joyStickCommand = DOWN;
}
if (vy > 1024-400)
{
if (joyStickCommand != UP && sound)
{
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
joyStickCommand = UP;
}
if (vx < 400)
{
if (joyStickCommand != LEFT && sound)
{
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
joyStickCommand = LEFT;
}
if (vx > 1024-400)
{
if (joyStickCommand != RIGHT && sound)
{
stopsnd();
tri.play();
sndtimer = millis() + 100;
}
joyStickCommand = RIGHT;
}
}
}
}
}
Comments