Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float cannonX, cannonY;
- float cannonLength = 50; // How long the cannon on the tank extends
- float cannonAngle = PI*3/4;
- float cannonAngleStep = 0.005; // step to change cannonAngle
- float gravity = 0.5; // Strength of gravity
- float time; // calculate time for the gravity component
- float deltat = 0.5;
- float wind; // Strength of wind
- float explodePositionX;
- float explodePositionY;
- boolean explode = false;
- float explosionStrength = 1;
- boolean objectInMotion = false;
- float targetDistance = 70; // distance from left boundary the target is positioned
- boolean targetHit = false;
- int score = 0;
- int total = 0;
- float[] groundLevel; // Y coordinate of the ground for each X coordinate
- float x = 100,y; // position of the projectile
- float x0, y0; // start position of the projectile
- float v0x, v0y; // start speeds of the projectile
- float vx, vy; // current speeds of the projectile
- float r = 5; // radius of the projectile
- float speed = 25.0; // speed of projectile
- void setup (){
- size (1000,600);
- cannonX = width-20;
- cannonY = height*2/3 + random(-100, 0); // set position of the cannon at 2/3 height measured from the upper boundary of the viewport
- wind = random(0.5, 2);
- // Initialize the ground level
- // Implement random surface of the ground
- groundLevel = new float[width];
- for(int i = 0; i < width; i++) {
- groundLevel[i] = height/3 + random(-10, 10);
- }
- }
- void draw(){
- background(200);
- time += deltat;
- drawGround();
- drawTarget();
- drawCannon();
- aimCannon();
- shootCannon();
- drawProjectile();
- checkTarget();
- drawScore();
- }
- void drawGround(){
- // See the groundLevel[] variable to know where to draw the ground
- stroke(#666A68); // set dark gray
- for(int x = 0; x < width; x++) {
- line (x, height, x, height-groundLevel[x]); // draw in loop a vertikal line at lokation x
- }
- }
- void drawTarget(){
- ellipseMode(RADIUS);
- for (int i = 10; i > 0; i--){
- if (i<4) {
- fill(0);
- stroke(255);
- }
- else{
- fill(255);
- stroke(0);
- }
- ellipse(targetDistance, height/2, 10*i, height/22*i);
- }
- stroke(2555,0,0);
- line(targetDistance,height/22, targetDistance,height-height/22);
- }
- void drawCannon(){
- stroke(85,107,47); // set darkolivegreen color to draw the cannon
- fill(85,107,47);
- smooth();
- strokeWeight(8);
- // invert the sign of the angle in order to point the cannon upwards (and not downwards)
- line(cannonX, cannonY, cannonX+cannonLength*cos(-cannonAngle), cannonY+cannonLength*sin(-cannonAngle));
- strokeWeight(1); // reset to default widht
- arc(cannonX, cannonY, cannonLength*3/4, cannonLength/2, -PI-PI/6, PI/6);
- }
- void drawProjectile(){
- if (objectInMotion) {
- if(targetHit){
- fill(#FCD545);
- } else {
- fill(#ff0000);
- }
- ellipse(x, y, 2*r, 2*r);
- }
- }
- void shootCannon(){
- if ((mousePressed || (keyPressed && key == ' ')) && objectInMotion == false){
- r = 5;
- targetHit = false;
- objectInMotion = true;
- time = 0;
- x0 = cannonX+cannonLength*cos(-cannonAngle);
- y0 = cannonY+cannonLength*sin(-cannonAngle);
- v0x = speed * cos(cannonAngle);
- v0y = speed * sin(cannonAngle);
- }
- if (objectInMotion){
- x = x0 + v0x * time;
- y = y0 - v0y * time + 0.5 * gravity * time * time - wind * time;
- }
- }
- void aimCannon() {
- if (objectInMotion == false){
- if (keyPressed) {
- if (keyCode == RIGHT){
- cannonAngle += cannonAngleStep;
- } else if (keyCode == LEFT){
- cannonAngle -= cannonAngleStep;
- } else if (keyCode == UP){
- cannonY -= 3;
- } else if (keyCode == DOWN){
- cannonY += 3;
- }
- }
- }
- }
- void checkTarget(){
- if (x <= -10){
- objectInMotion = false;
- }
- if (x <= targetDistance && !targetHit
- && y < height/2 + height/22*10 && y > height/2 - height/22*10){
- targetHit = true;
- r += 50;
- for (int i = 1; i <= 10; i++){
- if (y < height/2 + height/22*i && y > height/2 - height/22*i){
- score = 11 - i;
- total += score;
- break;
- }
- score = 0;
- }
- }
- }
- void drawScore(){
- textSize(30);
- fill(255);
- rect(280, 15, 180, 50);
- fill(#0000FF);
- text("Score: " + score, 300, 50);
- fill(255);
- rect(480, 15, 180, 50);
- fill(#FF00FF);
- text("Total: " + total, 500, 50);
- fill(255);
- rect(680, 15, 250, 50);
- fill(#4F9BC9);
- text("Wind: " + nf(wind, 1, 3) + " SE", 700, 50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement