看到有人发五子棋,翻出我多年前帮人写的大作业,刚试了一下还能跑
有需要的同学尽管拿去交作业
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class GreedySnake extends JFrame {
GamePanel panel = new GamePanel();
/**
* 键盘监听器
*/
KeyBoardListener keyboardListener = new KeyBoardListener(panel);
/**
* 定时任务
*/
RefreshTimerTask refreshTimerTask = new RefreshTimerTask(panel, keyboardListener);
/**
* 定时器
*/
Timer refreshTimer = new Timer();
GreedySnake() {
setFocusable(true);
setLayout(null);
addKeyListener(keyboardListener);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setBounds(0, 0, 490, 300);
//延迟一秒后启动任务,每 30ms 执行一次
refreshTimer.scheduleAtFixedRate(refreshTimerTask, 1000, 30);
add(panel);
}
public static void main(String[] args) {
GreedySnake frame = new GreedySnake();
frame.setTitle("贪吃蛇 (w: 加速,s: 减速)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(496, 300);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
/**
* 每 30ms 执行的任务操作,在 run() 里
*/
class RefreshTimerTask extends TimerTask {
GamePanel panel;
KeyBoardListener keyboardlistener;
int count = 24;
RefreshTimerTask(GamePanel panel, KeyBoardListener keyboardlistener) {
this.panel = panel;
this.keyboardlistener = keyboardlistener;
}
public void run() {
if (panel.snake.state) {
count -= panel.snake.speed;
if (count <= 0) {
if (!keyboardlistener.pause) {
panel.draw();
keyboardlistener.judge = true;
}
count = 24;
}
} else {
panel.restart();
}
}
}
class KeyBoardListener extends KeyAdapter {
GamePanel panel;
boolean judge;
boolean pause;
KeyBoardListener(GamePanel panel) {
this.panel = panel;
}
public void keyPressed(KeyEvent e) {
int n = e.getKeyCode();
if (n == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
if (n == KeyEvent.VK_SPACE) {
pause = !pause;
} else if (judge) {
if (n == KeyEvent.VK_UP) {
panel.snake.control(0);
} else if (n == KeyEvent.VK_DOWN) {
panel.snake.control(2);
} else if (n == KeyEvent.VK_RIGHT) {
panel.snake.control(1);
} else if (n == KeyEvent.VK_LEFT) {
panel.snake.control(3);
} else if (n == KeyEvent.VK_W) {
if (panel.snake.speed != 8)
panel.snake.speed += 2;
} else if (n == KeyEvent.VK_S) {
if (panel.snake.speed != 2)
panel.snake.speed -= 2;
}
judge = false;
}
}
}
class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
int[][] space = new int[27][49];
Snake snake = new Snake();
Food food = new Food();
Wall[] wall = new Wall[4];
GamePanel() {
wall[0] = new Wall(0, 0, 0, 49);
wall[1] = new Wall(0, 1, 1, 25);
wall[2] = new Wall(0, 26, 0, 49);
wall[3] = new Wall(48, 1, 1, 25);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < wall[i].count; j++) {
space[wall[i].walls[j].y][wall[i].walls[j].x] = wall[i].walls[j].type;
}
}
drawSpace();
}
void drawSpace() {
int i;
for (i = 0; i < snake.length; i++) {
space[snake.body.get(i).y][snake.body.get(i).x] = snake.body.get(i).type;
}
space[food.y][food.x] = 4;
}
/**
* 根据当前数据绘制界面
*
* @param g
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < snake.length; i++) {
g.fillRoundRect(snake.body.get(i).x * 10, snake.body.get(i).y * 10, 10, 10, 3, 3);
}
g.drawOval(food.x * 10, food.y * 10, 10, 10);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < wall[i].count; j++) {
g.drawRect(wall[i].walls[j].x * 10, wall[i].walls[j].y * 10, 10, 10);
g.drawLine(wall[i].walls[j].x * 10, wall[i].walls[j].y * 10, wall[i].walls[j].x * 10 + 10, wall[i].walls[j].y * 10 + 10);
g.drawLine(wall[i].walls[j].x * 10 + 10, wall[i].walls[j].y * 10, wall[i].walls[j].x * 10, wall[i].walls[j].y * 10 + 10);
}
}
}
void draw() {
//蛇移动一格(坐标集合发生改变)
int x = snake.body.get(snake.length - 1).x;
int y = snake.body.get(snake.length - 1).y;
space[y][x] = 0;
snake.move();
checkGameOver();
repaint();
}
/**
* 检测游戏是否结束,如果结束弹结束框
*/
void checkGameOver() {
if (space[snake.body.get(0).y][snake.body.get(0).x] == 4) {
snake.grow();
food.refresh();
space[snake.body.get(0).y][snake.body.get(0).x] = 1;
space[snake.body.get(snake.length - 1).y][snake.body.get(snake.length - 1).x] = 2;
if (space[food.y][food.x] != 0) {
food.refresh();
}
space[food.y][food.x] = 4;
} else if (space[snake.body.get(0).y][snake.body.get(0).x] == 0) {
space[snake.body.get(0).y][snake.body.get(0).x] = 1;
} else {
JOptionPane.showMessageDialog(null, "游戏结束");
space[snake.body.get(0).y][snake.body.get(0).x] = 0;
for (int m = 1; m < snake.length; m++) {
space[snake.body.get(m).y][snake.body.get(m).y] = 0;
}
snake.state = false;
}
}
/**
* 清除数据,重新开始游戏
*/
void restart() {
snake.body.clear();
Brick head = new Brick(1, 24, 15);
Brick tail = new Brick(2, 22, 15);
Brick body1 = new Brick(2, 23, 15);
snake.direction = 1;
snake.body.add(head);
snake.body.add(body1);
snake.body.add(tail);
snake.length = 3;
snake.state = true;
snake.speed = 2;
food.refresh();
drawSpace();
repaint();
}
}
/**
* 蛇本体对象
*/
class Snake {
/**
* 身体的坐标集合
*/
ArrayList<Brick> body = new ArrayList<>();
/**
* 头部朝向,0 下, 1 右, 2 上, 3 左
*/
int direction;
boolean state = true;
int length;
/**
* 移动速度
*/
int speed = 4;
Snake() {
Brick head = new Brick(1, 24, 15);
Brick tail = new Brick(2, 22, 15);
Brick body1 = new Brick(2, 23, 15);
direction = 1;
body.add(head);
body.add(body1);
body.add(tail);
length = 3;
}
void grow() {
int x = 2 * body.get(length - 1).x - body.get(length - 2).x;
int y = 2 * body.get(length - 1).y - body.get(length - 2).y;
body.add(new Brick(2, x, y));
++length;
}
void move() {
for (int i = length - 1; i > 0; i--) {
body.get(i).x = body.get(i - 1).x;
body.get(i).y = body.get(i - 1).y;
}
switch (direction) {
case 0:
body.get(0).y -= 1;
break;
case 1:
body.get(0).x += 1;
break;
case 2:
body.get(0).y += 1;
break;
case 3:
body.get(0).x -= 1;
break;
}
}
/**
* 键盘控制前进方向
*
* @param a
*/
void control(int a) {
int m = Math.abs(a - direction);
if (m != 2 && m != 0) {
direction = a;
}
}
}
/**
* 界面上的一个点
*/
class Brick {
int x;
int y;
int type;//1-蛇头,2-蛇身,3-边界,4-食物
Brick(int a, int b, int c) {
this.type = a;
this.x = b;
this.y = c;
}
}
class Food extends Brick {
Food() {
super(4, 0, 0);
refresh();
}
/**
* 刷新食物坐标
*/
void refresh() {
this.x = (int) (Math.random() * 47 + 1);
this.y = (int) (Math.random() * 25 + 1);
}
}
/**
* 游戏边界
*/
class Wall {
Brick[] walls;
int count;
int direction;
int x;
int y;
Wall(int a, int b, int c, int d) {
this.x = a;
this.y = b;
this.direction = c;
this.count = d;
walls = new Brick[d];
for (int i = 0; i < count; i++) {
if (c == 0) {
walls[i] = new Brick(3, (x + i), y);
} else {
walls[i] = new Brick(3, x, (y + i));
}
}
}
}