
/**
* Created by Kaosaier on 7/3/2017.
*/
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PongBall {
private final int TABLE_WIDTH = 300;
private final int TABLE_HEIGHT = 400;
private final int RACKET_Y = 340;
private final int RACKET_HEIGHT = 20;
private final int RACKET_WIDTH = 80;
private final int BALL_SIZE = 16;
private final int BWIDTH = 100;
private final int BHEIGHT = 20;
private final int B1y = 400;
private final int B1x = 0;
private Frame f = new Frame("PongBall");
Random rand = new Random();
private int ySpeed = 10;
private double xyRate = rand.nextDouble() - 0.5;
private int xSpeed = (int)(ySpeed * xyRate * 2);
private int ballX = rand.nextInt(200) + 20;
private int ballY = rand.nextInt(10) + 20;
private int racketX = rand.nextInt(200);
private MyCanvas tableArea = new MyCanvas();
Timer timer;
private boolean isLose = false;
public void init()
{
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH , TABLE_HEIGHT));
f.add(tableArea);
KeyAdapter keyProcessor = new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
{
if (racketX > 0)
racketX -= 10;
}
if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
if (racketX < TABLE_WIDTH - RACKET_WIDTH)
racketX += 10;
}
}
};
f.addKeyListener(keyProcessor);
tableArea.addKeyListener(keyProcessor);
ActionListener taskPerformer = evt ->
{
if (ballX <= 0 || ballX == TABLE_WIDTH - BALL_SIZE)
{
xSpeed = -xSpeed;
}
if (ballY >= RACKET_Y - BALL_SIZE + 5)
{
timer.stop();
isLose = true;
tableArea.repaint();
}
else if (ballY <= 0 || ballY >= RACKET_Y - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)
{
ySpeed = -ySpeed;
}
ballY += ySpeed;
ballX += xSpeed;
tableArea.repaint();
};
timer = new Timer(60, taskPerformer);
timer.start();
f.pack();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
public static void main(String[] args)
{
new PongBall().init();
}
class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
if (isLose)
{
g.setColor(new Color(255, 0, 0));
g.drawString("game over:-(" , 75 ,200);
}
else
{
g.setColor(new Color(0 , 2 , 225));
g.fillRect(0 , 400 , BWIDTH , BHEIGHT);
g.setColor(new Color(241, 255, 0));
g.fillRect(0 , 0 , 300 , 400);
g.setColor(new Color(0, 0, 0));
g.fillOval(ballX , ballY , BALL_SIZE, BALL_SIZE);
g.setColor(new Color(165, 163, 200));
g.fillRect(racketX , RACKET_Y, RACKET_WIDTH , RACKET_HEIGHT);
}
}
}
}
* Created by Kaosaier on 7/3/2017.
*/
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PongBall {
private final int TABLE_WIDTH = 300;
private final int TABLE_HEIGHT = 400;
private final int RACKET_Y = 340;
private final int RACKET_HEIGHT = 20;
private final int RACKET_WIDTH = 80;
private final int BALL_SIZE = 16;
private final int BWIDTH = 100;
private final int BHEIGHT = 20;
private final int B1y = 400;
private final int B1x = 0;
private Frame f = new Frame("PongBall");
Random rand = new Random();
private int ySpeed = 10;
private double xyRate = rand.nextDouble() - 0.5;
private int xSpeed = (int)(ySpeed * xyRate * 2);
private int ballX = rand.nextInt(200) + 20;
private int ballY = rand.nextInt(10) + 20;
private int racketX = rand.nextInt(200);
private MyCanvas tableArea = new MyCanvas();
Timer timer;
private boolean isLose = false;
public void init()
{
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH , TABLE_HEIGHT));
f.add(tableArea);
KeyAdapter keyProcessor = new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
{
if (racketX > 0)
racketX -= 10;
}
if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
if (racketX < TABLE_WIDTH - RACKET_WIDTH)
racketX += 10;
}
}
};
f.addKeyListener(keyProcessor);
tableArea.addKeyListener(keyProcessor);
ActionListener taskPerformer = evt ->
{
if (ballX <= 0 || ballX == TABLE_WIDTH - BALL_SIZE)
{
xSpeed = -xSpeed;
}
if (ballY >= RACKET_Y - BALL_SIZE + 5)
{
timer.stop();
isLose = true;
tableArea.repaint();
}
else if (ballY <= 0 || ballY >= RACKET_Y - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)
{
ySpeed = -ySpeed;
}
ballY += ySpeed;
ballX += xSpeed;
tableArea.repaint();
};
timer = new Timer(60, taskPerformer);
timer.start();
f.pack();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
public static void main(String[] args)
{
new PongBall().init();
}
class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
if (isLose)
{
g.setColor(new Color(255, 0, 0));
g.drawString("game over:-(" , 75 ,200);
}
else
{
g.setColor(new Color(0 , 2 , 225));
g.fillRect(0 , 400 , BWIDTH , BHEIGHT);
g.setColor(new Color(241, 255, 0));
g.fillRect(0 , 0 , 300 , 400);
g.setColor(new Color(0, 0, 0));
g.fillOval(ballX , ballY , BALL_SIZE, BALL_SIZE);
g.setColor(new Color(165, 163, 200));
g.fillRect(racketX , RACKET_Y, RACKET_WIDTH , RACKET_HEIGHT);
}
}
}
}
最后的几个画的地方g.setColor(new Color(0 , 2 , 225));不能运行,没出错误,只是画不出来,看不到



g.fillRect(0 , 400 , BWIDTH , BHEIGHT);