Java课程设计------贪吃蛇

贪吃蛇

这个小程序总体来说比较简单,就不做太多的解释了,相信大家很容易看懂的


执行程序:程序启动的入口

[java]  view plain  copy
  1. package cn.hncu;  
  2.   
  3. public class GreedySnake {  
  4.     public static void main(String[] args) {  
  5.         Model model=new Model(8050);  
  6.         Control control=new Control(model);  
  7.         View view=new View(model,control);  
  8.           
  9.         model.addObserver(view);  
  10.         (new Thread(model)).start();  
  11.     }  
  12. }  

控制类:主要进行键盘的按键收集和传递

[java]  view plain  copy
  1. package cn.hncu;  
  2.   
  3. import java.awt.event.KeyEvent;  
  4. import java.awt.event.KeyListener;  
  5.   
  6. public class Control implements KeyListener{  
  7.     Model model;  
  8.       
  9.     public Control(Model model){  
  10.         this.model=model;  
  11.     }  
  12.       
  13.     public void keyPressed(KeyEvent e) {  
  14.         //此方法收集按键  
  15.         int key=e.getKeyCode();  
  16.         if (model.running) {  
  17.             switch (key) {  
  18.             case KeyEvent.VK_UP:  
  19.                 model.changeDirection(model.up);  
  20.                 break;  
  21.             case KeyEvent.VK_DOWN:  
  22.                 model.changeDirection(model.down);  
  23.                 break;  
  24.             case KeyEvent.VK_LEFT:  
  25.                 model.changeDirection(model.left);  
  26.                 break;  
  27.             case KeyEvent.VK_RIGHT:  
  28.                 model.changeDirection(model.right);  
  29.                 break;  
  30.             default:  
  31.                 break;  
  32.             }  
  33.         }  
  34.         if (key==KeyEvent.VK_ENTER) {  
  35.             model.reset();  
  36.         }  
  37.     }  
  38.   
  39.     public void keyReleased(KeyEvent e) {  
  40.     }  
  41.   
  42.     public void keyTyped(KeyEvent e) {  
  43.     }  
  44. }  
模型类:创建蛇身和蛇的运动方式的实现,用到了线程,关于线程的知识可见本博客的多线程 点击打开链接
[java]  view plain  copy
  1. package cn.hncu;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.LinkedList;  
  5. import java.util.Observable;  
  6. import java.util.Random;  
  7.   
  8. import javax.swing.JOptionPane;  
  9.   
  10. public class Model extends Observable implements Runnable{  
  11.     public static final int left=1;  
  12.     public static final int up=2;  
  13.     public static final int right=3;  
  14.     public static final int down=4;  
  15.       
  16.     public boolean coordinate[][];//用这个来当做界面的坐标  
  17.     public LinkedList node=new LinkedList();  
  18.     public int direction=2;  
  19.     boolean running=false;  
  20.     public int maxX,maxY;  
  21.     Node food;  
  22.     public int sleeptime=200;  
  23.       
  24.     public Model(int maxX,int maxY){  
  25.         this.maxX=maxX;  
  26.         this.maxY=maxY;  
  27.         reset();  
  28.     }  
  29.       
  30.     public void reset() {  
  31.         direction=this.up;  
  32.         sleeptime=200;  
  33.         coordinate=new boolean[maxX][];  
  34.         for (int i = 0; i < maxX; i++) {  
  35.             coordinate[i]=new boolean[maxY];  
  36.             Arrays.fill(coordinate[i], false);  
  37.         }  
  38.           
  39.         //initialize the Snake'body  
  40.         int initlenght=10;  
  41.         node.clear();  
  42.         for (int j = 0; j < initlenght; j++) {  
  43.             int x=maxX/2+j;  
  44.             int y=maxY/2;  
  45.             node.addLast(new Node(x,y));  
  46.             coordinate[x][y]=true;  
  47.         }  
  48.           
  49.         food=createFood();  
  50.         coordinate[food.x][food.y]=true;  
  51.     }  
  52.   
  53.     public boolean move(){  
  54.         Node n=(Node)node.getFirst();  
  55.         int x=n.x;  
  56.         int y=n.y;  
  57.           
  58.         switch (direction) {  
  59.             case up:  
  60.                 y--;  
  61.                 break;  
  62.             case down:  
  63.                 y++;  
  64.                 break;  
  65.             case left:  
  66.                 x--;  
  67.                 break;  
  68.             case right:  
  69.                 x++;  
  70.                 break;  
  71.         default:  
  72.             break;  
  73.         }  
  74.           
  75.         if ((x>=0&&x<maxX)&&(y>=0&&y<maxY)) {  
  76.             if (coordinate[x][y]) {  
  77.                 if (x==food.x&&y==food.y) {  
  78.                     node.addFirst(food);  
  79.                     if (sleeptime>35) {  
  80.                         sleeptime-=20;  
  81.                     }  
  82.                       
  83.                     food=createFood();  
  84.                     coordinate[food.x][food.y]=true;  
  85.                     return true;  
  86.                 }else {  
  87.                     return false;  
  88.                 }  
  89.             }else {  
  90.                 node.addFirst(new Node(x,y));  
  91.                 coordinate[x][y]=true;  
  92.                 n=(Node)node.getLast();  
  93.                 node.removeLast();  
  94.                 coordinate[n.x][n.y]=false;  
  95.                 return true;  
  96.             }  
  97.         }  
  98.           
  99.         return false;  
  100.     }  
  101.       
  102.     public void changeDirection(int newdir){  
  103.         if (direction!=newdir) {  
  104.             direction=newdir;  
  105.         }  
  106.     }  
  107.       
  108.     public Node createFood() {  
  109.         int x=0,y=0;  
  110.         do {  
  111.             Random r = new Random();  
  112.             x = r.nextInt(maxX);  
  113.             y = r.nextInt(maxY);  
  114.         } while (coordinate[x][y]);  
  115.   
  116.         return new Node(x, y);  
  117.     }  
  118.   
  119.     public void run() {  
  120.         running=true;  
  121.         while(running){  
  122.             try {  
  123.                 Thread.sleep(sleeptime);  
  124.             } catch (Exception e) {  
  125.                 break;  
  126.             }  
  127.               
  128.             if (move()) {  
  129.                 setChanged();  
  130.                 notifyObservers();  
  131.             }else {  
  132.                 JOptionPane.showMessageDialog(null"Game Over");  
  133.                 break;  
  134.             }  
  135.         }  
  136.     }  
  137.       
  138. }  
  139.   
  140. class Node{//创建蛇身  
  141.     public int x,y;  
  142.     public Node(int x,int y){  
  143.         this.x=x;  
  144.         this.y=y;  
  145.     }  
  146. }  
界面层:展现给用户看的,用图形界面展现蛇的运动
[java]  view plain  copy
  1. package cn.hncu;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Canvas;  
  5. import java.awt.Color;  
  6. import java.awt.Container;  
  7. import java.awt.Graphics;  
  8. import java.util.Iterator;  
  9. import java.util.LinkedList;  
  10. import java.util.Observable;  
  11. import java.util.Observer;  
  12.   
  13. import javax.swing.JFrame;  
  14. import javax.swing.JLabel;  
  15. import javax.swing.JPanel;  
  16.   
  17. public class View extends JFrame implements Observer{  
  18.     Control control;  
  19.     Model model;  
  20.       
  21.     Canvas canvas;  
  22.       
  23.     public static final int canvasWidth=800,canvasHeight=500;  
  24.     public static final int nodeWidth=10,nodeHeight=10;  
  25.       
  26.       
  27.     public View(Model model,Control control){  
  28.         super("GreedySnake");  
  29.         this.control=control;  
  30.         this.model=model;  
  31.           
  32.         this.setLocation(400300);  
  33.         this.setResizable(false);  
  34.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  35.           
  36.         canvas=new Canvas();  
  37.         canvas.setSize(canvasWidth+1, canvasHeight+1);  
  38.         canvas.addKeyListener(control);  
  39.         this.add(canvas,BorderLayout.NORTH);  
  40.           
  41.         JPanel panel=new JPanel();  
  42.         this.add(panel,BorderLayout.SOUTH);  
  43.         JLabel label=new JLabel("Enter for restart");  
  44.         panel.add(label);  
  45.           
  46.         this.pack();  
  47.         this.addKeyListener(control);  
  48.         this.setVisible(true);  
  49.     }  
  50.       
  51.     public void repaint() {  
  52.         Graphics g=canvas.getGraphics();  
  53.           
  54. //      draw background  
  55.         g.setColor(Color.white);  
  56.         g.fillRect(00, canvasWidth, canvasHeight);  
  57.           
  58. //      draw snake  
  59.         g.setColor(Color.red);  
  60.         LinkedList node=model.node;  
  61.         Iterator it=node.iterator();  
  62.         while(it.hasNext()){  
  63.             Node n=(Node)it.next();  
  64.             drawNode(g,n);  
  65.         }  
  66.           
  67. //      draw food  
  68.         g.setColor(Color.black);  
  69.         Node n=model.food;  
  70.         drawNode(g,n);  
  71.     }  
  72.       
  73.     private void drawNode(Graphics g, Node n) {  
  74.         g.fillOval(n.x*nodeWidth, n.y*nodeHeight, nodeWidth, nodeHeight);  
  75.     }  
  76.   
  77.     public void update(Observable o, Object arg) {  
  78.         repaint();  
  79.     }  
  80. }  


  • 11
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
第1章 记忆测试软件1.1. 设计内容1.2. 设计要求1.3. 总体设计1.4. 具体设计1.4.1. 运行效果与程序发布1.4.2. 主类Memory1.4.3. 方块 Block1.4.4. 记忆测试板MemoryTestPane1.4.5. 显示成绩 ShowRecord1.4.6. 记录成绩 Record1.4.7. 随机排列图标 RandomSetIcon1.4.8. 测试者 People1.5. 课程设计作业第2章 计算器2.1. 设计内容2.2. 设计要求2.3. 总体设计2.4. 具体设计2.4.1. 运行效果与程序发布2.4.2. 主类 ComputerPad2.4.3. 数值按钮NumberButton2.4.4. 运算符号按钮OperationButton2.5. 课程设计作业第3章 HANNOI-塔3.1. 设计内容3.2. 设计要求3.3. 总体设计3.4. 具体设计3.4.1. 运行效果与程序发布3.4.2. 主类 Tower3.4.3. Hannoi-塔 HannoiTower3.4.4. 塔点 TowerPoint3.4.5. 盘子 Disk3.5. 课程设计作业第4章 JPEG图象生成器4.1. 设计内容4.2. 设计要求4.3. 总体设计4.4. 具体设计4.4.1. 运行效果与程序发布4.4.2. 主类 MakeJPEG.java4.5. 课程设计作业第5章 标准化考试系统 (单机版)5.1. 设计内容5.2. 设计要求5.3. 总体设计5.4. 具体设计5.4.1. 运行效果与程序发布5.4.2. 主类EnglishTest5.4.3. 考试区域TestArea5.4.4. 读取试题 ReadTestquestion5.5. 课程设计作业第6章 标准化考试系统 (C/S网络版)6.1. 设计内容6.2. 设计要求6.3. 总体设计6.4. 具体设计6.4.1. 运行效果与程序发布6.4.2. 客户端主类Client6.4.3. 客户端选择试题界面ChoiceFile6.4.4. 客户端考试界面ClientTestArea6.4.5. 服务器端主类Server6.4.6. 服务器端读取试题 ReadTestquestion6.5. 课程设计作业第7章 标准化考试系统 (B/S网络版)7.1. 设计内容7.2. 设计要求7.3. 总体设计7.4. 具体设计7.4.1. 运行效果与程序发布7.4.2. 客户端主类ClientBS7.4.3. 客户端选择试题界面ChoiceFile7.4.4. 客户端考试界面ClientTestArea7.4.5. 服务器端主类Server7.4.6. 服务器端读取试题 ReadTestquestion7.5. 课程设计作业第8章 日历记事本8.1. 设计内容8.2. 设计要求8.3. 总体设计8.4. 具体设计8.4.1. 运行效果与程序发布8.4.2. 主类CalendarPad8.4.3. 记事本NotePad8.4.4. 年Year8.4.5. 月Month8.5. 课程设计作业18.6. 课程设计作业2第9章 学籍管理系统9.1. 设计内容9.2. 设计要求9.3. 总体设计9.4. 具体设计9.4.1. 运行效果与程序发布9.4.2. 主类StudentManager9.4.3. 录入界面StudentSituation9.4.4. 查询界面Inquest9.4.5. 修改界面ModifySituation9.4.6. 删除界面Delete9.4.7. 学生对象Student9.5. 课程设计作业第10章 图书查询系统 (B/S网络版)10.1. 设计内容10.2. 设计要求10.3. 总体设计10.4. 具体设计10.4.1. 运行效果与程序发布10.4.2. 客户端主类DatabaseClient10.4.3. 服务器端主类DatabaseServer10.5. 课程设计作业第11章 中国象棋打谱软件11.1. 设计内容11.2. 设计要求11.3. 总体设计11.4. 具体设计11.4.1. 运行效果与程序发布11.4.2. 主类 Chess11.4.3. 对弈棋盘ChessBoard11.4.4. 棋子ChessPiece11.4.5. 棋点 ChessPoint11.4.6. 走棋法则Rule11.4.7. 步骤MoveStep11.4.8. 记录棋谱MakeChessManual11.4.9. 棋谱演示Demon11.5. 课程设计作业111.6. 课程设计作业2第12章 魔板游戏12.1. 设计内容12.2. 设计要求12.3. 总体设计12.4. 具体设计12.4.1. 运行效果与程序发布12.4.2. 主类PuzzleFrame12.4.3. 魔板PuzzlePad12.4.4. 魔板中的点SquarePoint12.5. 课程设计作业第13章 挖雷游戏13.1. 设计内容13.2. 设计要求13.3. 总体设计13.4. 具体设计13.4.1. 运行效果与程序发布13.4.2. 主类Game13.4.3. 方块 Block13.4.4. 雷区 MineSquare13.4.5. 雷标数目 FindAroundMineMarkNumber13.4.6. 雷标判断 DetermineMineMarkIsRightOrWrong13.4.7. 成片挖开区域 DetermineDigArea13.4.8. 无雷连通区 FindSafeArea13.4.9. 随机布雷 RandomSetMine13.4.10. 周围地雷个数FindAroundMineNumber13.4.11. 显示剩余雷数CountMine13.4.12. 计时器TimeCount13.4.13. 英雄榜录入对话框Record13.4.14. 显示英雄榜对话框ShowRecord13.4.15. 挖雷成功DecideWinner13.5. 课程设计作业第14章 网络聊天室 (B/S模式)14.1. 设计内容14.2. 设计要求14.3. 总体设计14.4. 具体设计14.4.1. 运行效果与程序发布14.4.2. 客户端主类ClientChat14.4.3. 客户端输入妮称界面InputNameTextField14.4.4. 客户端聊天界面ChatArea14.4.5. 服务器端主类ChatServer14.5. 课程设计作业第15章 局域网络广播系统15.1. 设计内容15.2. 设计要求15.3. 总体设计15.4. 具体设计15.4.1. 运行效果与程序发布15.4.2. 客户端主类Receive15.4.3. 服务器端主类BroadCastWord15.5. 课程设计作业

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值