gpt4 book ai didi

java - 对于基于图 block 的横向卷轴游戏,如何让角色在跳跃后遇到方 block 时停止?

转载 作者:行者123 更新时间:2023-11-30 04:42:35 27 4
gpt4 key购买 nike

好吧,我制作了一个基本游戏,其中我让一个角色向左和向右移动(使用 a 和 d)并允许它跳跃。我现在想做的就是获取它,以便生成的地形允许角色停在它上面。例如,如果角色跳跃,那么当它接触到一个 block 时,它会停在它的顶部。我真正需要的只是一些适用于我已经编写的代码的基本代码,而不是整个重写。作为旁注,许多人都说使用 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;
}
}
}
}
}

最佳答案

我想你会发现this guide对于实现 2D 平台游戏很有用。

关于java - 对于基于图 block 的横向卷轴游戏,如何让角色在跳跃后遇到方 block 时停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com