Results 1 to 2 of 2

Thread: 2D Shooter Game

  1. #1
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default 2D Shooter Game

    Hi all I just finished my first java game "Shooter" but I have only one trouble and it's that

    whenever I try to Shoot the bullets from Player 1 to Player 2 or viceverza I can't see the

    bullets going trough the screen as it really should be happening.

    Here's the source code. :)

    Java Code:
    //Shooter Class
    import java.awt.event.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    
    
    public class Shooter extends JFrame implements KeyListener{
    
    	private static final long serialVersionUID = 1L;
    	
    	Image img;
    	Graphics dbi;
    	boolean u, d, w, s;
    	int S, E;
    	
    	Player p1 = new Player(0, 150, 10, 50, Color.RED, "Images/Left.gif");
    	Player p2 = new Player(585, 150, 10, 50, Color.GREEN, "Images/Right.gif");
    	ArrayList<Bullets>b = new ArrayList<Bullets>();
    
    	public Shooter(){
    		
    		setTitle("Shooter_Game");
    		setSize(600,400);
    		setResizable(false);
    		setBackground(Color.BLACK);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		addKeyListener(this);
    		u=d=w=s = false;
    		S=E=0;
    		setVisible(true);
    		
    	}
    	
    	public void paint(Graphics g){
    		
    		img = createImage(getWidth(), getHeight());
    		dbi = img.getGraphics();
    		
    		paintComponent(dbi);
    		g.drawImage(img, 0, 0, this);
    		repaint();
    		
    	}
    	
    	public void paintComponent(Graphics g){
    		
    		if(p1.health > 0 && p2.health > 0){
    			
    			for(Bullets b1:b){ //new for loop
    				
    				b1.draw(g);
    			}
    			
    			update();
    		}
    		
    		else {
    			
    			if(p1.health == 0){
    				
    				g.setColor(p1.col);
    				g.drawString("Player2 WINS!", 250, 190);
    			}
    			
    			else {
    				
    				if(p2.health == 0){
    				
    				g.setColor(p2.col);
    				g.drawString("Player1 WINS!", 250, 190);
    				}
    			}
    		}
    		
    		p1.draw(g);
    		p2.draw(g);
    	}
    	
    	public void update(){
    		
    		if (w && p1.y > 24)p1.moveUP();
    		if (s && p1.y < 347)p1.moveDown();
    		if (u && p2.y > 24)p2.moveUP();
    		if (d && p2.y < 347)p2.moveDown();
    		if (E == 1){
    			
    			Bullets add = p2.getBull();
    			add.xVel = -3;
    			b.add(add);
    			E ++;
    		}
    		
    		if(S == 1){
    			
    			Bullets add = p1.getBull();
    			add.xVel = -3;
    			b.add(add);
    			S ++;
    		}
    		
    		for( int x = 0; x < b.size(); x++ ){
    			
    			b.get(x).move();
    			if( b.get(x).rect.intersects(p2.rect) && b.get(x).xVel < 0 ){
    				
    				p1.health --;
    				b.remove(x);
    				x--;
    				continue;
    			}
    			
    			else if( b.get(x).rect.intersects(p1.rect) && b.get(x).xVel < 0 ){
    				
    				p2.health --;
    				b.remove(x);
    				x--;
    				continue;
    			}
    		}
    	}
    
    	public void keyTyped( KeyEvent e ){}
    	public void keyPressed( KeyEvent e ){
    		
    		switch( e.getKeyCode() ){
    		
    			case KeyEvent.VK_UP: u = true; break;
    			case KeyEvent.VK_DOWN: d = true; break;
    			case KeyEvent.VK_W: w = true; break;
    			case KeyEvent.VK_S: s = true; break;
    		
    			case KeyEvent.VK_SPACE: S++; break;
    			case KeyEvent.VK_ENTER: E++; break;
    		
    		}
    	}
    	
    	public void keyReleased( KeyEvent e ){
    		
    		switch(e.getKeyCode()){
    		
    			case KeyEvent.VK_UP: u = false; break;
    			case KeyEvent.VK_DOWN: d = false; break;
    			case KeyEvent.VK_W: w = false; break;
    			case KeyEvent.VK_S: s = false; break;
    	
    			case KeyEvent.VK_SPACE: S = 0; break;
    			case KeyEvent.VK_ENTER: E = 0; break;
    			
    		}
    	}
    	
        public static void main(String []beans){
    		
    		KeyListener s = new Shooter();
    	}
    	
    	
    }
    
    ____________________________________________________________________________________________
    
    //Player CLASS
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    
    //Name
    //Shooter Game
    //Player Class
    
    public class Player {
    
    	int x, y;
    	int height, width, health;
    	Image img;
    	Rectangle rect;
    	Color col;
    	
    	public Player(){
    		
    		x = y = height = width = 0;
    		img = null;
    		col = Color.WHITE;
    		health = 10;
    		rect = new Rectangle(x, y, height, width);
    	
    	}
    	
    	public Player(int x, int y){
    		
    		this.x = x;
    		this.y = y;
    		height = width = 0;
    		img = null;
    		col = Color.WHITE;
    		health = 10;
    		rect = new Rectangle(x, y, height, width);
    		
    	}
    	
    	public Player(int x, int y, int wd, int ht){
    		
    		this.x = x;
    		this.y = y;
    		height = ht;
    		width = wd;
    		img = null;
    		col = Color.white;
    		health = 10;
    		rect = new Rectangle(x, y, width, height);
    		
    	}
    	
    	public Player(int x, int y, int ht, int wd, String s){
    		
    		this.x = x;
    		this.y = y;
    		height = ht;
    		width = wd;
    		health = 10;
    		col = Color.WHITE;
    		img = Toolkit.getDefaultToolkit().getImage(s);
    		rect = new Rectangle(x, y, height, width);
    		
    	}
    	
    	public Player(int x, int y, int ht, int wd, Color c,String s){
    		
    		this.x = x;
    		this.y = y;
    		height = ht;
    		width = wd;
    		health = 10;
    		col = c;
    		img = Toolkit.getDefaultToolkit().getImage(s);
    		rect = new Rectangle(x, y, height, width);
    		
    	}
    
    
    	public void draw(Graphics g){
    		
    		g.drawImage(img, x, y, null);
    	}
    	
    	public void setImage(String s){
    		
    		img = Toolkit.getDefaultToolkit().getImage(s);
    	}
    	
    	public void moveUP(){
    		
    		y -= 3;
    		rect.setLocation(x, y);
    	}
    	
    	public void moveDown(){
    		
    		y += 3;
    		rect.setLocation(x, y);
    	}
    	
    	public Bullets getBull(){
    		
    		return new Bullets(x + 3, y + 23, 8, 8, col);
    	}
    	
    }
    
    ____________________________________________________________________________________________
    
    //Bullets CLASS
    
    import java.awt.*;
    
    public class Bullets {
    
    	
    	int x, y;
    	int xVel;
    	int height, width;
    	Rectangle rect;
    	Color col;
    	
    	public Bullets(){
    		
    		x = y = height = width = 0;
    		col = Color.WHITE;
    		rect = new Rectangle(x, y, width, height);
    		
    	}
    	
    	public Bullets(int x, int y, int wd, int ht, Color c){
    		
    		this.x = x;
    		this.y = y;
    		this.xVel = 0;
    		height = ht;
    		width = wd;
    		col = c;
    		rect = new Rectangle(x, y, width, height);
    	}
    	
    	public Bullets(int x, int y, int wd, int ht, int xVel, Color c){
    		
    		this.x = x;
    		this.y = y;
    		this.xVel = xVel;
    		height = ht;
    		width = wd;
    		col = c;
    		rect = new Rectangle(x, y, width, height);
    		
    	}
    	
    	public void draw(Graphics g){
    		
    		g.setColor(col);
    		g.fillOval(x, y, width, height);
    	}
    	
    	public void move(){
    		
    		x += xVel;
    		rect.setLocation(x, y);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You shouldn't draw directly on to a JFrame but rather in a JPanel. Moreover, JFrame doesn't have a public void method paintComponent(Graphics g), and you'll discover that you're not overriding anything with this method if you precede it with an @Override annotation.

    Also, what are you using for your animation loop as I don't see a Swing Timer or the equivalent using threads anywhere.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. 2D Game
    By Elffus in forum Java 2D
    Replies: 58
    Last Post: 08-05-2010, 02:47 AM
  3. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 01:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Posting Permissions

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