**Midterm – GAME: Survive in the Sea of Jellyfish **

__Date__: October 31st, 2017

__Documented on__: October 31st, 2017

__Documented by__: Qianchun Yuan (Kaye)

__Instructor__: Moon Junghyun

For the midterm project, I made a small game using only Processing. In the game, a person (controlled by **the position of the mouse**) is surrounded by a lot of poisonous jellyfish. The player needs to help the person get on the boat to get out of the hellish condition, without touching any jellyfish. Following is how the main page for the game looks like.

Main page for the game

This is how the whole game looks like:

Continue reading →

```
// I separate the code into 6 labels
// I am copying and pasting them here respectively
//label 1 -- (main body)
int mode=0;
float r=random(255);
float g=random(255);
float B=random(255);
float A=random(50, 100);
int set_num=10;
float set_numY=385;
float set_speed=0.5;
float set_speedY=425;
float set_visib=200;
float set_visibY=465;
Ball [] balls = new Ball [100]; //b is the object
float speedRange;
float visibility;
void setup() {
size(600, 700);
background(255);
}
void draw() {
switch(mode) {
case 0:
opening();
if (dist(mouseX, mouseY, width/2, 657)<25) {
setupGame();
mode=1;
}
break;
case 1: //play
playGame();
float d_ship=dist(mouseX, mouseY, 300, 50);
if (d_ship<35) {
println("you are saved");
mode=3; //win
}
break;
case 2: //touch jellyfish //lose the game
lose();
if (mousePressed) {
mode=0;
}
if (keyPressed) {
mode=1;
}
break;
case 3: // get on the ship //win the game
win();
if (mousePressed) {
mode=0;
}
break;
}
}
// label 2 -- Ball
//object
class Ball {
//fields (variables )
float x;
float y;
float size;
float xSpeed;
float ySpeed;
color c;
//construtor
Ball(float _x, float _y, float _size) {
x=_x;
y= _y;
size=_size;
xSpeed=random(-speedRange, speedRange);
ySpeed=random(-speedRange, speedRange);
c=color(random(255), random(255), random(255), random(20,visibility));
}
//methods (functions)
void display() {
float a=x;
float b=y;
float s=size;
fill(c);
//draw the jellyfish body
noStroke();
ellipse(a, b, s, s);
arc(a, b-s*0.02, s*0.75, s*0.75, PI*0.95, PI*2.05);
//draw the jellyfish tail
noFill();
strokeWeight(s*0.015);
stroke(c);
float x1=a-s*0.5;
float y1=b+s*0.1;
float x2=a;
float y2=b;
float x3=a;
float y3=y2+s*0.3;
float x4=a+s*0.5;
float y4=b+s*0.2;
float t1=s*0.25;
float t2=s*0.1;
curve(x1-t1, y1, x2-t1, y2, x3-t1, y3, x4-t1, y4);
curve(x1-t2, y1, x2-t2, y2, x3-t2, y3, x4-t2, y4);
curve(x1+t2, y1, x2+t2, y2, x3+t2, y3, x4+t2, y4);
curve(x1+t1, y1, x2+t1, y2, x3+t1, y3, x4+t1, y4);
fill(0);
}
void move() {
x=x+xSpeed;
y=y+ySpeed;
}
void bounce() {
if (x<0 || x>width) {
xSpeed=-xSpeed;
}
if (y<100 || y>height-100) {
ySpeed=-ySpeed;
}
}
void distance() {
float d=dist(mouseX, mouseY, x, y);
if (d-20<size/2) {
mode=2;
}
}
}
//label 3 -- lose
void lose() {
//fill(255);
textSize(40);
strokeWeight(5);
fill(255, 0, 0);
text("You Are Poisoned to Death", 20, height/2-50 );
//restart
fill(255);
textSize(20);
strokeWeight(2);
text("Press your mouse to restart", 300, 670);
text("Press your key to continue", 300, 688);
}
//label 4 -- opening page
void opening() {
background(255);
//jellyfish pattern
float a=width/2;
float b=height/2-130;
float s=200;
//draw the jellyfish body
noStroke();
fill(r, g, B, A);
ellipse(a, b, s, s);
arc(a, b-s*0.02, s*0.75, s*0.75, PI*0.95, PI*2.05);
//draw the jellyfish tail
noFill();
strokeWeight(s*0.015);
stroke(r, g, B, A);
float x1=a-s*0.5;
float y1=b+s*0.1;
float x2=a;
float y2=b;
float x3=a;
float y3=y2+s*0.3;
float x4=a+s*0.5;
float y4=b+s*0.2;
float t1=s*0.25;
float t2=s*0.1;
curve(x1-t1, y1, x2-t1, y2, x3-t1, y3, x4-t1, y4);
curve(x1-t2, y1, x2-t2, y2, x3-t2, y3, x4-t2, y4);
curve(x1+t2, y1, x2+t2, y2, x3+t2, y3, x4+t2, y4);
curve(x1+t1, y1, x2+t1, y2, x3+t1, y3, x4+t1, y4);
fill(0);
//the ship
noStroke();
quad(270, 50, 330, 50, 315, 70, 285, 70);
strokeWeight(3);
stroke(0);
line(300, 50, 300, 20);
triangle(300, 20, 315, 30, 300, 40);
//introduction to the game
textSize(20);
text("You are in the sea of poisonous jellyfish.", 100, 200);
text("The only way out is to get on the ship", 100, 250);
text("without touching any jellyfish.", 150, 280);
//set
textSize(25);
text("Choose difficulty level:", 80, 350);
textSize(20);
text("number of jellyfish:", 100, 390);
line(300, 385, 500, 385);
textSize(15);
text("10", 295, 380);
text("100", 495, 380);
if (mouseY>380 && mouseY<390) {
if (mouseX>300 && mouseX<500) {
if (mousePressed) {
set_num=int(map(mouseX, 300, 500, 10, 100));
}
}
}
ellipse(map(set_num, 10, 100, 300, 500), set_numY, 6, 6);
textSize(20);
text("speed of jellyfish:", 100, 430);
line(300, 425, 500, 425);
textSize(15);
text("0.5", 295, 420);
text("5", 495, 420);
if (mouseY>420 && mouseY<430) {
if (mouseX>300 && mouseX<500) {
if (mousePressed) {
set_speed=map(mouseX, 300, 500, 0.5, 5);
}
}
}
ellipse(map(set_speed, 0.5, 5, 300, 500), set_speedY, 6, 6);
textSize(20);
text("visibility of jellyfish:", 100, 470);
line(300, 465, 500, 465);
textSize(15);
text("20", 295, 460);
text("200", 495, 460);
if (mouseY>460 && mouseY<470) {
if (mouseX>300 && mouseX<500) {
if (mousePressed) {
set_visib=map(mouseX, 300, 500, 20, 200);
}
}
}
ellipse(map(set_visib, 20, 200, 300, 500), set_visibY, 6, 6);
//start
fill(0);
ellipse(width/2, 657, 50, 50);
text("put your mouse here and start", 200, 695);
}
//label 5 -- playGame
void setupGame() {
speedRange=set_speed;
visibility=set_visib;
for (int i=0; i<balls.length; i++) {
balls [i] = new Ball(random(width), random(100, 600), random(30, 80));
}
}
void playGame() {
background(0);
textSize(12);
strokeWeight(2);
fill(255);
text("number of jellyfish:", 10, 20);
text(set_num, 130, 20);
text("speed of jellyfish:", 10, 40);
text(speedRange, 120, 40);
text("visibility of jellyfish:", 10, 60);
text(visibility, 130, 60);
for (int i=0; i<set_num; i++) {
//update value
balls[i].move();
//check
balls[i].bounce(); //check the position of the ball
//display
balls[i].display();
fill(255);
//the ship
noStroke();
quad(270, 50, 330, 50, 315, 70, 285, 70);
strokeWeight(3);
stroke(255);
line(300, 50, 300, 20);
triangle(300, 20, 315, 30, 300, 40);
//player
ellipse(mouseX, mouseY, 40, 40);
stroke(0);
strokeWeight(2);
ellipse(mouseX, mouseY-10, 10, 10);
line(mouseX, mouseY-5, mouseX, mouseY+10);//body
line(mouseX, mouseY, mouseX-5, mouseY+5);//left hand
line(mouseX, mouseY, mouseX+5, mouseY+5);//right hand
line(mouseX, mouseY+10, mouseX-5, mouseY+15);//left leg
line(mouseX, mouseY+10, mouseX+5, mouseY+15);//right leg
//distance to jellyfish //switch to mode=2
balls[i].distance();
}
}
//label 6 -- win
void win() {
background(255);
float a=width/2;
float b=height/2-130;
float s=200;
//draw the jellyfish body
noStroke();
fill(r, g, B, A);
ellipse(a, b, s, s);
arc(a, b-s*0.02, s*0.75, s*0.75, PI*0.95, PI*2.05);
//draw the jellyfish tail
noFill();
strokeWeight(s*0.015);
stroke(r, g, B, A);
float x1=a-s*0.5;
float y1=b+s*0.1;
float x2=a;
float y2=b;
float x3=a;
float y3=y2+s*0.3;
float x4=a+s*0.5;
float y4=b+s*0.2;
float t1=s*0.25;
float t2=s*0.1;
curve(x1-t1, y1, x2-t1, y2, x3-t1, y3, x4-t1, y4);
curve(x1-t2, y1, x2-t2, y2, x3-t2, y3, x4-t2, y4);
curve(x1+t2, y1, x2+t2, y2, x3+t2, y3, x4+t2, y4);
curve(x1+t1, y1, x2+t1, y2, x3+t1, y3, x4+t1, y4);
textSize(32);
strokeWeight(4);
fill(255,36,54);
text("You are saved!", 190, 190 );
text("Congratulations!", 175, 280 );
fill(0);
//the ship
noStroke();
quad(width/2-150, height/2+200, width/2+150, height/2+200, width/2+90, height/2+290, width/2-90, height/2+290);
strokeWeight(8);
stroke(0);
line(width/2, height/2+200, width/2, height/2);
triangle(width/2, height/2, width/2+100, height/2+60, width/2, height/2+120);
//player
stroke(0);
strokeWeight(4);
ellipse(230, 460, 30, 30);
line(230, 445, 230, 520);//body
line(230, 490, 210, 510);//left hand
line(230, 490, 250, 510);//right hand
line(230, 520, 210, 550);//left leg
line(230, 520, 250, 550);//right leg
//restart
textSize(20);
text("Press your mouse to restart", 300, 680);
}
```