/**
* 贪吃蛇程序
*/
package demo1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Timer;
public class G1 extends JFrame {
private static final long serialVersionUID = 1L;
MyPanel p1;
int k=0,i=0;
public G1(){
p1=new MyPanel();
this.add(p1);//把面板加入到框架中
this.addKeyListener(p1);//按键监听注册
p1.timer.schedule(new java.util.TimerTask()//没0.2秒程序启动一遍
{
public void run(){
p1.repaint();//画图片
//判断是否吃到食物
if(p1.snake1.x[p1.snake1.n-1]==p1.food.getM()&&p1.snake1.y[p1.snake1.n-1]==p1.food.getK())
{
switch(p1.snake1.dir) //每次画图时,变化坐标的函数
{
case "left":
p1.snake1.x[p1.snake1.n]=p1.snake1.x[p1.snake1.n-1]-1;
p1.snake1.y[p1.snake1.n]=p1.snake1.y[p1.snake1.n-1];
break;
case "right":
p1.snake1.x[p1.snake1.n]=p1.snake1.x[p1.snake1.n-1]+1;
p1.snake1.y[p1.snake1.n]=p1.snake1.y[p1.snake1.n-1];
break;
case "up":
p1.snake1.y[p1.snake1.n] =p1.snake1.y[p1.snake1.n-1]-1;
p1.snake1.x[p1.snake1.n]=p1.snake1.x[p1.snake1.n-1];
break;
case "down":
p1.snake1.y[p1.snake1.n]=p1.snake1.y[p1.snake1.n-1]+1;
p1.snake1.x[p1.snake1.n]=p1.snake1.x[p1.snake1.n-1];
break;
}
p1.snake1.n++;
do//判断生成的食物是否和蛇重合
{
p1.food.change();
k=0;
for(i=0;i<p1.snake1.n-1;i++) {
if(p1.snake1.x[i]==p1.food.getM()&&p1.snake1.y[i]==p1.food.getK())
{
k=1;
break;
}
}
}while(k!=0);
}
//咬到自己
for(int i=0;i<p1.snake1.n-2;i++) {
if(p1.snake1.x[i]==p1.snake1 .x[p1.snake1.n-1]&&p1.snake1.y[i]==p1.snake1 .y[p1.snake1.n-1])
{
p1.timer.cancel();
}
}
//撞墙游戏结束
if(p1.snake1.x[p1.snake1.n-1]>36|p1.snake1.x[p1.snake1.n-1]<0||p1.snake1.y[p1.snake1.n-1]<0||p1.snake1.y[p1.snake1.n-1]>24)
{
p1.timer.cancel();
}
}
},0,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
this.setSize(500, 400);//设置屏幕大小
this.setVisible(true);//显示JFrame
this.setTitle("贪吃蛇游戏");
this.setLocation(700, 300);
}
public static void main(String[] args)
{
G1 g1=new G1();
}
}
class MyPanel extends JPanel implements KeyListener{
//绘画蛇身的地方
int i,m,k;
Snake snake1=new Snake();
Food food=new Food();
Timer timer=new Timer();
JLabel ii=new JLabel("游戏分数");
public MyPanel() {
this.add(ii);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.green);//设置颜色
g.drawRect(0, 0, 380, 255);
switch(snake1.dir) //每次画图时,变化坐标的函数
{
case "left":
for(i=0;i<snake1.n-1;i++)
{
snake1.x[i]=snake1.x[i+1];
snake1.y[i]=snake1.y[i+1];
}
snake1.x[snake1.n-1]=snake1.x[snake1.n-2]-1;
break;
case "right":
for(i=0;i<snake1.n-1;i++)
{
snake1.x[i]=snake1.x[i+1];
snake1.y[i]=snake1.y[i+1];
}
snake1.x[snake1.n-1]=snake1.x[snake1.n-2]+1;
break;
case "up":
for(i=0;i<snake1.n-1;i++)
{
snake1.x[i]=snake1.x[i+1];
snake1.y[i]=snake1.y[i+1];
}
snake1.y[snake1.n-1] =snake1.y[snake1.n-2]-1;
break;
case "down":
for(i=0;i<snake1.n-1;i++)
{
snake1.x[i]=snake1.x[i+1];
snake1.y[i]=snake1.y[i+1];
}
snake1.y[snake1.n-1]=snake1.y[snake1.n-2]+1;
break;
}
for( i=0;i<snake1.n;i++) {
g.fill3DRect(snake1.x[i]*10, snake1.y[i]*10, 10, 10, false);
}
g.setColor(Color.blue);
g.fill3DRect(food.getM()*10, food.getK()*10, 10, 10, false);
ii.setText("游戏分数"+(snake1.n-3)*10);
ii.setLocation(400,50);
//画蛇一节一节
}
public void keyPressed(KeyEvent arg0)//当按键被按下时,修改贪吃蛇的方向
{
if(arg0.getKeyCode()==KeyEvent.VK_DOWN&&snake1.dir!="up") {
snake1.dir="down";
}else if(arg0.getKeyCode()==KeyEvent.VK_LEFT&&snake1.dir!="right") {
snake1.dir="left";
}else if(arg0.getKeyCode()==KeyEvent.VK_RIGHT&&snake1.dir!="left") {
snake1.dir="right";
}else if(arg0.getKeyCode()==KeyEvent.VK_UP&&snake1.dir!="down") {
snake1.dir="up";
}
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
class Snake//关于蛇的内容
{
int x[]=new int[100];
int y[]=new int[100];//蛇身的坐标
String dir="right";//蛇走的方向
int n;//蛇的长度
public Snake()//初始化蛇的长度,三节蛇身
{
this.n=3;
this.x[0]=15;
this.y[0]=11;
this.x[1]=16;
this.y[1]=11;
this.x[2]=17;
this.y[2]=11;
}
public void changesnake(int m,int k)//当吃到食物的时候,能够增加蛇身的长度
{
x[n]=m;
y[n]=k;
n++;
}
}
class Food//蛇的食物内容
{
int m=20;//横坐标
int k=7;//纵坐标
Random random = new Random();
public void change(){
this.m=random.nextInt(29)+2;
this.k=random.nextInt(19)+2;
}
public int getM() {
return m;
}
public int getK() {
return k;
}
}