Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Fix Capture Bug and Game Over Screen. (Runnable Code!)

$
0
0

``Hello everyone!

So I have been making a game similar to the traditional snake game. The difference being that it is on individual shape catching the food rather than it growing. However, after awhile the icon wont pickup the food anymore and I cannot figure out why. I also cant figure out how to create a game over box when the icon hits the wall. Any help would be greatly appreciated! I adjusted the code to not use the images so it could be run by anyone willing to help. Thank you in advance!

_________Main Body____________

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo B;
int grid = 35;
int Time;
//PImage BH;
//PImage SC;
//PImage Rink;
PFont Silom;

PVector Cup;


void setup() {

  size(600, 631);
  B = new Logo();
  frameRate(8);
  pickLocation();
  //BH = loadImage("BH.png");
  //SC = loadImage("SC.png");
  //Rink = loadImage("Rink.jpg");
  Silom = loadFont("Silom.vlw");


  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(155);


  if (B.capture(Cup)) {
    pickLocation();
  }
  B.death();
  B.update();
  B.show();

  //image(SC, Cup.x, Cup.y, grid, grid);
  rect(Cup.x, Cup.y, grid, grid);
  fill (0);
  textFont(Silom, 20);
  text("Press the arrow keys", 10, 30);
  text("to move around", 40, 50);
}

void keyPressed() {

  if (keyCode == UP) {
    B.dir(0, -1);
  } else if (keyCode == DOWN) {
    B.dir(0, 1);
  } else if (keyCode == RIGHT) {
    B.dir(1, 0);
  } else if (keyCode == LEFT) {
    B.dir(-1, 0);
  }
  else if (keyCode == ' ');

}

__________Logo_____________

    class Logo {
      float x = 0;
      float y = 0;
      float xspeed = 1;
      float yspeed = 0;
      int total = 0;
      boolean endgame=false;
      ArrayList<PVector> hawk = new ArrayList<PVector>();

      Logo() {
      }

      boolean capture(PVector pos) {
        float d = dist(x, y, pos.x, pos.y);
        if (d < 1) {
          return true;
        } else {
          return false;
        }
      }

      void dir(float x, float y) {
        xspeed = x;
        yspeed = y;
      }

      void death() {
        for (int i = 0; i < hawk.size(); i++) {
          PVector pos = hawk.get(i);
          float d = dist(x, y, pos.x, pos.y);
          if (d < 1) {
            text("starting over",100,100);
            total = 0;
            hawk.clear();
          }
        }
      }

      void update() {

        x = x + xspeed*grid;
        y = y + yspeed*grid;

        x = constrain(x, 0, width-grid);
        y = constrain(y, 0, height-grid);
      }

      void show() {
        //image(BH, x, y, grid, grid);
        rect(x, y, grid, grid);
        fill(255, 150, 20);
      }
    }

Viewing all articles
Browse latest Browse all 2896

Trending Articles