免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1378 | 回复: 0

贪吃蛇电脑版本 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-07-07 22:20:00
发表于 2015-07-06 14:26 |显示全部楼层
Frame.java
  1. package snake;

  2. import java.awt.Graphics;
  3. import java.awt.Menu;
  4. import java.awt.MenuBar;
  5. import java.awt.MenuItem;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;

  10. import javax.swing.JFrame;

  11. public class Frame extends JFrame implements KeyListener {

  12.     /**
  13.      *
  14.      */
  15.      
  16.     Boolean isAlive;
  17.     Boolean isPause;
  18.     Panel panel;
  19.     Character  direction;
  20.     private static final long serialVersionUID = 1L;

  21.     public Frame(){
  22.         // TODO Auto-generated constructor stub
  23.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.         setSize(300,300);
  25.         addKeyListener(this);
  26.         panel = new Panel();
  27.         add(panel);
  28.         setVisible(true);
  29.         isAlive = true;
  30.         isPause = false;
  31.         direction = new Character('d');
  32.         MenuBar menuBar = new MenuBar();
  33.         Menu menu = new Menu("menu");
  34.         MenuItem reset = new MenuItem("newgame");
  35.         MenuItem pause= new MenuItem("pause");
  36.          
  37.         pause.addActionListener(new ActionListener(){

  38.             @Override
  39.             public void actionPerformed(ActionEvent e) {
  40.                 // TODO Auto-generated method stub
  41.                 if(!isPause) isPause= true;
  42.                 else         isPause= false;
  43.             }
  44.             
  45.         });
  46.          
  47.         reset.addActionListener(new ActionListener(){

  48.             @Override
  49.             public void actionPerformed(ActionEvent e) {
  50.                 // TODO Auto-generated method stub
  51.                 reset();
  52.             }
  53.             
  54.         });
  55.          
  56.         menu.add(reset);
  57.         menu.add(pause);
  58.         menuBar.add(menu);
  59.         setMenuBar(menuBar);
  60.          
  61.          
  62.     }
  63.      
  64.     public void reset(){
  65.         panel.reset();
  66.         isAlive = true;
  67.     }

  68.     @Override
  69.     public void keyTyped(KeyEvent e) {
  70.         // TODO Auto-generated method stub
  71.          
  72.     }

  73.     @Override
  74.     public void keyPressed(KeyEvent e) {
  75.         // TODO Auto-generated method stub
  76.         if(e.getKeyCode()==KeyEvent.VK_UP)    direction = 'w';
  77.         if(e.getKeyCode()==KeyEvent.VK_DOWN)  direction = 's';
  78.         if(e.getKeyCode()==KeyEvent.VK_LEFT)  direction = 'a';
  79.         if(e.getKeyCode()==KeyEvent.VK_RIGHT) direction = 'd';
  80.     }

  81.     @Override
  82.     public void keyReleased(KeyEvent e) {
  83.         // TODO Auto-generated method stub
  84.          
  85.     }
  86.      
  87.     public void paint(Graphics g){
  88.         panel.repaint();
  89.     }

  90. }
复制代码
Launch.java
  1. package snake;

  2. import java.util.Timer;
  3. import java.util.TimerTask;

  4. public class Launch extends TimerTask {

  5.     Frame frame = new Frame();
  6.     public Launch() {
  7.         // TODO Auto-generated constructor stub
  8.          
  9.     }
  10.      
  11.     boolean crashWall(){
  12.         SnakeBody sb = frame.panel.snake.getFirst();
  13.         if((sb.x<0)||(sb.y<0)||(sb.x>=Panel.LINE)||(sb.y>=Panel.LINE))
  14.             return true;
  15.         else
  16.             return false;
  17.     }
  18.     void initial(){
  19.         frame.panel.snake.add(newBody());
  20.         frame.panel.food = newBody();
  21.     }
  22.     @Override
  23.     public void run() {
  24.         // TODO Auto-generated method stub
  25.         if(frame.panel.snake.isEmpty())
  26.             initial();
  27.         if(frame.isAlive)
  28.             if(!frame.isPause){
  29.                 if(goStraight())
  30.                      frame.isAlive = false;
  31.                 frame.repaint();
  32.             }
  33.        if(crashWall())  frame.isAlive = false;
  34.     }
  35.      
  36.     SnakeBody newBody(){
  37.         SnakeBody sb = new SnakeBody();

  38.         boolean overlap = true;
  39.         while(overlap){
  40.             overlap =false;
  41.             sb.x  =  (int) (Math.random()*(Panel.LINE-2)+1);
  42.             sb.y  =  (int) (Math.random()*(Panel.LINE-2)+1);
  43.             if(!frame.panel.snake.isEmpty())
  44.             for(SnakeBody s : frame.panel.snake)
  45.                 if(sb.equals(s))
  46.                     overlap =true;
  47.         }
  48.          
  49.         return sb;
  50.     }

  51.      
  52.     void eat(SnakeBody sb){
  53.         frame.panel.snake.addFirst(sb);
  54.     }
  55.     boolean goStraight(){
  56.          
  57.         boolean result = false;
  58.          
  59.         SnakeBody sb =new SnakeBody(frame.panel.snake.getFirst());
  60.         frame.panel.snake.removeLast();
  61.         if(frame.direction=='w')
  62.             sb.turnUp();
  63.         if(frame.direction=='s')
  64.             sb.turnDown();
  65.         if(frame.direction=='a')
  66.             sb.turnLeft();
  67.         if(frame.direction=='d')
  68.             sb.turnRight();
  69.          
  70.         for(SnakeBody s : frame.panel.snake){
  71.             if(sb.equals(s))  result = true;
  72.         }
  73.          
  74.         frame.panel.snake.addFirst(sb);
  75.         if(sb.equals(frame.panel.food)){
  76.             if(frame.direction=='w')
  77.                 frame.panel.food.turnUp();
  78.             if(frame.direction=='s')
  79.                 frame.panel.food.turnDown();
  80.             if(frame.direction=='a')
  81.                 frame.panel.food.turnLeft();
  82.             if(frame.direction=='d')
  83.                 frame.panel.food.turnRight();
  84.             eat(frame.panel.food);
  85.             frame.panel.food = newBody();
  86.         }
  87.          
  88.         return result;
  89.     }

  90.      
  91.      
  92.     public static void main(String[] args){
  93.         // TODO Auto-generated method stub
  94.          Launch  timertask = new Launch();
  95.          Timer timer = new Timer();
  96.          
  97.          timer.schedule(timertask,0,500);
  98.     }

  99. }
复制代码
Panel.java
  1. package snake;

  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.util.LinkedList;

  5. import javax.swing.JPanel;

  6. public class Panel extends JPanel {

  7.        /**
  8.      *
  9.      */
  10.     private static final long serialVersionUID = 1L;
  11.     public LinkedList<SnakeBody> snake = new LinkedList<SnakeBody>();
  12.     static final int LINE = 10;
  13.     SnakeBody  food = new SnakeBody(-99,-99);
  14.         
  15.     public Panel() {
  16.         // TODO Auto-generated constructor stub
  17.     }
  18.      
  19.     public void reset(){
  20.        snake.clear();   
  21.     }
  22.      
  23.     public void paint(Graphics g){
  24.         g.setColor(Color.white);
  25.         g.fillRect(0, 0, getWidth(), getHeight());

  26.    
  27.         for(SnakeBody sb : snake){
  28.             g.setColor(Color.black);
  29.             g.drawRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
  30.             g.setColor(Color.orange);
  31.             g.fillRect(sb.x*getWidth()/LINE,sb.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
  32.         }
  33.         g.setColor(Color.red);
  34.         g.fillRect(food.x*getWidth()/LINE,food.y*getHeight()/LINE,getWidth()/LINE,getHeight()/LINE);
  35.          
  36.     }
  37. }
复制代码
SnakeBody.java
  1. package snake;

  2. class SnakeBody {
  3.      
  4.     int x;
  5.     int y;
  6.      
  7.     public SnakeBody() {
  8.         // TODO Auto-generated constructor stub
  9.        x = 0;
  10.        y = 0;
  11.     }
  12.      
  13.     public SnakeBody(int a,int b){
  14.         x = a;
  15.         y = b;
  16.     }
  17.     public SnakeBody(SnakeBody sb){
  18.         this(sb.x,sb.y);
  19.     }
  20.      
  21.     public void turnUp(){
  22.         y--;
  23.     }
  24.     public void turnDown(){
  25.         y++;
  26.     }
  27.     public void turnLeft(){
  28.         x--;
  29.     }
  30.     public void turnRight(){
  31.         x++;
  32.     }
  33.      
  34.     boolean equals(SnakeBody s){
  35.         if((x==s.x)&&(y==s.y))  return true;
  36.         else                    return false;
  37.     }

  38. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP