- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我制作了一个基本游戏,其中我让一个角色向左和向右移动(使用 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/
好吧,我有这个静态的导航菜单,但我想让它在用户向下或向上滚动他的页面时流畅地流动...请帮助编写代码,因为我已经尝试过做一些事情,但一切都变坏了。 代码:
我有两个 ionic 滚动标签。在每个 ion-scroll 标签中都有一个列表。这些列表中的每一个都以 10 个项目开始,当用户到达特定 ionic 卷轴的底部时,它应该加载更多。我面临的问题是这无
我的 ionic 应用程序中有一个看起来像这样的模板: {{ monthName }} ... 类为“nav”的
我拼命地试图将目标 div 保持在 float 菜单栏下方。正确的做法是什么? $(document).ready(function () { $('a[href^="#"]').on('cl
我在 silverstripe 网站上运行了无限的 ajax 滚动。我还运行了“NoneLeft”扩展,让人们知道,他们到达了底部。 IAScroll 在 ArticleHolder 页面中显示 Ar
我是一名优秀的程序员,十分优秀!