- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我似乎无法重置我的游戏。我想要发生的是一旦 numLives
达到 0 消息应该显示:
press R to Restart
我添加了我认为是“r”的 KeyListener
的内容,它符合要求,但不起作用。
其他一切似乎都按计划进行。
class CharlieBrownGamePanel extends JPanel implements Runnable
{
/** data variables */
private int xCord=135, yCord=0;
private int randXCord, randYCord;
//private String movingMsg = "Moving Forward ";
private int numHits=0, numLives=5;
private boolean xNeedsToTurn=false, yNeedsToTurn=false;
private String message = "CLICK ON THE BALL TO WIN!";
private String hitsMsg = "Number of Hits"+numHits;
private String livesMsg = "Number of Lives: "+numLives;
private boolean hitTarget = false;
private static final int EDGE = 30;
private static final int ADJUST = 5;
private Dimension dim = null;
private Thread animate = null;
public CharlieBrownGamePanel(Dimension dim) // as a JPanel, need a constructor
{
this.dim = dim;
setBackground(Color.black);
setForeground(Color.blue);
addMouseListener(new MouseHandler());
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== 'r') {
xCord=getRandXCord();
yCord=getRandYCord();
animate.start();
numHits=0;
numLives=5;
hitTarget = false;
hitsMsg = "Number of Hits:" +numHits;
livesMsg= "Number of Lives: "+numLives;
message = "MISSED AGAIN";
repaint();
}
}
});
}
public void run() // as a Runnable class, need to override run() method
{
try
{
while(true)
{
repaint();
Thread.sleep(100);
// char temp = movingMsg.charAt(0);
// movingMsg = movingMsg.substring(1,movingMsg.length());
// movingMsg += temp ;
/** determine if ball is close to edge of screen,
if so, reverse ball's direction
*/
if(!xNeedsToTurn && xCord<(dim.width-EDGE)) //if still more room to go right
{
xCord+=ADJUST;
}
else // otherwise
{
xNeedsToTurn = (xCord>=EDGE);
xCord-=ADJUST;
}
if(!yNeedsToTurn && yCord<(dim.height-EDGE)) //if still more room to go down
{
yCord+=ADJUST;
}
else // otherwise
{
numLives-=1;
xCord=getRandXCord();
yCord=getRandYCord();
hitsMsg = "Number of Hits:" +numHits;
livesMsg= "Number of Lives: "+numLives;
message = "MISSED AGAIN";
}
if(numLives==0)
{
animate.stop();
hitsMsg = "Number of Hits:" +numHits;
livesMsg= "Number of Lives: "+numLives;
message = "GAME OVER! Press R to restart.";
}
}//end of while loop
}
catch(InterruptedException e) {
}
} //end of run method
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.setColor(Color.red);
g.fillOval(xCord,yCord,15,15);
g.setFont(new Font("SanSerif",Font.BOLD,25));
g.drawString(message,30,30);
g.drawString(hitsMsg,30,70);
g.drawString(livesMsg,30,110);
}
public void checkForHit(int newx, int newy)
{
if((newx >=xCord) && (newx <=(xCord+15)) &&
(newy >=yCord) && (newy <=(yCord+15)))
{
hitTarget = true;
numHits+=1;
hitsMsg = "Number of Hits:" +numHits;
livesMsg= "Number of Lives: "+numLives;
message = "That's a Hit!";
xCord=getRandXCord();
yCord=getRandYCord();
}
else
{
hitTarget = false;
hitsMsg = "Number of Hits:" +numHits;
livesMsg= "Number of Lives: "+numLives;
message = "MISSED AGAIN";
}
}
/** USING THE ADAPTER CLASS FOR Mouse Listener */
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
checkForHit(e.getX(),e.getY());
repaint();
}
} //end of MouseHandler class
private int getRandXCord(){
randXCord= (int)(Math.random() * (dim.width +1));
return randXCord;
}
private int getRandYCord(){
randYCord= (int)(Math.random() * (100 +1));
return randYCord;
}
} //end of class task and panel
最佳答案
首先,KeyEvent#getKeyCode
返回虚拟键代码,而不是字符。看一下 KeyEvent.VK_R
。
其次,KeyListerners
仅当它们注册的组件可聚焦并且具有键盘焦点时才会响应。 JPanel
,默认情况下无法接收键盘焦点
第三,您应该使用 Key Bindings因为他们会克服这些缺点
关于java - 在 JApplet 上使用 KeyAdapter 和 Thread 重置/重新启动我的迷你游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16559354/
我编写了一些应用程序并想向其中添加一些键盘输入。我的主类扩展了 JPanel,因此我可以将 keyAdapter 添加到构造函数中。keyAdapter 是一个名为“InputAdapter”的新类,
我正在使用 Java 尝试制作一款基本游戏,但在使用 KeyAdapter 时遇到了一些问题。我以前使用过一种非常相似的格式,并认为我理解它,但这让我感到难过。 任何帮助将不胜感激,这是我正在使用的主
该程序编译执行成功。但是当我输入一些字符时,框架不会显示其中的这些字符。为什么 ?错误是什么? import java.awt.*; import java.awt.event.*; import j
我正在创建一个简单的突破游戏。但是,KeyAdapter 未接收输入。代码对我来说看起来不错,但也许我错过了一些更基本的东西? public DatGamePanel(BustOut bo, long
将 KeyListener 类添加到 org.eclipse.swt.widgets.Text 类。当我输入 'a' 时,我得到多个 keyEvent。我只期待一个关键事件。这是为什么。 text.a
我目前正在编写一个小程序,我应该在其中使用基本转换。现在,我正在研究如何使用箭头键移动多边形。现在我可以通过按鼠标将其向右移动,但我更愿意使用向右箭头键。但是,无论我尝试哪种方法,我都无法做到。 im
我正在用java制作一个简单的游戏,并且我正在使用按键适配器通过按键进行移动。它工作正常,但是当我切换方向时,玩家会向新方向移动一个像素,然后停止一会儿,然后就正常了(你知道,当你在键盘上按一个字母时
我正在为一个学校项目使用 Java 制作一个简单的游戏,并且我将 JFrame 与扩展 KeyAdapter 的类结合使用以监听输入。但是,一旦我将焦点转移到另一个窗口(例如截图工具)然后返回游戏窗口
背景 我有一个简单的游戏。我有一个 JPanel 类,里面什么都有(菜单、游戏、游戏结束)。 然后我决定,我应该让我的游戏更好,我制作了两个面板(一个用于菜单,第二个用于游戏等级)。 一切都很好,但是
我只是想了解简单绘图程序的基础知识。我有一个程序可以绘制一个矩形,然后让我将其向任何方向移动。我使用线程来实现平滑的 Action 。然而,令我困扰的是,如果我按下三个键并释放其中两个,程序将不会检测
我如何在这段代码中使用键绑定(bind),因为键适配器不能正常工作?我已经查看了键绑定(bind)的文档,但我不明白如何执行此操作。 private class KeyLis extends KeyA
大家好,我使用的是 netbeans 7.2.1并正在遵循键盘输入教程 这段代码应该在 JFrame 中画一个圆圈,它确实做到了但随后应该使用箭头键输入来移动它,但事实并非如此 谢谢你的帮助:) pa
我创建了一个简单的 Nibbles (Snakes) 游戏并且一切正常。 但是,当我将所有内容复制到我的 mac(在 PC 上开发)并编译相同的 java 文件时,用于移动我的蛇的 keyAdapte
只是想知道创建需要处理鼠标点击和按键的类的最佳实践方法是什么。显然不可能在 Java 中继承多个类,所以目前我正在使用 MouseListener 和 KeyListener 接口(interface
这是我的代码: tabbedPane.addKeyListener(new java.awt.event.KeyAdapter() { public void
当我尝试为该组件之外的某个 swing 组件添加 KeyListener 对象时,构造函数 KeyListener 没有回答,为什么?例如: public class ActionViewer
就目前情况而言,这个问题不太适合我们的问答形式。我们期望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
背景 我正在构建一个游戏库以加快游戏开发速度,到目前为止进展相当顺利。 我有以下内容: 主入口类public static void main(String[] args) 这个类然后从我的库中启动一
我似乎无法重置我的游戏。我想要发生的是一旦 numLives 达到 0 消息应该显示: press R to Restart 我添加了我认为是“r”的 KeyListener 的内容,它符合要求,但不
我是一名优秀的程序员,十分优秀!