#Adding enemy fire to my space shooter game
61 messages · Page 1 of 1 (latest)
If you can offer any input here is my repo on github.
I just uploaded my changes.
Everything is working with a test button, I am just figuring out how to control when the enemy fires back.
boolean enemy_fire() {
float c_time = millis();
float d_time = c_time - last_burst;
boolean state = false;
if (last_burst == 0 || d_time >= enemy_fire_interval) {
state = !state;
last_burst = c_time;
}
println("led state is.........." + state);
return state;
}```
void add_E_bullet() {
int lastIndex = enemy_list.size() - 1;
for (int i = lastIndex; i >= 0; i--) {
if (enemy_list.get(i).enemy_fire() == true) {
float enemy_x = enemy_list.get(i).x;
float enemy_y = enemy_list.get(i).y;
e_bullet = new Bullet()
.setPosition(enemy_x, enemy_y)
.setDimension(9, 9)
.setSpeed(-15, 0)
.setColor(e_bullet_color);
e_bullet_list.add(e_bullet);
//println("e_bullet_list size.........." +e_bullet_list.size());
}
}
}```
I do get a bullet, but only one. I can see a change to true but it doesn't produce any bullets. How can I make it stay on for just a bit longer? Any input is welcome.
@bright yoke Any input? Sorry to bug you again.
Don't worry! One moment while I download everything
Nested if statements? outer for on and inner for off?
What does the state variable mean
It is the state of the enemy fire. On = true Off = false.
I want a short burst of fire every n milliseconds.
I do get false with a true value for like 1 frame on the interval.
Something very weird is happening. last_burst is being updated, but state is not. I add some print statements to check this: ```processing
boolean enemy_fire() {
float c_time = millis();
float d_time = c_time - last_burst;
println("Millis:", c_time, "Last burst:", last_burst, "D Time:", d_time);
boolean state = false;
if (last_burst == 0 || d_time >= enemy_fire_interval) {
state = !state;
last_burst = c_time;
println("HERE");
}
println("led state is.........." + state);
return state;
}
And this is the kind of output I would get: ```
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
Millis: 13765.0 Last burst: 13257.0 D Time: 508.0
led state is..........false
...
If last_burst was being changed, I would expect to see "HERE" somewhere in the output, but I never did
At first, I thought last_burst was being changed somewhere else in the code, but I couldn't find any other place it was updated
If I watche the print statement in my testjava float last_burst = 0; float enemy_fire_interval = 1000; void setup() { size(200, 200); background(0); } void draw() { enemy_fire(); } boolean enemy_fire() { float c_time = millis(); float d_time = c_time - last_burst; boolean state = false; if (last_burst == 0 || d_time >= enemy_fire_interval) { state = !state; last_burst = c_time; } println("led state is.........." + state); return state; }
I see false with a blip of true, so I thought I was good. When I drop it in it doesnt work right.
I was hoping for about 5 bullets or so in a burst.
The test does act like I expect.
I'm doing some tests
I'm fooling around too.🤓
Fun stuff. I love working on a problem.
Been thinking on it most of the day today
I knew the real work was going to be taking out the test button.
Interesting. Making your changes to my test method works.```java
Millis: 18317.0 Last burst: 17333.0 D Time: 984.0
led state is..........false
Millis: 18335.0 Last burst: 17333.0 D Time: 1002.0
HERE
led state is..........true
Millis: 18350.0 Last burst: 18335.0 D Time: 15.0
led state is..........false
I got this when I put your print statements in.```java
Millis: 3051.0 Last burst: 2058.0 D Time: 993.0
led state is..........false
Millis: 3068.0 Last burst: 2058.0 D Time: 1010.0
HERE
led state is..........true
Millis: 3068.0 Last burst: 3068.0 D Time: 0.0
led state is..........false
Millis: 3084.0 Last burst: 3068.0 D Time: 16.0
led state is..........false
How to make it stay true for just a few more milliseconds?
I wonder if I shorten the interval
Nope. As expected it just goes true more often, not for longer duration. Maybe a duration variable?
I also just learned from you that you can use commas in a println statement. LOL
Yeah! But keep in mind, that's specific to Processing. In Java, when you use System.out.println(); you can only print one object. You have to use System.out.printf(); with a format string to use it like that in normal Java
I think I have brain lock. I have been at it for about 6 hours. lol
ok so I didn't exactly solve the problem, but I managed to do what you wanted to, in a sperate sketch. Here's a basic sketch that does what you want.
BulletTest:
int BURST_INTERVAL = 1000;
int FIRE_INTERVAL = 75;
ArrayList<Enemy> enemies;
ArrayList<Bullet> bullets;
void setup() {
size(600, 600);
enemies = new ArrayList<>();
bullets = new ArrayList<>();
enemies.add( new Enemy(width/2 - 25, height/2 - 25) );
}
void draw() {
background(125);
for (Enemy enemy : enemies) {
enemy.display();
enemy.fire();
}
for (int i = bullets.size() - 1; i >= 0; i--) {
Bullet bullet = bullets.get(i);
bullet.display();
bullet.update();
if (bullet.x < 0)
bullets.remove(i);
}
}
void mouseClicked() {
enemies.add( new Enemy(mouseX, mouseY) );
}
Bullet:
class Bullet {
float x, y;
float speed;
Bullet(float x, float y) {
this.x = x;
this.y =y;
speed = 5;
}
void display() {
fill(255);
stroke(0);
ellipse(x, y, 10, 10);
}
void update() {
x -= speed;
}
}
Enemy
class Enemy {
float x, y;
int remBullets;
int lastBurst, lastFire;
Enemy(float x, float y) {
this.x = x;
this.y =y;
remBullets = 5;
lastBurst = 0;
lastFire = 0;
}
void display() {
stroke(0);
fill(200, 0, 0);
rect(x, y, 50, 50);
}
void fire() {
int cTime = millis();
if (cTime - lastBurst < BURST_INTERVAL) {
if (cTime - lastFire > FIRE_INTERVAL) {
if (remBullets > 0) {
lastFire = cTime;
bullets.add( new Bullet(x, y + 25) );
remBullets -= 1;
}
}
}
else {
lastBurst = cTime;
remBullets = 5;
}
}
}
Ahhhh, nested if statements. I was on the right track above. ```java
if (cTime - lastBurst < BURST_INTERVAL) {
if (cTime - lastFire > FIRE_INTERVAL) {
if (remBullets > 0) {
lastFire = cTime;
bullets.add( new Bullet(x, y + 25) );
remBullets -= 1;
}
}
}
else {
lastBurst = cTime;
remBullets = 5;
}```
Here
This is what Im looking for too. The bullets stay if the enemy is shot so the ship will need to avoid them.
Once again. Thank you very much for the input.
Another thing I noticed about your code, you have the last_burst outside of any class. It's in the Enemy tab, but not the Enemy class. I suggest giving each enemy their own last_burst variable, like I did.
Happy to help!
OH wow. When I moved last burst inside the class, each enemy now fires 1 bullet. That is a step in the right direction. Man scope still confuses me.
Just gotta work on a little longer of a burst.
Here's a simpler version of the fire() method I wrote ```processing
void fire() {
int cTime = millis();
if (cTime - lastBurst < BURST_INTERVAL) {
if (cTime - lastFire > FIRE_INTERVAL && remBullets > 0) {
lastFire = cTime;
bullets.add( new Bullet(x, y + 25) );
remBullets -= 1;
}
}
else {
lastBurst = cTime;
remBullets = 5;
}
}
(The only difference is that I eliminated that third if-statement by conjoining it to the other one)
I am so happy with my progress.
You might notice that with my code it actually starts with a burst of 10 rather than 5. You can fix that by changing lastBurst = -BURST_INTERVAL; in my constructor so that it waits the right amount of time
I do see that. Your solution is very good. This should work good.
I need to start thinking about an explosion after an enemy is shot. A particle burst at the location of the enemy that was shot.
I have to make/eat supper. I will be working on this later and tomorrow. Soooo much fun!!!!
Another solution would be setting remBullets = 0; in the constructor, but this would mean it doesnt fire until one "burst" of time
Explosions are cool
I like the burst of 5-10 bullets.
Here's 10 bullets. I could make them even faster, but they're hard enough to track as it is
I am settling on 5.
cool
I need to feed myself and my family LOL. I will make my updates and take it from there. I consider you my friend. I am sure I will talk to you soon.
Alrighty, see you later! 👋