//****************************//
// The Submarine
// By: Kehao Fan //
//****************************//
int x= 300;
int y= 300;
float time;
int charX = 300;
int charY = 300;
int rockX;
int rockY;
float Horizontalspeed = 5;
float fastspeed = 4;
float slowspeed =2;
float risespeed =2;
float sinkspeed =2;
boolean moveUp, moveDown, moveLeft, moveRight, sink;
int brush;
void setup() {
size (600, 600);
frameRate(40);
smooth();
ellipseMode(CENTER);
rectMode(CENTER);
noCursor();
sink = true;
time = millis();
}
void draw() {
TheSea();
TheConditonOfMovement();
Submarine();
Timer();
}
///////////////////////////////The Code packages///////////////////////////////
/////////////////The Background
void TheSea() {
background(1);
//The colour on the background that flashing randomly
for (int a = 0; a < 600; a++)
{
float r = random(50);
noStroke();
fill(125-a/2, (170-a/2)+(r/8), (245-a/2)+r);
rect(width/2, a, width, 2);
}
rectMode(CORNER);
fill(30, 80, 150, 120);
rect(0, 80, 600, 520);
}
/////////////////The Timer
void Timer() {
time = millis();
textSize(32);
fill(255);
text((time)/1000, 20, 580);
}
/////////////////The Moving Object
void Submarine() {
rectMode(CENTER);
fill(210, 200, 150);
rect(charX-2, charY-22, 5, 5);
fill(190, 180, 120);
rect(charX, charY-15, 5, 20);
fill(230, 140, 150);
ellipse(charX, charY, 70, 30);
fill(100, 100, 100);
ellipse(charX, charY, 67, 25);
fill(150, 150, 150);
ellipse(charX, charY, 55, 15);
fill(230, 230, 90);
ellipse(charX-20, charY, 15, 15);
ellipse(charX-2, charY, 15, 15);
ellipse(charX+16, charY, 15, 15);
fill(0, 0, 0);
ellipse(charX-20, charY, 10, 10);
ellipse(charX-2, charY, 10, 10);
ellipse(charX+16, charY, 10, 10);
}
/////////////////use the mouse to drow
//The shape of the rock
void rock() {
fill(0);
ellipse(rockX, rockY, 50, 50);
}
void mouseDragged()
{
rockX = mouseX;
rockY = mouseY;
rock();
}
/////////////////The movement
void TheConditonOfMovement() {
if (charY > 580) {
sinkspeed = 0;
fastspeed = 0;
}
if (charY <= 580) {
sinkspeed = 2;
fastspeed = 5;
}
if (charY < 80) {
risespeed = 0;
}
if (charY >= 80) {
risespeed = 2;
}
if (charX > width) {
charX = 0;
}
if (charX < 0) {
charX = 600;
}
if (sink) charY += sinkspeed;
if (moveLeft) charX -= Horizontalspeed;
if (moveRight) charX += Horizontalspeed;
if (moveUp) charY -= risespeed;
if (moveDown) charY += fastspeed;
}
void keyPressed() {
if (key == 'a') {
moveLeft = true;
} else if (key == 'd') {
moveRight = true;
} else if (key == 'w') {
moveUp = true;
sink = false;
} else if (key == 's') {
moveDown = true;
sink = false;
}
}
void keyReleased() {
if (key == 'a') {
moveLeft = false;
} else if (key == 'd') {
moveRight = false;
} else if (key == 'w') {
moveUp = false;
sink = true;
} else if (key == 's') {
moveDown = false;
sink = true;
}
}