Java 带有KeyListener的Pong控件
我遇到了一点麻烦让我的乒乓球游戏工作,这个项目开始只是简单地使一个球有物理,但后来我决定做一些更多的工作 我让球来回弹跳,除了W键和S键不能控制球员1,上下箭头键不能控制球员2Java 带有KeyListener的Pong控件,java,swing,keylistener,key-bindings,pong,Java,Swing,Keylistener,Key Bindings,Pong,我遇到了一点麻烦让我的乒乓球游戏工作,这个项目开始只是简单地使一个球有物理,但后来我决定做一些更多的工作 我让球来回弹跳,除了W键和S键不能控制球员1,上下箭头键不能控制球员2 public void keyPressed(KeyEvent e){ if(e.getKeyCode() == e.VK_UP){ p2Y -= 3; System.out.println("UP");
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == e.VK_UP){
p2Y -= 3;
System.out.println("UP");
}
if(e.getKeyCode() == e.VK_DOWN){
p2Y += 3;
System.out.println("Down");
}
if(e.getKeyCode() == e.VK_W){
p1Y -= 3;
System.out.println("Up");
}
if(e.getKeyCode() == e.VK_S){
p1Y += 3;
System.out.println("down");
}
repaint();
}
它甚至不会显示系统打印消息
我不知道问题是出在代码的这一部分,还是出在其他地方
如果它在其他地方,这里是指向该文件其余部分的链接
整个代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Pong extends JPanel
{
Circle circle1;
javax.swing.Timer timer;
ArrayList<Point> points = new ArrayList<Point>();
int p1X=10, p1Y=320;
int p2X=760, p2Y = 320;
int y, y1;
int p1Score, p2Score;
boolean playing = true;
public Pong(Color backcolor, int Width, int Height)
{
setBackground(backcolor);
setPreferredSize(new Dimension(Width, Height));
//first circle
circle1 = new Circle(Width / 2, 360, 15, Color.white);
circle1.setVelocity(4);
circle1.setDirection(180);
timer = new javax.swing.Timer(5, new MoveListener());
addKeyListener(new KeyWatcher());
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//first circle
circle1.draw(g);
circle1.fill(g);
// Draw Players
g.setColor(Color.red);
g.fillRect(p1X, p1Y, 10, 75);
g.setColor(Color.blue);
g.fillRect(p2X, p2Y, 10, 75);
//Draw Scores
g.setColor(Color.white);
g.drawString("Player 1's Score: " + p1Score, 10, 10);
g.drawString("Player 2's Score: " + p2Score, getWidth() - 120, 10);
repaint();
if(!playing)
{
g.setColor(Color.red);
g.drawString("GAMEOVER!!!", (getWidth() / 2) - 15, 10);
}
else
{
}
}
private class MoveListener extends MouseAdapter implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
int x1 = circle1.getX();
int y1 = circle1.getY();
int width = getWidth();
int height = getHeight();
int radius1 = circle1.getRadius();
//first circle
if(x1 - radius1 <= 0)
{
p2Score++;
circle1.setX(getWidth()/2);
circle1.setY(getHeight()/2);
}
if(x1 + radius1 >= width)
{
p1Score++;
circle1.setX(getWidth()/2);
circle1.setY(getHeight()/2);
}
if(y1 - radius1 <= 0 || y1 + radius1 >= height )
{
circle1.turnY();
}
if((x1 - radius1 == p1X) || (x1 + radius1 == p2X))
{
circle1.turnX();
}
circle1.move();
repaint();
}
public void GameOver()
{
playing = false;
}
}
private class KeyWatcher implements KeyListener
{
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == e.VK_UP){
p2Y -= 3;
System.out.println("UP");
}
if(e.getKeyCode() == e.VK_DOWN){
p2Y += 3;
System.out.println("Down");
}
if(e.getKeyCode() == e.VK_W){
p1Y -= 3;
System.out.println("Up");
}
if(e.getKeyCode() == e.VK_S){
p1Y += 3;
System.out.println("down");
}
repaint();
}
@Override
public void keyReleased(KeyEvent e){
//if(e.getKeyCode() == e.VK_UP){
// y1 = p2Y;
// repaint();
// }
// if(e.getKeyCode() == e.VK_DOWN){
// y1 = p2Y;
// repaint();
// }
//if(e.getKeyCode() == e.VK_W){
// y = p1Y;
// repaint();
// }
// if(e.getKeyCode() == e.VK_S){
// y = p1Y;
// repaint();
// }
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
公共类Pong扩展了JPanel
{
圆圈1;
javax.swing.Timer;
ArrayList points=新的ArrayList();
int p1X=10,p1Y=320;
int p2X=760,p2Y=320;
int y,y1;
整型p1分,p2分;
布尔播放=真;
公共乒乓球(颜色背景色、整数宽度、整数高度)
{
挫折背景(背景色);
setPreferredSize(新尺寸(宽度、高度));
//第一圈
圆圈1=新圆圈(宽度/2,360,15,颜色为白色);
圆圈1.设定速度(4);
圆圈1.设定方向(180);
timer=newjavax.swing.timer(5,newmovelistener());
addKeyListener(新的KeyWatcher());
timer.start();
}
公共组件(图形g)
{
超级组件(g);
//第一圈
圆圈1.绘制(g);
圆圈1.填充物(g);
//吸引玩家
g、 setColor(Color.red);
g、 fillRect(p1X,p1Y,10,75);
g、 setColor(Color.blue);
g、 fillRect(p2X,p2Y,10,75);
//得分
g、 setColor(Color.white);
g、 抽绳(“玩家1的分数:”+P1分数,10,10);
g、 抽绳(“玩家2的分数:+p2Score,getWidth()-120,10);
重新油漆();
如果(!玩)
{
g、 setColor(Color.red);
g、 抽绳(“GAMEOVER!!!”,(getWidth()/2)-15,10;
}
其他的
{
}
}
私有类MoveListener扩展MouseAdapter实现ActionListener
{
已执行的公共无效操作(操作事件arg0)
{
int x1=circle1.getX();
int y1=circle1.getY();
int width=getWidth();
int height=getHeight();
int radius1=circle1.getRadius();
//第一圈
如果(x1-半径1=宽度)
{
p1分数++;
circle1.setX(getWidth()/2);
圆圈1.setY(getHeight()/2);
}
如果(y1-半径1=高度)
{
圆圈1.turnY();
}
如果((x1-半径1==p1X)| |(x1+半径1==p2X))
{
圆圈1.turnX();
}
圆圈1.move();
重新油漆();
}
公众假期
{
玩=假;
}
}
私有类KeyWatcher实现KeyListener
{
@凌驾
按下公共无效键(键事件e){
如果(e.getKeyCode()==e.VK_UP){
p2Y-=3;
System.out.println(“UP”);
}
如果(e.getKeyCode()==e.VK_DOWN){
p2Y+=3;
System.out.println(“向下”);
}
如果(e.getKeyCode()==e.VK_W){
p1Y-=3;
System.out.println(“Up”);
}
如果(e.getKeyCode()==e.VK_S){
p1Y+=3;
System.out.println(“向下”);
}
重新油漆();
}
@凌驾
公开无效密钥已释放(密钥事件e){
//如果(e.getKeyCode()==e.VK_UP){
//y1=p2Y;
//重新油漆();
// }
//如果(e.getKeyCode()==e.VK_DOWN){
//y1=p2Y;
//重新油漆();
// }
//如果(e.getKeyCode()==e.VK_W){
//y=p1Y;
//重新油漆();
// }
//如果(e.getKeyCode()==e.VK_S){
//y=p1Y;
//重新油漆();
// }
}
@凌驾
public void keyTyped(KeyEvent arg0){
//TODO自动生成的方法存根
}
}
}
我的新代码
这个游戏的问题是一次只能控制一个玩家
它也有我愚蠢的尝试,通过使2 MyKeyAction来修复它,但它根本没有改变它
package Bouncy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Pong extends JPanel
{
Circle circle1;
javax.swing.Timer timer;
ArrayList<Point> points = new ArrayList<Point>();
int p1X=10, p1Y=320;
int p2X=760, p2Y = 320;
int y, y1;
int p1Score, p2Score;
boolean playing = true;
public Pong(Color backcolor, int Width, int Height)
{
setBackground(backcolor);
setPreferredSize(new Dimension(Width, Height));
//first circle
circle1 = new Circle(Width / 2, 360, 15, Color.white);
circle1.setVelocity(4);
circle1.setDirection(180);
timer = new javax.swing.Timer(5, new MoveListener());
setupKeyBinding();
setupKeyBinding2();
timer.start();
}
private void setupKeyBinding() {
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inMap = getInputMap(condition);
ActionMap actMap = getActionMap();
// this uses an enum of Direction that holds ints for the arrow keys
for (DirectionP1 direction : DirectionP1.values()) {
int key = direction.getKey();
String name = direction.name();
// add the key bindings for arrow key and shift-arrow key
inMap.put(KeyStroke.getKeyStroke(key, 0), name);
inMap.put(KeyStroke.getKeyStroke(key, InputEvent.SHIFT_DOWN_MASK), name);
actMap.put(name, new MyKeyAction(direction));
}
}
private void setupKeyBinding2() {
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inMap = getInputMap(condition);
ActionMap actMap = getActionMap();
// this uses an enum of Direction that holds ints for the arrow keys
for (DirectionP2 direction : DirectionP2.values()) {
int key = direction.getKey();
String name = direction.name();
// add the key bindings for arrow key and shift-arrow key
inMap.put(KeyStroke.getKeyStroke(key, 0), name);
inMap.put(KeyStroke.getKeyStroke(key, InputEvent.SHIFT_DOWN_MASK), name);
actMap.put(name, new MyKeyAction2(direction));
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//first circle
circle1.draw(g);
circle1.fill(g);
// Draw Players
g.setColor(Color.red);
g.fillRect(p1X, p1Y, 10, 75);
g.setColor(Color.blue);
g.fillRect(p2X, p2Y, 10, 75);
//Draw Scores
g.setColor(Color.white);
g.drawString("Player 1's Score: " + p1Score, 10, 10);
g.drawString("Player 2's Score: " + p2Score, getWidth() - 120, 10);
repaint();
if(!playing)
{
g.setColor(Color.red);
g.drawString("GAMEOVER!!!", (getWidth() / 2) - 15, 10);
}
else
{
}
}
private class MoveListener extends MouseAdapter implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
int x1 = circle1.getX();
int y1 = circle1.getY();
int width = getWidth();
int height = getHeight();
int radius1 = circle1.getRadius();
//first circle
if(x1 - radius1 <= 0)
{
p2Score++;
circle1.setX(getWidth()/2);
circle1.setY(getHeight()/2);
}
if(x1 + radius1 >= width)
{
p1Score++;
circle1.setX(getWidth()/2);
circle1.setY(getHeight()/2);
}
if(y1 - radius1 <= 0 || y1 + radius1 >= height )
{
circle1.turnY();
}
if((x1 - radius1 <= p1X) || (x1 + radius1 >= p2X))
{
circle1.turnX();
}
circle1.move();
repaint();
}
public void GameOver()
{
playing = false;
}
}
enum DirectionP1 {
W(KeyEvent.VK_W), S(KeyEvent.VK_S);
private int key;
private DirectionP1(int key) {
this.key = key;
}
public int getKey() {
return key;
}
}
enum DirectionP2 {
UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN);
private int key;
private DirectionP2(int key) {
this.key = key;
}
public int getKey() {
return key;
}
}
class MyKeyAction extends AbstractAction {
private DirectionP1 direction;
public MyKeyAction(DirectionP1 direction) {
this.direction = direction;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (direction) {
case W:
p1Y -= 6;
break;
case S:
p1Y += 6;
break;
default:
break;
}
}
}
class MyKeyAction2 extends AbstractAction {
private DirectionP2 direction2;
public MyKeyAction2(DirectionP2 direction2) {
this.direction2 = direction2;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (direction2) {
case UP:
p2Y -= 6;
break;
case DOWN:
p2Y += 6;
break;
default:
break;
}
}
}
}
包装有弹性;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
公共类Pong扩展了JPanel
{
圆圈1;
javax.swing.Timer;
ArrayList points=新的ArrayList();
int p1X=10,p1Y=320;
int p2X=760,p2Y=320;
int y,y1;
整型p1分,p2分;
布尔播放=真;
公共乒乓球(颜色背景色、整数宽度、整数高度)
{
挫折背景(背景色);
setPreferredSize(新尺寸(宽度、高度));
//第一圈
圆圈1=新圆圈(宽度/2,360,15,颜色为白色);
圆圈1.设定速度(4);
圆圈1.设定方向(180);
timer=newjavax.swing.timer(5,newmovelistener());
setupKeyBinding();
setupKeyBinding2();
timer.start();
}
私有void setupKeyBinding(){
int condition=JComponent.WHEN在聚焦窗口中;
InputMap inMap=getInputMap(条件);
ActionMap actMap=getActionMap();
//这将使用一个方向枚举来保存箭头键的整数
对于(方向P1方向:方向P1.values()){
int key=direction.getKey();
字符串名称=direction.name();
//添加箭头键和shift箭头键的键绑定
英马普特酒店(
public class Pong extends JPanel
{
//...
public Pong(Color backcolor, int Width, int Height)
{
// ...
timer = new javax.swing.Timer(5, new MoveListener());
addKeyListener(new KeyWatcher());
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EnumMap;
import javax.swing.*;
public class NewArrowTest extends JPanel {
private static final String PRESSED = "pressed";
private static final String RELEASED = "released";
private static final int TIMER_DELAY = 20;
private EnumMap<Key, Boolean> keyMap = new EnumMap<NewArrowTest.Key, Boolean>(Key.class);
public NewArrowTest() {
keyMap.put(Key.W, false);
keyMap.put(Key.S, false);
keyMap.put(Key.UP, false);
keyMap.put(Key.DOWN, false);
// set up key binding
ActionMap actionMap = getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
for (Key key : Key.values()) {
KeyStroke pressedKeyStroke = KeyStroke.getKeyStroke(key.getKeyCode(), 0, false);
KeyStroke releasedKeyStroke = KeyStroke.getKeyStroke(key.getKeyCode(), 0, true);
inputMap.put(pressedKeyStroke, key.getText() + PRESSED);
inputMap.put(releasedKeyStroke, key.getText() + RELEASED);
actionMap.put(key.getText() + PRESSED, new MyArrowBinding(key, false));
actionMap.put(key.getText() + RELEASED, new MyArrowBinding(key, true));
}
// start polling timer or game loop
new Timer(TIMER_DELAY, new TimerListener()).start();
}
private static void createAndShowGui() {
NewArrowTest mainPanel = new NewArrowTest();
JFrame frame = new JFrame("NewArrowTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class TimerListener implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
for (Key key : keyMap.keySet()) {
System.out.printf("%6s %b%n", key, keyMap.get(key));
// here we'd move things based on which key is true
}
System.out.println();
};
}
private class MyArrowBinding extends AbstractAction {
private Key key;
private boolean released;
public MyArrowBinding(Key key, boolean released) {
this.key = key;
this.released = released;
}
@Override
public void actionPerformed(ActionEvent aEvt) {
keyMap.put(key, !released);
}
}
enum Direction {
UP("Up"), DOWN("Down"), NEUTRAL("Neutral");
private String text;
private Direction(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
enum Key {
W("W", Direction.UP, KeyEvent.VK_W), S("S", Direction.DOWN, KeyEvent.VK_S),
UP("Up", Direction.UP, KeyEvent.VK_UP), DOWN("Down", Direction.DOWN, KeyEvent.VK_DOWN);
private String text;
private Direction direction;
private int keyCode;
private Key(String text, Direction direction, int keyCode) {
this.text = text;
this.direction = direction;
this.keyCode = keyCode;
}
public String getText() {
return text;
}
public Direction getDirection() {
return direction;
}
public int getKeyCode() {
return keyCode;
}
}
}