文章目录
-
-
- 1.游戏规则
- 2.游戏操作
- 3.代码片段
-
- 1)GameWin(主入口)
- 2)GameObj(游戏物品类)
- 3)GameUtils(游戏工具类)
- 4)HeadObj(蛇头类)
- 5)BodyObj(蛇身类)
- 6)FoodObj(食物类)
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();
}
}
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);
}
}
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);
}
}
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;
}
}
}
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);
}
}