Java贪吃蛇

Posted VRGamer-006

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java贪吃蛇相关的知识,希望对你有一定的参考价值。

说明:X加速,Z减速,空格暂停,长度无上限直到死为止。

1、Game.java

 1 package game;
 2 
 3 import java.awt.BorderLayout;
 4 import javax.swing.JFrame;
 5 import control.Controller;
 6 import grid.Grid;
 7 import mysnake.Food;
 8 import mysnake.Ground;
 9 import mysnake.Snake;
10 import view.GamePanel;
11 
12 public class Game {
13 
14     public static void main(String[] args) {
16         Snake snake = new Snake();
17         Food food = new Food();
18         GamePanel gamepanel = new GamePanel();
19         Ground ground = new Ground();
20         Controller controller = new Controller(snake,food,ground,gamepanel);
21         JFrame frame = new JFrame("      Snake --- By xxxxxxx      "+ "↑↓←→ : move      Z : decelerate      X : accelerate      Space : pause");
22         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23         gamepanel.setSize(Grid.WIDTH*Grid.CELL_SIZE,Grid.HEIGHT*Grid.CELL_SIZE);
24         frame.setSize(Grid.WIDTH*Grid.CELL_SIZE+10,Grid.HEIGHT*Grid.CELL_SIZE+35);
25         frame.add(gamepanel,BorderLayout.CENTER);
26         gamepanel.addKeyListener(controller);
27         snake.addListener(controller);
28         frame.addKeyListener(controller);
29         frame.setVisible(true);
30         controller.newGame();
31 }
32 }

2、Grid.java

1 package grid;
2 
3 public class Grid {
4     public static final int CELL_SIZE = 1;
5     public static final int WIDTH = 888;
6     public static final int HEIGHT = 666;
7 }

3、Food.java

 1 package mysnake;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Point;
 6 import java.util.Random;
 7 import grid.Grid;
 8 
 9 public class Food extends Point{
10     private int xx = new Random().nextInt(7);
11     public boolean isSnakeEatFood(Snake snake){
12         if((((snake.getHead().x*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==this.x*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
13                 (snake.getHead().y>(this.y-Grid.CELL_SIZE*10))&&
14                         (snake.getHead().y<(this.y+Grid.CELL_SIZE*10)))||
15         (((snake.getHead().x*Grid.CELL_SIZE)==this.x*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
16                 (snake.getHead().y>(this.y-Grid.CELL_SIZE*10))&&
17                                 (snake.getHead().y<(this.y+Grid.CELL_SIZE*10)))||
18         (((snake.getHead().y*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==this.y*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
19                 (snake.getHead().x>(this.x-Grid.CELL_SIZE*10))&&
20                    (snake.getHead().x<this.x+Grid.CELL_SIZE*10))||
21         (((snake.getHead().y*Grid.CELL_SIZE)==this.y*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
22                 (snake.getHead().x>(this.x-Grid.CELL_SIZE*10))&&
23                    (snake.getHead().x<this.x+Grid.CELL_SIZE*10))){
24             return true;
25     }else{
26         return false;
27     }
28 }
29 
30     public void drawMe(Graphics g){
31         switch(xx){
32         case 0:g.setColor(Color.RED);break;
33         case 1:g.setColor(Color.ORANGE);break;
34         case 2:g.setColor(Color.YELLOW);break;
35         case 3:g.setColor(Color.GREEN);break;
36         case 4:g.setColor(Color.BLUE);break;
37         case 5:g.setColor(Color.CYAN);break;
38         case 6:g.setColor(Color.PINK);break;
39         }
40         g.fill3DRect(x*Grid.CELL_SIZE,y*Grid.CELL_SIZE,Grid.CELL_SIZE*10,Grid.CELL_SIZE*10,true);
41     }
42     
43     public void newFood(Point p){
44         this.setLocation(p);
45     }
46 }

4、Ground.java

 1 package mysnake;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.util.Random;
 6 import grid.Grid;
 7 
 8 public class Ground{
 9     private int[][] rocks = new int[Grid.WIDTH][Grid.HEIGHT]; 
10     private int xx = new Random().nextInt(7);
11     public boolean isSnakeEatRock(Snake snake){
12         for(int i=0;i<Grid.WIDTH;i++){
13             for(int j=0;j<Grid.HEIGHT;j++){
14         if((rocks[i][j]==1)&&
15                 ((((snake.getHead().x*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==i*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
16                 (snake.getHead().y>(j-Grid.CELL_SIZE*10))&&
17                 (snake.getHead().y<(j+Grid.CELL_SIZE*10)))||
18 (((snake.getHead().x*Grid.CELL_SIZE)==i*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
19         (snake.getHead().y>(j-Grid.CELL_SIZE*10))&&
20                         (snake.getHead().y<(j+Grid.CELL_SIZE*10)))||
21 (((snake.getHead().y*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==j*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
22         (snake.getHead().x>(i-Grid.CELL_SIZE*10))&&
23            (snake.getHead().x<i+Grid.CELL_SIZE*10))||
24 (((snake.getHead().y*Grid.CELL_SIZE)==j*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
25         (snake.getHead().x>(i-Grid.CELL_SIZE*10))&&
26            (snake.getHead().x<i+Grid.CELL_SIZE*10))))
27         {return true;}}}
28          return false;
29 }
30     
31     public boolean catchfood(Food food){
32         for(int i=0;i<Grid.WIDTH;i++){
33             for(int j=0;j<Grid.HEIGHT;j++){
34         if((rocks[i][j]==1)&&((((food.x*Grid.CELL_SIZE+10*Grid.CELL_SIZE>i*Grid.CELL_SIZE)
35                 &&(food.x*Grid.CELL_SIZE<(i*Grid.CELL_SIZE+Grid.CELL_SIZE*10)))&&
36                 ((food.y*Grid.CELL_SIZE+10*Grid.CELL_SIZE>j*Grid.CELL_SIZE)&&
37                         (food.y*Grid.CELL_SIZE<(j*Grid.CELL_SIZE+Grid.CELL_SIZE*10))))))
38         {return true;}}}
39          return false;
40 }
41     
42     public Ground(){
43         for(int i=0;i<100;i++){
44             int a = new Random().nextInt(Grid.WIDTH);
45             int b = new Random().nextInt(Grid.HEIGHT);
46             if(b<0){b=Grid.HEIGHT-1;}
47             if(b+10>=Grid.HEIGHT){b=0;}
48             if(a<0){a=Grid.WIDTH-1;}
49             if(a+10>=Grid.WIDTH){a=0;}
50             rocks[a][b] = 1;
51             }
52         }
53     
54     public void drawMe(Graphics g){
55         switch(xx){
56         case 0:g.setColor(Color.RED);break;
57         case 1:g.setColor(Color.ORANGE);break;
58         case 2:g.setColor(Color.YELLOW);break;
59         case 3:g.setColor(Color.GREEN);break;
60         case 4:g.setColor(Color.BLUE);break;
61         case 5:g.setColor(Color.CYAN);break;
62         case 6:g.setColor(Color.PINK);break;
63         }
64     for(int x=0;x<Grid.WIDTH;x++){
65         for(int y=0;y<Grid.HEIGHT;y++){
66             if(rocks[x][y]==1){
67                 g.fill3DRect(x*Grid.CELL_SIZE,y*Grid.CELL_SIZE,Grid.CELL_SIZE*10,Grid.CELL_SIZE*10,true);
68             }
69         }
70     }
71 }
72 }

5、Snake.java

  1 package mysnake;
  2 
  3 import java.awt.Color;
  4 import java.awt.Graphics;
  5 import java.awt.Point;
  6 import java.util.HashSet;
  7 import java.util.LinkedList;
  8 import java.util.Set;
  9 import grid.Grid;
 10 
 11 public class Snake {
 12         
 13         public Snake(){
 14             init();
 15         }
 16         
 17         public void init(){
 18             int x = Grid.WIDTH/2;
 19             int y = Grid.HEIGHT/2;
 20             for(int i=0;i<33;i++){
 21                 body.addFirst(new Point(x--,y));
 22             }
 23         olddirection = newdirection = LEFT;
 24         life = true;
 25         efil = true;
 26         efil1 = 0;
 27         threadspeed = 10;
 28         }
 29     
 30         public static final int UP = 0;
 31         public static final int DOWN = 1;
 32         public static final int LEFT = 2;
 33         public static final int RIGHT = 3;
 34         
 35         private int olddirection,newdirection;
 36         private LinkedList<Point> body = new LinkedList<Point>();
 37         private Set<Snakelisten> listener = new HashSet<Snakelisten>();
 38         private boolean life;
 39         private boolean efil;
 40         private int efil1;
 41         private int threadspeed;
 42         public Point getHead(){
 43             return body.getFirst();
 44         }
 45         public void move(){
 46             int x = body.getFirst().x;
 47             int y = body.getFirst().y;    
 48             switch(efil1){case 0: 
 49                 if((olddirection+newdirection!=5)&&(olddirection+newdirection!=1)){
 50                     olddirection = newdirection;
 51                 }
 52                 body.removeLast();
 53                 switch(olddirection){
 54                 case UP:y--;if(y<0){y=Grid.HEIGHT-1;}break;
 55                 case DOWN:y++;if(y+10>=Grid.HEIGHT){y=0;}break;
 56                 case LEFT:x--;if(x<0){x=Grid.WIDTH-1;}break;
 57                 case RIGHT:x++;if(x+10>=Grid.WIDTH){x=0;}break;
 58                 }
 59                 Point newHead = new Point(x,y);
 60                 body.addFirst(newHead);
 61                 break;
 62                 case 1:break;}    
 63         }
 64         public void changeDirection(int direction){            
 65             newdirection = direction;        
 66         }
 67         public void eatFood(){
 68             int x = body.getFirst().x;
 69             int y = body.getFirst().y;
 70             for(int i=0;i<10;i++){
 71             switch(olddirection){
 72             case UP:y--;if(y<0){y=Grid.HEIGHT-1;}break;
 73             case DOWN:y++;if(y+10>=Grid.HEIGHT){y=0;}break;
 74             case LEFT:x--;if(x<0){x=Grid.WIDTH-1;}break;
 75             case RIGHT:x++;if(x+10>=Grid.WIDTH){x=0;}break;
 76             }
 78             Point newHead = new Point(x,y);
 79             body.addFirst(newHead);}
 80         }
 81         public boolean isEatBody(){
 82             for(int i=22;i<body.size();i++){
 83             if((((this.getHead().x*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==
 84                     body.get(i).x*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
 85                     (this.getHead().y>(body.get(i).y-Grid.CELL_SIZE*10))&&
 86                     (this.getHead().y<(body.get(i).y+Grid.CELL_SIZE*10)))||
 87     (((this.getHead().x*Grid.CELL_SIZE)==body.get(i).x*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
 88             (this.getHead().y>(body.get(i).y-Grid.CELL_SIZE*10))&&
 89                             (this.getHead().y<(body.get(i).y+Grid.CELL_SIZE*10)))||
 90     (((this.getHead().y*Grid.CELL_SIZE+Grid.CELL_SIZE*10)==body.get(i).y*Grid.CELL_SIZE+1*Grid.CELL_SIZE)&&
 91             (this.getHead().x>(body.get(i).x-Grid.CELL_SIZE*10))&&
 92                (this.getHead().x<body.get(i).x+Grid.CELL_SIZE*10))||
 93     (((this.getHead().y*Grid.CELL_SIZE)==body.get(i).y*Grid.CELL_SIZE+9*Grid.CELL_SIZE)&&
 94             (this.getHead().x>(body.get(i).x-Grid.CELL_SIZE*10))&&
 95                (this.getHead().x<body.get(i).x+Grid.CELL_SIZE*10))){
 96                 return true;
 97             }}
 98             return false;
 99         }
100         public boolean foodcatchsnakebody(Food food){
101             for(int i=10;i<body.size();i++)
102             {
103                 if(((food.x*Grid.CELL_SIZE+10*Grid.CELL_SIZE>body.get(i).x*Grid.CELL_SIZE)
104                         &&(food.x*Grid.CELL_SIZE<(body.get(i).x*Grid.CELL_SIZE+Grid.CELL_SIZE*10)))&&
105                         ((food.y*Grid.CELL_SIZE+10*Grid.CELL_SIZE>body.get(i).y*Grid.CELL_SIZE)&&
106                                 (food.y*Grid.CELL_SIZE<(body.get(i).y*Grid.CELL_SIZE+Grid.CELL_SIZE*10))))
107                             
108                 return true;
109             }
110             return false;
111         }
112         public void drawMe(Graphics g){
113             g.setColor(Color.WHITE);
114             for(Point i:body){
115                 g.fill3DRect(i.x*Grid.CELL_SIZE,i.y*Grid.CELL_SIZE,Grid.CELL_SIZE*10,Grid.CELL_SIZE*10,true);
116             }
117         }
118         private class SnakeDriver implements Runnable{
119 
120             @Override
121             public void run() {
122                 while(life){
123                     move();
124                     for(Snakelisten i:listener){
125                         i.move(Snake.this);
126                     }
127                     try{Thread.sleep(threadspeed);}
128                     catch(InterruptedException e){
129                         e.printStackTrace();
130                     }
131                 }
132                 
133             }
134             
135         }
136         public void addListener(Snakelisten l){
137             if(l!=null){
138                 this.listener.add(l);
139             }
140         }
141         public void start(){
142             new Thread(new SnakeDriver()).start();
143         }
144 
145         public void die() {
146             life = false;
147         }
148         public void pause(){
149             if(efil){
150                 efil = false;
151                 efil1 = 1;
152             }
153             else{
154                 efil = true;
155                 efil1 = 0;
156             }
157         }
158 
159         public void acc() {
160             if(threadspeed>1)threadspeed--;
161         }
162 
163         public void slow() {
164             if(threadspeed<19)threadspeed++;
165         }
166 }

6、Snakelisten.java

1 package mysnake;
2 
3 public interface Snakelisten {
4     void move(Snake snake);
5 }

7、GamePanel.java

 1 package view;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import javax.swing.JPanel;
 6 import grid.Grid;
 7 import mysnake.Food;
 8 import mysnake.Ground;
 9 import mysnake.Snake;
10 
11 public class GamePanel extends JPanel{
12     
13     private Food food;
14     private Snake snake;
15     private Ground ground;
16     
17     public void display(Snake snake,Food food,Ground ground){
18         this.snake = snake;
19         this.food = food;
20         this.ground = ground;
21         this.repaint();
22     }
23 
24     @Override
25     protected void paintComponent(Graphics g) {
26         g.setColor(Color.BLACK);
27         g.fillRect(0,0,Grid.WIDTH*Grid.CELL_SIZE,Grid.HEIGHT*Grid.CELL_SIZE);
28         if(ground!=null&&snake!=null&&food!=null){
29         this.snake.drawMe(g);
30         this.ground.drawMe(g);
31         this.food.drawMe(g);
32         }
33     }
34     
35 }

8.Controller.java

  1 package control;
  2 
  3 import java.awt.Point;
  4 import java.awt.event.KeyAdapter;
  5 import java.awt.event.KeyEvent;
  6 import java.util.Random;
  7 import grid.Grid;
  8 import mysnake.Food;
  9 import mysnake.Ground;
 10 import mysnake.Snake;
 11 import mysnake.Snakelisten;
 12 import view.GamePanel;
 13 
 14 public class Controller extends KeyAdapter implements Snakelisten{
 15     
 16     private Snake snake;
 17     private Food food;
 18     private Ground ground;
 19     private GamePanel gamepanel;
 20 
 21     public Controller(Snake snake, Food food, Ground ground, GamePanel gamepanel) {
 22         super();
 23         this.snake = snake;
 24         this.food = food;
 25         this.ground = ground;
 26         this.gamepanel = gamepanel;
 27     }
 28 
 29     @Override
 30     public void keyPressed(KeyEvent e) {
 31         switch(e.getKeyCode()){
 32         case KeyEvent.VK_UP:snake.changeDirection(Snake.UP);break;
 33         }
 34         switch(e.getKeyCode()){
 35         case KeyEvent.VK_DOWN:snake.changeDirection(Snake.DOWN);break;
 36         }
 37         switch(e.getKeyCode()){
 38         case KeyEvent.VK_LEFT:snake.changeDirection(Snake.LEFT);break;
 39         }
 40         switch(e.getKeyCode()){
 41         case KeyEvent.VK_RIGHT:snake.changeDirection(Snake.RIGHT);break;
 42         }
 43         switch(e.getKeyCode()){
 44         case KeyEvent.VK_SPACE:snake.pause();break;
 45         }
 46         switch(e.getKeyCode()){
 47         case KeyEvent.VK_X:snake.acc();break;
 48         }
 49         switch(e.getKeyCode()){
 50         case KeyEvent.VK_Z:snake.slow();break;
 51         }
 52         
 53     }
 54 
 55     @Override
 56     public void move(Snake snake) {
 57         gamepanel.display(snake, food, ground);
 58         if(food.isSnakeEatFood(snake)){
 59             snake.eatFood();
 60             food.newFood(getPoint());
 61         }
 62         if(ground.isSnakeEatRock(snake)){
 63             snake.die();
 64             try {
 65                 Thread.sleep(1000);
 66             } catch (InterruptedException e) {
 67                 e.printStackTrace();
 68             }
 69             System.exit(0);
 70         }
 71         if(ground.catchfood(food)){
 72             food.newFood(getPoint());
 73         }
 74         if(snake.isEatBody()){
 75             snake.die();
 76             try {
 77                 Thread.sleep(1000);
 78             } catch (InterruptedException e) {
 79                 e.printStackTrace();
 80             }
 81             System.exit(0);
 82         }
 83         if(snake.foodcatchsnakebody(food)){
 84             food.newFood(getPoint());
 85         }
 86     }
 87     
 88     public void newGame(){
 89         snake.start();
 90         food.newFood(getPoint());
 91     }
 92     
 93     public Point getPoint(){
 94         Random random = new Random();
 95         int x = random.nextInt(Grid.WIDTH);
 96         int y = random.nextInt(Grid.HEIGHT);
 97         if(x<0){x=Grid.WIDTH-1;}
 98         if((x+10)>Grid.WIDTH){x=0;}
 99         if(y<0){y=Grid.HEIGHT-1;}
100         if(y+10>Grid.HEIGHT){y=0;}
101         return new Point(x,y);
102     }
103 }

 

以上是关于Java贪吃蛇的主要内容,如果未能解决你的问题,请参考以下文章

Java实现贪吃蛇游戏代码

贪吃蛇的java代码分析

100行Python代码,轻松完成贪吃蛇小游戏?

利用c# 编写贪吃蛇游戏如何修改label的属性

Java贪吃蛇小游戏

Java一个简单的贪吃蛇