よし、基本的なゲームを作った。キャラクターを左右に動かし (a と d を使用)、ジャンプさせた。私が今やろうとしているのは、生成された地形によってキャラクターがその上で停止できるようにすることです。たとえば、キャラクターがジャンプした場合、ブロックに触れるとブロックの上で止まります。私が本当に必要としているのは、全体を書き直すことではなく、すでに書いたコードでうまく機能するいくつかの基本的なコードです。また、補足として、多くの人が KeyListener の代わりに KeyBindings を使用すると言っていますが、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;
}
}
}
}
}