'I'm trying to make a simple java processing game where I can shoot bullets from a shape but they don't seem to be shooting
I'm a beginner in coding and creating a castle defender game.
I'm trying to add bullet shooting to my code but I'm having a hard time implementing it to my project.
Here is the code I came up with for shooting the bullets downwards in the void draw() function
float bulletY = DefenderY; //bullet being set in player x position
float bulletX= DefenderX; //bullet set in player y position
rect(bulletX, bulletY, 8, 8); //creating the bullet
text("score: ", countA, 20, 80); //score counter not implemented yet
if (keyPressed && key==CODED && key == 's')// if the space bar is pressed //key press detection,
{
dropping = true; //boolean is now true when previously false
}
if(dropping == true){ if (bulletY<=400) // checking if the bullet is still within the frame
bulletY= bulletY + 1;} //boolean true then the rectangle should move from the player position
}
if(bulletY>=400){dropping = false; //when bullet hits game borders reset to player position
bulletY=DefenderY;
bulletX=DefenderX;}
However the rectangle bullet is not moving from the (DefenderY, DefenderY) position, it stays attached the the player, any help would be appreciated.
I will also post the whole code below if that helps with my question thanks.
class attackers{
float max = 400;
float min = 1;
float posx = (int)Math.floor(Math.random()*(max-min+1)+min);
float posy = 400;
float speed = 1;
}
attackers one, two, three, four;
float DefenderX= 100;
float DefenderY=100;
void setup()
{
one = new attackers();
two = new attackers();
three = new attackers();
four = new attackers();
size(400,400); //fairly large relative to objects
rectMode(CENTER); //set rect x,y to be the centre.
}
float gunX=40;
boolean dropping = false;
int countA = 0;
void draw()
{
background(#0000FF); //clear background
ellipse(DefenderX,DefenderY,20,20);//Draw player current position (x,y)
if ( abs(DefenderX - one.posx)<20 && abs(DefenderY-one.posy )<20 ||
abs(DefenderX - two.posx)<20 && abs(DefenderY-two.posy )<20 ||
abs(DefenderX - three.posx)<20 && abs(DefenderY-three.posy )<20 ||
abs(DefenderX - four.posx)<20 && abs(DefenderY-four.posy )<20
)//are they close together?
{
print ("GAMEOVER!");
}
float bulletY = DefenderY;
float bulletX= DefenderX;
rect(bulletX, bulletY, 8, 8);
text("score: ", countA, 20, 80);
if (keyPressed && key==CODED && key == 's')// if the space bar is pressed
{
dropping = true;
print("its working");
if (bulletY<=400)
bulletY= bulletY + 1;
}
if(dropping == true){
}
if(bulletY>=400){dropping = false;
bulletY=DefenderY;
bulletX=DefenderX +30;}
int randomattack, randomattack1, randomattack2, randomattack3;
randomattack = (int)Math.floor(Math.random()*(two.max-two.min+1)+two.min);
ellipse(one.posx,one.posy, 10,10); //draw ball at current position : x, y fixed at 125!
one.posy = one.posy - 1;
if (abs(one.posy -1 )<1 )
{one.posy = one.posy+ 400;
one.posx = randomattack;}
randomattack1 = (int)Math.floor(Math.random()*(two.max-two.min+1)+two.min);
ellipse(two.posx,two.posy, 10,10); //draw ball at current position : x, y fixed at 125!
two.posy = two.posy - 1;
if (abs(two.posy -1 )<1 )
{two.posy = two.posy+ 400;
two.posx = randomattack1;}
randomattack2 = (int)Math.floor(Math.random()*(two.max-two.min+1)+two.min);
ellipse(three.posx,three.posy, 10,10); //draw ball at current position : x, y fixed at 125!
three.posy = three.posy - 1;
if (abs(three.posy -1 )<1 )
{three.posy = three.posy+ 400;
three.posx = randomattack2;}
randomattack3 = (int)Math.floor(Math.random()*(one.max-one.min+1)+one.min);
ellipse(four.posx,four.posy, 10,10); //draw ball at current position : x, y fixed at 125!
four.posy = four.posy - 1;
if (abs(four.posy -1 )<1 )
{four.posy = four.posy+ 400;
four.posx = randomattack3;}
}void keyPressed()
{
if (key==CODED)
{
if (keyCode == LEFT)
{ DefenderX = DefenderX - 5; }
if (keyCode == RIGHT)
{ DefenderX = DefenderX + 5; }
}
}
Solution 1:[1]
This is the part of your code that draws the bullet:
float bulletY = DefenderY;
float bulletX = DefenderX;
rect(bulletX, bulletY, 8, 8);
You set the bullet position to Defender position and then immediately draw it.
You have some code later on that adjusts the bullet position, but it won't have any effect because you've already drawn the bullet on screen at the original position.
Also note that the draw function runs every frame, so you probably don't want to be resetting the bullet position each frame like that or it will be difficult to get it to move anywhere. Declare the variables for bullet position outside of draw and only reset them when the bullet is initially fired.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cadin |
