Question: I ' m completing a java game of flying machine shooting meteorites, please help me complete some part of the java code as required below,
Im completing a java game of flying machine shooting meteorites, please help me complete some part of the java code as required below, thank you very much
Task Adding a Bullet Actor Class
Adding a Moving Bullet
To shoot a bullet you need to add a bullet image as anActorclass in the scenario. The bullet image is already given in the starting scenario, inside theimagesfolder The image looks like this:
In your starting scenario you need add a newActorsubclass using the above image. Let's call this classBullet
After adding the bullet class you will need to make it move. Even though your bullet is supposed to move along the direction of the spaceship, you only need to make it move to the right at this stage. This is similar to what we have done for theRockclass in the lesson. When you move the bullet you need to make it travel faster than the spaceship and the rock, say, using a distance value of when you move the bullet.
Removing the Bullet
Once your bullet moves out of the game area, you need to remove the bullet from the world. You have to add the code in theactmethod of the class. First you use an if statement to check whether the location of the bullet is not within the game area. If the bullet is out of the world you will then need to use theremoveObjectmethod to remove it
Task Shooting the Bullet from the Spaceship
Shooting a Bullet When the Spacebar Key Is Pressed
You can shoot a bullet by pressing the spacebar key. In theactmethod of the spaceship, you see the code to turn the spaceship left the left arrow key turn right the right arrow key and move forward the up arrow key You will add an additional key for controlling the shooting of the bullet. The key you need to use is the spacebar key. For example, the following code checks if the spacebar key, using the textspace has been pressed:
if GreenfootisKeyDownspace
shoot the bullet...
If the spacebar key has been pressed you can shoot the bullet by creating a new instance of theBulletclass and adding it to the world using theaddObjectmethod To do that, you can use the following code:
Bullet bullet new Bullet;
getWorldaddObjectbullet getX getY;
Checking for the Existing Bullet in the World
To solve the first problem you don't shoot the bullet every time the player presses the spacebar. You only shoot the bullet when there is no bullet currently on the screen.You can use thegetObjectsmethod of the world object to do that. For example, the following if statement checks whether there is any bullet in the world:
if getWorldgetObjectsBulletclassisEmpty
Turning the Bullet to the Direction of the Spaceship
You need to turn the bullet so that it travels in the direction of the spaceship.
There are two helpful methods in this task. The first method is thegetRotationmethod which allows you to get the current direction of an actor. Another method is thesetRotationmethod which lets you change the direction of an actor to a specific angle.You need to do this when you add the bullet to the world.
Task Destroying the Rocks
When a bullet hits a rock, both the bullet and the rock are destroyed from the world. Recall that we have done a similar task in our game. When the spaceship is very close to the rock, we show the explosion image, remove the spaceship and the game is over. Therefore, you can use similar code to do this task.
You will write the code in theactmethod of theBulletclass First, you check whether the bullet is very close to a rock, using this if statement:
if getObjectsInRange Rock.classisEmpty
To destroy the bullet you simply usegetWorldremoveObjectthisto do it However, it is a bit of trouble to remove the rock. The reason is that you don't know which rock is very close to the bullet in the code.
So you need to get the corresponding rock first. To do that you use thegetObjectsInRangemethod again, together with thegetmethod on the returned list, like this:
Rock rock Rock getObjectsInRange Rock.classget;
The above code means getting the first item from the list returned bygetObjectsInRangeand putting the item in therockvariable The variable now contains the rock that is very close to the bullet. You can use theremoveObjectmethod to remove it from the world.
Since both the bullet and the rock have been removed you can use the return statement at the end of the if statement like this:
if getObjectsInRange Rock.classisEmpty
remove the rock and the bullet from the world...
return;
The return statement ensures that you don't do anything more when both the rock and the bullet have been destroyed.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
