Help with Space Invaders Game

Space Invaders , Need Programming help , Games , Java Belfast, United Kingdom
  • 8 years ago

    Hi, im new to these forums so forgive if i dont follow the right way on laying things out. Im trying to create space invaders game but im stuck when im trying to create multiple aliens/ invaders. I have a class called Aliens (which is the invaders), and i can create an instance of an Alien. But when I try to create multiple instances they are not showing up on screen. I am not getting any error message sthough (which im hoping is a good sign, lol).

    GamePanel is where all the action is and Alien is the alien/ invader class.

    Here is the code:

    import java.awt.; import javax.swing.; import java.awt.Image; import java.awt.event.KeyListener; import java.awt.event.KeyEvent;

    public class GamePanel extends JComponent implements KeyListener { SpaceInvaders game; static Player player1; Aliens alien1; static public Image spaceship2R; static public Image spaceship2L;

    public GamePanel(SpaceInvaders game)
    {
    	this.game = game;
    
    	player1 = new Player(this);
    	player1.start();
    
    	for(int i=0; i<4; i++)
    	{
    		alien1 = new Aliens(this);
    		alien1.setX(i*100);
    		alien1.start();
    	}
    
    
        this.setFocusable(true);
        this.addKeyListener(this);
    }
    
    public void paintComponent(Graphics g)
    {
    	Image pic1 = player1.getImage();
    	Image pic2 = alien1.getImage();
    
    	super.paintComponent(g);
    
    	g.drawImage(pic1, player1.getX(), player1.getY(), 50, 50,this);
    	g.drawImage(pic2, alien1.getX(), alien1.getY(), 50, 50,this);
    
    }
    

    }//end class

    import java.awt.Image; import java.awt.*;

    class Aliens extends Thread { //variables GamePanel parent; private boolean isRunning; private int xPos = 0, yPos = 0; Thread thread; static char direction = 'r'; Aliens alien; static public Image alien1; static public Image alien2;

    public Aliens(GamePanel parent)
    {
    	this.parent = parent;
    	isRunning = true;
    }
    
    public void run()
    {
    	while(isRunning)
    	{
    		Toolkit kit = Toolkit.getDefaultToolkit();
    		alien1 = kit.getImage("alien1.gif");
    		alien2 = kit.getImage("alien2.gif");
    
    		try
                 {       
                 	thread.sleep(100);
                 }
                 catch(InterruptedException e)
                 {
                	 System.err.println(e);
                 }
            	updateAliens();
            	parent.repaint();
    	}
    
    }
    
    public static Image getImage()
    {
    	Image alienImage = alien1;
        switch(direction){
        	case('l'):
        		alienImage = alien1;
        		break;
        	case('r'):
        		alienImage = alien1;
        		break;
        }
        return alienImage;	
    }
    
    public void updateAliens()
    {
    	if(direction=='r')
    	{
    		xPos+=20;
    		if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
    		{
    			yPos+=50;
    			xPos-=20;
    			direction='l';
    		}
    	}
    	else
    	{
    		xPos-=20;
    		if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
    		{
    			yPos+=50;
    			xPos+=20;
    			direction='r';
    		}
    	}
    
    }
    
    public void setDirection(char c){ direction = c; }
    public void setX(int x){ xPos = x; }
    public void setY(int y){ yPos = y; }
    public int getX(){ return xPos; }
    public int getY(){ return yPos; }
    

    }//end class

  • 8 years ago

    problem was sorted, fixed it.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”