前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA简易贪吃蛇的实现

JAVA简易贪吃蛇的实现

作者头像
明明如月学长
发布2021-08-27 11:03:23
3220
发布2021-08-27 11:03:23
举报
文章被收录于专栏:明明如月的技术专栏

原文地址:JAVA简易贪吃蛇的实现 作者:云台听雪

//---主方法-----------------

public class GreedSnake {

public static void main(String[] args) {

SnakeModel model=new SnakeModel();

SnakeControl control=new SnakeControl(model);

SnakeView view=new SnakeView(model,control);

new Thread(model).start();

new Thread(view).start();

}

}

//---SnakeModel------------

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Random;

public class SnakeModel implements Runnable {

static boolean [][]matrix;

int maxX,maxY;

static int freshTime;

int bufDir;

Node head;

Node food;

public static final int UP=1;

public static final int DOWN=3;

public static final int LEFT=2;

public static final int RIGHT=4;

LinkedList snakeList=new LinkedList();

public Node createFood(){

Random r=new Random();

int x,y;

while(true){

x=r.nextInt(maxX);

y=r.nextInt(maxY);

if(!matrix[x][y]){

break;

}

}

Node n=new Node(x,y,LEFT);

matrix[x][y]=true;

System.out.println("matrix "+x+" "+y);

return n;

}

public SnakeModel(){

head=new Node(2,3,RIGHT);

maxX=30;

maxY=30;

freshTime=200;

matrix=new boolean[maxX][maxY];

snakeList.add(head);

snakeList.add(new Node(2,2,DOWN));

food=createFood();

bufDir=head.drct;

}

public void move(){ 

ListIterator it=snakeList.listIterator();

//吃掉食物

System.out.println("head "+head.x+" "+head.y);

boolean near=false;

switch(head.drct){

case UP:

if(food.x==head.x&&food.y==(head.y-1)){

near=true;

}

break;

case DOWN:

if(food.x==head.x&&food.y==(head.y+1)){

near=true;

}

break;

case LEFT:

if(food.x==(head.x-1)&&food.y==head.y){

near=true;

}

break;

case RIGHT:

if(food.x==(head.x+1)&&food.y==head.y){

near=true;

}

break;

}

if(near){

System.out.println("eat");

snakeList.addFirst(food);

food.drct=head.drct;

head=food;

food=createFood();

near=false;

}

else{

it.next();

while(it.hasNext()){

Node n=it.next();

if(head.x==n.x&&head.y==n.y){

System.out.println("你输了");

System.exit(0);

}

}

while(it.hasPrevious()){

Node n=it.previous();

if(it.hasPrevious()){

if(snakeList.get(it.previousIndex()).xx){

n.drct=LEFT;

}

else if(snakeList.get(it.previousIndex()).x>n.x){

n.drct=RIGHT;

}

else if(snakeList.get(it.previousIndex()).yy){

n.drct=UP;

}

else if(snakeList.get(it.previousIndex()).y>n.y){

n.drct=DOWN;

}

}

}

while(it.hasNext()){

Node n=it.next();

try{

nodeMove(n);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("你输了");

System.exit(0);

}

}

}

}

public void changeDirection(){

if(Math.abs(bufDir-head.drct)==2){

bufDir=head.drct;

}

else

head.drct=bufDir;

}

public void nodeMove(Node n){

matrix[n.x][n.y]=false;

switch(n.drct){

case UP:

n.y--;

break;

case DOWN:

n.y++;

break;

case LEFT:

n.x--;

break;

case RIGHT:

n.x++;

break;

}

matrix[n.x][n.y]=true;

}

public void run(){

while(true){

changeDirection();

move();

try{

Thread.sleep(freshTime);

}catch(Exception e){}

}

}

}

class Node{

int x;

int y;

int drct;

Node(int x,int y,int direct){

this.x=x;

this.y=y;

drct=direct;

}

}

//---SnakeControl-------------------------

import java.awt.Frame;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

public class SnakeControl extends KeyAdapter{

SnakeModel model;

SnakeControl(SnakeModel model){

this.model=model;

}

public void keyPressed(KeyEvent e){

switch(e.getKeyCode()){

case 38:

model.bufDir=SnakeModel.UP;

break;

case 40:

model.bufDir=SnakeModel.DOWN;

break;

case 37:

model.bufDir=SnakeModel.LEFT;

break;

case 39:

model.bufDir=SnakeModel.RIGHT;

}

}

}

//---SnakeView----------------------

import java.awt.Color;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class SnakeView extends Frame implements Runnable{

int nodeWidth=10;

int nodeHeight=10;

SnakeView(SnakeModel model,SnakeControl control){

model.maxX=model.maxX;

model.maxY=model.maxY;

setSize(model.maxX*nodeWidth+20,model.maxY*nodeHeight+40);

setLocation(300,200);

setVisible(true);

addKeyListener(control);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setBackground(Color.RED);

setResizable(false);

}

public void repaint(){

Graphics g=getGraphics();

for(int i=0;imatrix.length;i++){

for(int j=0;jmatrix[i].length;j++){

if(SnakeModel.matrix[i][j]){

g.setColor(Color.BLACK);

}

else{

g.setColor(Color.YELLOW);

}

g.fillRect(i*nodeWidth+10, j*nodeHeight+30, nodeWidth-1, nodeHeight-1);

}

}

}

public void run(){

while(true){

try{

Thread.sleep(SnakeModel.freshTime);

}catch(Exception e){

System.out.println("refresh error");

}

repaint();

}

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015/06/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档