Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 59

Thread: 2D Game

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

    Default

    It returns 0, 0, 0. If I fill the whole canvas red (255, 0, 0) it'll just return 0, 0, 0.

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    The code you posted only gets one pixel a location 0,0. Remember the array index is 0 based.
    int pixel = pixels[ 0 * 0 ];

    When I run your code with your image I get: 186 | 186 | 186 for the pixel at 0,0.

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

    Default

    Aha! Yes, perfect! Thanks a lot, I'll try and integrate it into the game now!

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

    Default

    Java Code:
    		if ( key == KeyEvent.VK_DOWN )
    		{
    			
    			String downCol = checkForCollission( 'd' );
    			System.out.println( "DOWNCOL: " + downCol );
    			
    			if ( downCol == "200 | 200 | 200" )
    				System.out.println( "It's that colour!" );
    			else
    				dy = 1;
    			
    		}
    Java Code:
    Console Output:
    DOWNCOL: 200 | 200 | 200
    DOWNCOL: 200 | 200 | 200
    DOWNCOL: 200 | 200 | 200
    DOWNCOL: 200 | 200 | 200
    DOWNCOL: 200 | 200 | 200
    I'm now making it so that when you press the down key it calls the checkForCollission function. If it returns the color 200 | 200 | 200 then we want to stop the player from moving down, otherwise let's move down. However, even though downCol is 200 | 200 | 200 when printed, the if statement seems to say it isn't and goes straight to the else..

    Any ideas?

    No worries, just did this:

    if ( downCol.equals( "200 | 200 | 200" ) )
    Last edited by Elffus; 08-02-2010 at 05:45 PM.

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

    Default

    That's weird code. What's the meaning of: "200 | 200 | 200"
    Why does the method return that string?????

    What is the purpose/function of the checkForCollission method? Should it return a boolean if there is a collusion? A collusion being if the next pixel in the direction being moved in is a particular color:
    Java Code:
    boolean checkForCollission(Point, <direction>, <BoundaryColor>)
    // Returns true if a move from  Point in direction is BoundaryColor
    BTW Use the equals() method to compare Strings

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

    Default

    I know, I edited it at the bottom, just had to use equals. Right, the meaning of 200 | 200 | 200. Basically the Collision function gets the RGB ( R200 | G200 | B200) values of the 3 pixels infront/below it, etc. It returns them, and if the function then picks them up we can stop the player moving. Here is what I have for when the right key is pressed:

    Java Code:
    		if ( key == KeyEvent.VK_RIGHT )
    		{
    			
    			String downCol = checkForCollission( 'r' );
    			int right = downCol.indexOf( "200 | 200 | 200232 | 232 | 232" );
    			//int skySpace = downCol.indexOf( "137 | 205 | 255" );
    			
    			System.out.println( downCol );
    			//System.out.println( "Floor: " + floor + " | Skyspace: " + skySpace );
    			//if ( downCol.equals( "200 | 200 | 200" ) )
    			if ( right < 0 )
    			{
    				
    				ImageIcon i = new ImageIcon( "D:/PW/Java/right.png" );
    				still = i.getImage( );
    				dx = 1;
    				
    			}
    			
    		}
    If the player is next to the wall, and they press the right arrow key it wont let them through (the wall has two colors). However, if they hold it down they can get through easily as this is only KeyPressed. Is their anyway to continuously check when it's being held?

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    downCol.indexOf( "200 | 200 | 200232 | 232 | 232" );
    This is getting weirder and weirder.

    the wall has two colors
    Pass an array of boundary colors to the collusion tester

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

    Default

    Java Code:
    	public boolean checkForCollision( char type, String match[] )
    	{
    		
    		String returnS = "";
    		
    		for ( int i = 0; i < 2; i++ )
    		{
    			
    			// the background image.
    			Image background = new ImageIcon( "D:/PW/Java/background.png" ).getImage( );
    			
    			// declare width & height.
    			int width = 1, height = 1;
    			
    			// automtatically a right check.
    			int playerX = x + ( still.getWidth( null ) - 1 + i );
    			int playerY = y + ( still.getHeight( null ) / 2 );
    			
    			// if it's a down check.
    			if ( type == 'd' )
    			{
    				
    				playerX = x + ( still.getWidth( null ) / 2 );
    				playerY = y + ( still.getHeight( null ) - 1 + i );
    				
    			}
    			
    			// create the pixel grabber.
    			int[] pixels = new int[ 1 * 1 ];
    			PixelGrabber pg = new PixelGrabber( background, playerX, playerY, width, height, pixels, 0, width );
    			try
    			{
    			
    			pg.grabPixels( );
    			
    			}
    			catch ( InterruptedException e )
    			{
    			
    			System.out.println( "Couldn't grab pixels." );
    			
    			}
    			
    			int pixel = pixels[ 0 * 0 ];
    			
    			int red   = ( pixel >> 16 ) & 0xff;
    			int green = ( pixel >>  8 ) & 0xff;
    			int blue  = ( pixel       ) & 0xff;
    			
    			returnS = returnS + ( red + " | " + green + " | " + blue );
    			
    			System.out.println( returnS );
    			
    		}
    				
    		return false;
    		
    	}
    I constructed it as you advised.. but I am unsure of how to check whether the pixels match what the array wants?

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Here's a very rough sample of my idea:
    Java Code:
    public boolean checkForCollision(char direction,  int x, int y, Color[] boundaryColors ) {
    		// check the color at the point in the direction
                    int pixel = getColorInDirection(x,y,direction);
    		
    		for ( int i = 0; i < boundaryColors.length; i++ ) {
                         if(boundaryColors[i].getRGB() == pixel)
                              return true;
                    }
                    return false; // pixel doesn't touch a boundary
        }

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

    Default

    Okay, thanks! I created this:

    Java Code:
    	public boolean checkForCollision( char type, Color[] match )
    	{
    		
    		// declare width & height.
    		int width = 1, height = 1;
    		
    		// automatically a right check.
    		int playerX = x + ( still.getWidth( null ) - 1 );
    		int playerY = y + ( still.getHeight( null ) / 2 );
    		
    		// if it's a down check.
    		if ( type == 'd' )
    		{
    			
    			playerX = x + ( still.getWidth( null ) / 2 );
    			playerY = y + ( still.getHeight( null ) - 1 );
    			
    		}
    		
    		// create the pixel grabber.
    		int[] pixels = new int[ 1 * 1 ];
    		PixelGrabber pg = new PixelGrabber( background, playerX, playerY, width, height, pixels, 0, width );
    		try
    		{
    			
    			pg.grabPixels( );
    			
    		}
    		catch ( InterruptedException e )
    		{
    			
    			System.out.println( "Couldn't grab pixels." );
    			
    		}
    		
    		int pixel = pixels[ 0 * 0 ];
    		
    		int red   = ( pixel >> 16 ) & 0xff;
    		int green = ( pixel >>  8 ) & 0xff;
    		int blue  = ( pixel       ) & 0xff;
    		
    		//returnS = returnS + ( red + " | " + green + " | " + blue );
    		//System.out.println( returnS );
    		
    		Color currentColor = new Color( red, green, blue );
    		
    		System.out.println( "|| COLORCURRENT ||" + currentColor );
    		
    		for ( int j = 0; j < match.length; j++ )
    		{
    			
    			System.out.println( "|| COLORMATCH ||" + match[j] );
    			
    			if ( match[j].equals( currentColor ) )
    			{
    				
    				// pixel hits a boundary.
    				System.out.println( "|| INCREMENTING ||" );
    				return true;
    				
    			}
    			
    		}
    		
    		// nothing is touched.
    		return false;
    		
    	}
    and to run this:

    Java Code:
    		if ( key == KeyEvent.VK_DOWN )
    		{
    			
    			Color[] typeArray = { greyEdge, whiteParts };
    			boolean collision = checkForCollision( 'd', typeArray );
    			
    			if ( collision == false )
    			{
    				
    				dy = 1;
    				
    			}
    			
    			
    		}
    It works perfectly in the sense that if you stop at a door and press the key you wont be able to go through - but if you hold down the key you can (because this is only in the KeyPress function). How do I make it check whenever the character is moving, rather than when the key is pressed?

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    How do I make it check whenever the character is moving
    "is moving" means at different locations at different times. To detect rate of movement, you need to save an older location and the time the character was there and then get the time at the new location and compare. If the locations for different times are the same, the character did not move over that time slice.

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

    Default

    Okay. I recoded the whole base and redid the Sprite class. To check collisions I now have a separate image, and you draw red lines to symbolize a collision. The only problem now is that when you run the applet and you move you can sometimes run through the collisions.

    It checks like this:

    Java Code:
    	public void update( long timePassed )
    	{
    		
    		// call collision check if velocityX/Y has a value.
    		if ( velocityX != 0 )
    		{
    			
    			// Check for a collision.
    			if ( velocityX > 0 )
    				aCollision = runCollisionCheck( "right" );
    			else if ( velocityX < 0 )
    				aCollision = runCollisionCheck( "left" );
    			
    			//System.out.printf( "RUNNING COLLISION CHECK (%s)\n", aCollision );
    			
    			if ( aCollision == false )
    			{
    				
    				// So if velocity is distance / time, then
    				// distance = velocity * time.
    				x += velocityX * timePassed;
    				
    			}
    			else if ( aCollision == true )
    			{
    				
    				velocityX = 0;
    				//System.out.println( "STOPPING CHARACTER (X)" );
    				
    			}
    			
    			animation.update( timePassed );
    			
    		}
    		
    	}
    Which is called in Sprite.java.

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    problem now is that when you run the applet and you move you can sometimes run through the collisions.
    Sounds like a logic problem.
    How do you flag a collision? Does the code check that a collision has occurred and then NOT run thru it? What prevents "running thru" a collision?

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

    Default

    The code above. If their is a collision it sets the velocity to 0. Otherwise, it moves the player depending on the time it takes to move.

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    So what allows it to "run thru" a collision? The code must not see or it ignores the condition. Time for some more debugging.

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

    Default

    Java Code:
    if ( velocityX > 0 )
    				aCollision = runCollisionCheck( "right" );
    So when the velocity for moving across X is higher than 0 (it has to be to move) it makes a check for a collision.

    runCollisionCheck:
    Java Code:
    	private boolean runCollisionCheck( String side )
    	{
    		
    		int[] pixels;
    		int playerX, playerY;
    		int imageHeight, imageWidth;
    		Image getImage = getImage( );
    		
    		//for ( int i = 0; i < 4; i++ )
    		//{
    			
    			// we need to check each side of the sprite.
    			//playerX = x + ( still.getWidth( null ) - 1 );
    			//playerY = y + ( still.getHeight( null ) / 2 );
    			
    			imageHeight = getImage.getHeight( null );
    			imageWidth = getImage.getWidth( null );
    			
    			// check right.
    			if ( side == "right" )
    			{
    				
    				pixels = new int[ 1 * 1 ];
    				playerX = Math.round( x + ( imageWidth - 1 ) );
    				
    				// inner loop. check every pixel on the right side of the player.
    				for ( int j = 0; j < 2; j++ )
    				{
    					
    					if ( j == 0 )
    						playerY = Math.round( y );
    					else
    						playerY = Math.round( y + imageHeight - 1);
    					
    					PixelGrabber pg = new PixelGrabber( collisionImg, playerX, playerY, 1, 1, pixels, 0, 1 );
    					try
    					{
    						
    						pg.grabPixels( );
    						
    					}
    					catch ( InterruptedException e )
    					{
    						
    						System.out.println( "Couldn't grab pixels (1)." );
    						
    					}
    					
    					int pixel = pixels[ 0 * 0 ];
    					
    					int red   = ( pixel >> 16 ) & 0xff;
    					int green = ( pixel >>  8 ) & 0xff;
    					int blue  = ( pixel       ) & 0xff;
    					
    					//String returnS = ( red + " | " + green + " | " + blue );
    					//System.out.println( returnS );
    					
    					Color currentColor = new Color( red, green, blue );
    					
    					if ( currentColor.equals( Color.RED ))
    						return true;
    					
    					
    				}
    				
    			}
    So if there is a collision pixel one across from the top right of the sprite, or the bottom of the sprite, it returns true.

    Java Code:
    			else if ( aCollision == true )
    			{
    				
    				velocityX = 0;
    				//System.out.println( "STOPPING CHARACTER (X)" );
    				
    			}
    Which stops the character. I'll start the applet and run through the wall now; with the debugs uncommented. Each test I'll run the character through a collision line (red) of 1px.

    First run:
    Java Code:
    MOVING CHARACTER (X)
    RUNNING COLLISION CHECK (true)
    STOPPING CHARACTER (X)
    Second run:
    Java Code:
    RUNNING COLLISION CHECK (false)
    MOVING CHARACTER (X)
    Java Code:
    RUNNING COLLISION CHECK (false)
    MOVING CHARACTER (X)
    SO as you can see, sometimes it works, other times it doesn't.

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    other times it doesn't.
    What is there about the times it doesn't? What is different? Why doesn't it stop?
    The only info I see in your debug output is true one time and false another.
    What other variables can you print out to show WHY the code works differently?

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

    Default

    I don't think there is any other variables. My class which implements Runnable calls

    Java Code:
    gameUpdate( timePassed );
    in the run( ) method.

    Java Code:
    	public void gameUpdate( long timePassed )
    	{
    		
    		if ( gameOver )
    			isRunning = false;
    				
    		player.update( timePassed );
    		
    	}
    That above is game update. Then that calls

    Java Code:
    public void update( long timePassed )
    	{
    		
    		doUpdate = false;
    		
    		// call collision check if velocityX/Y has a value.
    		if ( velocityX != 0 )
    		{
    			
    			aCollision = false;
    			
    			// Check for a collision.
    			if ( velocityX > 0 )
    				aCollision = runCollisionCheck( "right" );
    			else if ( velocityX < 0 )
    				aCollision = runCollisionCheck( "left" );
    			
    			System.out.printf( "RUNNING COLLISION CHECK (%s)\n", aCollision );
    			
    			if ( aCollision == false )
    			{
    				
    				// So if velocity is distance / time, then
    				// distance = velocity * time.
    				System.out.println( "MOVING CHARACTER (X)" );
    				doUpdate = true;
    
    				
    			}
    			else if ( aCollision == true )
    			{
    				
    				velocityX = 0;
    				System.out.println( "STOPPING CHARACTER (X)" );
    				
    			}
    			
    		}
    		
    		if ( velocityY == 0 )
    		{
    			
    			aCollision = false;
    			aCollision = runCollisionCheck( "below" );
    			
    			System.out.printf( "RUNNING COLLISION CHECK (Y0) (%s)\n", aCollision );
    			
    			if ( aCollision == false )
    			{
    				
    				velocityY = 0.1f;
    				System.out.println( "MOVING CHARACTER (Y0)" );
    				doUpdate = true;
    				
    			}
    			
    			animation.update( timePassed );
    			
    		}
    		
    		// call collision check if velocityX/Y has a value.
    		if ( velocityY != 0 )
    		{
    			
    			aCollision = false;
    			
    			// Check for a collision.
    			if ( velocityY < 0 )
    				aCollision = runCollisionCheck( "top" );
    			else if ( velocityY > 0 )
    				aCollision = runCollisionCheck( "below" );
    			
    			System.out.printf( "RUNNING COLLISION CHECK (Y) (%s)\n", aCollision );
    			
    			if ( aCollision == false )
    			{
    				
    				// So if velocity is distance / time, then
    				// distance = velocity * time.
    				System.out.println( "MOVING CHARACTER (Y)" );
    				doUpdate = true;
    				
    			}
    			else if ( aCollision == true )
    			{
    				
    				velocityY = 0;
    				System.out.println( "STOPPING CHARACTER (Y)" );
    				
    			}
    			
    		}
    		
    		if ( doUpdate )
    		{
    			
    			// So if velocity is distance / time, then
    			// distance = velocity * time.
    			x += velocityX * timePassed;
    			y += velocityY * timePassed;
    			animation.update( timePassed );
    			// elf's code is sex.
    			// don't you think? :D
    			
    		}
    		
    	}
    (Yes I have modified it slightly) but it still doesn't always get it. Sometimes it does, other time's it doesn't. Yet the velocity is 0.1, the lowest it can be when you take into account it uses Math.round to get the exact pixel. I don't think there are any other variables, well there aren't any for sure as far as I know.

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    I don't think there is any other variables
    By variables I mean: x, y, velocityX, velocityY, timePassed, etc
    These variables (and maybe some others) control how your code executes.
    You need to look at how these variables change. Are they changing unexpectedly or to values that you don't handle? Your debug output doesn't show them.

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

Page 2 of 3 FirstFirst 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
  •