Page 1 of 3 123 LastLast
Results 1 to 20 of 59

Thread: 2D Game

  1. #1
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Smile 2D Game

    Hi,

    I recently started learning Java, after doing around 5 years of web development, and have, for my first game project, decided to create a simple 2D worms-style game - in the sense that the background doesn't scroll: your character moves.

    I have a class for the Player, a class for the Frame, and a Board class which calls paint and contains an action listener.

    What I currently have is simply a background with a house:



    I don't wish to use a tile system as it'll mean recreating the maps.

    You can move the player by using the left, right, up and down arrow.. However this doesn't really work. The background is a simple image, and I want to do collisions with it and similar backgrounds without remaking the maps. I discovered the PixelGrabber functions and made this:

    Java Code:
    	public void checkGroundCollision( )
    	{
    		
    		ImageIcon b = new ImageIcon( "D:/PW/Java/background.png" );
    		background = b.getImage( );
    		
    		pw = still.getWidth( null ) + this.getX( );
    		ph = still.getHeight( null ) + this.getY( );
    		
    		int[] pixels = new int[ 800 * 600 ];
    		
    		for ( int i = 0; i < still.getWidth( null ); i++ )
    		{
    			
    			int nextPixel = ( still.getWidth( null ) + this.getX( ) ) - i;
    			PixelGrabber pg = new PixelGrabber( background, ph, nextPixel, 800, 600, pixels, 0, 800 );
    			
    			try
    			{
    				
    				pg.grabPixels( );
    				
    			}
    			catch ( InterruptedException e )
    			{
    				
    				System.err.println( "Image fetch aborted or error'd!" );
    				
    			}
    			
    			int c = pixels[ pw * 800 + ph ];
    			int red = ( c & 0x00ff0000 ) >> 16;
    			int green = ( c & 0x0000ff00 ) >> 8;
    			int blue = c & 0x000000ff;
    			
    			//System.out.println( red + " | " + green + " | " + blue );
    			//System.out.println( pw + " : " + ph );
    			
    			if ( red == 200 && green == 200 && blue == 200 )
    			{
    				
    				y += 1;
    				
    			}
    			
    		}
    		
    	}
    However, if I call this anywhere the game decides to lag and the function seems to loop forever...

    Any ideas as to what I should do, or any other different ways of approaching it?

  2. #2
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    I changed the code to the following, however when I uncomment the print line in the handlesinglepixel( ) method it prints the colour of dirt (which is not one pixel below the sprite) or 0, 0, 0. However, when that line is not there it continues with the if statement and moves the player up... I'm not sure why?

    Java Code:
    	public void checkGroundCollision( )
    	{
    		
    		ImageIcon b = new ImageIcon( "D:/PW/Java/background.png" );
    		background = b.getImage( );
    		
    		int w = 800;
    		int h = 600;
    		
    		int[] pixels = new int[ w * h ];
    		PixelGrabber pg = new PixelGrabber( background, x, y, w, h, pixels, 0, w );
    		
    		try 
    		{
    			
    			pg.grabPixels();
    			
    		} 
    		catch ( InterruptedException e ) 
    		{
    			System.err.println("interrupted waiting for pixels!");
    			return;
    			
    		}
    		
    		if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 ) 
    		{
    			
    			System.err.println("image fetch aborted or errored");
    			return;
    			
    		}
    		
    		for ( int j = 0; j < h; j++ ) 
    		{
    			
    			for ( int i = 0; i < w; i++ ) 
    			{
    				
    				handlesinglepixel(x+i, y+j, pixels[j * w + i]);
    				
    			}
    			
    		}
    		
    	}
    	
    	public void handlesinglepixel( int x, int y, int pixel ) 
    	{
    		
    		//int alpha = ( pixel >> 24 ) & 0xff;
    		int red   = ( pixel >> 16 ) & 0xff;
    		int green = ( pixel >>  8 ) & 0xff;
    		int blue  = ( pixel       ) & 0xff;
    		
    		//System.out.println( red + " | " + green + " | " + blue );
    		
    		if ( red == 200 && green == 200 && blue == 200 )
    		{
    			
    			System.out.println( "MOVE UP DAMMIT" );
    			dy = -1;
    			
    		}
    		
    	 }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    the function seems to loop forever
    Where is is looping? What is keeping it looping?
    Do some debugging by adding more println()s to show what/where the problem is.

  4. #4
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    My board class calls this:

    Java Code:
    	public void actionPerformed( ActionEvent e )
    	{
    		
    		player.move( );
    		repaint( );
    		
    	}
    which runs this:

    Java Code:
    	public void move( )
    	{
    		
    		x += dx;
    		y += dy;
    		
    		checkGroundCollision( );
    		
    		if ( x < 0 )
    			x = 0;
    		
    		if ( x >= 800 - still.getWidth( null ) )
    			x = 800 - still.getWidth( null );
    		
    		if ( y < 0 )
    			y = 0;
    		
    		if ( y >= 600 - still.getHeight( null ) )
    			y = 600 - still.getHeight( null );
    		
    		
    	}
    Should the function be called somewhere else?

    As for more debugging everything seems to run fine until it gets to that method.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    until it gets to that method.
    What happens when it gets to that method?

    How do the values of x and y change? What are the values of dx and dy?
    What values are returned by the getWidth and getHeight methods?

    Comment on your code:

    Don't use hardcoded values like 600 and 800 throughout your code. The compiler won't tell you if you do a typo on one of the values.
    Define a final int with a self documenting name and use that. Comparing x to MaxWidth reads better than comparing it to 800
    Last edited by Norm; 08-02-2010 at 01:28 PM.

  6. #6
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    When it gets to that method the variables red, green and blue grab the data that is processed from the collision method. However when you print them they show not as 200, 200, 200 but as 0, 0, 0 or the dirt colour. When you don't print them, the if function continues as if they were 200, 200, 200 no matter what and permanently moves you up.

    When a key is pressed dx/dy correspondingly (x for x-axis and y for y-axis) set to -1 or 1, when a key is released they set to 0 to stop movement. getWidth and getHeight (in that code) are returning the width and height of the image used for the players sprite as an integer.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    when you print them they show not as 200, 200, 200 but as 0, 0, 0 or the dirt colour. When you don't print them, the if function continues as if they were 200, 200, 200
    I don't believe it. Can you copy and paste the output here?
    Java Code:
    //System.out.println( red + " | " + green + " | " + blue );
    		
    		if ( red == 200 && green == 200 && blue == 200 )
    		{
    			
    			System.out.println( "MOVE UP DAMMIT" );
    I would expect to see:
    200|200|200
    MOVE UP DAMMIT

  8. #8
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    I figured out why it was not outputting correctly. This loop:

    Java Code:
    		for ( int j = 0; j < h; j++ ) 
    		{
    			
    			for ( int i = 0; i < w; i++ ) 
    			{
    				
    				handlesinglepixel( x + i, y + j, pixels[ j * w + i ] );
    				
    			}
    			
    		}
    Made it loop through every pixel. So inevitably it would find 200, 200, 200 but only for a split second. Though it still didn't work. I changed the code to this:

    Java Code:
    		int w = 800;
    		int h = 600;
    		
    		int[] pixels = new int[ w * h ];
    		PixelGrabber pg = new PixelGrabber( background, 50, 50, w, h, pixels, 0, w );
    		
    		try 
    		{
    			
    			pg.grabPixels( );
    			
    		} 
    		catch ( InterruptedException e ) 
    		{
    			System.err.println( "interrupted waiting for pixels!" );
    			return;
    			
    		}
    		
    		if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 ) 
    		{
    			
    			System.err.println( "image fetch aborted or errored" );
    			return;
    			
    		}
    		
    		handlesinglepixel( 50, 50, pixels[ 50 * w + 50 ] );
    So technically it should take the exact value of the pixel at 50, 50. However it doesn't (I changed the pixel at 50, 50 in the image to red, and it outputs the colour of the sky).

  9. #9
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    I did some more testing by changing the colours of the image and found out that the colour at 100, 100 is being returned.. when x & y are set to 50?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    the colour at 100, 100 is being returned.. when x & y are set to 50
    What code are you executing that does this?
    How do you verify the truth of that statement?

  11. #11
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    I read up more on the PixelGrabber function and realised it draws a rectangle...

    Java Code:
    PixelGrabber pg = new PixelGrabber( background, 0, 0, 50, 50, pixels, 0, w );
    So it details that this should get all the pixels in the area of 0 > 50, 0 > 50. However I'm not sure how I recieve the 50th x and y value from the array for this function:

    Java Code:
    handlesinglepixel( 50, 50, pixels[ 50 * w + 50 ] );
    As the function (like it is above) just returns 0, 0, 0 for RGB.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you explain what you are trying to do at a higher level? Your posting single lines of code doesn't show the bigger picture.

    I don't understand: " function (like it is above) just returns 0, 0, 0"
    public void handlesinglepixel( int x, int y, int pixel ) // this is a void method, it doesn't return anything

  13. #13
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Basically I'm trying to make a 2D platform game. You move your character on a background (there is no tile system). The only way to detect collisions is to work out what colours are below/around the player. The colour of the floor you walk on is 200, 200, 200; so if the PixelGrabber grabs the colour underneath the players sprite and it's RGB is 200, 200, 200 I want it to deny the players movement.

    Java Code:
    	public void checkGroundCollision( )
    	{
    		
    		ImageIcon b = new ImageIcon( "D:/PW/Java/background.png" );
    		background = b.getImage( );
    		
    		int w = 800;
    		int h = 600;
    		
    		int[] pixels = new int[ w * h ];
    		PixelGrabber pg = new PixelGrabber( background, 50, 50, 1, 1, pixels, 0, w );
    		
    		try 
    		{
    			
    			pg.grabPixels( );
    			
    		} 
    		catch ( InterruptedException e ) 
    		{
    			System.err.println( "interrupted waiting for pixels!" );
    			return;
    			
    		}
    		
    		if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 ) 
    		{
    			
    			System.err.println( "image fetch aborted or errored" );
    			return;
    			
    		}
    		
    		handlesinglepixel( 50, 50, pixels[ 50 * w + 50 ] );
    		
    		/*for ( int j = 0; j < h; j++ ) 
    		{
    			
    			for ( int i = 0; i < w; i++ ) 
    			{
    				
    				handlesinglepixel( x + i, y + j, pixels[ j * w + i ] );
    				
    			}
    			
    		}*/
    		
    	}
    	
    	public void handlesinglepixel( int x, int y, int pixel ) 
    	{
    		
    		//System.out.println( "hereee" );
    		
    		//int alpha = ( pixel >> 24 ) & 0xff;
    		int red   = ( pixel >> 16 ) & 0xff;
    		int green = ( pixel >>  8 ) & 0xff;
    		int blue  = ( pixel       ) & 0xff;
    		
    		System.out.println( red + " | " + green + " | " + blue );
    		
    		if ( red == 200 && green == 200 && blue == 200 )
    		{
    			
    			System.out.println( "MOVE UP DAMMIT" );
    			dy = -1;
    			
    		}
    		
    	 }
    This code shows two functions. The first one will be called when a player presses a button. For now I am just showing you the example of pressing the key down (the second method checks for 200, 200, 200 and executes if the RGB values of the pixels underneath the player are 200, 200, 200 - if they are it will not conduct with the movement).

    However, this didn't seem to work. So I'm now trying to make it work. Basically I'm trying to create a function that gets a pixel underneath the players sprite and if it's RGB values are 200R, 200G, 200B then it will not continue (and so not move the character down) - a pixel collision system.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    OK.
    The function of the handlesinglepixel method is to set dy to -1 if the color in pixel matches the 200 values.
    What does handlesinglepixel do with the x and y values it receives?
    Could the method be changed to take a pixel and a color and return true if the pixel has that color? Which would be a single compare.

    How are you getting the pixels to be tested? Are you sure they are correct.


    Comment on your code here: Hardcoding the 200 values is poor. Use variables.

  15. #15
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    It does nothing with them currently, I think it's safe to just remove them.

    I'm not sure what you mean by taking a pixel and a color?

    I'm testing them by making a different coloured pixel at a spot on the background, for example 50, 50 that is 255R, 0G, 0B. The method prints 255, 0, 0 so I know that the pixels being tested are correct.

    Do you mean setting a variable to 200 and doing if ( red == var .. etc ?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    what you mean by taking a pixel and a color?
    That's what handlesinglepixel does (sort of) it takes a pixel and has a HARDCODEd color built into its logic(200,200,200). I'd remove that HARDCODEd color and make it a parameter to the method.
    boolean handlesinglepixel(int pixel, Color c) ...


    How are you getting the pixels to be tested? Are you sure they are correct.

  17. #17
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    But the colour handlesinglepixel doesn't have to have the color 200, 200, 200; my if statment just checks it is?

    Oh wait, I see what you mean. So something like

    Java Code:
    public boolean handlesinglepixel( int pixel, Color col )
    {
         
         getrgb..
         
         if ( rgbvalues = col )
              return true;
         else
              return false;
         
    }
    Like that? It still doesn't solve the problem that I can't get the PixelGrabber to work, though. Thanks for your help so far, however!

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I can't get the PixelGrabber to work
    Can you make a small program that compiles and executes to demo your problem with PixelGrabber?

  19. #19
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Image;
    import java.awt.image.PixelGrabber;
    import javax.swing.ImageIcon;
    
    public class Main
    {
    	
    	public static void main ( String args[] )
    	{
    		
    		// the background image.
    		Image background = new ImageIcon( "D:/PW/Java/background.png" ).getImage( );
    		
    		// declare width & height.
    		int width = 1, height = 1;
    		
    		// create the pixel grabber.
    		int[] pixels = new int[ 2 * 2 ];
    		PixelGrabber pg = new PixelGrabber( background, 0, 0, width, height, pixels, 0, width );
    		try
    		{
    			
    			pg.grabPixels( );
    			
    		}
    		catch ( InterruptedException e )
    		{
    			
    			System.out.println( "Couldn't grab pixels." );
    			
    		}
    		
    		int pixel = pixels[ 1 * 1 ];
    		
    		int red   = ( pixel >> 16 ) & 0xff;
    		int green = ( pixel >>  8 ) & 0xff;
    		int blue  = ( pixel       ) & 0xff;
    		
    		System.out.println( red + " | " + green + " | " + blue );
    		
    	}
    	
    }

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Can you show where the problem is?
    I don't see any explanation or test code to demo the problem
    For example:
    The color at x=20, y=50 is 200, 230, 33 but my code returns 33, 44, 55 for that location.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  3. Help! Game.
    By MIA6 in forum New To Java
    Replies: 4
    Last Post: 11-08-2009, 12:22 AM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 12:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06: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
  •