I'm working on a game and when the user gets to the GAME_OVER state, I want them to click to go back to the main menu. Here they can start the game again, but at the moment, when they start playing again - the PLAYING state is the same as it was just before the user died and went to GAME_OVER.
I'm trying to implement a levelling system in my program. I have an object that is initialised at the beginning of the program, in setup, and halfway through (random time length really) I want to initialise another copy of that object class.
I tried doing it in draw, because it was only if a condition was met that it would initialise. But as you can imagine it kept drawing it, so that I had more of the object than I wanted.
Is there something I'm missing?
I did try something like this:
boolean conditionMet = false;
void setup() {
myObject = new myObject(10);
if(conditionMet) {
println("working");
myObject = new myObject(20);
}
}
void draw() {
myObject.draw();
myObject.move();
if(mills() > 10000) {
conditionMet = true;
}
}
But the object just would not initialise again!
Oh! I forgot to mention, my object class is an ArrayList too :)
Hello everyone, I wonder if anyone would help me with an issues I have regarding pathfinding.
I am making a 2D zombie shooter and I am working on making my zombies separate rather than group together. With a lot of help from a friend (who wrote this section of code) I have now got an algorithm that:
Checks for the nearest zombie
Works out the possible move it can make
Checks to see if there is another zombie in the way
If not then it can move
If there is, see if you can move on the x axis (and vice versa)
BUT! When the program is run, some of the zombies will not make the required move, so I have zombies dotted around the screen just not moving - unless I make the player move somewhere else to make the zombie move again.
I don't want to paste the whole program into this post as there is too much of it, but have a look at the above function and you may be able to help!
void seekTarget() {
Mover closestZombie = null;
float distanceToClosestZombie = 10000; // arbitrary large number
float distance;
int tryX = x;
int tryY = y;
// find which of the other zombies is nearest to this one,
// and what distance separates them
for (Mover m: movers) {
if (m == this) {
// don't compare this zombie to itself!
continue;
}
distance = dist(x, y, m.x, m.y);
if (distance < distanceToClosestZombie) {
// the zombie being tested is the closest found yet
closestZombie = m;
distanceToClosestZombie = distance;
}
}
// work out the "ideal" new x and y coordinates, assuming
// for now that the closest zombie is not in the way
// (we'll find that out in a moment)
/*if(targetX > x) {
tryX++;
//println("working");
} else if(targetX < x) {
tryX--;
//println("working");
}
if (targetY > y) {
tryY++;
//println("working");
} else if(targetY < y) {
tryY--;
//println("working");
}*/
if(targetX > player.x) {
//tryX = tryX + int(zombieSpeed);
tryX--;
image(zombieLEFT, tryX, tryY);
println("working");
} else if(targetX < player.x) {
tryX++;
//tryX = tryX - int(zombieSpeed);
image(zombieRIGHT, tryX, tryY);
}
if(targetY > player.y) {
tryY--;
//tryY = tryY - int(zombieSpeed);
image(zombieUP, tryX, tryY);
} else if(targetY < player.y) {
tryY++;
//tryY = tryY + int(zombieSpeed);
image(zombieDOWN, tryX, tryY);
}
// if the closest zombie is not already too close, make the
// ideal move computed above
if (distanceToClosestZombie > MIN_ZOMBIE_SEPARATION) {
x = tryX;
y = tryY;
}
// otherwise, only make a move that will cause the separation
// between this zombie and its closest neighbour to either
// increase or remain the same
else {
if (dist(tryX, tryY, closestZombie.x, closestZombie.y) >=
distanceToClosestZombie) {
// the ideal move increases or doesn't change the separation
x = tryX;
y = tryY;
}
else if (dist(tryX, y, closestZombie.x, closestZombie.y) >=
distanceToClosestZombie) {
// only the horizontal component of the ideal move increases
// or doesn't change the separation
x = tryX;
}
else if (dist(x, tryY, closestZombie.x, closestZombie.y) >=
distanceToClosestZombie) {
// only the vertical component of the ideal move increases
// or doesn't change the separation
y = tryY;
}
}
}
The Mover is the zombie :)
Ah - My mate thinks it is to do with the fact that I also have a move() function separate to this, and that I need to integrate it into the above function, below is the other move() function:
I'm making a 2D zombie shooter and I have the zombies chasing the player; but at the moment they can move in any direction (360).
I'm trying to get them so that they only move in 8 different directions (up,down,right,left,and the 4 diagonal directions).
I think it's to do with working out whether the player (whom they are chasing) is a within a certain angle from the zombie, but I don't know how to do it!
I'm trying to make a Nazi Zombie shooter in 2D and I'm having a bit of trouble making the bullets (which are in an arrayList) shoot in the direction that the player is facing/last moved in.
I was told to make a variable in the move() function in the player class to work out which button was pressed (out of UP,DOWN,LEFT,RIGHT) and to then call it in the main program, like this:
void move(int direction) {
if(direction == UP) {
y = y - 8;
} else if(direction == DOWN) {
y = y + 8;
} else if(direction == RIGHT) {
x = x + 8;
} else if(direction == LEFT) {
x = x - 8;
}
}
And call it in the main program:
void keyPressed() {
if(key == CODED) {
if(keyCode == UP) {
player.move(UP);
player.y = constrain(player.y,15,height);
} else if (keyCode == DOWN) {
player.move(DOWN);
player.y = constrain(player.y,0,height-17);
} else if (keyCode == LEFT) {
player.move(LEFT);
player.x = constrain(player.x,3,width);
} else if (keyCode == RIGHT) {
player.move(RIGHT);
player.x = constrain(player.x,0,width-17);
}
}
}
But then I have my b.move(); in the bullet arrayList in the main program:
void updateBullets() {
ArrayList<Bullet> bulletsToDelete = new ArrayList<Bullet>();
I am trying to get bullets to shoot in the direction the player last moved in however now, when I shoot, and then try and move again, the bullets that have been shot respond to the player moving. Run the code and you'll see!
w,a,s and d are to move up, down etc
Mouse click to shoot! :)
CODE:
//Classes
Player player;
//Arrays
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
//Variables
float x = 100;
float y = 100;
float angle;
int lastPressed = millis();
void setup() {
size(200,200);
smooth();
rectMode(CENTER);
player = new Player(1);
}
void draw() {
background(255);
fill(34,56,200);
updateBullets();
player.setLocation(player.x,player.y);
player.display();
}
void updateBullets() {
ArrayList<Bullet> bulletsToDelete = new ArrayList<Bullet>();
I have a problem with looping, now I know how to constrain values and stuff with constrain() but I am not sure how to stop an infinite loop when I am using a keyPressed function.
I basically want the 'shooting effect', so when I press spacebar a bullet is shot, but it's not visible before pressing and it just appears every time the spacebar is pressed.
Hi everyone, I am fairly new at Processing, and java, so I hope some of you can help!
I'm making a shooter game with a movable player. However I am stuck on how to make him face where the mouse is, so that I can then code him to shoot in that direction. Hopefully that's clear for you guys?
Here is what I have at the moment*:
void setup() {
size(200,200);
smooth();
}
void draw() {
background(255);
rectMode(CENTER);
fill(0);
rect(50,50,20,50);
}
Although my real code for my game has classes, arrays etc, this is what I have done to make my problem as specific as possible - so my player is just a rectangle...