Welcome to Day 7. Thank you to those of you who submitted ideas on the story of this game. We will be implementing some of them in the coming lessons.
This lesson will be dedicated to arming our character, so that next week, when we go over collision detection, we will have more to work with.
This is how we will approach today's task:
1. We will first create a Projectiles class, which we will use as a blueprint for our bullet objects.
2. Within the Robot class, we will create a method that handles shooting.
3. Finally, in the StartingClass, we will paint the bullets to the screen, and allow the user to shoot with the Control button.
Lesson #2-20: Creating the Projectile Class
This lesson will be dedicated to arming our character, so that next week, when we go over collision detection, we will have more to work with.
This is how we will approach today's task:
1. We will first create a Projectiles class, which we will use as a blueprint for our bullet objects.
2. Within the Robot class, we will create a method that handles shooting.
3. Finally, in the StartingClass, we will paint the bullets to the screen, and allow the user to shoot with the Control button.
Lesson #2-20: Creating the Projectile Class
The Projectile class, as mentioned, will be a blueprint for creating bullets!
As with many of the previous classes, we will do the following:
1. Create the class.
2. Declare variables
3. Create an update() method and create helper Getters and Setters methods
As with many of the previous classes, we will do the following:
1. Create the class.
2. Declare variables
3. Create an update() method and create helper Getters and Setters methods
I. Creating the Projectile Class
1. Right click on the kiloboltgame package >> New >> Class
2. Name it "Projectile".
3. Check the box labeled "Constructors from superclass:"
4. Press OK!
You should now see the following:
package kiloboltgame;
public class Projectile {
public Projectile() {
// TODO Auto-generated constructor stub
}
}
2. Name it "Projectile".
3. Check the box labeled "Constructors from superclass:"
4. Press OK!
You should now see the following:
package kiloboltgame;
public class Projectile {
public Projectile() {
// TODO Auto-generated constructor stub
}
}
II. Declaring Variables
Let's now declare the variables that we will be using.
private int x, y, speedX;
private boolean visible;
These are class-wide variables, so they should be below the class declaration:
public class Projectile{
private int x, y, speedX;
private boolean visible;
Now, we need to make some changes to the constructor:
public Projectile(){
}
As of now, it takes in no parameters. We want it to take in two values, a starting X coordinate and a starting Y coordinate, which will represent the top left corner of each painted bullet.
1. So we add the two parameters:
public Projectile(int startX, int startY){
}
2. To set these startX and startY variables into the class-wide x and y variables, and to set the speedX of the bullet and to initialize the visible boolean, we declare the following four statements inside the constructor:
x = startX;
y = startY;
speedX = 7;
visible = true;
Your constructor should now look like this:
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
}
private int x, y, speedX;
private boolean visible;
These are class-wide variables, so they should be below the class declaration:
public class Projectile{
private int x, y, speedX;
private boolean visible;
Now, we need to make some changes to the constructor:
public Projectile(){
}
As of now, it takes in no parameters. We want it to take in two values, a starting X coordinate and a starting Y coordinate, which will represent the top left corner of each painted bullet.
1. So we add the two parameters:
public Projectile(int startX, int startY){
}
2. To set these startX and startY variables into the class-wide x and y variables, and to set the speedX of the bullet and to initialize the visible boolean, we declare the following four statements inside the constructor:
x = startX;
y = startY;
speedX = 7;
visible = true;
Your constructor should now look like this:
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
}
III. Creating the Update() and Helper Methods
The update() method in this class, as in the other classes, will be called with each update of the game.
Naturally, then, we should change the location of the bullet here with respect to speed, and react when the bullet collides or goes off the screen (we won't handle collision just yet).
1. So first create an update method:
public void update(){
}
2. Add the following statements:
x += speedX;
if (x > 800) {
visible = false;
}
- The x += speedX; statement will continually update the x coordinate by adding to it the speed in the x direction.
- The if statement checks if the bullet is off the screen, and makes it invisible. In the other classes, we will remove these bullets so they do not take up unnecessary memory.
3. Now create Getters and Setters:
- Right click on the code, go to Source >> Generate Getters and Setters
- Select All
- Press OK
You should get the following:
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
That should be it for the Projectile class for now. Here's the final code:
Naturally, then, we should change the location of the bullet here with respect to speed, and react when the bullet collides or goes off the screen (we won't handle collision just yet).
1. So first create an update method:
public void update(){
}
2. Add the following statements:
x += speedX;
if (x > 800) {
visible = false;
}
- The x += speedX; statement will continually update the x coordinate by adding to it the speed in the x direction.
- The if statement checks if the bullet is off the screen, and makes it invisible. In the other classes, we will remove these bullets so they do not take up unnecessary memory.
3. Now create Getters and Setters:
- Right click on the code, go to Source >> Generate Getters and Setters
- Select All
- Press OK
You should get the following:
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
That should be it for the Projectile class for now. Here's the final code:
FIGURE 2-29: Projectile Class
package kiloboltgame;
public class Projectile {
private int x, y, speedX;
private boolean visible;
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
}
public void update(){
x += speedX;
if (x > 800){
visible = false;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
public class Projectile {
private int x, y, speedX;
private boolean visible;
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
}
public void update(){
x += speedX;
if (x > 800){
visible = false;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
Lesson #2-21: Creating an ArrayList in the Robot Class
It would be extremely time consuming and inefficient if we were to manually create bullets each time that the player pressed "shoot." So we will be implementing a type of List called an ArrayList, which will allow us to store our Projectile objects in a "container" of increasing size.
1. To do so, we first declare (below all the other variable declarations):
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
(Make sure you import ArrayList, Ctrl+Shift+O).
Doing this creates an ArrayList (of Projectiles) called projectiles.
2. Now, below the jump() method, I will add the shoot() method:
public void shoot() {
Projectile p = new Projectile(centerX + 50, centerY - 25);
projectiles.add(p);
}
This method simply creates a new Projectile, labels it p, and adds it to the projectiles ArrayList. We create this 50 pixels to the right and 25 pixels above the center of the robot, which is where the gun is.
3. Create a Getter method as follows:
public ArrayList getProjectiles() {
return projectiles;
}
This will allow us to reference this newly created ArrayList from the other classes.
Your finished Robot class will look like this:
1. To do so, we first declare (below all the other variable declarations):
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
(Make sure you import ArrayList, Ctrl+Shift+O).
Doing this creates an ArrayList (of Projectiles) called projectiles.
2. Now, below the jump() method, I will add the shoot() method:
public void shoot() {
Projectile p = new Projectile(centerX + 50, centerY - 25);
projectiles.add(p);
}
This method simply creates a new Projectile, labels it p, and adds it to the projectiles ArrayList. We create this 50 pixels to the right and 25 pixels above the center of the robot, which is where the gun is.
3. Create a Getter method as follows:
public ArrayList getProjectiles() {
return projectiles;
}
This will allow us to reference this newly created ArrayList from the other classes.
Your finished Robot class will look like this:
FIGURE 2-30: Robot Class (Changes in Bold)
package kiloboltgame;
import java.util.ArrayList;
public class Robot {
// Constants are Here
final int JUMPSPEED = -15;
final int MOVESPEED = 5;
final int GROUND = 382;
private int centerX = 100;
private int centerY = GROUND;
private boolean jumped = false;
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean ducked = false;
private int speedX = 0;
private int speedY = 1;
private Background bg1 = StartingClass.getBg1();
private Background bg2 = StartingClass.getBg2();
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
public void update() {
// Moves Character or Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200){
bg1.setSpeedX(-MOVESPEED);
bg2.setSpeedX(-MOVESPEED);
}
// Updates Y Position
centerY += speedY;
if (centerY + speedY >= GROUND) {
centerY = GROUND;
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
if (centerY + speedY >= GROUND) {
centerY = GROUND;
speedY = 0;
jumped = false;
}
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
}
public void moveRight() {
if (ducked == false) {
speedX = MOVESPEED;
}
}
public void moveLeft() {
if (ducked == false) {
speedX = -MOVESPEED;
}
}
public void stopRight() {
setMovingRight(false);
stop();
}
public void stopLeft() {
setMovingLeft(false);
stop();
}
private void stop() {
if (isMovingRight() == false && isMovingLeft() == false) {
speedX = 0;
}
if (isMovingRight() == false && isMovingLeft() == true) {
moveLeft();
}
if (isMovingRight() == true && isMovingLeft() == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
public void shoot() {
Projectile p = new Projectile(centerX + 50, centerY - 25);
projectiles.add(p);
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public boolean isJumped() {
return jumped;
}
public int getSpeedX() {
return speedX;
}
public int getSpeedY() {
return speedY;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setJumped(boolean jumped) {
this.jumped = jumped;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public boolean isDucked() {
return ducked;
}
public void setDucked(boolean ducked) {
this.ducked = ducked;
}
public boolean isMovingRight() {
return movingRight;
}
public void setMovingRight(boolean movingRight) {
this.movingRight = movingRight;
}
public boolean isMovingLeft() {
return movingLeft;
}
public void setMovingLeft(boolean movingLeft) {
this.movingLeft = movingLeft;
}
public ArrayList getProjectiles() {
return projectiles;
}
}
import java.util.ArrayList;
public class Robot {
// Constants are Here
final int JUMPSPEED = -15;
final int MOVESPEED = 5;
final int GROUND = 382;
private int centerX = 100;
private int centerY = GROUND;
private boolean jumped = false;
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean ducked = false;
private int speedX = 0;
private int speedY = 1;
private Background bg1 = StartingClass.getBg1();
private Background bg2 = StartingClass.getBg2();
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
public void update() {
// Moves Character or Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200){
bg1.setSpeedX(-MOVESPEED);
bg2.setSpeedX(-MOVESPEED);
}
// Updates Y Position
centerY += speedY;
if (centerY + speedY >= GROUND) {
centerY = GROUND;
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
if (centerY + speedY >= GROUND) {
centerY = GROUND;
speedY = 0;
jumped = false;
}
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
}
public void moveRight() {
if (ducked == false) {
speedX = MOVESPEED;
}
}
public void moveLeft() {
if (ducked == false) {
speedX = -MOVESPEED;
}
}
public void stopRight() {
setMovingRight(false);
stop();
}
public void stopLeft() {
setMovingLeft(false);
stop();
}
private void stop() {
if (isMovingRight() == false && isMovingLeft() == false) {
speedX = 0;
}
if (isMovingRight() == false && isMovingLeft() == true) {
moveLeft();
}
if (isMovingRight() == true && isMovingLeft() == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
public void shoot() {
Projectile p = new Projectile(centerX + 50, centerY - 25);
projectiles.add(p);
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public boolean isJumped() {
return jumped;
}
public int getSpeedX() {
return speedX;
}
public int getSpeedY() {
return speedY;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setJumped(boolean jumped) {
this.jumped = jumped;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public boolean isDucked() {
return ducked;
}
public void setDucked(boolean ducked) {
this.ducked = ducked;
}
public boolean isMovingRight() {
return movingRight;
}
public void setMovingRight(boolean movingRight) {
this.movingRight = movingRight;
}
public boolean isMovingLeft() {
return movingLeft;
}
public void setMovingLeft(boolean movingLeft) {
this.movingLeft = movingLeft;
}
public ArrayList getProjectiles() {
return projectiles;
}
}
Lesson #2-22: Painting the Bullets in the StartingClass
I. Changing the Run() Method
Make the changes in bold to your run() method:
@Override
public void run() {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = character;
}
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
hb.update();
hb2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = character;
}
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
hb.update();
hb2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
(Make sure you import ArrayList: Ctrl+Shift+O)
Let's talk about what we did here:
We created a new ArrayList called projectiles and gave it the value of the projectiles ArrayList in robot.
We then created a for loop, which runs as long as the index i is lesser than the size of the projectiles ArrayList (which is the number of Projectile objects stored in it).
Then we check which Projectile object is in the i-th (4th, 5th, etc). place in the ArrayList (the Projectile object with an index of i).
To illustrate indexes, here's another example:
List = [1, 2, 3, 5, 7]
The indexes of each of these are: 0, 1, 2, 3, and 4. This means that the for loop will go through each object in the ArrayList of Projectiles and set it equal to p
After that, it checks if this p is on the screen (isVisible()). If true, it updates it. Else, it removes this p by removing the i-th index from the projectiles ArrayList.
To summarize all this, we create a for loop with an index called i which goes up by 1 each time. We set the i-th Projectile in the projectiles ArrayList, set it equal to p, and update it (if on screen) or remove it (if not on screen).
Let's talk about what we did here:
We created a new ArrayList called projectiles and gave it the value of the projectiles ArrayList in robot.
We then created a for loop, which runs as long as the index i is lesser than the size of the projectiles ArrayList (which is the number of Projectile objects stored in it).
Then we check which Projectile object is in the i-th (4th, 5th, etc). place in the ArrayList (the Projectile object with an index of i).
To illustrate indexes, here's another example:
List = [1, 2, 3, 5, 7]
The indexes of each of these are: 0, 1, 2, 3, and 4. This means that the for loop will go through each object in the ArrayList of Projectiles and set it equal to p
After that, it checks if this p is on the screen (isVisible()). If true, it updates it. Else, it removes this p by removing the i-th index from the projectiles ArrayList.
To summarize all this, we create a for loop with an index called i which goes up by 1 each time. We set the i-th Projectile in the projectiles ArrayList, set it equal to p, and update it (if on screen) or remove it (if not on screen).
II. Changing the Paint() Method
Make the changes in bold in the paint() method:
@Override
public void paint(Graphics g) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.YELLOW);
g.fillRect(p.getX(), p.getY(), 10, 5);
}
g.drawImage(currentSprite, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);
g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);
}
@Override
public void paint(Graphics g) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.YELLOW);
g.fillRect(p.getX(), p.getY(), 10, 5);
}
g.drawImage(currentSprite, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);
g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);
}
The same concepts apply here.
We created an ArrayList called projectiles (the one from the update() method is only usable by the update() method, so this is a unique ArrayList) and give it the value of the projectiles ArrayList in the Robot class.
We used a for loop and painted each Projectile object called p, which represents the i-th Projectile in the ArrayList at the current iteration of the for loop, and we draw a yellow rectangle with width 10 and height 5 at the X and Y coordinate of the p Projectile.
We created an ArrayList called projectiles (the one from the update() method is only usable by the update() method, so this is a unique ArrayList) and give it the value of the projectiles ArrayList in the Robot class.
We used a for loop and painted each Projectile object called p, which represents the i-th Projectile in the ArrayList at the current iteration of the for loop, and we draw a yellow rectangle with width 10 and height 5 at the X and Y coordinate of the p Projectile.
Lesson #2-23: Shooting on Keypress
We want to use the Ctrl button to shoot our bullets.
Navigate to the keyPressed() method in StartingClass, and make the following changes in bold:
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
currentSprite = characterDown;
if (robot.isJumped() == false) {
robot.setDucked(true);
robot.setSpeedX(0);
}
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
robot.setMovingLeft(true);
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
robot.setMovingRight(true);
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
case KeyEvent.VK_CONTROL:
if (robot.isDucked() == false && robot.isJumped() == false) {
robot.shoot();
}
break;
}
}
All we did was add another case to the switch, so that when CONTROL is pressed, the robot calls its shoot() method. We also check if the robot is ducking or jumping, and prevent shooting if it is doing either.
We can now run the game, and you should be able to shoot with the Control button!
Navigate to the keyPressed() method in StartingClass, and make the following changes in bold:
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
currentSprite = characterDown;
if (robot.isJumped() == false) {
robot.setDucked(true);
robot.setSpeedX(0);
}
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
robot.setMovingLeft(true);
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
robot.setMovingRight(true);
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
case KeyEvent.VK_CONTROL:
if (robot.isDucked() == false && robot.isJumped() == false) {
robot.shoot();
}
break;
}
}
All we did was add another case to the switch, so that when CONTROL is pressed, the robot calls its shoot() method. We also check if the robot is ducking or jumping, and prevent shooting if it is doing either.
We can now run the game, and you should be able to shoot with the Control button!
The final StartingClass follows (changes in bold):
Figure 2-31: StartingClass, end of Day 7
package kiloboltgame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
public class StartingClass extends Applet implements Runnable, KeyListener {
private Robot robot;
private Heliboy hb, hb2;
private Image image, currentSprite, character, characterDown,
characterJumped, background, heliboy;
private Graphics second;
private URL base;
private static Background bg1, bg2;
@Override
public void init() {
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
// Image Setups
character = getImage(base, "data/character.png");
characterDown = getImage(base, "data/down.png");
characterJumped = getImage(base, "data/jumped.png");
currentSprite = character;
heliboy = getImage(base, "data/heliboy.png");
background = getImage(base, "data/background.png");
}
@Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
robot = new Robot();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = character;
}
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
hb.update();
hb2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.YELLOW);
g.fillRect(p.getX(), p.getY(), 10, 5);
}
g.drawImage(currentSprite, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);
g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
currentSprite = characterDown;
if (robot.isJumped() == false) {
robot.setDucked(true);
robot.setSpeedX(0);
}
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
robot.setMovingLeft(true);
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
robot.setMovingRight(true);
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
case KeyEvent.VK_CONTROL:
if (robot.isDucked() == false && robot.isJumped() == false) {
robot.shoot();
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
break;
case KeyEvent.VK_DOWN:
currentSprite = character;
robot.setDucked(false);
break;
case KeyEvent.VK_LEFT:
robot.stopLeft();
break;
case KeyEvent.VK_RIGHT:
robot.stopRight();
break;
case KeyEvent.VK_SPACE:
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
public static Background getBg2() {
return bg2;
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
public class StartingClass extends Applet implements Runnable, KeyListener {
private Robot robot;
private Heliboy hb, hb2;
private Image image, currentSprite, character, characterDown,
characterJumped, background, heliboy;
private Graphics second;
private URL base;
private static Background bg1, bg2;
@Override
public void init() {
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
// Image Setups
character = getImage(base, "data/character.png");
characterDown = getImage(base, "data/down.png");
characterJumped = getImage(base, "data/jumped.png");
currentSprite = character;
heliboy = getImage(base, "data/heliboy.png");
background = getImage(base, "data/background.png");
}
@Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
robot = new Robot();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = character;
}
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
if (p.isVisible() == true) {
p.update();
} else {
projectiles.remove(i);
}
}
hb.update();
hb2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
ArrayList projectiles = robot.getProjectiles();
for (int i = 0; i < projectiles.size(); i++) {
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.YELLOW);
g.fillRect(p.getX(), p.getY(), 10, 5);
}
g.drawImage(currentSprite, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
g.drawImage(heliboy, hb.getCenterX() - 48, hb.getCenterY() - 48, this);
g.drawImage(heliboy, hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;
case KeyEvent.VK_DOWN:
currentSprite = characterDown;
if (robot.isJumped() == false) {
robot.setDucked(true);
robot.setSpeedX(0);
}
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
robot.setMovingLeft(true);
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
robot.setMovingRight(true);
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
case KeyEvent.VK_CONTROL:
if (robot.isDucked() == false && robot.isJumped() == false) {
robot.shoot();
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
break;
case KeyEvent.VK_DOWN:
currentSprite = character;
robot.setDucked(false);
break;
case KeyEvent.VK_LEFT:
robot.stopLeft();
break;
case KeyEvent.VK_RIGHT:
robot.stopRight();
break;
case KeyEvent.VK_SPACE:
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
public static Background getBg2() {
return bg2;
}
}
Thank you for reading this lesson! Things are starting to get a bit repetitive, so we will be mixing things up next time around.
As always, we appreciate your support, and we're extremely pleased that we broke 750 Facebook Likes!
As always, we appreciate your support, and we're extremely pleased that we broke 750 Facebook Likes!

kiloboltday7.zip |
