You are viewing our Forum Archives. To view or take place in current topics click here.
Chase Arcade Game
Posted:

Chase Arcade GamePosted:

-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
I've been making a little arcade game as something to practice my Java skills (I started 2 weeks ago).
It could be improved greatly. If you have any tips on how to do so, feel free to suggest stuff.

[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]

Just double click the jar file to play.
Let me know what you think and post your highscores!

Source Code:

MyGame.java

import com.golden.gamedev.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

public class MyGame extends Game
{
   private Hero hero;
   private Monster monster;
   private Monster monster2;
   private Monster monster3;
   private Monster monster4;
   private Coin coin;
   
    public void initResources()
    {
       hero = new Hero();
       monster = new Monster("1");
       monster2 = new Monster("2");
       monster3 = new Monster("3");
       monster4 = new Monster("4");
       coin = new Coin();
    }
   
    public void update(long elapsedTime)
    {
       
       if(keyDown(KeyEvent.VK_LEFT) || keyDown(KeyEvent.VK_A) || keyDown(KeyEvent.VK_NUMPAD4)) {
          hero.left();
       } else if (keyDown(KeyEvent.VK_RIGHT) || keyDown(KeyEvent.VK_D) || keyDown(KeyEvent.VK_NUMPAD6)) {
          hero.right();
       } else if (keyDown(KeyEvent.VK_UP) || keyDown(KeyEvent.VK_W) || keyDown(KeyEvent.VK_NUMPAD8)) {
          hero.up();
       } else if (keyDown(KeyEvent.VK_DOWN) || keyDown(KeyEvent.VK_S) || keyDown(KeyEvent.VK_NUMPAD2)) {
          hero.down();
       }

       monster.chase(hero);
       monster2.chase(hero);
       monster3.chase(hero);
       monster4.chase(hero);
       
       if (monster.eaten(hero) || monster2.eaten(hero) || monster3.eaten(hero) || monster4.eaten(hero)) {
          hero.loseLife();
          hero.respawn();
          monster = null;
          monster2 = null;
          monster3 = null;
          monster4 = null;
          monster = new Monster("1");
           monster2 = new Monster("2");
           monster3 = new Monster("3");
           monster4 = new Monster("4");
       }
       
       if (hero.getLives() == 0) {
          gameOver();
       }
       
       if (coin.collectCoin(hero)) {
          hero.increaseScore();
          coin = null;
          coin = new Coin();
       }
    }

    private void gameOver() {
       System.out.println("Game Over");
       String message = "Final Score: " + hero.getScore();
      infoBox(message, "Game Over");
      System.exit(0);
    }
   
    public void render(Graphics2D g)
    {
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, getWidth(), getHeight());
       hero.draw(g);
       monster.draw(g);
       monster2.draw(g);
       monster3.draw(g);
       monster4.draw(g);
       coin.draw(g);
    }
   
    public void infoBox(String infoMessage, String titleBar) {
        JOptionPane.showMessageDialog(null, infoMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
    }
   
    public static void main (String args[])
    {
       System.out.println("Game development environment OK! ");
       
       GameLoader gameLoader = new GameLoader();
       MyGame myGame = new MyGame();
       gameLoader.setup(myGame, new Dimension(640,480), false);
       gameLoader.start();
    }
}


Hero.java

import java.awt.Color;
import java.awt.Graphics2D;

public class Hero extends MyGame {

   private String name;
   private int score, lives, x, y;
   
   public Hero() {
      lives = 3;
      score = 0;
      x = 320;
      y = 240;
      System.out.println("Hero created");
   }
   
   public void loseLife() { // Decrement the lives.
      lives--;
      System.out.println("OUCH!");
   }
   
   public void increaseScore() { // Increase score by 10.
      score++;
      System.out.println("Coin collected: +10 points!");
   }
   
   public String toString() { // Print name, score and lives remaining for the hero.
      System.out.println("Name: " + name);
      System.out.println("Score: " + score);
      System.out.println("Lives: " + lives);
      System.out.println("");
      return "";
   }
   
   public boolean isAlive() {
      if (lives > 0) {
         return true;
      } else {
         return false;
      }
   }
   
   public void draw(Graphics2D g) {
      g.setColor(Color.GREEN);
       g.fillOval(x, y, 10, 10);
   }
   
   public void respawn() {
      System.out.println("Lives remaining: " + lives);
      x = 320;
      y = 240;
   }
   
   public void left() {
      if (x > 0) {
         x -= 2;
      }
   }
   
   public void right() {
      if (x < 630) {
         x += 2;
      }
   }
   
   public void up() {
      if (y > 0) {
         y -= 2;
      }
   }
   
   public void down() {
      if (y < 470) {
         y += 2;
      }
   }
   
// Accessor Methods
   public int getScore() {
      return score;
   }
   
   public int getLives() {
      return lives;
   }
   
   public int getX() {
      return x;
   }
   
   public int getY() {
      return y;
   }
}


Monster.java

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;

public class Monster extends MyGame {
   private int x, y;
   private String name;
   Random rand = new Random();
   
   public Monster(String nameIn) {
      x = rand.nextInt(640);
      y = rand.nextInt(480);
      name = nameIn;
   }
   
   public void chase(Hero hero) {
      if (name == "1" || name == "2") {
         if (hero.getX() < x) {
            x--;
         } else if (hero.getX() > x) {
            x++;
         } else if (hero.getY() < y) {
            y--;
         } else if (hero.getY() > y) {
            y++;
         }
      } else if (hero.getY() < y) {
         y--;
      } else if (hero.getY() > y) {
         y++;
      } else if (hero.getX() < x) {
         x--;
      } else if (hero.getX() > x) {
         x++;
      }
   }
   
   public boolean eaten(Hero hero) {
      if (x == hero.getX() && y == hero.getY()){
         return true;
      } else {
         return false;
      }
   }

   public void draw(Graphics2D g) {
      g.setColor(Color.RED);
      g.fillOval(x, y, 10, 10);
   }
// Accessor Methods
   public int getX() {
      return x;
   }
   
   public int getY() {
      return y;
   }
}


Coin.java

import java.awt.*;
import java.util.Random;

public class Coin extends MyGame {
   
   private int x, y;
   private Random rand = new Random();
   
   public Coin() {
      x = rand.nextInt(640);
      y = rand.nextInt(480);   
   }
   
   public void draw(Graphics2D g) {
      g.setColor(Color.YELLOW);
       g.fillOval(x, y, 10, 10);
   }
   
   public boolean collectCoin(Hero hero) {
      if ((hero.getX() >= x - 10) && (hero.getX() <= x + 10)) {
          if ((hero.getY() >= y - 10) && (hero.getY() <= y + 10)) {
             return true;
          }
       }
      return false;
   }
   
   public int getX() {
      return x;
   }
   
   public int getY() {
      return y;
   }
}


[ Register or Signin to view external links. ]


Last edited by -Deano ; edited 1 time in total

The following 1 user thanked -Deano for this useful post:

Skittle (02-10-2016)
#2. Posted:
Skittle
  • V5 Launch
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
Status: Offline
Joined: Aug 20, 20149Year Member
Posts: 6,813
Reputation Power: 413
Fun little game to play, nice job!

One thing I would like to see would be a menu to start a game rather than it starting straight away, and when you lose it returns to the menu rather than closing the application.
#3. Posted:
9nty
  • TTG Elite
Status: Offline
Joined: Jan 26, 20168Year Member
Posts: 11,790
Reputation Power: 11040
Motto: 9nty.gg
Motto: 9nty.gg
Status: Offline
Joined: Jan 26, 20168Year Member
Posts: 11,790
Reputation Power: 11040
Motto: 9nty.gg
It said on my end this file is harmful to download, any other way to download this safely?
#4. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
F4D wrote It said on my end this file is harmful to download, any other way to download this safely?


Your browser will say it can be harmful because it is a .jar file. These are similar to .exe files except that it has all the project files compacted within. The virustotal link shows that there is no problem with the file.

If you really don't trust it then feel free to read through the source code. Other than that there's no other solution I can think of tbh.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.