i dont know what specific title should i post for this topic, its a typing game that I'm writing at the moment.. the codes that i will post here is just the original code where it came from, well any way ill drop the codes first. it consist of 6 classes and i need to included for the program to run properly except some 3-4 images file to show the entire game, its not actually the game that im writing.. i just modify it (returned to its original form) to make it short as much as possible.
Alien.Class
import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; public class Alien { private int x; private int y; private int width; private int height; private boolean visible; private Image image; public Alien(int x, int y) { ImageIcon ii = new ImageIcon("C:\\Space\\alien.png"); image = ii.getImage(); width = image.getWidth(null); height = image.getHeight(null); visible = true; this.x = x; this.y = y; } public void move() { if (x < 0) { x = 1400; } x -= .01; } public int getX() { return x; } public int getY() { return y; } public boolean isVisible() { return visible; } public void setVisible(Boolean visible) { this.visible = visible; } public Image getImage() { return image; } public Rectangle getBounds() { return new Rectangle(x, y, width, height); } }
Craft.class
import java.awt.Image; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.util.ArrayList; import javax.swing.ImageIcon; public class Craft { private int dx; private int x; private int width; private int height; private boolean visible; private Image image; private ArrayList missiles; public Craft() { ImageIcon ii = new ImageIcon("C:\\Space\\spaceCraft.png"); image = ii.getImage(); width = image.getWidth(null); height = image.getHeight(null); missiles = new ArrayList(); visible = true; x = 40; } public void move() { x += dx; if (x < 1) { x = 1; } } public int getX() { return x; } public Image getImage() { return image; } public ArrayList getMissiles() { return missiles; } public void setVisible(boolean visible) { this.visible = visible; } public boolean isVisible() { return visible; } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_SPACE) { fire(); } } public void fire() { missiles.add(new Missile(x + width, 59)); } }
Missile.class
import java.awt.Image; import java.awt.Rectangle; import javax.swing.ImageIcon; public class Missile { private int x, y; private Image image; boolean visible; private int width, height; private final int BOARD_WIDTH = 900; private final int MISSILE_SPEED = 15; public Missile(int x, int y) { ImageIcon ii = new ImageIcon("C:\\Space\\missile.png"); image = ii.getImage(); visible = true; width = image.getWidth(null); height = image.getHeight(null); this.x = x; this.y = y; } public Image getImage() { return image; } public int getX() { return x; } public int getY() { return y; } public boolean isVisible() { return visible; } public void setVisible(Boolean visible) { this.visible = visible; } public Rectangle getBounds() { return new Rectangle(x, y, width, height); } public void move() { x += MISSILE_SPEED; if (x > BOARD_WIDTH) { visible = false; } } }
Collision.class
import javax.swing.JFrame; /** * * @author zab */ public class Collision extends JFrame { public Collision() { add(new Board()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(550, 300); setLocationRelativeTo(null); setTitle("Collision"); setResizable(false); setVisible(true); } public static void main(String[] args) { new Collision(); } }
Board.class (where the whole game logic goes)
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.Timer; public class Board extends JPanel implements ActionListener { private Timer timer; private Craft craft; private ArrayList alienList; private Image bg; private Character charac; private String userTyped; private RandomWords rWords; private int[][] alienPosition = { {450, 59}, {400, 59}, {500, 59}, }; public Board() { addKeyListener(new TAdapter()); setFocusable(true); setBackground(Color.BLACK); setSize(400, 300); craft = new Craft(); rWords = new RandomWords(); userTyped = ""; timer = new Timer(15, this); timer.start(); initAliens(); } public void initAliens() { alienList = new ArrayList(); for (int i = 0; i < alienPosition.length; i++ ) { alienList.add(new Alien(alienPosition[i][0], alienPosition[i][1])); } } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(bg, 0, 0, null); g2d.setColor(Color.WHITE); g2d.drawString("Type the Word and Press Space Bar To Shoot, Press Back space to restart the word", 0, 10); g2d.drawImage(craft.getImage(), craft.getX(), 59, this); ArrayList ms = craft.getMissiles(); // draws the missiles for (int i = 0; i < ms.size(); i++) { Missile m = (Missile) ms.get(i); g2d.drawImage(m.getImage(), m.getX(), m.getY(), this); } //draws the aliens for (int i = 0; i < alienList.size(); i++) { Alien alien = (Alien) alienList.get(i); if (alien.isVisible()) { g2d.drawImage(alien.getImage(), alien.getX(), alien.getY(), this); g2d.setColor(Color.WHITE); g2d.drawString(rWords.getWord(), alien.getX(), alien.getY()); } } g2d.setFont(new Font("Monospaced", Font.BOLD, 15)); g2d.drawString(userTyped, 10, 30); } public void actionPerformed(ActionEvent e) { ArrayList missileList = craft.getMissiles(); // movement of missiles for (int i = 0; i < missileList.size(); i++) { Missile missile = (Missile) missileList.get(i); if (missile.isVisible()) { missile.move(); } else { missileList.remove(i); } } // movement of aliens for (int i = 0; i < alienList.size(); i++) { Alien alien = (Alien) alienList.get(i); if (alien.isVisible()) { alien.move(); } else { alienList.remove(i); } } craft.move(); checkCollisions(); repaint(); } public void checkCollisions() { ArrayList missileList = craft.getMissiles(); for (int i = 0; i < missileList.size(); i++) { Missile missile = (Missile) missileList.get(i); Rectangle r1 = missile.getBounds(); for (int j = 0; j<alienList.size(); j++) { Alien a = (Alien) alienList.get(j); Rectangle r2 = a.getBounds(); if (r1.intersects(r2)) { missile.setVisible(false); a.setVisible(false); rWords.generateWord(); userTyped = ""; } } } } private class TAdapter extends KeyAdapter { @Override public void keyReleased(KeyEvent e) { if (userTyped.equals(rWords.getWord()) && e.getKeyCode() == KeyEvent.VK_SPACE) { craft.keyPressed(e); } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { userTyped = ""; charac = null; } } @Override public void keyPressed(KeyEvent e) { if (userTyped.equals(rWords.getWord()) && e.getKeyCode() == KeyEvent.VK_SPACE) { craft.keyPressed(e); } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { userTyped = ""; charac = null; } } public void keyTyped(KeyEvent e) { charac = e.getKeyChar(); userTyped = userTyped + charac; } } }
RandomWords.class
public class RandomWords { private String temp[] = {"as", "qe", "uu"}; private String word; public RandomWords() { generateWord(); } public String getWord() { return word; } public void generateWord() { int randomWord = (int) Math.round(Math.random() * 2); word = temp[randomWord]; } public void clearWord() { word = ""; } }
although its a very long code.. the only focus of my problem is on the Board class where all the game logic happens, and RandomWords class that supports the idea of my problem
if you run the program assuming that you already got the images to see whats happening, you can see that you can only fire a missile to an enemy if you type the words brought by the enemies
actually the program runs almost exactly what i want..
The only problem is .. Im stuck on a part where i need to show the words one by one,
the program shows all the words that i needed to type, in this case when you run the program.. the 3 words are already shown... together with the 3 enemies(aliens). What i want is when the program runs.. the first enemy will only show the first word.. the 2 will not (YET)
until the first word(enemy) will be shot down.. then the second word will appear .. then so on
i tried to use some boolean (shouldPaint) switching to true or false. everytime the craft shoots the enemy.. but i just messing it up.. i need help with this to continue on my next step on the game that im writing..
i know its too long.. but this is as far as i can do to make it short as possible..
only the Board class.. the rest doesnt have anything to do with the problem.. i just included it to make the program run... and sorry for the bad english