Sometimes my character stops collecting the "food"

edited December 2016 in Library Questions

Hello everyone!

I'm making a snake game where an individual icon collecting "food" but sometimes the icon wont pick up the food and sometimes it will and then will stop at some random point. Is there an obvious part of my code causing this that im missing? Also, I cannot for the life of me figure out how to get the game to have a game over and restart setting when the icon hits the wall. Also, how can I call the "Welcome" screen into the main code to pop up before the game? I'm sorry for all these questions. I've been working on this for 10+ hours and I'm not seeing everything clearly anymore.

_________________________Main Body__________________________

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

Minim minim;
AudioPlayer player;

Logo B;
Welcome W;
int grid = 35;
int Time;
PImage BH;
PImage SC;
PImage Rink;


PVector Cup;


void setup() {

  size(800, 422);
  B = new Logo();
  W= new Welcome();
  frameRate(8);
  pickLocation();
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Rink = loadImage("Rink.jpg");

  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(Rink);


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


  image(SC, Cup.x, Cup.y, grid, grid);
}

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);
  }
}

_________________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);
  }
}

____________________Welcome______________________

class Welcome {
  PFont Silom;
  boolean buttonpressed;
  int buttonA, buttonB, buttonC, buttonD;

  Welcome()
  {
    Silom = loadFont("Silom.vlw");

    buttonpressed = false;
    buttonC = 215;
    buttonD = 100;
    buttonA = (width-510);
    buttonB = (height-410);


    textFont(Silom, 70);
    if (buttonpressed) {
      println("Begin");
    } else {
      fill(255);
      rect(buttonA, buttonB, buttonC, buttonD);
      fill (0);
      text("START", buttonA+5, buttonB+buttonD-25);
    }
    {     

      if (buttonpressed) {
        fill(255, 255, 255, 0);
      } else {
        textFont(Silom, 20);
        text("Press the arrow keys to move around", 210, 235);
        textFont(Silom, 60);
        text("Catch the cup!", 180, 200);
      }
    }
  }

  void mousePressed() {
    if (mouseX>buttonA && mouseX<buttonA+buttonC && mouseY>buttonB && mouseY<buttonB+buttonC)
      buttonpressed = true;
  }
}
Tagged:

Answers

  • Try searching for some tutorial on making a snake game in Processing and testing it, then just modify it to meet your needs.

Sign In or Register to comment.