Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: shoot in 2d java game applet

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default shoot in 2d java game applet

    i am trying so that when i hit space bar my player should shoot fire ball.
    right now what happening is that when i hit space bar my fire is drawn at 0, 0 pixs. but i want it so that the fire ball shoot right front of my player. and it should keep going to right.

    public class Shoot
    {
    	private int x;
    	private int y;
    	private int dx;
    	private int width ;
    	private int height;
     
    	private boolean hitSPACE = false;  //user hit space bar
    	private boolean dead = true;     //shoot is dead
     
    	private static ImageIcon shoot_image = new ImageIcon("Image/shoot/bull2.png");
     
     
    	public Shoot()
    	{	
    		 dx = 3;
    		 width = 20;
    		 height = 20;
    	}
     
     
     
    	/*** get/set methods ***/
    	public static Image getSIMAGE()  
    	{ return shoot_image.getImage(); }
     
    	public int getX()
    	{ return x; }
    	public void setX(int value) 
    	{ this.x =  value; }
     
    	public int getY()
    	{ return y; }
    	public void setY(int value) 
    	{ this.y =  value; }
     
    	public int getWIDTH()
    	{ return width; }
    	public void setWIDTH(int value) 
    	{ this.width =  value; }
     
    	public int getHEIGHT()
    	{ return height; }
    	public void setHEIGHT(int value) 
    	{ this.height =  value; }
     
    	public boolean isDEAD()
    	{ return dead; }
    	public void setDEAD(boolean value)
    	{ this.dead = value; }
     
     
    	/*** Key pressed ***/
    	public void hitSHOOT()  //user hit spacebar
    	{
    		hitSPACE = true;
    		dead = true;
    	}
    	public void stopSHOOT()  //user stop spacebar
    	{
    		hitSPACE = false;
    	}
     
            // this method is in loop. so it will loop for every.
    	public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s)
    	{				
    		x += dx;
    	}/*** end of PLAYER_SHOOT method ***/
     
     
    	/*** paint method ***/
    	public void paint(Graphics g) 
    	{
    		if(hitSPACE == true && dead == true)
    		{
     
    			g.drawImage(this.getSIMAGE(), x, y, width, height, null); 
    		}
    	}/*** paint method ***/
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: shoot in 2d java game applet

    I am not really sure what you expect us to do with this out-of-context code snippet. If you want help, you'll have to provide an SSCCE (not your whole game, just a circle that moves when you hit space) that demonstrates exactly what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default how to make ur player shooot

    i am trying so that when i hit space bar my player should shoot fire ball.
    right now what happening is that when i hit space bar my fire is drawn at 0, 0 pixs. but i want it so that the fire ball shoot right front of my player. and it should keep going to right.



    public class Main extends Applet implements Runnable, /*TimerListener,*/ KeyListener
    {
    	//Double buffering variables
    	Image dbImage;
    	Graphics dbGraphics;
     
    	Thread thread;
    	Timer timer;
     
    	//Class variables
    	Ground ground_class;
    	Player player_class;
    	Platform platform_class;
    	Shoot shoot_class;
    	Enemy  enemy_class;
     
    	Container c;
     
    	/*** init method ***/
    	@Override
    	public void init() 
    	{
    		setSize(900, 400);
    		addKeyListener(this);
    	}/*** end of init method ***/
     
     
     
     
    	/*** start method ***/
    	@Override
    	public void start()
    	{
    		ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
    		player_class = new Player(10, 250);  //x, y
    		platform_class = new Platform();
    		shoot_class = new Shoot();
    		enemy_class = new Enemy(500, 345);   //x, y
     
    		thread = new Thread(this); //start run method
    		thread.start();
    		//timer = new Timer(1000,this); //1 sec
    		//timer.start();
    		//void stop()
    		//void setDelay(int delay)
    		//boolean isRunning()
    		//int getDelay
    	}/*** end of start method ***/
     
     
     
    	/*** run method ***/
     
    	public void run()
    	{
    		while(true) //player moves
    		{
    			ground_class.update(this, ground_class ,player_class);                         //ground + player collions
    			player_class.PLAYER_MOVE(this, ground_class);                                  //player moves
    			platform_class.update(this, player_class);
    			shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class);  //player shoot bullet moves
    			player_class.PLAYER_ENEMY_COLLISION(this, enemy_class);          //player + enemy collision
     
    			repaint();
    			try
    			{
    				Thread.sleep(10);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}/*** end of run method ***/
     
     
     
     
     
     
     
    	/*** update method ***/
    	@Override
    	public void update(Graphics g) 
    	{
    		if(dbImage == null) //if image is empty than create new image
    		{
    			dbImage = createImage(this.getSize().width, this.getSize().height);
    			dbGraphics = dbImage.getGraphics();
    		}
    		dbGraphics.setColor(getBackground());  //set the background color
    		dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
    		dbGraphics.setColor(getForeground());
    		paint(dbGraphics);     //call paint method
    		g.drawImage(dbImage, 0, 0, this);
    	}/*** end of update method ***/
     
     
     
     
     
     
    	/*** paint method ***/
    	@Override
    	public void paint(Graphics g) 
    	{
    		ground_class.paint(g);             //call paint method from Ground class  //draw ground than player on top
    		player_class.paint(g);             //call paint method from Player classr 
    		platform_class.paint(g);
    		shoot_class.paint(g); //draw bullet
    		if(enemy_class.isDEAD() == false)  //draw enemy if it's not dead
    		{
    			enemy_class.paint(g);
    		}
     
    	}/*** end of paint method ***/
     
     
     
     
     
     
    	/*** stop method ***/
    	@Override
    	public void stop()
    	{
     
    	}/*** end of stop method ***/
     
    	/*** destroy method ***/
    	@Override
    	public void destroy()
    	{
     
    	}/*** end of destroy method ***/
     
     
     
     
     
     
     
     
     
     
     
     
     
    	/************** key *********************/
    	@Override
    	public void keyPressed(KeyEvent e)
    	{
    		int keys = e.getKeyCode();
    		if(keys == KeyEvent.VK_RIGHT) 
    		{
    			player_class.hitRIGHT();
    		}
    		else if(keys == KeyEvent.VK_LEFT)
    		{
    			player_class.hitLEFT();
    		}	
    		else if (keys == KeyEvent.VK_UP)
    		{
    			player_class.hitJUMP(ground_class);
    		}
    		else if(keys == KeyEvent.VK_SPACE)
    		{
    			shoot_class.hitSHOOT();
    		}
    	}
    	@Override
    	public void keyReleased(KeyEvent e)
    	{
    		int keys = e.getKeyCode();
    		if(keys == KeyEvent.VK_RIGHT) 
    		{
    			player_class.stopRIGHT();
    		}
    		else if(keys == KeyEvent.VK_LEFT)
    		{
    			player_class.stopLEFT();
    		}	
    		//else if (keys == KeyEvent.VK_UP)
    		//{
    			//player_class.stopJUMP();
    		//}
    		else if(keys == KeyEvent.VK_SPACE)
    		{
    			shoot_class.stopSHOOT();
    		}
    	}
    	@Override
    	public void keyTyped(KeyEvent e){}
     
    	/************    MOUSE  **********/
    	public void mousePressed(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mouseClicked(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    }




    player.java
    public class Player 
    {
    	//Player movement variable
    	private int sx = 10;
    	private int sy = 200;
    	private int x;     //current x
    	private int y;     //current y
    	private int dx = 2;    //speed of player
     
    	private double dy = 6.0;  //change in y over time
    	private double gravity = 0.2;    
    	private double dt = .2;  
     
    	private boolean dead = false;
     
    	private boolean look_right = true;
    	private boolean walk_right;
    	private boolean walk_left;
    	private boolean jump = false;
    	private boolean jump_lock = false;
    	private boolean fall = false;                  
    	private boolean hitSPACE = false;
     
    	/*** image variable ***/
    	private static ImageIcon player_walk_right = new ImageIcon("Image/player/player_walk_right.gif");
    	private static ImageIcon player_walk_left = new ImageIcon("Image/player/player_walk_left.gif");
    	private static ImageIcon player_stand_right = new ImageIcon("Image/player/player_stand_right.gif");
    	private static ImageIcon player_stand_left = new ImageIcon("Image/player/player_stand_left.gif");
    	private static ImageIcon player_jump_right = new ImageIcon("Image/player/player_jump_right.gif");
    	private static ImageIcon player_jump_left = new ImageIcon("Image/player/player_jump_left.gif");
     
     
     
    	/*** constructor Method ***/
    	public Player()
    	{
    	}
    	/*** constructor Method2 ***/
    	public Player(int ix, int iy) 
    	{
    		x = ix;
    		y = iy;
    	}	
     
    		/*** get/set method ***/
    		//need these to draw image's
    		public static Image get_player_walk_right()  
    		{ return player_walk_right.getImage(); }
    		public static Image get_player_walk_left()  
    		{ return player_walk_left.getImage(); }
    		public static Image get_player_stand_right()  
    		{ return player_stand_right.getImage(); }
    		public static Image get_player_stand_left()  
    		{ return player_stand_left.getImage(); }
    		public static Image get_player_jump_right()  
    		{ return player_jump_right.getImage(); }
    		public static Image get_player_jump_left()  
    		{ return player_jump_left.getImage(); }
     
    		public boolean isDEAD()
    		{ return dead; }
    		public void setDEAD(boolean value) 
    		{ this.dead =  value; }
     
    		public int getX()
    		{ return x; }
    		public void setX(int value) 
    		{ this.x =  value; }
     
    		public int getY()
    		{ return y; }
    		public void setY(int value) 
    		{ this.y =  value; }
     
    		public int getDX()
    		{ return dx; }
    		public void setDX(int value) 
    		{ this.dx =  value; }
     
    		public double getDY()
    		{ return dy; }
    	    public void setDY(double value) 
    		{ this.dy =  value; }
     
     
     
    		/*** main key methods ***/
    		/*** RIGHT ***/
    		public void hitRIGHT()  //if user hit right button
    		{ 
    			walk_right = true; 
    			look_right = true;
    		}
    		public void stopRIGHT() //if user let go of right button
    		{
    			walk_right = false;  //stop 
    			look_right = true;
    		}
     
    		/*** LEFT ***/
    		public void hitLEFT()  //move left
    		{
    			walk_left = true;
    			look_right = false;
    		}
    		public void stopLEFT()
    		{
    			walk_left = false;  //stop
    			look_right = false;
    		}
     
    		/*** JUMP ***/
    		public void hitJUMP(Ground g)
    		{
    			if(y >= g.getY()-30) 
    			{
    				jump = true;
    				dy = 6.0;	//reset
    			}
    		}
     
     
     
     
     
    		/**************************/
    		/****** LOOP METHODS ******/
    		/**************************/
    		/*** player move method ***/
    		public void PLAYER_MOVE(Main m, Ground g) //player move's
    		{
    			if(walk_right == true)
    			{
    					x += dx;
    			}
    			else if(walk_right == false)
    			{
    			}
     
    			if(walk_left == true)  //walking left
    			{
    				if(x - dx < 0)  //cant go left
    				{
    					x = 0;
    				}
    				else
    				{
    					x -= dx;      //move left
    				}
    			}
    			else if(walk_left == false)
    			{
    			}
     
     
     
     
    			if(jump == true) 
    			{
    				y--; //move player down -start of game
     
    				dy -= gravity;
    				y -= dy;
     
    				if(y >= g.getY()-30) //if on ground
    				{
    					jump = false;
    				}
    			}
    			else if(jump == false) //move player down when game starts
    			{
    				y++;
    			}
    		}/***end of update method ***/
     
     
     
     
     
     
     
     
     
    		/*** player + enemy collion ***/
    		public void PLAYER_ENEMY_COLLISION(Main m,Enemy e)
    		{
    			int enemyX = e.getX();
    			int enemyY = e.getY();
    			if(x < enemyX && x > enemyX + 13)
    			{
    				if(y > enemyY && y > enemyY+25)
    				{
    					x -= 20;
    					y -=20;
    				}
    			}
     
    		}
    		/*** end of PlAYER_ENEMY_COLLISION METHOD ***/
     
     
     
     
     
     
    		/*** paint method ***/
    		public void paint(Graphics g)
    		{
    			if(jump == false)
    			{
    				if(look_right == true)
    					{
    						if(walk_right == true)
    						{
    							g.drawImage(this.get_player_walk_right(), x, y, null);
    						}
    						else if(walk_right == false)
    						{
    							g.drawImage(this.get_player_stand_right(), x, y, null);
    						}
    					}
    				else if(look_right == false)
    				{
    					if(walk_left == true)
    						{
    							g.drawImage(this.get_player_walk_left(), x, y, null);
    						}
    					else if(walk_left == false)
    					{
    						g.drawImage(this.get_player_stand_left(), x, y, null);
    					}
    				}	
    			}
    			else if(jump == true)
    			{
    				if (look_right == true)
    				{
    					g.drawImage(this.get_player_jump_right(), x, y, null);
    				}
    				else if(look_right == false)
    				{
    					g.drawImage(this.get_player_jump_left(), x, y, null);
    				}
    			}
    		}
    }/*** end of class ***/




    public class Shoot
    {
    	private int x;
    	private int y;
    	private int dx;
    	private int width ;
    	private int height;
     
    	private boolean hitSPACE = false;  //user hit space bar
    	private boolean dead = true;     //shoot is dead
     
    	private static ImageIcon shoot_image = new ImageIcon("Image/shoot/bull2.png");
     
     
     
     
     
    	public Shoot()
    	{	
    		 dx = 3;
    		 width = 20;
    		 height = 20;
    	}
     
     
     
    	/*** get/set methods ***/
    	public static Image getSIMAGE()  
    	{ return shoot_image.getImage(); }
     
    	public int getX()
    	{ return x; }
    	public void setX(int value) 
    	{ this.x =  value; }
     
    	public int getY()
    	{ return y; }
    	public void setY(int value) 
    	{ this.y =  value; }
     
    	public int getWIDTH()
    	{ return width; }
    	public void setWIDTH(int value) 
    	{ this.width =  value; }
     
    	public int getHEIGHT()
    	{ return height; }
    	public void setHEIGHT(int value) 
    	{ this.height =  value; }
     
    	public boolean isDEAD()
    	{ return dead; }
    	public void setDEAD(boolean value)
    	{ this.dead = value; }
     
     
    	/*** Key pressed ***/
    	public void hitSHOOT()  //user hit spacebar
    	{
    		hitSPACE = true;
    		dead = true;
    	}
    	public void stopSHOOT()  //user stop spacebar
    	{
    		hitSPACE = false;
    	}
     
     
     
    	/******************/
    	/*** LOOP METHOD ***/
    	/*******************
    	/*** update method ****/
    	public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s)
    	{				
    		x += dx;
    	}/*** end of PLAYER_SHOOT method ***/
     
     
    	/*** paint method ***/
    	public void paint(Graphics g) 
    	{
    		if(hitSPACE == true && dead == true)
    		{
     
    			g.drawImage(this.getSIMAGE(), x, y, width, height, null); 
    		}
    	}/*** paint method ***/
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: shoot in 2d java game applet

    I've merged your duplicate post into your existing thread. Please keep this question to a single thread. I've asked you to provide an SSCCE, and 3 classes is way too much.

    The reason I'm asking for that is because you're misunderstanding some pretty basic concepts here. First you need to set up a game loop using a Timer. Then you need to encapsulate the drawing and updating of an Object. Then you need to work on accepting input to change the rules of how that Object is updated.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Java applet game
    By qadeerhussain007 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 9th, 2012, 07:46 AM
  2. Help with game applet
    By skerridge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 02:24 PM
  3. Beginner. Need Help finishing a simple Shoot em up game ASAP!!!!
    By cbock55 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 08:13 AM
  4. Need help with java applet game.
    By vlan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 04:18 AM
  5. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM