Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:29:00+00:00 2026-06-09T16:29:00+00:00

All right, so I have made a basic game, in which I made a

  • 0

All right, so I have made a basic game, in which I made a character go left and right (using a and d) and allowed it to jump. What I’m trying to do right now is get it so that the terrain generated will allow the character to stop on top of it. For example, if the character jumps, then it will stop on top of a block when it touches it. What I really need is just some basic code that will work well for the code I have already written, not an entire re-writing. And as a side note, many have said to use KeyBindings instead of KeyListener, but the KeyListener works a lot better in the way I’m handling this coding. Thanks for your help!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.Random.*;
import java.util.*;
import javax.swing.JApplet;
import java.awt.Graphics;

public class Ultima extends JFrame implements KeyListener
{
    String map [][] = 
    {{" ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", "#", "#", " ", " ", " ", " ", " ", " ", " ", " ", "#", "#", " ", " ", " ", " ", " ", " ", " "},
     {" ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"},
     {" ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", "#", " ", " ", " "},
     {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#"},
     {" ", " ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", " ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {" ", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
     {"#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};

    int score = 0;
    double gravity = 1;
    boolean jumping = false;
    boolean moveRight = false;
    boolean moveLeft = false;
    boolean jumpRight = false;
    boolean jumpLeft = false;
    boolean moveRightWhileJumping = false;
    boolean moveLeftWhileJumping = false;
    final int WIDTH = 900;
    final int HEIGHT = 650;
    int RESPAWNX = WIDTH/20;
    int RESPAWNY = HEIGHT/2;

    Rectangle charRect = new Rectangle( RESPAWNX, RESPAWNY, 20, 32 );
    JLabel character = new JLabel( new ImageIcon( "characterg.gif" ) );
    JLabel scoreLabel = new JLabel( "Score: " + score );
    JMenuBar mb = new JMenuBar( );
    JMenu menuFile = new JMenu("File");
    JMenuItem menuItemSave = new JMenuItem("Save");
    JMenuItem menuItemLoad = new JMenuItem("Load");

    ArrayList trailList = new ArrayList( );
    Runner runner;
    Container cont;
    JLabel spaceLabel = null;

    public static void main( String args[] )
    {
        new Ultima( );
    }

    public Ultima( )
    {
        super( "Ultima" );
        setSize( WIDTH, HEIGHT );
        setVisible( true );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        cont = getContentPane( );
        cont.setLayout( null );
        addKeyListener( this );
        cont.setBackground( Color.WHITE );

        cont.add( mb );
        setJMenuBar( mb );
        menuFile.add( menuItemSave );
        menuFile.add( menuItemLoad );
        mb.add( menuFile );

        cont.add( scoreLabel );
        scoreLabel.setBounds( 50, 50, 100, 30 );
        scoreLabel.setFont( new Font( "arial", Font.BOLD, 13 ) );
        scoreLabel.setForeground( Color.BLACK );

        cont.add( character );
        character.setBounds( charRect.x, charRect.y, 20, 32 );

    for( int row = 0; row < map.length; row++ )
    {
        for( int col = 0; col < map[0].length; col++ )
        {
            if( map[row][col].equals( "#" ) )
            {
                spaceLabel = new JLabel( new ImageIcon( "block.png" ) );
            }
            else if( map[row][col].equals( " " ) )
            {
                spaceLabel = new JLabel(new ImageIcon( "air.png" ) );
            }
            else
            {

            }
            trailList.add( spaceLabel );
            cont.add( spaceLabel );
            cont.setComponentZOrder( spaceLabel, 1 );
            spaceLabel.setBounds( col*30, row*30, 30, 30 );
        }
    }

    repaint( );
    cont.validate( );
    runner = new Runner( );
    runner.start( );
    setContentPane( cont );
}

public void keyPressed( KeyEvent e )
{
    if( e.getKeyChar( ) == 'd' || e.getKeyChar( ) == 'D' )
    {
        moveRight = true;
    }
    if( e.getKeyChar( ) == 'a' || e.getKeyChar( ) == 'A' )
    {
        moveLeft = true;
    }
}

public void keyReleased( KeyEvent e )
{
    if( e.getKeyChar( ) == 'd' || e.getKeyChar( ) == 'D' )
    {
        moveRight = false;
    }
    if( e.getKeyChar( ) == 'a' || e.getKeyChar( ) == 'A' )
    {
        moveLeft = false;
    }
}

public void keyTyped( KeyEvent e )
{
    if( e.getKeyChar( ) == KeyEvent.VK_SPACE )
    {
        jumping = true;
    }
}

public class Runner extends Thread
{
    public void run( )
    {
        while( true )
        {
            try
            {
                int j = 10;
                double t = 0;

                while( jumping )
                {
                    charRect.y = ( int ) ( charRect.y - j + gravity );
                    gravity *= 1.2;
                    character.setBounds( charRect.x, charRect.y, 20, 32 );
                    repaint( );
                    cont.validate( );
                    t++;
                    Thread.sleep( 30 );//basically, lower #, faster, and higher fps
                }

                if( moveLeft )
                {
                    charRect.x = charRect.x - ( int ) ( j/5 );
                    character.setBounds( charRect.x, charRect.y, 20, 32 );
                    repaint( );
                    cont.validate( );
                    t++;
                    Thread.sleep( 30 );
                }
                if( moveRight )
                {
                    charRect.x = charRect.x + ( int ) ( j/5 );
                    character.setBounds( charRect.x, charRect.y, 20, 32 );
                    t++;
                    Thread.sleep( 30 );
                    repaint( );
                    cont.validate( );
                }
                scoreLabel.setText( "Score: " + score );
            }
            catch( Exception e )
            {
                break;
            }
        }
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:29:01+00:00Added an answer on June 9, 2026 at 4:29 pm

    I think you will find this guide on implementing 2d platformers useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All right so I tried using the button set. So fair, I have been
Right now we have AD/Exchange to manage all of our users logins/e-mail on-site at
Right now, I have all the employees of my company login to an external
All right, do not get angry now, I know there are several questions about
Im trying to get into some basic JavaFX game development and I'm getting confused
I have a very basic LEFT OUTER JOIN to return all results from the
I have two Lists, the firs (right) represents a list of all cars and
I'm using the OnIdle-event for some simple animations, and it works all right. The
I have made a simple web page using jQuery. When opening it on another
I have made a form with the php code right under the html form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.