Game Programming practice
<p>I am beginning my step in learning how to make simple games. So far what i can do is this. I created a main panel and i created a rectangularComponent(alien) which will go down the panel. if pressed "space bar" the bullet will be fired and if theres collision detected with alien and bullet, the program will stop. I would like to ask on things that I needed to change or improve with my code or if this programming method that I used is incorrect?</p>

<p>this if for main panel</p>

<pre><code>    package spaceInvader;


    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;


    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Space_GUI extends JPanel implements ActionListener, KeyListener {


        public RectangleComponent rc;
        private Timer timer;
        private static Bullet bullet;

        public Space_GUI(){
        rc = new RectangleComponent();
        add(rc);
        timer=  new Timer(5,this);
        timer.start();
        addKeyListener(this);

    }
    public void actionPerformed(ActionEvent arg0) {

    if(bullet!=null){
        bullet.move();
    }
     if(!reachedEnd()){
         rc.move();
         repaint();
     }
     else{
         System.out.println("You Lost.");
         timer.stop();
     }


    }

private boolean reachedEnd() {
    if(rc._yCoord&gt;=500){
        return true;
    }
    return false;
}

    private boolean collide() {
    if(bullet!=null){
        Rectangle r1 = rc.bounds();
        Rectangle r2 = bullet.bounds();
        if(r1.intersects(r2)){
            return true;
        }
    }
        return false;
    }

    public void paintComponent(Graphics g){

        super.paintComponent(g);

            if(!collide()){ 
        if (bullet!=null)bullet.draw(g);
        rc.draw(g);
            }
            else{
                bullet = null;
                timer.stop();
            }

        }

    public static void main(String args[]){
        JFrame jf = new JFrame("HAAHAHAHA!");
        Space_GUI sg = new Space_GUI();
        sg.setFocusable(true);

        jf.setSize(500,500);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(sg,BorderLayout.CENTER);
        jf.setVisible(true);
    }
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        int code = arg0.getKeyCode();
    if(code == KeyEvent.VK_SPACE){
        bullet = new Bullet();  
    }
    }
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    }
</code></pre>

<p>this is for alien</p>

<pre><code>    package spaceInvader;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Rectangle;

    import javax.swing.JComponent;

    public class RectangleComponent extends JComponent{

        public int _xCoord;
        public int _yCoord;
        private boolean goRight;
        public   static final int wSIZE = 50;
        public  static final int hSIZE = 25;

        public RectangleComponent(){
            setPreferredSize(new Dimension(wSIZE,hSIZE));
            goRight = true;
            _xCoord = 0;
            _yCoord = 0;    
        }

        public void move(){
            if(_xCoord==0){
                goRight = true;
                _yCoord+=25;
            }
            if(_xCoord==450){
                goRight = false;
                _yCoord+=25;
            }

            if(goRight){
                _xCoord++;
            }
            else
            {
                _xCoord--;
            }   
        }   
        public void draw(Graphics g){   

            g.setColor(Color.black);
            g.drawRect(_xCoord, _yCoord, wSIZE, hSIZE);
            g.fillRect(_xCoord, _yCoord, wSIZE, hSIZE);
        }
        public Rectangle bounds (){

            return new Rectangle(_xCoord, _yCoord, wSIZE, hSIZE);
        }
    }
</code></pre>

<p>this is for bullet</p>

<pre><code>package spaceInvader;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;

public class Bullet extends JComponent {

    public int _xCoord;
    public int _yCoord;

    public   static final int wSIZE = 6;
    public  static final int hSIZE = 10;
    public Bullet(){

        setPreferredSize(new Dimension(wSIZE,hSIZE));

        _xCoord = 253;
        _yCoord = 500;  
    }

    public void move(){
    _yCoord-=10;        
    }   
    public void draw(Graphics g){   
        g.setColor(Color.black);
        g.drawRect(_xCoord, _yCoord, wSIZE, hSIZE);
        g.fillRect(_xCoord, _yCoord, wSIZE, hSIZE);
    }

public Rectangle bounds (){

        return new Rectangle(_xCoord, _yCoord, wSIZE, hSIZE);
    }

}
</code></pre>
