最不会加注释了,不知道行不行,一共149行,要求全部完成。
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Racer extends JFrame {
private MyPanel drawPanel;
public static final int LINE_LEN = 20; // 方格的边长
public static final int NUM = 20; // 方格的个数为NUM*NUM
private static final int EDGE_WIDTH = 25; // 边距
public static final int OBSTACLE_NUM = 10; //屏幕上同时出现的障碍物数量
Racer() {
getContentPane().setLayout(new BorderLayout());
drawPanel = new MyPanel(this);
getContentPane().add(drawPanel, "Center"); //把游戏面板放到屏幕中央
setSize((LINE_LEN + 1) * NUM + EDGE_WIDTH, (LINE_LEN + 1) * NUM
+ EDGE_WIDTH); //设置窗口大小
setVisible(true); //显示窗口
drawPanel.requestFocusInWindow(); //在窗口显示后要求接受键盘输入
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时结束程序
}
public static void main(String[] args) {
Racer r = new Racer();
}
}
class MyPanel extends JPanel implements ActionListener, KeyListener {
private Racer owner;
private static boolean flag = false; // 画路时,交替地画出黑白方块
private int num = Racer.NUM;
private int len = Racer.LINE_LEN;
private int x = 0; //汽车的位置,一开始为0
private int[] obstacleX = new int[Racer.OBSTACLE_NUM]; //所有障碍物的X坐标
private int[] obstacleY = new int[Racer.OBSTACLE_NUM]; //所有障碍物的Y坐标
private int[] carX = {8, 7, 8, 9, 8, 7, 8, 9}; //汽车方格的X坐标
private int[] carY = {16, 17, 17, 17, 18, 19, 19, 19}; //汽车方格的Y坐标
Random rand = new Random(); //随机数生成器
Timer timer; //动画计时更新,用javax.swing.Timer和键盘的输入较为匹配,可以避免直接用线程出现的一些问题
int LEFT_KEY = KeyEvent.VK_W; //按w键为往左,如果要按左键往左的话LEFT_KEY = KeyEvent.VK_LEFT
int RIGHT_KEY = KeyEvent.VK_D; //按w键为往左,如果要按右键往右的话LEFT_KEY = KeyEvent.VK_RIGHT
MyPanel(Racer r) {
owner = r;
timer = new Timer(200, this); //时间间隔为200毫秒
timer.start(); //开始游戏更新
addKeyListener(this); //注册键盘输入
for(int i=0;i
obstacleY[i] = rand.nextInt(Racer.NUM) - num; //y在路面上方-1到-1-NUM之间,游戏开始时看不到
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==LEFT_KEY) x--; //按往左的键时减小x变量
else if(e.getKeyCode()==RIGHT_KEY) x++; //按往右的键时增加x变量
if(x<-6) x=-6; //如果出了左边界的话把车放回边界内
if(x>9) x=9; //如果出了右边界的话把车放回边界内
repaint(); //重新画一编移动后的车
}
public void paint(Graphics g) {
g.clearRect(0, 0, getSize().width, getSize().height);
drawBkgnd(g); // 画背景
drawRoad(g); // 画路
drawCar(g); // 画车
drawObstacles(g); //画障碍物
}
private void drawBkgnd(Graphics g) {
for (int i = 0; i <= num; i++) {
g.drawLine(0, i * len, len * num, i * len); //画横线
g.drawLine(i * len, 0, i * len, len * num); //画竖线
}
}
private void drawRoad(Graphics g) {
if (flag) {
for (int i = 0; i < num; i += 2) { //交替涂路边的格子
g.fillRect(0, i * len, len, len);
g.fillRect((num - 1) * len, i * len, len, len);
}
} else {
for (int i = 1; i < num; i += 2) {
g.fillRect(0, i * len, len, len);
g.fillRect((num - 1) * len, i * len, len, len);
}
}
}
private void drawCar(Graphics g) {
for(int i=0;i
}
}
private void drawObstacles(Graphics g) {
for(int i=0;i
}
}
private boolean collision() {
for(int i=0;i
return true; //相撞发生了
}
}
return false;
}
public void actionPerformed(ActionEvent e) {
if(collision()) { //检测有没有撞到障碍物
timer.stop(); //暂停游戏
JOptionPane.showMessageDialog(this, "你输了,按OK重新开始。"); //显示结束信息
//这里用户按下了ok
for(int i=0;i
obstacleY[i] = rand.nextInt(Racer.NUM) - num;
}
x=0; //重置汽车的位置
timer.start(); //恢复游戏
}
for(int i=0;i
if(obstacleY[i]>=num) { //如果障碍物移动到网格的下面去了
obstacleX[i]=rand.nextInt(num-2)+1; //随机给一个新的X坐标
obstacleY[i]=0; //恢复到屏幕上放
}
}
flag=!flag; //更新路边的斑马格
repaint(); //重画屏幕
}
public void keyReleased(KeyEvent e) {} //没有用到的方法,继承KeyListener必须声明
public void keyTyped(KeyEvent e) {} //同上
}
期待
!!!!加注释!
vbvbvc
飘过~~~~~~~~~~~~~~~~`a wo wei shen me mei fen a