Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Question Space Invaders - Help with modifying Java code

    I've searched about Space Invaders in Java Forums but didn't find a post that could help me.
    I've been following a tutorial and written the code needed from there. But this game is far
    from the orgianal Space Invaders game and I want to be able to:

    Tutorial link (java code here): Java games Space Invaders
    * The player should have 3 lives
    * The aliens in the different rows should give points (10, 20, 30, 40p)
    * It should exist four shelters to protect the player
    * (Extra) A red UFO should be flying around sometimes

    The game that I'm trying to program can be found here: SPACE INVADERS, play the original old school arcade game

    So I'm wondering if I somehow could get some help or clues of how to do this? I'm using the code
    in the tutorial.

    My code for the Board (which is the class handling most of the game) is here:

    Board.java
    Java Code:
    import java.awt.Component;
    import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Font; 
    import java.awt.FontMetrics; 
    import java.awt.Graphics; 
    import java.awt.Toolkit; 
    import java.awt.event.KeyAdapter; 
    import java.awt.event.KeyEvent; 
    
    import java.util.ArrayList; 
    import java.util.Iterator; 
    import java.util.Random; 
    
    import javax.swing.ImageIcon; 
    import javax.swing.JPanel;
    
    public class Board extends JPanel implements Runnable, Commons {
    	
    	private Dimension d; 
    	private ArrayList<Object> aliens; 
    	private Player player;
    	private Shot shot;
    	private Shelter shelter;
    	
    	private int alienX = 150; 
    	private int alienY = 5; 
    	private int direction = -1; 
    	private int deaths = 0; 
    	
    	private boolean inGame = true; 
    	private final String explosion = "sprites/explosion.png";
    	private final String alienPix = "sprites/alien.gif"; 
    	private String message = "Game Over";
    	
    	private Thread animator;
    	
    	public Board() {
    		this.addKeyListener(new TAdapter());
    		this.setFocusable(true);
    		
    		d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); 
    		this.setBackground(Color.BLACK);
    	}
    	
    	public void addNotify() {
    		super.addNotify();
    		gameInit();
    	}
    	
    	public void gameInit() {
    		aliens = new ArrayList<>();
    		
    		ImageIcon ii = new ImageIcon(this.getClass().getResource(alienPix));
    		
    		for (int i = 0; i < 4; i++) {
    			for (int j = 0; j < 6; j++) {
    				Alien alien = new Alien(alienX + 60 * j, alienY + 60 * i); 
    				alien.setImage(ii.getImage());
    				aliens.add(alien);
    			}
    		}
    		
    		player = new Player(); 
    		shot = new Shot();
    		shelter = new Shelter(600, 600);
    		
    		if (animator == null || !inGame) {
    			animator = new Thread(this); 
    			animator.start(); 
    		}
    	}
    	
    	// Draw the aliens
    	public void drawAliens(Graphics g) {
    		Iterator<Object> it = aliens.iterator(); 
    		
    		while (it.hasNext()) {
    			Alien alien = (Alien)it.next();
    			
    			if (alien.isVisible()) {
    				g.drawImage(alien.getImage(), alien.getX(), alien.getY(), this); 
    			}
    			
    			if (alien.isDying()) {
    				alien.die();
    			}
    		}
    	}
    	
    	// Draw the player
    	public void drawPlayer(Graphics g) {
    		if (player.isVisible()) {
    			g.drawImage(player.getImage(), player.getX(), player.getY(), this); 
    		}
    		
    		if (player.isDying()) {
    			player.die();
    			inGame = false;
    		}
    	}
    	
    	// Draw the shot
    	public void drawShot(Graphics g) {
    		if (shot.isVisible()) {
    			g.drawImage(shot.getImage(), shot.getX(), shot.getY(), this);
    		}
    	}
    	
    	// Draw the bombs
    	public void drawBombing(Graphics g) {
    		Iterator<Object> i3 = aliens.iterator(); 
    		
    		while (i3.hasNext()) {
    			Alien a = (Alien)i3.next(); 
    			
    			Alien.Bomb b = a.getBomb(); 
    			
    			if (!b.isDestroyed()) {
    				g.drawImage(b.getImage(), b.getX(), b.getY(), this);
    			}
    		}
    	}
    	
    	// Draw the shelters
    	public void drawShelters(Graphics g) {
    		if (shelter.isVisible()) {
    			g.drawImage(shelter.getImage(), shelter.getX(), shelter.getY(), this);
    		}
    	}
    	
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, d.width, d.height);
    		g.setColor(Color.GREEN);
    		
    		if (inGame) {
    			g.drawLine(0, GROUND, BOARD_WIDTH, GROUND);
    			drawAliens(g);
    			drawPlayer(g);
    			drawShot(g);
    			drawBombing(g);
    			drawShelters(g);
    		}
    		
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
    	
    	public void gameOver() {
    		Graphics g = this.getGraphics(); 
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
    		
    		g.setColor(new Color(0, 32, 48));
    		g.fillRect(50, BOARD_WIDTH/2 - 30, BOARD_WIDTH - 100, 50);
    		g.setColor(Color.WHITE);
    		g.fillRect(50, BOARD_WIDTH/2 - 30, BOARD_WIDTH - 100, 50);
    		
    		Font small = new Font("Helvetica", Font.BOLD, 14); 
    		FontMetrics metr = this.getFontMetrics(small);
    		
    		g.setColor(Color.WHITE);
    		g.setFont(small);
    		g.fillRect(50, BOARD_WIDTH/2 - 30, BOARD_WIDTH - 100, 50);
    		g.drawString(message, (BOARD_WIDTH - metr.stringWidth(message))/2, BOARD_WIDTH/2);
    		g.drawString(message, 200, 200);
    	}
    	
    	public void animationCycle() {
    		if (deaths == NUMBER_OF_ALIENS_TO_DESTROY) {
    			inGame = false; 
    			message = "Game Won!";
    		}
    		
    		// Player movement
    		player.act();
    		
    		// Shot
    		if (shot.isVisible()) {
    			Iterator<Object> it = aliens.iterator(); 
    			int shotX = shot.getX(); 
    			int shotY = shot.getY(); 
    			
    			while (it.hasNext()) {
    				Alien alien = (Alien) it.next(); 
    				int alienX = alien.getX(); 
    				int alienY = alien.getY(); 
    				
    				if (alien.isVisible() && shot.isVisible()) {
    					if (shotX >= (alienX) && 
    						shotX <= (alienX + ALIEN_WIDTH) && 
    						shotY >= (alienY) &&
    						shotY <= (alienY + ALIEN_HEIGHT)) {
    						
    						ImageIcon ii = new ImageIcon(this.getClass().getResource(explosion));
    						alien.setImage(ii.getImage());
    						alien.setDying(true);
    						
    						// How many aliens that have died
    						deaths++;
    						
    						// If a shot hit an alien
    						shot.die();
    					}
    						
    				}
    			}
    			
    			int y = shot.getY(); 
    			
    			// The speed of the shot
    			y -= 10; 
    			
    			if (y < 0) {
    				shot.die();
    			}
    			else {
    				shot.setY(y);
    			}
    		}
    		
    		// Aliens
    		Iterator<Object> it1 = aliens.iterator(); 
    		
    		while (it1.hasNext()) {
    			Alien a1 = (Alien)it1.next(); 
    			int x = a1.getX(); 
    			
    			if (x >= BOARD_WIDTH - BORDER_RIGHT && direction != -1) {
    				direction = -1; 
    				
    				Iterator<Object> i1 = aliens.iterator(); 
    				
    				while (i1.hasNext()) {
    					Alien a2 = (Alien)i1.next(); 
    					a2.setY(a2.getY() + GO_DOWN);
    				}
    			}
    			
    			if (x <= BORDER_LEFT && direction != 1) {
    				direction = 1; 
    				
    				Iterator<Object> i2 = aliens.iterator(); 
    				
    				while (i2.hasNext()) {
    					Alien a = (Alien)i2.next(); 
    					a.setY(a.getY() + GO_DOWN);
    				}
    			}
    		}
    		
    		// Make the aliens move horizontally
    		Iterator<Object> it = aliens.iterator(); 
    		
    		while (it.hasNext()) {
    			Alien alien = (Alien)it.next(); 
    			
    			if (alien.isVisible()) {
    				
    				int y = alien.getY(); 
    				
    				if (y > GROUND - ALIEN_HEIGHT) {
    					inGame = false; 
    					message = "Invasion!"; 
    				}
    				
    				alien.act(direction);
    			}
    		}
    		
    		// Bombs
    		Iterator<Object> i3 = aliens.iterator(); 
    		Random generator = new Random(); 
    		
    		while (i3.hasNext()) {
    			// Generate a number from 0 - 15
    			int shot = generator.nextInt(15);
    			
    			Alien a = (Alien)i3.next(); 
    			Alien.Bomb b = a.getBomb(); 
    			
    			// If the shot variable is equal to the CHANCE variables number
    			// the aliens will drop a bomb
    			if (shot == CHANCE && a.isVisible() && b.isDestroyed()) {
    				b.setDestroyed(false);
    				b.setX(a.getX());
    				b.setY(a.getY());
    			}
    			
    			int bombX = b.getX(); 
    			int bombY = b.getY(); 
    			int playerX = player.getX(); 
    			int playerY = player.getY(); 
    			
    			if (player.isVisible() && !b.isDestroyed()) {
    				if (bombX >= (playerX) &&
    					bombX <= (playerX + PLAYER_WIDTH) &&
    					bombY + BOMB_HEIGHT >= (playerY) &&
    					bombY + BOMB_HEIGHT <= (playerY + PLAYER_HEIGHT)) {
    					
    					ImageIcon ii = new ImageIcon(this.getClass().getResource(explosion)); 
    					player.setImage(ii.getImage());
    					player.setDying(true);
    					b.setDestroyed(true);
    				}
    			}
    			
    			if (!b.isDestroyed()) {
    				b.setY(b.getY() + 4);
    				if (b.getY() >= GROUND - BOMB_HEIGHT) {
    					b.setDestroyed(true);
    				}
    			}
    		}
    		
    		
    	}
    
    	@Override
    	public void run() {
    		long beforeTime, timeDifference, sleep;
    		
    		beforeTime = System.currentTimeMillis(); 
    		
    		while (inGame) {
    			repaint(); 
    			animationCycle(); 
    			
    			timeDifference = System.currentTimeMillis() - beforeTime; 
    			sleep = DELAY - timeDifference; 
    			
    			if (sleep < 0) {
    				sleep = 2; 
    			}
    			
    			try {
    				Thread.sleep(sleep);
    			}
    			catch (InterruptedException e) {
    				System.out.println("Interrupted!");
    			}
    			
    			beforeTime = System.currentTimeMillis();
    		}
    		
    		gameOver();
    		
    	}
    	
    	private class TAdapter extends KeyAdapter {
    		
    		public void keyPressed(KeyEvent e) {
    			player.keyPressed(e);
    			
    			int x = player.getX(); 
    			int y = player.getY();
    			
    			if (inGame) {
    				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    					if (!shot.isVisible()) {
    						shot = new Shot(x, y);
    					}
    				}
    			}
    		}
    		
    		public void keyReleased(KeyEvent e) {
    			player.keyReleased(e); 
    			
    			int x = player.getX(); 
    			int y = player.getY();
    			
    			if (!inGame) {
    				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    						shot.die();
    				}
    			}
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Space Invaders - Help with modifying Java code

    @OP: your first thread was moderated and needed manual approval; I just did so and moved it to another forum section because there's nothing advanced about it. I also deleted a duplicate thread, please have a bit of patience.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Space Invaders - Help with modifying Java code

    get some help or clues of how to do this
    Work through the list one item/feature at a time. Pick one and ask some questions about the problems you are having with it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help with an array in Space Invaders program
    By Mostpured in forum New To Java
    Replies: 13
    Last Post: 03-09-2015, 12:12 AM
  2. Modifying Given Code
    By jmscarlet9 in forum New To Java
    Replies: 5
    Last Post: 04-01-2012, 05:13 PM
  3. Problem with Space Invaders Game
    By andyman99008 in forum New To Java
    Replies: 8
    Last Post: 12-19-2010, 09:20 PM
  4. Space Invaders help!
    By Midge in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •