1.游戏规则
分为三个关卡,每个关卡需要吃掉15个食物,才可进入下一关。
当蛇头碰到自身时,游戏失败!
可随意穿墙,会从另一侧出来!
2.游戏操作
w:上
s:下
a:左
d:右
空格:暂停/继续
3.代码片段
1)GameWin(主入口)
package com;
import com.obj.BodyObj;
import com.obj.FoodObj;
import com.obj.HeadObj;
import com.utils.GameUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
public class GameWin extends JFrame {
public static int score=0;
public static int state=0;
int winWidth=800;
int winHight=600;
public HeadObj headObj=new HeadObj(GameUtils.rightImg,60,570,this);
public List<BodyObj> bodyObjList=new ArrayList<>();
public FoodObj foodObj=new FoodObj().getFood();
public void launch(){
this.setVisible(true);
this.setSize(winWidth,winHight);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setTitle("Jm养贪吃蛇");
bodyObjList.add(new BodyObj(GameUtils.bodyImg,30,570,this));
bodyObjList.add(new BodyObj(GameUtils.bodyImg,0,570,this));
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_SPACE){
switch (state){
case 0:
state=1;
break;
case 1:
state=2;
repaint();
break;
case 2:
state=1;
break;
case 3:
state=5;
break;
case 4:
state=6;
break;
default:
break;
}
}
}
});
while (true){
if(state==1){
repaint();
}
if(state==5){
state=0;
resetGame();
}
if(state==6){
if(GameUtils.level!=3)
{
state=1;
GameUtils.level++;
resetGame();
}else{
state=1;
GameUtils.level=1;
resetGame();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics gImage) {
gImage.setColor(Color.GREEN);
gImage.fillRect(0,0,winWidth,winHight);
gImage.setColor(Color.BLACK);
for(int i=0;i<=20;i++)
{
gImage.drawLine(0,30*i,600,30*i);
gImage.drawLine(30*i,0,30*i,600);
}
for(int i=bodyObjList.size()-1;i>=0;i--){
bodyObjList.get(i).paintSelf(gImage);
}
headObj.paintSelf(gImage);
foodObj.paintSelf(gImage);
GameUtils.drawWord(gImage,"第"+GameUtils.level+"关",Color.ORANGE,40,640,260);
GameUtils.drawWord(gImage,GameWin.score+"分",Color.red,40,650,320);
prompt(gImage);
}
void prompt(Graphics g){
if(state==0){
g.setColor(Color.BLUE);
g.fillRect(100,200,400,100);
GameUtils.drawWord(g,"按下空格,游戏开始",Color.YELLOW,40,110,260);
}
if(state==2){
g.setColor(Color.BLUE);
g.fillRect(100,200,400,100);
GameUtils.drawWord(g,"按下空格,游戏继续",Color.YELLOW,40,110,260);
}
if(state==3){
g.setColor(Color.BLUE);
g.fillRect(100,200,400,150);
GameUtils.drawWord(g,"很遗憾,游戏失败",Color.RED,40,110,260);
GameUtils.drawWord(g,"按下空格,重新开始",Color.RED,40,110,320);
}
if(state==4){
g.setColor(Color.BLUE);
if(GameUtils.level==3){
g.fillRect(100,200,400,150);
GameUtils.drawWord(g,"达成条件,游戏通关",Color.GREEN,40,110,260);
GameUtils.drawWord(g,"按下空格,再玩一次",Color.GREEN,40,110,320);
}else {
g.fillRect(100,200,400,100);
GameUtils.drawWord(g,"按下空格,进入下一关",Color.GREEN,40,110,260);
}
}
}
public void resetGame(){
score=0;
this.dispose();
String[] args={};
main(args);
}
public static void main(String[] args) {
GameWin gameWin=new GameWin();
gameWin.launch();
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
2)GameObj(游戏物品类)
package com.obj;
import com.GameWin;
import java.awt.*;
public class GameObj {
Image img;
int x;
int y;
int width = 30;
int height = 30;
GameWin frame;
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public GameWin getFrame() {
return frame;
}
public void setFrame(GameWin frame) {
this.frame = frame;
}
public GameObj() {
}
public GameObj(Image img, int x, int y, GameWin frame) {
this.img = img;
this.x = x;
this.y = y;
this.frame = frame;
}
public GameObj(Image img, int x, int y, int width, int height, GameWin frame) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.frame = frame;
}
public void paintSelf(Graphics g){
g.drawImage(img,x,y,null);
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
3)GameUtils(游戏工具类)
package com.utils;
import java.awt.*;
public class GameUtils {
public static Image upImg = Toolkit.getDefaultToolkit().getImage("src/img/up.png");
public static Image downImg = Toolkit.getDefaultToolkit().getImage("src/img/down.png");
public static Image leftImg = Toolkit.getDefaultToolkit().getImage("src/img/left.png");
public static Image rightImg = Toolkit.getDefaultToolkit().getImage("src/img/right.png");
public static Image bodyImg = Toolkit.getDefaultToolkit().getImage("src/img/body.png");
public static Image foodImg = Toolkit.getDefaultToolkit().getImage("src/img/food.png");
public static int level=1;
public static void drawWord(Graphics g,String str,Color color,int size,int x,int y){
g.setColor(color);
g.setFont(new Font("宋体",Font.BOLD,size));
g.drawString(str,x,y);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
4)HeadObj(蛇头类)
package com.obj;
import com.GameWin;
import com.utils.GameUtils;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class HeadObj extends GameObj{
private String direction = "right";
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public HeadObj() {
}
public HeadObj(Image img, int x, int y, GameWin frame) {
super(img,x,y,frame);
this.frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
changeDirection(e);
}
});
}
public void changeDirection(KeyEvent e){
switch (e.getKeyCode()){
case KeyEvent.VK_A:
if(!"right".equals(direction)){
direction="left";
img= GameUtils.leftImg;
}
break;
case KeyEvent.VK_D:
if(!"left".equals(direction)){
direction="right";
img= GameUtils.rightImg;
}
break;
case KeyEvent.VK_W:
if(!"down".equals(direction)){
direction="up";
img= GameUtils.upImg;
}
break;
case KeyEvent.VK_S:
if(!"up".equals(direction)){
direction="down";
img= GameUtils.downImg;
}
break;
default:
break;
}
}
public void move(){
for(int i=this.frame.bodyObjList.size()-1;i>=1;i--){
this.frame.bodyObjList.get(i).x=this.frame.bodyObjList.get(i-1).x;
this.frame.bodyObjList.get(i).y=this.frame.bodyObjList.get(i-1).y;
if(this.x==this.frame.bodyObjList.get(i).x&&this.y==this.frame.bodyObjList.get(i).y){
GameWin.state=3;
}
}
this.frame.bodyObjList.get(0).x=this.x;
this.frame.bodyObjList.get(0).y=this.y;
switch (direction){
case "up":
y-=height;
break;
case "down":
y+=height;
break;
case "left":
x-=width;
break;
case "right":
x+=width;
break;
default:
break;
}
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
Integer newX=null;
Integer newY=null;
FoodObj food=this.frame.foodObj;
if(food.x==this.x&&food.y==this.y){
this.frame.foodObj=food.getFood();
BodyObj lastBody=this.frame.bodyObjList.get(this.frame.bodyObjList.size()-1);
newX=lastBody.x;
newY=lastBody.y;
GameWin.score++;
}
if(GameWin.score>=15){
GameWin.state=4;
}
move();
if(newX!=null&&newY!=null){
this.frame.bodyObjList.add(new BodyObj(GameUtils.bodyImg,newX,newY,this.frame));
}
if (x < 0){
x = 570;
} else if (x > 570){
x = 0;
} else if (y < 30){
y = 570;
}else if (y > 570){
y = 30;
}
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
5)BodyObj(蛇身类)
package com.obj;
import com.GameWin;
import java.awt.*;
public class BodyObj extends GameObj{
public BodyObj(Image img, int x, int y,GameWin frame) {
super(img,x,y,frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
}
}
6)FoodObj(食物类)
package com.obj;
import com.GameWin;
import com.utils.GameUtils;
import java.awt.*;
import java.util.Random;
public class FoodObj extends GameObj{
Random random=new Random();
public FoodObj() {
super();
}
public FoodObj(Image img, int x, int y, GameWin frame) {
super(img, x, y, frame);
}
public FoodObj getFood(){
return new FoodObj(GameUtils.foodImg,random.nextInt(20)*30,(1+random.nextInt(19))*30,this.frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28