自学写了个贪吃蛇,传上来做的纪念
Yard类
package snake1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import snake1.Dir;
public class Yard extends Frame{
snake ss =new snake();
public egg eg = new egg();
private Font fontGameOver = new Font("宋体",Font.BOLD,50);
private Font fontscore = new Font("宋体",Font.LAYOUT_NO_START_CONTEXT,30);
public static boolean gameOver = false;
public static final int rows = 100;
public static final int cols = 100;
public static final int bLOCK_SIZE =8;
public static Thread tt ;
PaintThread t = new PaintThread();
/*
* yard的“启动器”
*/
public void launch(){
/*
* 设置边框大小
*/
this.setSize(rows*bLOCK_SIZE, cols*bLOCK_SIZE);
this.setLocation(500,150);
/*
* 添加可视,窗口监听事件,键盘监听事件
*/
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor());
/*
* 开启画yard的线程
*/
tt = new Thread(t);
tt.start();
// new Thread(new PaintThread()).start();
}
/*
*yard的具体画法
*
*/
@SuppressWarnings("deprecation")
public void paint(Graphics g){
/*
* 画yard
*/
Color c = g.getColor();
g.setColor(c.gray);
g.fillRect(0, 0, rows*bLOCK_SIZE, cols*bLOCK_SIZE);
for(int i = 1;i<rows;i++){
g.setColor(c.pink);
g.drawLine(0, bLOCK_SIZE*i, bLOCK_SIZE*cols,bLOCK_SIZE*i );
}
for(int i = 1;i<cols;i++){
g.setColor(c.pink);
g.drawLine(bLOCK_SIZE*i, 0, bLOCK_SIZE*i,bLOCK_SIZE*rows);
}
g.setFont(fontscore);
g.drawString("score "+snake.size, 40, 80);
/*
* 判断是否需要画蛇
*/
if(gameOver==false){
ss.paint(g);
}
else{
g.setFont(fontGameOver);
g.drawString("游戏结束", 300, 400);
tt.stop();
}
/*
* 判断蛋蛋是否还在
*/
eg.eated(g);
}
private class PaintThread implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(true){
repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter{
/*
* 调用蛇的监听方法
* (non-Javadoc)
* @see java.awt.event.KeyAdapter#keyPressed(java.awt.event.KeyEvent)
*/
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
ss.keyPressed(e);
}
}
public static void main(String[] args){
Yard yard = new Yard();
yard.launch();
}
}
Snake类
package snake1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class snake {
public Snode head,
tail;
public static int size = 0;
public boolean e = true;
Snode s = new Snode(70,40,Dir.L);
/*
* 构造函数
*/
snake(){
this.head = s;
this.tail = s;
}
/*
* 吃的方法
*/
void eating(){
if(head.x == egg.x && head.y == egg.y){
egg.state = false;
addtotail();
}
}
/*
* 检查蛇的状态
*/
void Checkdied(){
if(head.x<0||head.y<0||head.x>Yard.rows||head.y>Yard.cols)
Yard.gameOver = true;
}
/*
* 移动的方法
* 移动实质上就是将尾部一个节点添加到头部
*/
void addtohead(){
Snode node ;
switch(head.dir){
case L:
node = new Snode(head.x-1,head.y,head.dir);
node.next = head;
head.front = node;
head = node;
break;
case R:
node = new Snode(head.x+1,head.y,head.dir);
node.next = head;
head.front = node;
head = node;
break;
case U:
node = new Snode(head.x,head.y-1,head.dir);
node.next = head;
head.front = node;
head = node;
break;
case D:
node = new Snode(head.x,head.y+1,head.dir);
node.next = head;
head.front = node;
head = node;
break;
}
}
/*
* 添加到尾部
*/
void addtotail(){
Snode node ;
size++;
switch(tail.dir){
case L:
node = new Snode(tail.x+1,tail.y,tail.dir);
tail.next = node;
node.front = tail;
tail = node;
break;
case R:
node = new Snode(tail.x-1,tail.y,tail.dir);
tail.next = node;
node.front = tail;
tail = node;
break;
case U:
node = new Snode(tail.x,tail.y+1,tail.dir);
tail.next = node;
node.front = tail;
tail = node;
break;
case D:
node = new Snode(tail.x,tail.y-1,tail.dir);
tail.next = node;
node.front = tail;
tail = node;
break;
}
}
/*
* 画蛇
* 每次重画需要移动一次,并判断移动后是否eating
*/
void paint(Graphics g){
for(Snode n = head;n !=null;n=n.next){
n.paint(g);
}
move();
eating();
}
/*
* 移动的方法
* 添加到头部,删除尾部,判断移动后蛇的状态
*/
private void move() {
// TODO Auto-generated method stub
addtohead();
delecttial();
Checkdied();
}
//删除尾节点
private void delecttial() {
// TODO Auto-generated method stub
tail.front.next = null;
tail = tail.front ;
}
//节点内部类
class Snode{
int w =Yard.bLOCK_SIZE;
int h =Yard.bLOCK_SIZE;
int x;
int y;
Snode front = null;
Snode next = null;
Dir dir =Dir.L;
Snode(int x,int y,Dir dir){
this.x = x;
this.y = y;
this.dir = dir;
}
void paint(Graphics g){
Color c = g.getColor();
g.setColor(c.BLACK);
g.fillRect(x*w, y*h, w, h);
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT :
if(head.dir != Dir.R)
head.dir = Dir.L;
break;
case KeyEvent.VK_UP :
if(head.dir != Dir.D)
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT :
if(head.dir != Dir.L)
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN :
if(head.dir != Dir.U)
head.dir = Dir.D;
break;
}
}
}
Egg类
package snake1;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class egg {
static Random nd = new Random();
public static int x =50;
public static int y= 50;
//全局蛋蛋的状态
static boolean state = true;
private static int size = Yard.bLOCK_SIZE;
/*
* 判断蛋蛋状态
*/
public static void eated(Graphics g){
if(state == true){
print(g);
}
else{
x = nd.nextInt(90)+10;
y = nd.nextInt(90)+10;
print(g);
state = true;
}
}
/*
* 画蛋蛋
*/
static void print(Graphics g){
Color c = g.getColor();
g.setColor(c.red);
g.fillRect(x*size, y*size, size, size);
}
}
方向(枚举)
package snake1;
//枚举,方便在编译期switch语句中发现错误
public enum Dir {
L,R,U,D
}