本项目参照网上学习制作,素材均来源于网络!
package wwl.snake;
import javax.swing.*;
/**
* 开始游戏类
* @author 王
*
*/
public class StartGames {
public static void main(String[] args) {
//1.绘制静态窗口 JFrame
JFrame frame = new JFrame("第一个项目贪吃蛇");//wwl是标题
//设置界面大小
frame.setBounds(10, 10, 900, 720);
frame.setResizable(false);//窗口大小不可改变
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭事件游戏可关闭
//2.面板 Jpanel 可以加入到JFrame
frame.add(new Gamepanel());
frame.setVisible(true);//让窗口能够展现出来
}
}
package wwl.snake;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
/**
* 第一个项目贪吃蛇!!
* @author 王
*游戏主类
*/
public class Gamepanel extends JPanel implements KeyListener, ActionListener{ //监听键盘的操作implements KeyListener
int lenth;//蛇的长度
int[] snakeX = new int [600];//蛇的坐标x
int[] snakeY = new int [500];//蛇的坐标y
String fx;//R:右 L:左 U:上 D:下
boolean isStart = false;//游戏是否开始
Timer timer = new Timer(150, this);//定时器
//1.定义一个食物
int foodx;
int foody;
int foodredx;
int foodredy;
int foodfailx;
int foodfaily;
int foodbigx;
int foodbigy;
Random random = new Random();
//死亡判断:
boolean isFail = false;
//积分系统:
int score;
//构造器
public Gamepanel() {
init();//初始化
//获取键盘监听事件
this.setFocusable(true);//键盘焦点聚集在游戏上
this.addKeyListener(this);//添加当前这个监听类
timer.start();//让时间动起来
}
//初始化
public void init() {
lenth = 3;
snakeX[0] = 100;snakeY[0] = 100;//头部坐标
snakeX[1] = 75;snakeY[1] = 100;//第一个身体坐标
snakeX[2] = 50;snakeY[2] = 100;//第二个身体坐标
fx = "R";
foodx = 25+25*random.nextInt(34);
foody = 75+25*random.nextInt(24);
foodredx = 25+25*random.nextInt(34);
foodredy = 75+25*random.nextInt(25);
foodfailx = 25+25*random.nextInt(34);
foodfaily = 75+25*random.nextInt(25);
foodbigx = 25+25*random.nextInt(34);
foodbigy = 75+25*random.nextInt(25);
score = 0;
}
//画板:画界面,画蛇
//Graphics(g):画笔
//重写父类画板方法
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
this.setBackground(Color.white);//设置背景为黑色
//绘制头部广告栏
Data.heade.paintIcon(this, g, 25, 11);
//绘制游戏区域
g.fillRect(25, 75, 850, 600);
//画一条静态的小蛇
if(fx.equals("R")) {//判断方向
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("L")) {
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("U")) {
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("D")) {
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
}
for(int i = 1; i < lenth; i++) {
Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);//蛇的身体长度通过length控制
}
//画积分
g.setColor(Color.black);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度:" + lenth, 750, 35);
g.drawString("分数:" + score, 750 , 56);
//画食物
Data.food.paintIcon(this, g, foodx, foody);
Data.foodred.paintIcon(this, g, foodredx, foodredy);
Data.foodfail.paintIcon(this, g, foodfailx, foodfaily);
Data.foodbig.paintIcon(this, g, foodbigx, foodbigy);
//游戏提示:是否开始
if(isStart == false) {
//画一个文字:String
g.setColor(Color.yellow);//设置画笔颜色
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("请按空格开始游戏", 300, 300);//画文字
}
//食物提示:
g.setColor(Color.black);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
Data.foodred.paintIcon(this, g, 600, 35);
g.drawString("红色食物身体减小:", 530, 35);
Data.foodfail.paintIcon(this, g, 385, 35);
g.drawString("毒物直接结束游戏:", 350, 35);
Data.foodbig.paintIcon(this, g, 185, 35);
g.drawString("身体增加两个:", 150, 35);
//失败提醒:
if(isFail) {
//画一个文字:String
g.setColor(Color.red);//设置画笔颜色
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("游戏失败,按空格重新开始游戏", 200, 300);//画文字
}
}
//接受键盘的收入:监听
@Override
public void keyPressed(KeyEvent e) { //键盘按下,未释放
//获取按下的键盘是哪个键
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_SPACE) {//如果按下的是空格按键
if(isFail) {//失败,游戏再来一遍
isFail = false;
init();//重新初始化游戏
}else {
isStart = !isStart;//取反
}
repaint();//不停的刷新界面
}
//键盘控制走向
if(keyCode == KeyEvent.VK_LEFT) {
fx = "L";
}else if(keyCode == KeyEvent.VK_RIGHT) {
fx = "R";
}else if(keyCode == KeyEvent.VK_DOWN) {
fx = "D";
}else if(keyCode == KeyEvent.VK_UP) {
fx = "U";
}
}
//定时器,监听时间流动,帧:执行定时操作
@Override
public void actionPerformed(ActionEvent e) {
//如果游戏处于开始状态,并且游戏没失败
if(isStart && isFail == false) {
//右移
for(int i = lenth - 1; i > 0; i--) {//除了脑袋,身体都向前移动
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
//通过控制方向让头部移动
if(fx.equals("R")){
snakeX[0] = snakeX[0] + 25;//头部移动
//边界判断:
if(snakeX[0] > 850) {
isFail = true;
}
}else if(fx.equals("L")) {
snakeX[0] = snakeX[0] - 25;//头部移动
//边界判断:
if(snakeX[0] < 25) {
isFail = true;
}
}else if(fx.equals("U")) {
snakeY[0] = snakeY[0] - 25;//头部移动
//边界判断:
if(snakeY[0] < 75) {
isFail = true;
}
}else if(fx.equals("D")) {
snakeY[0] = snakeY[0] + 25;//头部移动
//边界判断:
if(snakeY[0] > 700) {
isFail = true;
}
}
//如果小蛇头和食物坐标重合
if(snakeX[0] == foodx && snakeY[0] ==foody) {
//长度加1
lenth++;
score = score + 5;
//重新生成食物
foodx = 25+25*random.nextInt(32);
foody = 75+25*random.nextInt(22);
foodredx = 25+25*random.nextInt(32);
foodredy = 75+25*random.nextInt(22);
foodbigx = 25+25*random.nextInt(32);
foodbigy = 75+25*random.nextInt(22);
}
if(snakeX[0] == foodredx && snakeY[0] ==foodredy) {
//身体减小
lenth--;
score = score - 5;
//重新生成食物
foodredx = 25+25*random.nextInt(32);
foodredy = 75+25*random.nextInt(22);
foodx = 25+25*random.nextInt(32);
foody = 75+25*random.nextInt(22);
foodbigx = 25+25*random.nextInt(32);
foodbigy = 75+25*random.nextInt(22);
}
if(snakeX[0] == foodbigx && snakeY[0] ==foodbigy) {
//长度加2
lenth+=2;
score = score + 10;
//重新生成食物
foodx = 25+25*random.nextInt(32);
foody = 75+25*random.nextInt(22);
foodredx = 25+25*random.nextInt(32);
foodredy = 75+25*random.nextInt(22);
foodbigx = 25+25*random.nextInt(32);
foodbigy = 75+25*random.nextInt(22);
}
if(snakeX[0] == foodfailx && snakeY[0] ==foodfaily) {
//死亡
isFail = true;
}
//结束判断
for (int i = 1; i < lenth; i++) {
if(snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isFail = true;
init();
}
}
//刷新界面
repaint();
}
timer.start();//让时间动起来
}
@Override
public void keyReleased(KeyEvent e) {
//释放某个键
}
@Override
public void keyTyped(KeyEvent e) {
//键盘按下,弹起
}
}
package wwl.snake;
/**
* 存放外部数据
* @author 王
*
*/
import java.net.URL;
import javax.swing.ImageIcon;
public class Data {
//头部图片 URL:定位图片的地址 ImageIcon:图片
public static URL hedreUrl = Data.class.getResource("/stati/header.png");
public static ImageIcon heade = new ImageIcon(hedreUrl);
//头部:
public static URL upUrl = Data.class.getResource("/stati/up.png");
public static ImageIcon up = new ImageIcon(upUrl);
//下头
public static URL dowUrl = Data.class.getResource("/stati/down.png");
public static ImageIcon down = new ImageIcon(dowUrl);
//左头
public static URL leftUrl = Data.class.getResource("/stati/left.png");
public static ImageIcon left = new ImageIcon(leftUrl);
//右头
public static URL rightUrl = Data.class.getResource("/stati/right.png");
public static ImageIcon right = new ImageIcon(rightUrl);
//身体
public static URL bodyUrl = Data.class.getResource("/stati/body.png");
public static ImageIcon body = new ImageIcon(bodyUrl);
//食物
public static URL foodUrl = Data.class.getResource("/stati/food.png");
public static ImageIcon food = new ImageIcon(foodUrl);
//红色减小身体
public static URL foodredUrl = Data.class.getResource("/stati/foodred.png");
public static ImageIcon foodred = new ImageIcon(foodredUrl);
//毒物结束游戏
public static URL foodfailUrl = Data.class.getResource("/stati/foodfail.png");
public static ImageIcon foodfail = new ImageIcon(foodfailUrl);
//吃一个长两个身体
public static URL foodbigUrl = Data.class.getResource("/stati/foodbig.png");
public static ImageIcon foodbig = new ImageIcon(foodbigUrl);
}