最近正在学java,就跟着视频做了一个贪吃蛇小游戏,虽然做的很low,但还是记录一下。
功能比较简单:
1.小蛇自由上下左右移动
2.可以吃食物
3.吃到食物后,可以增加积分
4.蛇头接触到身体为死亡
5.点击空格重新开开始
开始界面如下 :
图片材料取自这篇博客
https://blog.csdn.net/u011622021/article/details/81162083
代码:
package com.msb.game;
import javax.swing.*;
import java.net.URL;
public class images {
// 将图片路径封装成对象
public static URL topUrl = images.class.getResource("/images/top.jpg");
// 创建图片对象
public static ImageIcon topImg = new ImageIcon(topUrl);
// 将图片路径封装成对象
public static URL backgroundUrl = images.class.getResource("/images/background.jpg");
// 创建图片对象
public static ImageIcon backgroundImg = new ImageIcon(backgroundUrl);
// 将图片路径封装成对象
public static URL bottomUrl = images.class.getResource("/images/bottom.jpg");
// 创建图片对象
public static ImageIcon bottomImg = new ImageIcon(bottomUrl);
// 将图片路径封装成对象
public static URL failUrl = images.class.getResource("/images/fail.png");
// 创建图片对象
public static ImageIcon failImg = new ImageIcon(failUrl);
// 将图片路径封装成对象
public static URL foodUrl = images.class.getResource("/images/food.png");
// 创建图片对象
public static ImageIcon foodImg = new ImageIcon(foodUrl);
// 将图片路径封装成对象
public static URL leftUrl = images.class.getResource("/images/left.jpg");
// 创建图片对象
public static ImageIcon leftImg = new ImageIcon(leftUrl);
// 将图片路径封装成对象
public static URL rightUrl = images.class.getResource("/images/right.jpg");
// 创建图片对象
public static ImageIcon rightImg = new ImageIcon(rightUrl);
// 将图片路径封装成对象
public static URL snake_bodyUrl = images.class.getResource("/images/snake_body.png");
// 创建图片对象
public static ImageIcon snake_bodyImg = new ImageIcon(snake_bodyUrl);
}
package com.msb.game;
import javax.swing.*;
public class StartGame {
public static void main(String[] args) {
JFrame jf=new JFrame();
// 窗体设置标题
jf.setTitle("小游戏 贪吃蛇 by Alensandern");
// 获取屏幕宽高
int width = jf.getToolkit().getDefaultToolkit().getScreenSize().width;
int height = jf.getToolkit().getDefaultToolkit().getScreenSize().height;
// 设置窗体大小
jf.setBounds((width-1024)/2,(height-578)/2,1024,578);
// 窗口不可通过拖拽放大或缩小
jf.setResizable(false);
// 关闭窗口的同时,程序关闭
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 创建一个面板,在窗口中增加一个面板
GamePanel gp=new GamePanel();
jf.add(gp);
// 窗口可视
jf.setVisible(true);
}
}
package com.msb.game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
//必须继承JPanel才能是一个面板
public class GamePanel extends JPanel {
// 创建两个数组,用于存储小蛇的x,y轴坐标
int[] snakeX = new int[200];
int[] snakeY = new int[200];
int length;
// 小蛇是否死亡
boolean isDie=false;
// 积分
int score=0;
// 食物的坐标
int foodX;
int foodY;
String direction;
boolean gameStart=false;
// 定时器
Timer time;
// 小蛇的初始位置,用一个函数进行存储
public void gameInit() {
length = 3;
snakeX[0] = 175;
snakeY[0] = 275;
snakeX[1] = 150;
snakeY[1] = 275;
snakeX[2] = 125;
snakeY[2] = 275;
foodX=400;
foodY=200;
direction="R";
}
public GamePanel() {
gameInit();
// 将焦点聚到面板上
this.setFocusable(true);
// 加入监听事件
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int keyCode = e.getKeyCode();
if(keyCode==KeyEvent.VK_SPACE){
if(isDie){
gameInit();
isDie=false;
}else {
gameStart=!gameStart;
// 重绘面板, 重新调用paintComponent()函数
repaint();
}
}
else if(keyCode==KeyEvent.VK_UP){
direction="T";
}
else if(keyCode==KeyEvent.VK_DOWN){
direction="B";
}
else if(keyCode==KeyEvent.VK_LEFT){
direction="L";
}
else if(keyCode==KeyEvent.VK_RIGHT){
direction="R";
}
}
});
time=new Timer(150, new ActionListener() {
// 事件监听,监听的动作就是actionPerformed的内容,每隔150毫秒监听一次
@Override
public void actionPerformed(ActionEvent e) {
if(gameStart&&isDie==false){
for(int i=length-1;i>0;i--){
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
if(direction=="R"){
snakeX[0]+=25;
}
else if(direction=="L"){
snakeX[0]-=25;
}
else if(direction=="T"){
snakeY[0]-=25;
}
else if(direction=="B"){
snakeY[0]+=25;
}
if(snakeX[0]>1024){
snakeX[0]=0;
}
if(snakeX[0]>1024){
snakeX[0]=0;
}
else if(snakeX[0]<0){
snakeX[0]=1024;
}
else if(snakeY[0]<0){
snakeY[0]=578;
}
else if(snakeY[0]>578){
snakeY[0]=0;
}
if(foodX==snakeX[0]&&foodY==snakeY[0]){
length++;
score+=10;
foodX=((new Random().nextInt(40)+1)*25);
foodY=((new Random().nextInt(23)+1)*25);
}
for(int i=1;i<length;i++){
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isDie=true;
}
}
repaint();
}
}
});
time.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 插入背景图片
images.backgroundImg.paintIcon(this, g, 0, 0);
// 按方向插入头部
if("R".equals(direction)){
images.rightImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
else if("L".equals(direction)){
images.leftImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
else if("T".equals(direction)){
images.topImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
else if("B".equals(direction)){
images.bottomImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
//蛇身设计
for(int i=1;i<length;i++){
images.snake_bodyImg.paintIcon(this,g,snakeX[i],snakeY[i]);
}
// 按下空格停止
if(gameStart==false){
g.setColor(new Color(105, 146, 73, 255));
g.setFont(new Font("微软雅体",Font.BOLD,40));
g.drawString("按下空格开始游戏",350,250);
}
images.foodImg.paintIcon(this,g,foodX,foodY);
// 积分
g.setColor(new Color(175, 90, 44, 255));
g.setFont(new Font("微软雅体",Font.BOLD,40));
g.drawString("积分: "+score,800,50);
// 小蛇死亡提示
if(isDie){
g.setColor(new Color(175, 90, 44, 255));
g.setFont(new Font("微软雅体",Font.BOLD,40));
g.drawString("小蛇已死亡,点击空格,从新开始 ",150,250);
}
}
}
不嫌弃的伙伴可以提取资源哦!
源码压缩包:
链接:https://pan.baidu.com/s/11lo3Sw-dSjncrkJgz_lmPw 提取码:4kx0
图片链接:
http://链接:https://pan.baidu.com/s/17oNUcXn4X0HUflvJG45qtA 提取码:bhd9
.jar文件(可直接执行游戏)
http://链接:https://pan.baidu.com/s/13J6wi8W1NGGSApqLioWFxA 提取码:ubgq