Java 完全面向对象的贪吃蛇游戏

JavaSE的项目:贪吃蛇(完全面向对象程序设计)

只要有以下几个类:

package net.anduo.entity; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.LinkedList; import javax.swing.JOptionPane; import net.anduo.frame.Game; import net.anduo.util.GameGlobel; /** * 蛇类 */ public class Snack { public static final int DOWN = 2; public static final int UP = 4; public static final int LEFT = 1; public static final int RIGHT = 3; private int direction = LEFT; private LinkedList<Point> snack; private int size = GameGlobel.SIZE; public Snack() { snack = new LinkedList<Point>(); creatSnack(); } /** * 构建一个蛇 */ public void creatSnack() { int x = GameGlobel.WIDTH / 2; int y = GameGlobel.HEIGHT / 2; for (int i = 0; i < 3; i++) { Point p = new Point(); p.x = x++; p.y = y; snack.add(p); } } /** * 判断蛇的生死 取出蛇的头结点,然后将其与蛇的身体的每个节点比较,如果头结点在身体内,则判断为死 * * @return */ public boolean isDead() { boolean dead = false; Point head = snack.getFirst(); for (int i = 1; i < snack.size(); i++) { if (head.equals(snack.get(i))) { dead = true; } } return dead; } /** * 改变蛇的移动方向 * * @param newDirection */ public void changeDirection(int newDirection) { if (direction % 2 != newDirection % 2) this.direction = newDirection; // move(); } /** * 蛇的移动 */ public void move() { Point p = snack.getFirst(); int x = p.x; int y = p.y; // 根据蛇的行进方向以及当前蛇的头坐标,计算下一次蛇的头坐标 switch (direction) { // 到达顶端时,从最下方出来; // 到达底端时,从最上方出来; // 到达左端时,从最右方出来; // 到达右端时,从最左方出来; case UP: y--; if (y == -1) y = 19; break; case DOWN: y++; if (y == 20) y = 0; break; case LEFT: x--; if (x == -1) x = 19; break; case RIGHT: x++; if (x == 20) x = 0; break; } // 如果蛇不出界,构造新蛇 if (x < GameGlobel.WIDTH && y < GameGlobel.HEIGHT && x >= 0 && y >= 0) { p = new Point(x, y); if (eatFood()) { // 吃食物,然后把食物点加为头结点 // 加速蛇的行进速度 GameGlobel.level -= 25; if (GameGlobel.level < 50) GameGlobel.level = 50; // 加分 GameGlobel.score += 5; // Game.scoreTf.setText(String.valueOf(GameGlobel.score)); while (true) {// 让食物不出现在蛇的身上 Food.food = new Food().newFood(); if (!snack.contains(Food.food)) break; } snack.add(Food.food); } snack.addFirst(p); snack.removeLast(); // 构建好新蛇之后,先判断生死,然后,吃食物 if (this.isDead()) { JOptionPane.showConfirmDialog(Game.game, "GAME OVER!"); System.exit(0); } } // 游戏面板重绘 Game.game.repaint(); } /** * 判断是否是否可吃 */ public boolean eatFood() { Point p = snack.getFirst(); if (p.equals(Food.food)) { return true; } return false; } /** * 绘制蛇 * * @param g */ public void paint(Graphics g) { g.setColor(Color.gray); Point head = snack.getFirst(); g.fillOval(head.x * size + 4, head.y * size + 4, size - 8, size - 8); // g.fillRect(head.x * size, head.y * size, size, size); g.setColor(Color.PINK); for (int i = 1; i < snack.size(); i++) { Point p = snack.get(i); g.fillOval(p.x * size + 3, p.y * size + 3, size - 6, size - 6); } } }

package net.anduo.entity; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.Random; import net.anduo.util.GameGlobel; /** * 食物类 */ public class Food { public static Point food; Random random = new Random(); int size = GameGlobel.SIZE; public Food() { food = newFood(); } public Point newFood() { int x = random.nextInt(18) + 1; int y = random.nextInt(18) + 1; return new Point(x, y); } /** * 绘制食物 */ public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(food.x * size, food.y * size, size - 2, size - 2); } }

package net.anduo.util; /** * 游戏的全局参数 */ public class GameGlobel { public final static int HEIGHT = 20; public final static int SIZE = 20; public final static int WIDTH = 20; public static int level = 500; public static int score = 0; }

package net.anduo.control; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import net.anduo.entity.Food; import net.anduo.entity.Snack; import net.anduo.util.GameGlobel; import net.anduo.view.GamePanel; /** * 控制蛇的移动方向 */ public class SnackCtrol extends KeyAdapter { private Snack snack; private Food food; private GamePanel panel; public static Thread controlGame; public SnackCtrol(Snack snack, Food food, GamePanel panel) { System.out.println("snack ctrol"); this.snack = snack; this.food = food; this.panel = panel; creatGame(); } /** * 开始游戏,调用线程,然蛇自动行进 */ public void creatGame() { this.panel.display(this.snack, this.food); controlGame = new Thread(panel); controlGame.start(); } /** * 根据键盘的按键,适应的改变蛇的行进方向 */ public void keyPressed(KeyEvent e) { System.out.println(GameGlobel.score); int code = e.getKeyCode(); switch (code) { case KeyEvent.VK_UP: snack.changeDirection(Snack.UP); break; case KeyEvent.VK_DOWN: snack.changeDirection(Snack.DOWN); break; case KeyEvent.VK_LEFT: snack.changeDirection(Snack.LEFT); break; case KeyEvent.VK_RIGHT: snack.changeDirection(Snack.RIGHT); break; } panel.display(snack, food); } }

package net.anduo.view; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import net.anduo.entity.Food; import net.anduo.entity.Snack; import net.anduo.util.GameGlobel; /** * 游戏面板类 * * @author anduo * */ @SuppressWarnings("serial") public class GamePanel extends JPanel implements Runnable { private Snack snack; private Food food; public void paint(Graphics g) { int size = GameGlobel.SIZE; g.setColor(Color.green); for (int i = 0; i < GameGlobel.WIDTH; i++) {// 绘制了400个方格,背景 for (int j = 0; j < GameGlobel.HEIGHT; j++) { g.fill3DRect(i * size, j * size, size, size, true); } } if (snack != null) snack.paint(g); if (food != null) food.paint(g); g.dispose(); } public void run() { while (true) { try { Thread.sleep(GameGlobel.level); snack.move(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void display(Snack snack, Food food) { this.snack = snack; this.food = food; repaint(); } }

package net.anduo.frame; import javax.swing.JFrame; import net.anduo.control.SnackCtrol; import net.anduo.entity.Food; import net.anduo.entity.Snack; import net.anduo.view.GamePanel; @SuppressWarnings("serial") public class Game extends JFrame { public static GamePanel game; public static Snack snack; public static Food food; private SnackCtrol ctrl; // public static ToolPanel tool; public Game() { snack = new Snack(); food = new Food(); game = new GamePanel(); ctrl = new SnackCtrol(snack, food, game); addKeyListener(ctrl); // game.addKeyListener(ctrl); game.setBounds(20, 20, 400, 400); setLayout(null); getContentPane().add(game); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(getToolkit().getScreenSize().width / 2 - 150, getToolkit() .getScreenSize().height / 2 - 150, 560, 470); setVisible(true); } public static void main(String[] args) { new Game(); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值