We are about to switch to a new forum software. Until then we have removed the registration on this forum.
``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);
}
}
Answers
The problem is that you are using floats for positions, and this causes things that may look close, but not be within a dist() of 1 from each other.
Since your objects are on a grid, their positions could be better represented as a pair of integers that determine which cell of the grid the object is in. For example, the top left position is (0,0), to its right is (1,0), below that is (1,1), etc.
Then your collision check can be exact - and you would draw your objects at (grid cell size * coordinate).
That made a huge difference! How would you say to add in the wall collision game over screen? I tried the advice I was given from another post I made but it didnt work out for me.
if the position of an object is outside the range of possible cells, it's off the screen, right?
Example code:
use state to make a game over screen - see my example
Silom = loadFont("Silom.vlw");
file not found
player = minim.loadFile("CD.mp3");
file not found
@Chrisir can you please post your example code here, or give a link to it?
https://forum.processing.org/two/discussion/19323/creating-a-game#latest
I've been trying to get the code to work using states but for whatever reason, it won't switch to the game. Here is the code if anyone is able to help. Maybe someone can see something I'm not seeing.
what is that supposed to do:
in draw() there should be nothing outside switch when using state properly
uh oh
first the semicolon ; BAD.
It ends the if clause. When the condition applies, everything between ) and the ; is executed, which is nothing
Better always use {} with if :
and :
????
no, we want
here
there were so many errors, I stopped counting
It is like this when you just copy code from openprocessing without getting it
One example:
you had
instead of
oh boy....