import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Main extends JPanel implements KeyListener,ActionListener{
/**
* @param args
*/
static JMenuBar jmb;
JMenu operation,level;
JMenuItem g_start,g_stop,g_easy,g_normal,g_hard;
Food food;
Snake snake;
int score=0;
boolean snake_clear = false;
boolean tongguan = false;
boolean food_clear = false;
int delay =200;
boolean game_time = false;
Image wallimage,foodimage,snakeimage;
String imgwallname="";
String imgfoodname="";
String imgsnakename="";
Main(){
jmb = new JMenuBar();
operation = new JMenu("操作");
level = new JMenu("难度");
jmb.add(operation);
jmb.add(level);
g_start = new JMenuItem("开始(C)");
g_start.setMnemonic('C');
g_stop = new JMenuItem("暂停(D)");
g_stop.setMnemonic('D');
operation.add(g_start);
operation.add(g_stop);
g_easy = new JMenuItem("简单");
g_normal = new JMenuItem("一般");
g_hard = new JMenuItem("困难");
level.add(g_easy);
level.add(g_normal);
level.add(g_hard);
g_start.addActionListener(this);
g_stop.addActionListener(this);
g_easy.addActionListener(this);
g_normal.addActionListener(this);
g_hard.addActionListener(this);
imgwallname = "Image/wall.jpg";
imgfoodname = "Image/food.jpg";
imgsnakename = "Image/snake.jpg";
wallimage = Toolkit.getDefaultToolkit().getImage(imgwallname);
foodimage = Toolkit.getDefaultToolkit().getImage(imgfoodname);
snakeimage = Toolkit.getDefaultToolkit().getImage(imgsnakename);
food = new Food();
snake = new Snake();
addKeyListener(this);
snake.life = 0;
snake.direction = 1;//右
snake.x[0]=100;
snake.y[0]=100;//头
snake.x[1]=110;
snake.y[1]=100;
snake.node = 2;
Random a = new Random();
food.x = a.nextInt(480)+60;
food.y = a.nextInt(390)+60;
while(food.x%10!=0 || check_food(food.x,food.y)){
food.x++;
}
while(food.y%10!=0 || check_food(food.x,food.y)){
food.y++;
}
food.yes = 1;
}
//食物出现在蛇身上
private boolean check_food(int a,int b){
boolean flag = false;
for(int i=0;i<snake.node;i++){
if(a == snake.x[i] && b == snake.y[i])
flag = true;
else
flag = false;
}
return flag;
}
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Random random = new Random();
if(food.yes == 1){
food.x = random.nextInt(480)+60;
food.y = random.nextInt(380)+60;
while(food.x%10!=0 || check_food(food.x,food.y))
food.x++;
while(food.y%10!=0 || check_food(food.x,food.y))
food.y++;
food.yes = 0;
}
if(food.yes == 0){
food_clear = false;
}
//蛇前进
for(int i = snake.node-1;i>0;i--){
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction){
case 1:
snake.x[0]+=10;
break;
case 2:
snake.x[0]-=10;
break;
case 3:
snake.y[0]-=10;
break;
case 4:
snake.y[0]+=10;
break;
}
//自己碰自己
for(int i=3;i<snake.node;i++){
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){
snake.life = 1;
break;
}
}
//碰墙
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455){
snake.life= 1;
}
//吃食物
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
snake.x[snake.node]=-20;
snake.y[snake.node]=-20;
snake.node++;
food_clear = true;
food.yes = 1;
score +=10;
tongguan = false;
}
snake_clear = true;
repaint();
if(snake.life==1){
GameOver();
}
if(score==500 && tongguan == false){
JOptionPane.showMessageDialog(null,"恭喜你过关!进入下一关.","系统提示",JOptionPane.OK_OPTION);
timer.setDelay(100);
tongguan = true;
}
else if(score==1000 && tongguan == false){
JOptionPane.showMessageDialog(null,"恭喜你过关!进入下一关.","系统提示",JOptionPane.OK_OPTION);
timer.setDelay(60);
tongguan = true;
}
else if(score==1500 && tongguan == false){
JOptionPane.showMessageDialog(null,"恭喜你过关!过全关.","系统提示",JOptionPane.OK_OPTION);
tongguan = true;
}
}
};
javax.swing.Timer timer = new javax.swing.Timer(delay,taskPerformer);
public void paint(Graphics g){
//清屏
g.setColor(Color.WHITE);
g.fillRect(0, 0,700,600);
//画墙
g.setColor(Color.BLACK);
for(int i=50;i<=600;i+=10){
g.drawImage(wallimage,i,40,this);
g.drawImage(wallimage,i,451,this);
}
for(int j = 40;j<=450;j+=10){
g.drawImage(wallimage,50,j,this);
g.drawImage(wallimage,601,j,this);
}
//画蛇
g.setColor(Color.BLUE);
for(int i=0;i<snake.node;i++){
g.drawImage(snakeimage,snake.x[i], snake.y[i],this);
}
//画食物
if(!food_clear){
g.drawImage(foodimage,food.x,food.y,this);
food.yes=0;
}
//显示分数
g.setColor(Color.WHITE);
g.fillRect(270,0,100,30);
g.setColor(Color.RED);
g.drawString("你的分数:"+score,280,20);
}
public void actionPerformed(ActionEvent ev){
String actionCommand = ev.getActionCommand();
if("简单".equals(actionCommand)){
timer.setDelay(200);
snake.x = new int[200];
snake.y = new int[200];
score = 0;
snake.node = 2;
snake.life = 0;
snake.direction = 1;
snake.x[0]=100;
snake.y[0]=100;
snake.x[1]=110;
snake.y[1]=100;
food.yes=1;
game_time=false;
timer.start();
}
else if("一般".equals(actionCommand)){
timer.setDelay(100);
snake.x = new int[200];
snake.y = new int[200];
score = 0;
snake.node = 2;
snake.life = 0;
snake.direction = 1;
snake.x[0]=100;
snake.y[0]=100;
snake.x[1]=110;
snake.y[1]=100;
food.yes=1;
game_time=false;
timer.start();
}
else if("困难".equals(actionCommand)){
timer.setDelay(50);
snake.x = new int[200];
snake.y = new int[200];
score = 0;
snake.node = 2;
snake.life = 0;
snake.direction = 1;
snake.x[0]=100;
snake.y[0]=100;
snake.x[1]=110;
snake.y[1]=100;
food.yes=1;
game_time=false;
timer.start();
}
else if("开始(C)".equals(actionCommand)){
snake.x = new int[200];
snake.y = new int[200];
score = 0;
snake.node = 2;
snake.life = 0;
snake.direction = 1;
snake.x[0]=100;
snake.y[0]=100;
snake.x[1]=110;
snake.y[1]=100;
food.yes=1;
game_time=false;
timer.start();
}
else if("暂停(D)".equals(actionCommand)){
if(!game_time){
timer.stop();
game_time = true;
}
else if(game_time){
timer.start();
game_time = false;
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_RIGHT:
if(snake.direction!=2)
snake.direction=1;