/*////////////////////////////////////////////////////////////////// ******Silent Hunter****** By Kai Zhuang 2018.10.2 The game idea is from one of my favorite game Silent Hunter by Ubisoft. (I was planning to make more ships and a mechenic that the ship can attack player by deep water bomb , but im running out of time...) Use W,A,S,D to control the submarine Press J to lanuch missiles *//////////////////////////////////////////////////////////////////// //Set variables float subX; //Set variable for submarine's horizontal position float subY; //Set variable for submarine's vertical position float subspeed=1.5; //Set submarine's speed float missileX; //Set variable for missile's horizontal position float missileY; //Set variable for missile's vertical position float missilespeed=2.5;//Set missile's speed float missilelaunchX; //This is used to locate the position of the missile when it launched float shipX=-400; //Set variable for ship's horizontal position float shipspeed=random(3,8); //Each time it resets, the ship will has different speed //Variables for control system(move&fire) boolean aPressed; boolean dPressed; boolean wPressed; boolean sPressed; boolean jPressed; boolean fire = false; //Set the fire condition and initialize it to false int score=0; //Initialize score to 0 //Basic setup void setup() { size(400, 400); //Set the size of the screen to 400*400 rectMode(CORNERS); //Set the shape mode ellipseMode(CENTER); noStroke(); } //Call functions void draw() { drawbackground(); interFace(); drawship1(); drawsub(); movesub(); firemissile(); hitcondition(); } //Draw the interface(tutorial & score board) void interFace() { textSize(14); //Tutorial fill(0); text("WASD--Move", 5, 15); text("J--Launch Missile", 10, 30); fill(0); //Score board textSize(14); text("Score : ", 310, 20); text(score, 360, 20); } //Draw the background(sun,sky,sea,seabed) void drawbackground() { //Sea fill(59,89,212); rect(0, 100, 400, 150); fill(57,86,204); rect(0, 150, 400, 200); fill(52,69,204); rect(0, 200, 400, 250); fill(47,50,203); rect(0, 250, 400, 300); fill(54,54,179); rect(0, 300, 400, 350); fill(65,66,152); rect(0, 350, 400, 400); //Sky fill(167,213,246); rect(0, 0, 400, 100); //Sun fill(254,224,80,170); ellipse(370,30, 30+10*sin(frameCount*.02), 30+10*sin(frameCount*.02)); //The sun will flash fill(254,234,80,130); ellipse(370,30, 35+10*sin(frameCount*.02), 35+10*sin(frameCount*.02)); fill(254,255,95,50); ellipse(370,30,25,25); //Seabed float seabedgrass=0; //Set the seabedgrass start position to 0 fill(0, 221, 62); while //Do loop to keep drawing grass by cos function till fill the width (seabedgrass<width) { rect(seabedgrass, 400, seabedgrass+4, 400-cos(seabedgrass/2)*20-15); seabedgrass+=5; } float seabedrock=0; //Set the seabedrocks start position to 0 fill(93, 65, 21); while //Do loop to keep drawing rocks by cos function till fill the width (seabedrock<width) { rect(seabedrock, 400, seabedrock+15, 400-cos(seabedrock/15)*3-12); seabedrock+=2; } } //Draw the submarine void drawsub() { stroke(0); strokeWeight(0.8); fill(198, 175, 151); rect(137+subX, 300+subY, 140+subX, 310+subY); rect(137+subX, 310+subY, 140+subX, 320+subY); noStroke(); fill(43, 51, 53); quad(140+subX, 306+subY, 140+subX, 312+subY, 160+subX, 320+subY, 160+subX, 300+subY); rect(160+subX, 300+subY, 230+subX, 320+subY); ellipse(230+subX, 310+subY, 20, 20); fill(64, 72, 74); quad(190+subX, 290+subY, 185+subX, 300+subY, 200+subX, 300+subY, 200+subX, 290+subY); quad(203+subX, 280+subY, 197+subX, 300+subY, 220+subX, 300+subY, 215+subX, 280+subY); //Limit the moveable area for submarine(player) if (subX<-137) { //When submarine reaches the border subX=-137; //It can't keep going } if (subX>160) { subX=160; } if (subY>80) { subY=80; } if (subY<-179) { subY=-179; } } //Draw the battleship void drawship1() { shipX+=shipspeed; //The ship moves by its speed //Ship bridge fill(109, 109, 117); rect(shipX+160, 80, shipX+200, 90); rect(shipX+170, 70, shipX+190, 80); //Glass fill(71,107,255); rect(shipX+180, 74, shipX+190, 78); //Cannon fill(0); rect(shipX+140, 84, shipX+160, 86); //Ship kell fill(93, 92, 86); quad(shipX+150, 90, shipX+160, 100, shipX+220, 100, shipX+240, 80); //Signal tower stroke(0); strokeWeight(2); line(shipX+180, 52, shipX+180, 70); strokeWeight(1); line(shipX+175, 55, shipX+185, 55); line(shipX+175, 63, shipX+185, 63); if (shipX>= width) { //Every time the ship reaches the border shipX=-160; //Initialize its position } } //The movement system of player(submarine) void movesub() { if (aPressed==true) { //When player pressed the key subX=subX-subspeed; //The submarine moves with its speed } if (wPressed==true) { subY=subY-subspeed; } if (sPressed==true) { subY=subY+subspeed; } if (dPressed==true) { subX=subX+subspeed; } } //Core mechanic#1: Missile launch void firemissile() { if (jPressed==true) { //When player presses the key if (!fire) { //First make it not fired and missileY = subY; //set the original position of missileX = subX; //the missile equals to submarine fire = true; //Then set it to fired } fill(0); //Then draw the missile ellipse(missileX+177, missileY+300, 5, 25); //And set the position of the missile's position to missileX = missilelaunchX; //the submarine's position when player pressed. missileY-=missilespeed; //And it goes with its speed if (missileY<=-200){ //Limit missile's moveable area jPressed = false; //When it reaches the top missileY=subY; //Reset its position missileX=subX; fire = false; } } } //Core mechanic#2: Hit box void hitcondition() { if (missileY<-190 && missileX>(shipX-10) && missileX<(shipX+60)) { textSize(25); //When the missile's X is between the ship's head and tail strokeWeight(7); fill(255,0,0); text("BOOM!", shipX+150,100); //Show text on ship "BOOM!" score+=1; //Score goes up by 1 shipX = -400; //Initialize ship's position jPressed = false; //Reset keyPress,fire condition,missile position fire = false; missileY=subY; } } //When player presses certain key, the corresponding function works void keyPressed() { if (key=='a'||key=='A') { aPressed=true; } if (key=='w'||key=='W') { wPressed=true; } if (key=='s'||key=='S') { sPressed=true; } if (key=='j'||key=='J') { jPressed=true; //When 'J' is pressed, submarine's current position missilelaunchX=subX; //will equal to the position when missile will launch(missilelaunchX) } if (key=='d'||key=='D') { dPressed=true; } } ////When player release certain key, the corresponding function stops void keyReleased() { if (key=='a'||key=='A') { aPressed=false; } if (key=='w'||key=='W') { wPressed=false; } if (key=='s'||key=='S') { sPressed=false; } if (key=='d'||key=='D') { dPressed=false; } }