一个可玩的贪吃蛇源代码

原本以为做一个贪吃蛇的雏形很简单,因为它的行为很简单,但是到具体开发的时候发现有很多细节需要注意:

  • 键盘监听
  • 如何让蛇移动起来
  • 如何控制蛇的速度
  • 如何让蛇边长
  • 蛇碰到身子要死亡
  • 碰到墙也要死亡
  • 如何控制蛇的速度
  • 蛇不能后退走
  • 如何控制蛋的随机出现
  • 如何控制蛋的颜色变化
  • 还要有计分功能

以下是代码的具体实现,一共有四个类,其中一个是测试类

MyYard.java

package eggs;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

import yard.MyYard;

public class MyEgg {
    int row,col;
    private Color color;
    private static Random r =new Random();
    public MyEgg(){
        this(r.nextInt(MyYard.ROWS-2)+2,r.nextInt(MyYard.COLS));
    }
    public MyEgg(int row,int col){
        this.row=row; 
        this.col=col;
    }
    public void draw (Graphics g){
        if(color == Color.PINK){
            color = Color.GREEN;
        }else{
            color = Color.PINK;
        }
        g.setColor(color);
        g.fillOval(MyYard.BLOCK_SIZE * col, MyYard.BLOCK_SIZE * row, MyYard.BLOCK_SIZE, MyYard.BLOCK_SIZE);
    }
    public void reAppear(){
        this.row=r.nextInt(MyYard.ROWS-2)+2;
        this.col=r.nextInt(MyYard.COLS);
    }
    public Rectangle getRect(){
        return new Rectangle(MyYard.BLOCK_SIZE*col,MyYard.BLOCK_SIZE*row,MyYard.BLOCK_SIZE,MyYard.BLOCK_SIZE);
    }
}

MySnake.java

package snake;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import eggs.MyEgg;
import yard.MyYard;

public class MySnake {
    private Node head =null;
    private Node tail =null;
    private int SCORE =0;
    public MySnake(){
        tail=head=new Node(15,15,1);
    }
    public Node getHead(){
        return this.head;
    }
    public int getScore(){
        return this.SCORE;
    }
    public void addToHead(){
        Node node = null;
        switch (head.dir){
        case 1:
            node = new Node(head.row-1,head.col,1);
            break;
        case 2:
            node = new Node(head.row,head.col+1,2);
            break;
        case 3:
            node = new Node(head.row+1,head.col,3);
            break;
        case 4:
            node = new Node(head.row,head.col-1,4);
            break;
        }
        node.next=head;
        head.privious=node;
        head=node;
    }
    private void deleteFromTail() {
        tail=tail.privious;
        tail.next=null;
    }
    public void draw(Graphics g){
        addToHead();
        deleteFromTail();
        for(Node n=head;n!=null;n=n.next){
            n.draw(g);
        }
    }
    public class Node{
        public int row;
        public int col;
        int dir;
        public Node next;
        Node privious;
        Node(int row,int col,int dir){
            this.row=row;
            this.col=col;
            this.dir=dir;
        }
        public Rectangle getRect(){
            return new Rectangle(MyYard.BLOCK_SIZE*col,MyYard.BLOCK_SIZE*row,MyYard.BLOCK_SIZE,MyYard.BLOCK_SIZE);
        }
        void draw (Graphics g){
            g.setColor(Color.RED);
            g.fillRect(MyYard.BLOCK_SIZE * col, MyYard.BLOCK_SIZE * row, MyYard.BLOCK_SIZE, MyYard.BLOCK_SIZE);
        }
    }
    public void eat(MyEgg e){
            if(head.getRect().intersects(e.getRect())){
                e.reAppear();
                this.addToHead();
                SCORE +=5;
            }
        }
    public void keyPressed(KeyEvent e) {
        int key=e.getKeyCode();
        switch(key){
            case KeyEvent.VK_UP:
                if(head.dir!=3)
                    head.dir = 1;
                break;
            case KeyEvent.VK_RIGHT:
                 if(head.dir!=4)
                head.dir = 2;
                break;
            case KeyEvent.VK_DOWN:
                 if(head.dir!=1)
                head.dir = 3;
                break;
            case KeyEvent.VK_LEFT:
                 if(head.dir!=2)
                head.dir = 4;
                break;
        }
    }
}

MyEgg.java

package eggs;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

import yard.MyYard;

public class MyEgg {
    int row,col;
    private Color color;
    private static Random r =new Random();
    public MyEgg(){
        this(r.nextInt(MyYard.ROWS-2)+2,r.nextInt(MyYard.COLS));
    }
    public MyEgg(int row,int col){
        this.row=row; 
        this.col=col;
    }
    public void draw (Graphics g){
        if(color == Color.PINK){
            color = Color.GREEN;
        }else{
            color = Color.PINK;
        }
        g.setColor(color);
        g.fillOval(MyYard.BLOCK_SIZE * col, MyYard.BLOCK_SIZE * row, MyYard.BLOCK_SIZE, MyYard.BLOCK_SIZE);
    }
    public void reAppear(){
        this.row=r.nextInt(MyYard.ROWS-2)+2;
        this.col=r.nextInt(MyYard.COLS);
    }
    public Rectangle getRect(){
        return new Rectangle(MyYard.BLOCK_SIZE*col,MyYard.BLOCK_SIZE*row,MyYard.BLOCK_SIZE,MyYard.BLOCK_SIZE);
    }
}

TestDemo.java

package test;

import org.junit.Test;

import yard.MyYard;

public class TestDemo {
    @Test
    public void testSnakeAndEggs(){
        new MyYard().launch();
    }
}

总结:这个贪吃蛇雏形虽然只有两百多行,但是在编写的过程中我学到了很多东西。也意识到很重要的一点:很多在我们潜意识里认为理所应当的逻辑在实际开发中都需要我们考虑;

GitHub地址在这里

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值