我正在打空气曲棍球比赛。直到现在,我已经移动了两个手柄并在我的空气曲棍球桌上反弹球,实际上球可以从墙上反弹,但我的问题是我不能用手柄击球,我对此没有任何想法。我在网上搜索但我找不到任何东西。如果你能尽快帮助我,我将不胜感激!
这是我需要的所有代码:
public class StartGamePanel extends JPanel implements ActionListener, KeyListener, Runnable {
private static final long serialVersionUID = 1L;
double xCircle1=200;
double yCircle1 =100 ;
double xCircle2=200;
double yCircle2 =700 ;
double xBall=200;
double yBall=400;
double rBall=20;
double ballSpeedX=-3;
double ballSpeedY=0;
private Ball m_ball = new Ball(0, 0, 7, 7);
private int m_interval = 35;
private Timer m_timer;
double dx = 0, dy = 0, dx2=0, dy2=0;
private Graphics2D circle1;
private Graphics2D circle2;
private Graphics2D circle3;
public static void main(String[] args) {
JFrame f=new JFrame();
f.setSize(new Dimension(512,837));
StartGamePanel p=new StartGamePanel();
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public StartGamePanel(){
m_timer = new Timer(m_interval, new TimerAction());
m_timer.start(); // start animation by starting the timer.
Timer t = new Timer(5, this);
t.start();
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
addKeyListener(this);
ContainerTable table=new ContainerTable(2, 0, 493, 800, new Color(51,153,255), Color.WHITE);
table.draw(g);
circle1 = (Graphics2D) g;
circle1.setColor(new Color(255,51,51));
Shape theCircle = new Ellipse2D.Double(xCircle1 - 40, yCircle1 - 40, 2.0 * 40, 2.0 * 40);
circle1.fill(theCircle);
circle2 = (Graphics2D) g;
circle2.setColor(new Color(255,102,102));
Shape theCircle2 = new Ellipse2D.Double(xCircle1 - 35, yCircle1 - 35, 2.0 * 35, 2.0 * 35);
circle2.fill(theCircle2);
circle3 = (Graphics2D) g;
circle3.setColor(new Color(255,51,51));
Shape theCircle3 = new Ellipse2D.Double(xCircle1 - 20, yCircle1 - 20, 2.0 * 20, 2.0 * 20);
circle3.fill(theCircle3);
Graphics2D circleprim = (Graphics2D) g;
circleprim.setColor(new Color(0,51,102));
Shape theCircleprim = new Ellipse2D.Double(xCircle2 - 40, yCircle2 - 40, 2.0 * 40, 2.0 * 40);
circleprim.fill(theCircleprim);
Graphics2D circle2prim = (Graphics2D) g;
circle2prim.setColor(new Color(0,102,204));
Shape theCircle2prim = new Ellipse2D.Double(xCircle2 - 35, yCircle2 - 35, 2.0 * 35, 2.0 * 35);
circle2prim.fill(theCircle2prim);
Graphics2D circle3prim = (Graphics2D) g;
circle3prim.setColor(new Color(0,51,102));
Shape theCircle3prim = new Ellipse2D.Double(xCircle2 - 20, yCircle2 - 20, 2.0 * 20, 2.0 * 20);
circle3prim.fill(theCircle3prim);
g.setColor(Color.gray);
m_ball.draw(g);
Graphics2D goal = (Graphics2D) g;
goal.setColor(Color.BLACK);
goal.fill3DRect(100, 0, 300, 10, true);
Graphics2D goal2 = (Graphics2D) g;
goal2.setColor(Color.BLACK);
goal2.fill3DRect(100, 790, 300, 10, true);
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight());
m_ball.move();
repaint();
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
xCircle1 += dx;
yCircle1 += dy;
yCircle2 += dy2;
xCircle2+= dx2;
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
if(yCircle1>40){
dy = -2;
dx = 0;
}else{
yCircle1=40;
dy *= -1;
dx=0;
}
}
if (code == KeyEvent.VK_DOWN) {
if(yCircle1<360){
dy = 2;
dx = 0;
}else{
yCircle1=360;
dx =0;
dy *= -1;
}
}
if (code == KeyEvent.VK_LEFT) {
if (xCircle1 < 30) {
xCircle1=30;
dy = 0;
dx *= -1;
}else {
dy = 0;
dx = -2;
}
}
if (code == KeyEvent.VK_RIGHT) {
if (xCircle1 > 460) {
xCircle1= 460;
dx *= -1;
dy = 0;
}else{
dy = 0;
dx = 2;
}
}
if (code == KeyEvent.VK_F) {
if (xCircle2 > 460) {
xCircle2= 460;
dy2 = 0;
dx2 *= -1;
}else{
dy2 = 0;
dx2 = 2;
}
}
if (code == KeyEvent.VK_S) {
if (xCircle2 < 30) {
xCircle2=30;
dy2 = 0;
dx2 *= -1;
}else {
dy2 = 0;
dx2 = -2;
}
}
if (code == KeyEvent.VK_E) {
if(yCircle2>430){
dy2 = -2;
dx2 = 0;
}else{
yCircle2=430;
dy*= -1;
dx=0;
}}
if (code == KeyEvent.VK_D) {
if(yCircle2>750){
yCircle2=750;
dy2 *= -1;
dx2=0;
}else{
dy2 = 2;
dx2 = 0;
}}
}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_DOWN) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_LEFT) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_RIGHT) {
dy = 0;
dx = 0;
}
if (code == KeyEvent.VK_E) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_D) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_S) {
dy2 = 0;
dx2 = 0;
}
if (code == KeyEvent.VK_F) {
dy2 = 0;
dx2 = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
xBall += ballSpeedX;
yBall += ballSpeedX;
repaint();
if (xBall < 30) {
ballSpeedX = -ballSpeedX;
xBall = 30;
}
if (xBall > 470) {
ballSpeedX = -ballSpeedX;
xBall = 470;
}
try { Thread.sleep (20); }
catch (InterruptedException ex) {}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); }}}
class Ball {
final static int DIAMETER = 50;
private int m_x;
private int m_y;
private int m_velocityX;
private int m_velocityY;
private int m_rightBound;
private int m_bottomBound;
public Ball(int x, int y, int velocityX, int velocityY) {
m_x = x;
m_y = y;
m_velocityX = velocityX;
m_velocityY = velocityY;
}
public void setBounds(int width, int height) {
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}
public void move() {
m_x += m_velocityX;
m_y += m_velocityY;
if (m_x < 0) {
m_x = 0;
m_velocityX = -m_velocityX;
} else if (m_x > m_rightBound) {
m_x = m_rightBound;
m_velocityX = -m_velocityX;
}
if (m_y < 0) {
m_y = 0;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) {
m_y = m_bottomBound;
m_velocityY = -m_velocityY;
}
}
public void draw(Graphics g) {
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
public int getDiameter() { return DIAMETER;}
public int getX() { return m_x;}
public int getY() { return m_y;}
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
class ContainerTable {
public int minX;
public int maxX;
public int minY;
public int maxY;
private Color colorFilled;
private Color colorBorder;
public ContainerTable(int x, int y, int width, int height, Color colorFilled, Color colorBorder) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
this.colorFilled = colorFilled;
this.colorBorder = colorBorder;
}
public void set(int x, int y, int width, int height) {
minX = x;
minY = y;
maxX = x + width - 1;
maxY = y + height - 1;
}
public void draw(Graphics g) {
g.setColor(colorFilled);
g.fillRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
g.setColor(colorBorder);
g.drawRect(minX, minY, maxX - minX - 1, maxY - minY - 1);
Graphics2D southArc = ( Graphics2D ) g;
southArc.setColor ( Color.WHITE );
southArc.setStroke(new BasicStroke(3));
southArc.drawArc ( 98, 640 , 300, 300 , 0, 180 );
Graphics2D northArc = ( Graphics2D ) g;
northArc.setColor ( Color.WHITE );
northArc.setStroke(new BasicStroke(3));
northArc.drawArc ( 98, -143 , 300, 300 , 180, 180 );
Graphics2D line = ( Graphics2D ) g;
line.setStroke(new BasicStroke(3));
line.setColor(Color.white);
line.drawLine(4, 395, 491, 395);
Graphics2D dot=(Graphics2D) g;
dot.setColor(Color.black);
for(int j=10;j<800;j+=20){
for(int i=6;i<502;i+=20){
dot.drawLine(i, j, i, j);
}
}
}
public int getMinX() {
return minX;
}
public void setMinX(int minX) {
this.minX = minX;
}
public int getMaxX() {
return maxX;
}
public void setMaxX(int maxX) {
this.maxX = maxX;
}
public int getMinY() {
return minY;
}
public void setMinY(int minY) {
this.minY = minY;
}
public int getMaxY() {
return maxY;
}
public void setMaxY(int maxY) {
this.maxY = maxY;
}}