-
GAME DEVELOPMENT TUTORIAL: DAY 3-7: Health System
Welcome to Day 7. If you are reading this, you survived the apocalypse of 2012.
In today's lesson, we will be discussing the following:
If you are starting with this lesson, feel free to grab the source code at the end of Day 6!
Note: If you downloaded the source code before 1:40 AM (EST UTC -5) on 12/22/2012, there was a small mistake in there that you will have to correct. More on that below.
In today's lesson, we will be discussing the following:
- Incorporating a basic health system for the enemies.
- Handling player/enemy death.
If you are starting with this lesson, feel free to grab the source code at the end of Day 6!
Note: If you downloaded the source code before 1:40 AM (EST UTC -5) on 12/22/2012, there was a small mistake in there that you will have to correct. More on that below.
Correcting Day 6 Source Code
This is only applies to you if you read Day 6's lesson on 12/21/12
I made a mistake when writing the code for Day 6. In the update() method of the Projectile class, there was this if statement:
if (x < 810){
checkCollision();
}
Please change the condition to this:
if (x < 800){
checkCollision();
}
Or if you prefer, just download and import this project file (delete your original, with "delete project contents on disk" checked.
I made a mistake when writing the code for Day 6. In the update() method of the Projectile class, there was this if statement:
if (x < 810){
checkCollision();
}
Please change the condition to this:
if (x < 800){
checkCollision();
}
Or if you prefer, just download and import this project file (delete your original, with "delete project contents on disk" checked.

unit3day6corrected.zip | |
File Size: | 398 kb |
File Type: | zip |
Now that we are on the same page, let's talk about death.
Player Death
Virtually all games incorporate a failure system. In platform games, this failure tends to be death, which makes you restart the level.
When the player falls through the gaps in our game, we want to show the death screen.
Here's how we will approach it.
1. Create a Game State variable that keeps track of the current state of the game (running or dead).
2. Use an if statement to call different portions of the update() and paint() methods depending on the current state.
3. If the player's Y variable goes beyond the bottom of the screen (meaning that the player has fallen through the gaps and the robot is no longer visible), change the state to "dead" and show the death screen.
Let's begin.
When the player falls through the gaps in our game, we want to show the death screen.
Here's how we will approach it.
1. Create a Game State variable that keeps track of the current state of the game (running or dead).
2. Use an if statement to call different portions of the update() and paint() methods depending on the current state.
3. If the player's Y variable goes beyond the bottom of the screen (meaning that the player has fallen through the gaps and the robot is no longer visible), change the state to "dead" and show the death screen.
Let's begin.
1. Creating Game States - Enum types
1. We start by creating an enum a type that consists of constants (fixed variables).
In the StartingClass, add the following in bold:
// More Code Precedes
public class StartingClass extends Applet implements Runnable, KeyListener {
enum GameState {
Running, Dead
}
GameState state = GameState.Running;
// More Code Follows
Doing this creates an enum called GameState that comprises the two states Running and Dead.
We then initially set the state to be "Running."
2. Next, go down to the run() method. We will be surrounding everything in the run() method with an if statement like below, and we will also create the if statement that will cause the state to change to "Dead":
This ensures that the game will stop updating when the state changes to "Dead."
In the StartingClass, add the following in bold:
// More Code Precedes
public class StartingClass extends Applet implements Runnable, KeyListener {
enum GameState {
Running, Dead
}
GameState state = GameState.Running;
// More Code Follows
Doing this creates an enum called GameState that comprises the two states Running and Dead.
We then initially set the state to be "Running."
2. Next, go down to the run() method. We will be surrounding everything in the run() method with an if statement like below, and we will also create the if statement that will cause the state to change to "Dead":
This ensures that the game will stop updating when the state changes to "Dead."
@Override
public void run() {
if (state == GameState.Running) {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false
&& robot.isDucked() == false) {
currentSprite = anim.getImage();
}
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);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (robot.getCenterY() > 500) {
state = GameState.Dead;
}
}
}
}
public void run() {
if (state == GameState.Running) {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false
&& robot.isDucked() == false) {
currentSprite = anim.getImage();
}
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);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (robot.getCenterY() > 500) {
state = GameState.Dead;
}
}
}
}
3. Similarly, we make the following changes to the paint() method, enclosing all the statements inside an if statement and adding another if statement (changes in bold):
@Override
public void paint(Graphics g) {
if (state == GameState.Running) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
paintTiles(g);
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(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48, this);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48, this);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(score), 740, 30);
} else if (state == GameState.Dead) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.WHITE);
g.drawString("Dead", 360, 240);
}
}
public void paint(Graphics g) {
if (state == GameState.Running) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
paintTiles(g);
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(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48, this);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48, this);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(score), 740, 30);
} else if (state == GameState.Dead) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.WHITE);
g.drawString("Dead", 360, 240);
}
}
That's it! Now when you jump off a cliff, you will die.
Where to go from here:
If you want an exercise, you should try creating a Restart system (or you can wait until we do it next week in Android).
Tips:
1. Nullify every variable.
2. Create a restart method that calls all the methods you need to get the game running again.
3. Use if statements to check the state of the game to let certain keyboard buttons restart the game only when the player is dead.
Where to go from here:
If you want an exercise, you should try creating a Restart system (or you can wait until we do it next week in Android).
Tips:
1. Nullify every variable.
2. Create a restart method that calls all the methods you need to get the game running again.
3. Use if statements to check the state of the game to let certain keyboard buttons restart the game only when the player is dead.
ENEMY HEALTH/DEATH
1. cREATING THE HEALTH sYSTEM
Open up the Enemy class. When we first created this class, we added some variables that we intended to keep track of health with, but they will not be needed anymore. So do as follows:
1. Remove the variables: maxHealth, currentHealth from the variable declarations section.
This will give you four errors (from two getter methods that use the two variables, and the two setter methods that also use the two variables).
2. Erase these four methods that are giving you errors. They should be: getMaxHealth(), getCurrentHealth(), setMaxHealth(), and setCurrentHealth().
3. Finally, simply create a public int named health and initialize it with a value of 5 (shown below):
// More code precedes
public class Enemy {
private int power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();
public Rectangle r = new Rectangle(0, 0, 0, 0);
public int health = 5;
// More code follows
1. Remove the variables: maxHealth, currentHealth from the variable declarations section.
This will give you four errors (from two getter methods that use the two variables, and the two setter methods that also use the two variables).
2. Erase these four methods that are giving you errors. They should be: getMaxHealth(), getCurrentHealth(), setMaxHealth(), and setCurrentHealth().
3. Finally, simply create a public int named health and initialize it with a value of 5 (shown below):
// More code precedes
public class Enemy {
private int power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();
public Rectangle r = new Rectangle(0, 0, 0, 0);
public int health = 5;
// More code follows
2. Dealing Damage & Death
Open up the Projectile class. We will now be using this health system to deal damage to the enemy.
When we port this game to Android, we will be using an ArrayList to keep track of our enemies. Since for now, we only have two enemies, we will just rewrite the conditions twice.
All the changes we need to make will be within the checkCollision() method of the Projectile class.
The thought process is as follows: When the bullet hits the enemy: If health is greater than 0, subtract 1 health. If the health is 0, kill the enemy (by moving him off screen).
The checkCollision() method below handles this the way we want it:
When we port this game to Android, we will be using an ArrayList to keep track of our enemies. Since for now, we only have two enemies, we will just rewrite the conditions twice.
All the changes we need to make will be within the checkCollision() method of the Projectile class.
The thought process is as follows: When the bullet hits the enemy: If health is greater than 0, subtract 1 health. If the health is 0, kill the enemy (by moving him off screen).
The checkCollision() method below handles this the way we want it:
private void checkCollision() {
if(r.intersects(StartingClass.hb.r)){
visible = false;
if (StartingClass.hb.health > 0) {
StartingClass.hb.health -= 1;
}
if (StartingClass.hb.health == 0) {
StartingClass.hb.setCenterX(-100);
StartingClass.score += 5;
}
}
if (r.intersects(StartingClass.hb2.r)){
visible = false;
if (StartingClass.hb2.health > 0) {
StartingClass.hb2.health -= 1;
}
if (StartingClass.hb2.health == 0) {
StartingClass.hb2.setCenterX(-100);
StartingClass.score += 5;
}
}
}
if(r.intersects(StartingClass.hb.r)){
visible = false;
if (StartingClass.hb.health > 0) {
StartingClass.hb.health -= 1;
}
if (StartingClass.hb.health == 0) {
StartingClass.hb.setCenterX(-100);
StartingClass.score += 5;
}
}
if (r.intersects(StartingClass.hb2.r)){
visible = false;
if (StartingClass.hb2.health > 0) {
StartingClass.hb2.health -= 1;
}
if (StartingClass.hb2.health == 0) {
StartingClass.hb2.setCenterX(-100);
StartingClass.score += 5;
}
}
}
That's all the code we need to change for today. Scroll down for source code and more information about Unit 4!
Completed Classes, end of Day 7
StartingClass
package kiloboltgame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import kiloboltgame.framework.Animation;
public class StartingClass extends Applet implements Runnable, KeyListener {
enum GameState {
Running, Dead
}
GameState state = GameState.Running;
private static Robot robot;
public static Heliboy hb, hb2;
public static int score = 0;
private Font font = new Font(null, Font.BOLD, 30);
private Image image, currentSprite, character, character2, character3,
characterDown, characterJumped, background, heliboy, heliboy2,
heliboy3, heliboy4, heliboy5;
public static Image tilegrassTop, tilegrassBot, tilegrassLeft,
tilegrassRight, tiledirt;
private Graphics second;
private URL base;
private static Background bg1, bg2;
private Animation anim, hanim;
private ArrayList<Tile> tilearray = new ArrayList<Tile>();
@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");
character2 = getImage(base, "data/character2.png");
character3 = getImage(base, "data/character3.png");
characterDown = getImage(base, "data/down.png");
characterJumped = getImage(base, "data/jumped.png");
heliboy = getImage(base, "data/heliboy.png");
heliboy2 = getImage(base, "data/heliboy2.png");
heliboy3 = getImage(base, "data/heliboy3.png");
heliboy4 = getImage(base, "data/heliboy4.png");
heliboy5 = getImage(base, "data/heliboy5.png");
background = getImage(base, "data/background.png");
tiledirt = getImage(base, "data/tiledirt.png");
tilegrassTop = getImage(base, "data/tilegrasstop.png");
tilegrassBot = getImage(base, "data/tilegrassbot.png");
tilegrassLeft = getImage(base, "data/tilegrassleft.png");
tilegrassRight = getImage(base, "data/tilegrassright.png");
anim = new Animation();
anim.addFrame(character, 1250);
anim.addFrame(character2, 50);
anim.addFrame(character3, 50);
anim.addFrame(character2, 50);
hanim = new Animation();
hanim.addFrame(heliboy, 100);
hanim.addFrame(heliboy2, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy5, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy2, 100);
currentSprite = anim.getImage();
}
@Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
robot = new Robot();
// Initialize Tiles
try {
loadMap("data/map1.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
Thread thread = new Thread(this);
thread.start();
}
private void loadMap(String filename) throws IOException {
ArrayList lines = new ArrayList();
int width = 0;
int height = 0;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (true) {
String line = reader.readLine();
// no more lines to read
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("!")) {
lines.add(line);
width = Math.max(width, line.length());
}
}
height = lines.size();
for (int j = 0; j < 12; j++) {
String line = (String) lines.get(j);
for (int i = 0; i < width; i++) {
if (i < line.length()) {
char ch = line.charAt(i);
Tile t = new Tile(i, j, Character.getNumericValue(ch));
tilearray.add(t);
}
}
}
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
if (state == GameState.Running) {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = anim.getImage();
}
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);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (robot.getCenterY() > 500) {
state = GameState.Dead;
}
}}
}
public void animate() {
anim.update(10);
hanim.update(50);
}
@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) {
if (state == GameState.Running) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
paintTiles(g);
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(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48, this);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48, this);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(score), 740, 30);
} else if (state == GameState.Dead) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.WHITE);
g.drawString("Dead", 360, 240);
}
}
private void updateTiles() {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
t.update();
}
}
private void paintTiles(Graphics g) {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY(), 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.isReadyToFire()) {
robot.shoot();
robot.setReadyToFire(false);
}
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 = anim.getImage();
robot.setDucked(false);
break;
case KeyEvent.VK_LEFT:
robot.stopLeft();
break;
case KeyEvent.VK_RIGHT:
robot.stopRight();
break;
case KeyEvent.VK_SPACE:
break;
case KeyEvent.VK_CONTROL:
robot.setReadyToFire(true);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
public static Background getBg2() {
return bg2;
}
public static Robot getRobot() {
return robot;
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import kiloboltgame.framework.Animation;
public class StartingClass extends Applet implements Runnable, KeyListener {
enum GameState {
Running, Dead
}
GameState state = GameState.Running;
private static Robot robot;
public static Heliboy hb, hb2;
public static int score = 0;
private Font font = new Font(null, Font.BOLD, 30);
private Image image, currentSprite, character, character2, character3,
characterDown, characterJumped, background, heliboy, heliboy2,
heliboy3, heliboy4, heliboy5;
public static Image tilegrassTop, tilegrassBot, tilegrassLeft,
tilegrassRight, tiledirt;
private Graphics second;
private URL base;
private static Background bg1, bg2;
private Animation anim, hanim;
private ArrayList<Tile> tilearray = new ArrayList<Tile>();
@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");
character2 = getImage(base, "data/character2.png");
character3 = getImage(base, "data/character3.png");
characterDown = getImage(base, "data/down.png");
characterJumped = getImage(base, "data/jumped.png");
heliboy = getImage(base, "data/heliboy.png");
heliboy2 = getImage(base, "data/heliboy2.png");
heliboy3 = getImage(base, "data/heliboy3.png");
heliboy4 = getImage(base, "data/heliboy4.png");
heliboy5 = getImage(base, "data/heliboy5.png");
background = getImage(base, "data/background.png");
tiledirt = getImage(base, "data/tiledirt.png");
tilegrassTop = getImage(base, "data/tilegrasstop.png");
tilegrassBot = getImage(base, "data/tilegrassbot.png");
tilegrassLeft = getImage(base, "data/tilegrassleft.png");
tilegrassRight = getImage(base, "data/tilegrassright.png");
anim = new Animation();
anim.addFrame(character, 1250);
anim.addFrame(character2, 50);
anim.addFrame(character3, 50);
anim.addFrame(character2, 50);
hanim = new Animation();
hanim.addFrame(heliboy, 100);
hanim.addFrame(heliboy2, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy5, 100);
hanim.addFrame(heliboy4, 100);
hanim.addFrame(heliboy3, 100);
hanim.addFrame(heliboy2, 100);
currentSprite = anim.getImage();
}
@Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(2160, 0);
robot = new Robot();
// Initialize Tiles
try {
loadMap("data/map1.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
Thread thread = new Thread(this);
thread.start();
}
private void loadMap(String filename) throws IOException {
ArrayList lines = new ArrayList();
int width = 0;
int height = 0;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (true) {
String line = reader.readLine();
// no more lines to read
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("!")) {
lines.add(line);
width = Math.max(width, line.length());
}
}
height = lines.size();
for (int j = 0; j < 12; j++) {
String line = (String) lines.get(j);
for (int i = 0; i < width; i++) {
if (i < line.length()) {
char ch = line.charAt(i);
Tile t = new Tile(i, j, Character.getNumericValue(ch));
tilearray.add(t);
}
}
}
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void run() {
if (state == GameState.Running) {
while (true) {
robot.update();
if (robot.isJumped()) {
currentSprite = characterJumped;
} else if (robot.isJumped() == false && robot.isDucked() == false) {
currentSprite = anim.getImage();
}
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);
}
}
updateTiles();
hb.update();
hb2.update();
bg1.update();
bg2.update();
animate();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (robot.getCenterY() > 500) {
state = GameState.Dead;
}
}}
}
public void animate() {
anim.update(10);
hanim.update(50);
}
@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) {
if (state == GameState.Running) {
g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
paintTiles(g);
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(hanim.getImage(), hb.getCenterX() - 48,
hb.getCenterY() - 48, this);
g.drawImage(hanim.getImage(), hb2.getCenterX() - 48,
hb2.getCenterY() - 48, this);
g.setFont(font);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(score), 740, 30);
} else if (state == GameState.Dead) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.WHITE);
g.drawString("Dead", 360, 240);
}
}
private void updateTiles() {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
t.update();
}
}
private void paintTiles(Graphics g) {
for (int i = 0; i < tilearray.size(); i++) {
Tile t = (Tile) tilearray.get(i);
g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY(), 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.isReadyToFire()) {
robot.shoot();
robot.setReadyToFire(false);
}
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 = anim.getImage();
robot.setDucked(false);
break;
case KeyEvent.VK_LEFT:
robot.stopLeft();
break;
case KeyEvent.VK_RIGHT:
robot.stopRight();
break;
case KeyEvent.VK_SPACE:
break;
case KeyEvent.VK_CONTROL:
robot.setReadyToFire(true);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
public static Background getBg2() {
return bg2;
}
public static Robot getRobot() {
return robot;
}
}
Enemy Class
package kiloboltgame;
import java.awt.Rectangle;
public class Enemy {
private int power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();
public Rectangle r = new Rectangle(0,0,0,0);
public int health = 5;
// Behavioral Methods
public void update() {
centerX += speedX;
speedX = bg.getSpeedX()*5;
r.setBounds(centerX - 25, centerY-25, 50, 60);
if (r.intersects(Robot.yellowRed)){
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(Robot.rect) || r.intersects(Robot.rect2) || r.intersects(Robot.rect3) || r.intersects(Robot.rect4)){
System.out.println("collision");
}
}
public void die() {
}
public void attack() {
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
}
import java.awt.Rectangle;
public class Enemy {
private int power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();
public Rectangle r = new Rectangle(0,0,0,0);
public int health = 5;
// Behavioral Methods
public void update() {
centerX += speedX;
speedX = bg.getSpeedX()*5;
r.setBounds(centerX - 25, centerY-25, 50, 60);
if (r.intersects(Robot.yellowRed)){
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(Robot.rect) || r.intersects(Robot.rect2) || r.intersects(Robot.rect3) || r.intersects(Robot.rect4)){
System.out.println("collision");
}
}
public void die() {
}
public void attack() {
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
}
Projectile Class
package kiloboltgame;
import java.awt.Rectangle;
public class Projectile {
private int x, y, speedX;
private boolean visible;
private Rectangle r;
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
r = new Rectangle(0, 0, 0, 0);
}
public void update(){
x += speedX;
r.setBounds(x, y, 10, 5);
if (x > 800){
visible = false;
r = null;
}
if (x < 800){
checkCollision();
}
}
private void checkCollision() {
if(r.intersects(StartingClass.hb.r)){
visible = false;
if (StartingClass.hb.health > 0) {
StartingClass.hb.health -= 1;
}
if (StartingClass.hb.health == 0) {
StartingClass.hb.setCenterX(-100);
StartingClass.score += 5;
}
}
if (r.intersects(StartingClass.hb2.r)){
visible = false;
if (StartingClass.hb2.health > 0) {
StartingClass.hb2.health -= 1;
}
if (StartingClass.hb2.health == 0) {
StartingClass.hb2.setCenterX(-100);
StartingClass.score += 5;
}
}
}
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;
}
}
import java.awt.Rectangle;
public class Projectile {
private int x, y, speedX;
private boolean visible;
private Rectangle r;
public Projectile(int startX, int startY){
x = startX;
y = startY;
speedX = 7;
visible = true;
r = new Rectangle(0, 0, 0, 0);
}
public void update(){
x += speedX;
r.setBounds(x, y, 10, 5);
if (x > 800){
visible = false;
r = null;
}
if (x < 800){
checkCollision();
}
}
private void checkCollision() {
if(r.intersects(StartingClass.hb.r)){
visible = false;
if (StartingClass.hb.health > 0) {
StartingClass.hb.health -= 1;
}
if (StartingClass.hb.health == 0) {
StartingClass.hb.setCenterX(-100);
StartingClass.score += 5;
}
}
if (r.intersects(StartingClass.hb2.r)){
visible = false;
if (StartingClass.hb2.health > 0) {
StartingClass.hb2.health -= 1;
}
if (StartingClass.hb2.health == 0) {
StartingClass.hb2.setCenterX(-100);
StartingClass.score += 5;
}
}
}
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;
}
}
As this game was completely open-ended when the series began, there are many flaws with the way the game works; however, we can use this version as a prototype when we port to Android, and turn it into a better organized game with a cohesive system that allows for easy modification. We will be doing that as much as we can throughout Unit 4.
Thanks for reading, and remember to Like us on Facebook to stay updated.
Thanks for reading, and remember to Like us on Facebook to stay updated.
|
|

unit3day7.zip | |
File Size: | 400 kb |
File Type: | zip |