[英]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
Him 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这是我想出的用于在 void draw() function 中向下射击子弹的代码
float bulletY = DefenderY; //bullet being set in player x position
float bulletX= DefenderX; //bullet set in player y postion
rect(bulletX, bulletY, 8, 8); //creating the bullet
text("score: ", countA, 20, 80); //score counter not implimented yet
if (keyPressed && key==CODED && key == 's')// if the space bar is pressed //key press detection,
{
dropping = true; //boolean is now true when previously flase
}
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,但是矩形子弹没有从(DefenderY,DefenderY)position 移动,它与播放器保持连接,将不胜感激,
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; }
}
}
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.您将子弹 position 设置为 Defender position 然后立即绘制它。
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.稍后您有一些代码可以调整子弹 position,但它不会有任何效果,因为您已经在原始 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.另请注意, draw
function 运行每一帧,所以你可能不想像这样每帧都重置子弹 position,否则很难让它移动到任何地方。 Declare the variables for bullet position outside of draw
and only reset them when the bullet is initially fired.在draw
之外声明子弹 position 的变量,并且仅在子弹最初发射时重置它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.