1

好的,所以我制作了一个基本游戏,其中我让一个角色左右移动(使用 a 和 d)并让它跳跃。我现在要做的是得到它,以便生成的地形将允许角色停在它上面。例如,如果角色跳跃,那么当它接触到它时它会停在一个块的顶部。我真正需要的只是一些可以很好地适用于我已经编写的代码的基本代码,而不是整个重写。作为旁注,许多人说使用 KeyBindings 而不是 KeyListener,但 KeyListener 在我处理此编码的方式上工作得更好。谢谢你的帮助!

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;
            }
        }
    }
}
}
4

1 回答 1

2

我想你会发现这个关于实现 2d 平台游戏的指南很有用。

于 2012-08-12T02:06:26.543 回答