java图形化界面编程之贪吃蛇

package Snake;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


import day25.FrameUtil;


public class SnakeView extends JPanel{


//宽(列数)
public static  final  int WIDTH=50;
//高(行数)
public static final int HEIGHT=20;

//设置格子的宽
public static final int CELLWIDTH=20;
//设置格子的高
public static final int CELLHEIGHT=20;
//地图
public static  boolean[][] bankground=new boolean[HEIGHT][WIDTH];
//用集合来存蛇
static LinkedList snake=new LinkedList();
//食物
static Point food;



//使用四个常量表示方向
public static final int UP_DIRECTION=1; //上
public static final int DOWN_DIRECTION=-1; //下
public static final int LEFT_DIRECTION=2; //左
public static final int RIGHT_DIRECTION=-2; //右

//当前方向
int currentDirection=-2;


//游戏结束标志
static boolean isGameOver =false;
//初始化地图
public void InitBankground(){
for(int rows=0;rows for(int cols=0;cols if(rows==0||rows==(HEIGHT-1)){
    bankground[rows][cols]= true;
}  
}
}
}
//初始化蛇
public void InitSnake(){
int x=WIDTH/2;
int y=HEIGHT/2;
snake.addFirst(new Point(x-1,y));
snake.addFirst(new Point(x,y));
snake.addFirst(new Point(x+1,y));
}
public void Move(){
Point head=snake.getFirst();//先拿出蛇头

switch (currentDirection) {
case UP_DIRECTION:
 snake.addFirst(new Point(head.x,head.y-1));
break;
case DOWN_DIRECTION:
snake.addFirst(new Point(head.x,head.y+1));
break;
case LEFT_DIRECTION:
if(head.x==0){
snake.addFirst(new Point(WIDTH-1,head.y));
}else{
snake.addFirst(new Point(head.x-1,head.y));
}
break;
case RIGHT_DIRECTION:
if(head.x==WIDTH-1){
snake.addFirst(new Point(0,head.y));
}else{
snake.addFirst(new Point(head.x+1,head.y));
}
break;
default:
break;
}
if(EatFood()){
createFood();
repaint();


}else{
//删除蛇尾
snake.removeLast();
}

}

//吃食物
public boolean EatFood(){
Point head=snake.getFirst();
if(head.equals(food)){
return true;
}
return false;
}
//改变当前方向
public void changeDirection(int newDirection){
//判断新方向是否和当前方向相反
if((currentDirection+newDirection)!=0){
this.currentDirection=newDirection;
}
}
//游戏结束
public void isGameOver(){
Point head=snake.getFirst();
if(bankground[head.y][head.x]==bankground[HEIGHT-1][WIDTH-1]||bankground[head.y][head.x]==bankground[0][WIDTH-1]||bankground[head.y][head.x]==bankground[HEIGHT-1][0]){
isGameOver=true;
}
for(int i=1;i Point bady=snake.get(i);

if(head.equals(bady)){
isGameOver=true;
}
}
}
@Override
public void paint(Graphics g) {
//画地图
for(int rows=0;rows for(int cols=0;cols if(bankground[rows][cols]){
//墙
g.setColor(Color.GRAY);
}else{
//空地
g.setColor(Color.WHITE);
}
g.fill3DRect(cols*CELLWIDTH, rows*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
}

}
//画蛇
//取出蛇身
g.setColor(Color.BLUE);
for(int i=1;i Point body=snake.get(i);

g.fill3DRect(body.x*CELLWIDTH, body.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
}
//取出蛇头
Point head=snake.getFirst();
g.setColor(Color.RED);

g.fill3DRect(head.x*CELLWIDTH, head.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);
//画食物
g.setColor(Color.BLUE);
g.fill3DRect(food.x*CELLWIDTH, food.y*CELLHEIGHT, CELLWIDTH, CELLHEIGHT, true);

//结束字
if(isGameOver){
g.setColor(Color.RED);
g.setFont(new Font("正楷",Font.BOLD, 30));
g.drawString("GAME  OVER", 400, 200);
}

}
public static void createFood(){
Random random=new Random();
while(true){
int x=random.nextInt(WIDTH);
int y=random.nextInt(HEIGHT);
if(bankground[y][x]!=bankground[HEIGHT-1][0]||bankground[y][x]!=bankground[0][WIDTH-1]||bankground[y][x]!=bankground[HEIGHT-1][WIDTH-1]){
food=new Point(x,y);
break;
}
}
}



public boolean  flag=false;//
public static void main(String[] args) {
JFrame frame=new JFrame("贪吃蛇");
SnakeView snakeView=new SnakeView();
snakeView.InitBankground();
snakeView.InitSnake();
snakeView.createFood();

snakeView.repaint();

frame.add(snakeView);

FrameUtil.InitFrame(frame, WIDTH*CELLWIDTH+20,HEIGHT*CELLHEIGHT+40);

//给按钮添加事件监听器
frame.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
int code=e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
snakeView.changeDirection(UP_DIRECTION);
break;
case KeyEvent.VK_DOWN:
snakeView.changeDirection(DOWN_DIRECTION);
break;
case KeyEvent.VK_LEFT:
snakeView.changeDirection(LEFT_DIRECTION);
break;
case KeyEvent.VK_RIGHT:
snakeView.changeDirection(RIGHT_DIRECTION);
break;

default:
break;
}
if(!snakeView.flag){
snakeView.Move();
snakeView.isGameOver();

if(isGameOver){
snakeView.flag=true;

}
snakeView.repaint();
}else{

 System.exit(0);
}


}

});
}
}

你可能感兴趣的:(java)