博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java学习日记-------------------------------------贪吃蛇
阅读量:5165 次
发布时间:2019-06-13

本文共 5804 字,大约阅读时间需要 19 分钟。

天降瑞雪,大东北的冬天是真冷啊。昨天在寝室宅了一天,闲来无事索性就找了马士兵老师的贪吃蛇视频,跟着老师完成了这小游戏。放在此处留个纪念!

/***************院子类********************/

import java.awt.Color;import java.awt.Font;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Yard extends Frame {        PaintThread paintThread = new PaintThread();        private boolean GameOver=false;        public static final int  ROWS=30;    public static final int COLS=30;    public static final int BLOCK_SIZE=15;        private int score=0;        Snake s=new Snake(this);    Egg e=new Egg();        Image offScrenImage=null;        public void lauch(){         this.setLocation(200, 200);        this.setSize(COLS*BLOCK_SIZE,ROWS*BLOCK_SIZE);        this.addWindowListener(new WindowAdapter(){            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }                    });        this.setVisible(true);        this.addKeyListener(new KeyMonitor());                new Thread(new PaintThread()).start();    }            public static void main(String[] args) {        // TODO Auto-generated method stub     new Yard().lauch();    } public void stop(){        GameOver=true;}    @Override    public void paint(Graphics g) {        Color c=g.getColor();        g.setColor(Color.gray);        g.fillRect(0, 0, COLS*BLOCK_SIZE,ROWS*BLOCK_SIZE);        g.setColor(Color.DARK_GRAY);        //画线        for(int i=1;i

/**********************蛇类**************************/

import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.KeyEvent;public class Snake {    private Node head=null;    private Node tail=null;    private  int size=0;             private Node n=new Node(20,30,Dir.L);	private Yard y;        public Snake(Yard y){       head=n;       tail=n;       size=1;       this.y=y;    }    public void addTotail(){//加到尾巴上    	Node node=null;    	switch(tail.dir){    	case L:    		node=new Node(tail.row,tail.col+1,tail.dir);    		break;    	case U:    		node=new Node(tail.row+1,tail.col,tail.dir);    		break;    	case R:    		node=new Node(tail.row,tail.col-1,tail.dir);    		break;    	case D:    		node =new Node(tail.row-1,tail.col,tail.dir);    		break;    	}    	tail.next=node;    	node.prev=tail;    	tail=node;//新的尾巴    	size++;    }    public void addToHead(){    	Node node=null;    	switch(head.dir){    	case L:    		node=new Node(head.row,head.col-1,head.dir);    		break;    	case U:    		node=new Node(head.row-1,head.col,head.dir);    		break;    	case R:    		node=new Node(head.row,head.col+1,head.dir);    		break;    	case D:    		node =new Node(head.row+1,head.col,head.dir);    		break;    	}    	node.next=head;    	head.prev=node;    	head=node;//    	size++;    }        public void draw(Graphics g){    	if(size<=0)return ;    	move();    	for(Node n=head;n!=null;n=n.next){    		n.draw(g);    	}    	    }     private void move() {		// TODO Auto-generated method stub		addToHead();		deleteFromTail();		checkDead();	}	private void checkDead() {		// TODO Auto-generated method stub		if(head.row<2||head.col<0||head.row>Yard.ROWS||head.col>Yard.COLS){			y.stop();		}		for(Node n=head.next;n!=null;n=n.next){//和自己身体碰撞			if(head.row==n.row&&head.col==n.col){				y.stop();			}		}	}	private void deleteFromTail() {		// TODO Auto-generated method stub		if(size==0)return;		tail=tail.prev;		tail.next=null;	}	class Node{		int w=Yard.BLOCK_SIZE;		int h=Yard.BLOCK_SIZE;		int row,col;		Dir dir=Dir.D;		Node next=null;		Node prev=null;		 Node(int row, int col,Dir dir) {						this.row = row;			this.col = col;			this.dir=dir;		}		void draw(Graphics g){			Color c=g.getColor();			g.setColor(Color.BLACK);			g.fillRect(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);			g.setColor(c);		} 	}			public void eat(Egg e){		if(this.getRect().intersects(e.getRect())){			e.reAppear();			this.addToHead();			y.setScore(y.getScore()+5);		}	}		private Rectangle getRect(){		return new Rectangle(Yard.BLOCK_SIZE*head.col,Yard.BLOCK_SIZE*head.row,head.w,head.h);	}		public void keyPressed(KeyEvent e) {		// TODO Auto-generated method stub		int key=e.getKeyCode();		switch(key){		case KeyEvent.VK_LEFT:			if(head.dir!=Dir.R)			head.dir=Dir.L;			break;		case KeyEvent.VK_RIGHT:			if(head.dir!=Dir.L)			head.dir=Dir.R;			break;		case KeyEvent.VK_UP:			if(head.dir!=Dir.D)			head.dir=Dir.U;			break;		case KeyEvent.VK_DOWN:			if(head.dir!=Dir.U)			head.dir=Dir.D;			break;		}	}}

 /*********************蛋类(供蛇吃的)*******************/

import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.util.Random;public class Egg {    int row,col;    int w=Yard.BLOCK_SIZE;    int h=Yard.BLOCK_SIZE;    private static Random r=new Random();    private Color color=Color.GREEN;    	public Egg(int row, int col) {				this.row = row;		this.col = col;	}	public Egg(){		this(r.nextInt(Yard.ROWS-3)+3,r.nextInt(Yard.COLS));	}		public void reAppear(){		this.row=r.nextInt(Yard.ROWS-3)+3;		this.col=r.nextInt(Yard.COLS);	}		public Rectangle getRect(){		return new Rectangle(Yard.BLOCK_SIZE*col,Yard.BLOCK_SIZE*row,w,h);	}	public void draw(Graphics g){		Color c=g.getColor();		g.setColor(color);		g.fillOval(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);		g.setColor(c);		if(color==Color.GREEN)color=Color.RED;		else color=Color.GREEN;	}	public int getRow() {		return row;	}	public void setRow(int row) {		this.row = row;	}	public int getCol() {		return col;	}	public void setCol(int col) {		this.col = col;	}}

 /**********************枚举(方向)*****************************/

public enum Dir {    L,U,R,D;}

    从视频来看,这是老师当堂完成的,很羡慕老师的能力。同时,也发现了老师在做的时候,有些地方处理的不是太理想。

。。。

转载于:https://www.cnblogs.com/he-shao/p/4986096.html

你可能感兴趣的文章
Shader Overview
查看>>
Reveal 配置与使用
查看>>
Java中反射的学习与理解(一)
查看>>
C语言初学 俩数相除问题
查看>>
B/S和C/S架构的区别
查看>>
[Java] Java record
查看>>
jQuery - 控制元素显示、隐藏、切换、滑动的方法
查看>>
postgresql学习文档
查看>>
Struts2返回JSON数据的具体应用范例
查看>>
js深度克隆对象、数组
查看>>
socket阻塞与非阻塞,同步与异步
查看>>
团队工作第二天
查看>>
System类
查看>>
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>
原型设计工具
查看>>