the game I'm trying to make (for programming practice purposes), is just a small thing with rocks attacking a base that can turn and fire missiles that destroys the rocks (all rocks gone you win, 3 rocks hit and you lose).
the problem I'm having is that the rocks are spawning too close to the base. after lots of attempts and a lot of googling I'm finally submitting it on here, this is my code currently:
The problems lie within the "PlaceRocks" method, what I want it to do is spawn 50 (or maybe 100) ~units~ away from the middle of the world where the base (MissileBase) is.
currently, it will either spawn none to five (and sometimes more) or just not work at all with no error message in the terminal (the "System.ou.println("else" + z)" is just for debugging).
like I said I have tried a lot of different ways including inside the "Rock" class any help would be appreciated.
this is my first piece of programming and I also have dyslexia so I apologizes for any grammar or spelling mistakes in this post, thankfully I have Grammarly so it shouldn't be too bad.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class GameWorld here.
*
* @author (MrBB)
* @version (a version number or a date)
*/
public class GameWorld extends World
{
public int Lives;
int z = 0;
int i;
public GameWorld()
{
super(600, 400, 1);
addObject(new MissileBase(),getWidth() / 2, getHeight() / 2);
Lives = 3;
PlaceRocks();
}
public void PlaceRocks()
{
for(i = 0; i <= 4; i = i +1){
int x = 20 + Greenfoot.getRandomNumber(getWidth()- 20);
int y = 20 + Greenfoot.getRandomNumber(getHeight()- 20);
int rtcx1 = getWidth() / 2 - 50;
int rtcx2 = getWidth() / 2 + 50;
int rtcy1 = getHeight() / 2 - 50;
int rtcy2 = getHeight() / 2 + 50;
if(x >= rtcx1 && x <= rtcx2 || y >= rtcy1 && y <= rtcy2){
Rock rock = new Rock();
addObject(rock, x, y);
i--;
}
else{
System.out.println("else" + z);
}
z++;
}
}
public void removeLives()
{
Lives--;
if(Lives == 0){
Greenfoot.stop();
Lives = 3;
}
}
}
