鱼C论坛

 找回密码
 立即注册
查看: 2851|回复: 2

[学习笔记] 0基础学习Java ——贪吃蛇的项目(跟别人学的...)

[复制链接]
发表于 2017-8-15 10:40:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 回忆一遥远 于 2017-8-16 07:34 编辑

先上各个文件

Dir.java
  1. public enum Dir {
  2.         L, U, R, D
  3. }
复制代码

记录了四个方向的标识



Egg.java

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Rectangle;
  4. import java.util.Random;

  5. public class Egg {
  6.         public int getRow() {
  7.                 return row;
  8.         }

  9.         public void setRow(int row) {
  10.                 this.row = row;
  11.         }

  12.         public int getCol() {
  13.                 return col;
  14.         }

  15.         public void setCol(int col) {
  16.                 this.col = col;
  17.         }

  18.         int row, col;
  19.         int w = Yard.BLOCK_SIZE;
  20.         int h = Yard.BLOCK_SIZE;
  21.         private static Random r = new Random();
  22.         private Color color = Color.GREEN;
  23.        
  24.         public Egg(int row, int col) {
  25.                 this.row = row;
  26.                 this.col = col;
  27.         }

  28.         public Egg() {
  29.                 this(r.nextInt(Yard.ROWS - 3) + 3, r.nextInt(Yard.COLS));
  30.         }
  31.        
  32.         public void reAppear() {
  33.                 this.row = r.nextInt(Yard.ROWS - 3) + 3;
  34.                 this.col = r.nextInt(Yard.COLS);
  35.         }
  36.        
  37.         public Rectangle getRect() {
  38.                 return new Rectangle(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
  39.         }
  40.        
  41.         void draw(Graphics g) {
  42.                 Color c = g.getColor();
  43.                 g.setColor(color);
  44.                 g.fillOval(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
  45.                 g.setColor(c);
  46.                 if(color == Color.GREEN) color = Color.BLUE;
  47.                 else color = Color.GREEN;
  48.         }
  49. }
复制代码

定义和蛇要吃的蛋的行为和属性

Snake.java
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Rectangle;
  4. import java.awt.event.KeyEvent;

  5. public class Snake {
  6.         private Node head = null;
  7.         private Node tail = null;
  8.         private int size = 0;
  9.        
  10.         private Node n = new Node(20, 30, Dir.L);
  11.         private Yard y;
  12.        
  13. /*        public Snake(Node node) {
  14.                 head = node;
  15.                 tail = node;
  16.                 size = 1;
  17.         }*/
  18.        
  19.         public Snake(Yard y) {
  20.                 head = n;
  21.                 tail = n;
  22.                 size = 1;
  23.                 this.y = y;
  24.         }
  25.        
  26.         public void addToTall() {
  27.                 Node node = null;
  28.                 switch(tail.dir) {
  29.                 case L :
  30.                         node = new Node(tail.row, tail.col + 1, tail.dir);
  31.                         break;
  32.                 case U :
  33.                         node = new Node(tail.row + 1, tail.col, tail.dir);
  34.                         break;
  35.                 case R :
  36.                         node = new Node(tail.row, tail.col - 1, tail.dir);
  37.                         break;
  38.                 case D :
  39.                         node = new Node(tail.row - 1, tail.col, tail.dir);
  40.                         break;
  41.                 }
  42.                 tail.next = node;
  43.                 node.prev = tail;
  44.                 tail = node;
  45.                 size++;
  46.         }
  47.        
  48.         public void addToHead() {
  49.                 Node node = null;
  50.                 switch(head.dir) {
  51.                 case L :
  52.                         node = new Node(head.row, head.col - 1, head.dir);
  53.                         break;
  54.                 case U :
  55.                         node = new Node(head.row - 1, head.col, head.dir);
  56.                         break;
  57.                 case R :
  58.                         node = new Node(head.row, head.col + 1, head.dir);
  59.                         break;
  60.                 case D :
  61.                         node = new Node(head.row + 1, head.col, head.dir);
  62.                         break;
  63.                 }
  64.                 node.next = head;
  65.                 head.prev = node;
  66.                 head = node;
  67.                 size++;
  68.         }
  69.        
  70.         public void draw(Graphics g) {
  71.                 if(size <= 0) return;
  72.                 move();
  73.                 for(Node n = head; n != null; n = n.next) {
  74.                         n.draw(g);
  75.                 }
  76.                
  77.         }
  78.        
  79.         private void move() {
  80.                 // TODO Auto-generated method stub
  81.                 addToHead();
  82.                 deleteFromTail();
  83.                 checkDead();
  84.         }

  85.         private void checkDead() {
  86.                 // TODO Auto-generated method stub
  87.                 if(head.row < 2 || head.col < 0 || head.row > Yard.ROWS || head.col > Yard.COLS) {
  88.                         y.stop();
  89.                 }
  90.                
  91.                 for(Node n = head.next; n != null; n = n.next) {
  92.                         if(head.row == n.row && head.col == n.col) {
  93.                                 y.stop();
  94.                         }
  95.                 }
  96.         }

  97.         private void deleteFromTail() {
  98.                 // TODO Auto-generated method stub
  99.                 if(size == 0) return;
  100.                 tail = tail.prev;
  101.                 tail.next = null;
  102.                
  103.         }

  104.         private class Node{
  105.                 int w = Yard.BLOCK_SIZE;
  106.                 int h = Yard.BLOCK_SIZE;
  107.                 int row , col;
  108.                 Dir dir = Dir.L;
  109.                 Node next = null;
  110.                 Node prev = null;

  111.                 Node(int row, int col, Dir dir) {
  112.                         this.row = row;
  113.                         this.col = col;
  114.                         this.dir = dir;
  115.                 }
  116.                
  117.                 void draw(Graphics g) {
  118.                         Color c = g.getColor();
  119.                         g.setColor(Color.BLACK);
  120.                         g.fillRect(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
  121.                         g.setColor(c);
  122.                 }
  123.         }

  124.         public void eat(Egg e) {
  125.                 if(this.getRect().intersects(e.getRect())) {
  126.                         e.reAppear();
  127.                         this.addToHead();
  128.                         y.setScore(y.getScore() + 5);
  129.                 }
  130.         }
  131.        
  132.         private Rectangle getRect() {
  133.                 return new Rectangle(Yard.BLOCK_SIZE * head.col, Yard.BLOCK_SIZE * head.row, head.w, head.h);
  134.         }

  135.        
  136.        
  137.         public void keyPressed(KeyEvent e) {
  138.                 // TODO Auto-generated method stub
  139.                 int key = e.getKeyCode();
  140.                 switch(key) {
  141.                 case KeyEvent.VK_LEFT:
  142.                         if(head.dir != Dir.R)
  143.                                 head.dir = Dir.L;
  144.                         break;
  145.                 case KeyEvent.VK_UP:
  146.                         if(head.dir != Dir.D)
  147.                                 head.dir = Dir.U;
  148.                         break;
  149.                 case KeyEvent.VK_RIGHT:
  150.                         if(head.dir != Dir.L)
  151.                                 head.dir = Dir.R;
  152.                         break;
  153.                 case KeyEvent.VK_DOWN:
  154.                         if(head.dir != Dir.U)
  155.                                 head.dir = Dir.D;
  156.                         break;
  157.                 }
  158.         }
  159. }
复制代码

定义了蛇的行为和属性



Yard.java
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;

  10. public class Yard extends Frame {

  11.         PaintThread paintThread = new PaintThread();
  12.         public boolean gameOver = false;
  13.        
  14.         public static final int ROWS = 50;
  15.         public static final int COLS = 50;
  16.         public static final int BLOCK_SIZE = 20;
  17.        
  18.         private int score = 0;
  19.        
  20.         Snake s = new Snake(this);
  21.         Egg e = new Egg();
  22.        
  23.         Image offScreenImage = null;
  24.        
  25.         public void launch() {
  26.                 this.setLocation(300, 300);
  27.                 this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
  28.                 this.addWindowListener(new WindowAdapter() {

  29.                         @Override
  30.                         public void windowClosing(WindowEvent e) {
  31.                                 // TODO Auto-generated method stub
  32.                                 System.exit(0);
  33.                         }
  34.                        
  35.                 });
  36.                 this.setVisible(true);
  37.                 this.addKeyListener(new KeyMonitor());
  38.                
  39.                 new Thread(paintThread).start();
  40.                 //new Thread(paintThread).start();
  41.         }
  42.        
  43.         public static void main(String[] args) {
  44.                 // TODO Auto-generated method stub
  45.                 new Yard().launch();
  46.         }

  47.         public void stop() {
  48.                 gameOver = true;
  49.         }
  50.        
  51.         @Override
  52.         public void paint(Graphics g) {
  53.                 // TODO Auto-generated method stub
  54.                 Color c = g.getColor();
  55.                 g.setColor(Color.GRAY);
  56.                 g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
  57.                 g.setColor(Color.DARK_GRAY);
  58.                 // Print line
  59.                 for(int i = 0; i < ROWS; i++) {
  60.                         g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
  61.                 }
  62.                 for(int i = 0; i < COLS; i++) {
  63.                         g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
  64.                 }
  65.                
  66.                 g.setColor(Color.YELLOW);
  67.                 g.drawString("score: " + score, 10, 60);
  68.                
  69.                 if(gameOver) {
  70.                         g.setFont(new Font("Verdana", Font.BOLD, 100));
  71.                         g.drawString("Game over", 180, 400);

  72.                         paintThread.pause();
  73.                 }
  74.                
  75.                 g.setColor(c);
  76.                
  77.                 s.eat(e);
  78.                 e.draw(g);
  79.                 s.draw(g);
  80.                
  81.                
  82.         }
  83.        
  84.         @Override
  85.         public void update(Graphics g) {
  86.                 // TODO Auto-generated method stub
  87.                 if(offScreenImage == null) {
  88.                         offScreenImage = this.createImage(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
  89.                 }
  90.                 Graphics gOff = offScreenImage.getGraphics();
  91.                 paint(gOff);
  92.                 g.drawImage(offScreenImage, 0, 0, null);
  93.         }
  94.        
  95.        
  96.         private class PaintThread implements Runnable {
  97.                 private boolean runing = true;
  98.                 private boolean pause = false;
  99.                 @Override
  100.                 public void run() {
  101.                         // TODO Auto-generated method stub
  102.                         while(runing) {
  103.                                 if(pause) continue;
  104.                                 else repaint();
  105.                                
  106.                                 try {
  107.                                         Thread.sleep(120);
  108.                                 } catch (InterruptedException e) {
  109.                                         e.printStackTrace();
  110.                                 }
  111.                         }
  112.                        
  113.                 }
  114.                 public void pause() {
  115.                         this.pause = true;
  116.                 }
  117.                
  118.                 public void reStart() {
  119.                         /*this.pause = false;
  120.                         s = new Snake(Yard.this);
  121.                         gameOver = false;*/
  122.                         new Yard().launch();
  123.                 }
  124.                
  125.                 /*public void gameOver() {
  126.                         runing = false;
  127.                 }*/
  128.         }
  129.        
  130.         private class KeyMonitor extends KeyAdapter{

  131.                 @Override
  132.                 public void keyPressed(KeyEvent e) {
  133.                         int key = e.getKeyCode();
  134.                         if(key == KeyEvent.VK_F2) {
  135.                                 paintThread.reStart();
  136.                         }
  137.                         // TODO Auto-generated method stub
  138.                         s.keyPressed(e);
  139.                 }
  140.                
  141.         }
  142.        
  143.         public int getScore() {
  144.                 return score;
  145.         }
  146.        
  147.         public void setScore(int score) {
  148.                 this.score = score;
  149.         }
  150.        
  151. }
复制代码

定义了环境的属性...和游戏的判断(规则)

操作方法:方向键控制方向,F2 重置游戏


噗~不能上传图片了,明天再上传吧  




评分

参与人数 1鱼币 +7 收起 理由
小甲鱼 + 7

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-15 11:10:51 | 显示全部楼层
数组可以使用 length 来获取数组的长度

Math.random() 获取 0-1 之间的随机数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-16 17:13:56 | 显示全部楼层
谢谢分享,学习下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 10:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表