服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现简单的贪吃蛇游戏

Java实现简单的贪吃蛇游戏

2021-10-06 01:14wuhuizzj Java教程

这篇文章主要介绍了Java实现简单的贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下

代码

启动类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package snake;
 
import javax.swing.*;
 
//游戏的主启动类
public class startgame {
    public static void main(string[] args) {
        jframe frame = new jframe("贪吃蛇");
 
        frame.setbounds(10,10,900,720);
        frame.setresizable(false);      //窗口不可调整
        frame.setdefaultcloseoperation(windowconstants.exit_on_close);
 
        //正常游戏界面都应该在面板上
        frame.add(new gamepanel());
 
        frame.setvisible(true);
    }
}

游戏画板

?
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
package snake;
 
import javax.swing.*;
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;
 
//游戏面板
public class gamepanel extends jpanel implements keylistener, actionlistener {
 
    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(120,this);//定时器
    //定义一个食物
    int foodx;
    int foody;
    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);
 
        score = 0;
    }
 
    //绘制面板,游戏中的所以东西都使用这个东西来画
    @override
    protected void paintcomponent(graphics g) {
        super.paintcomponent(g); //清屏作用
        //绘制静态的面板
        this.setbackground(color.white);
        data.header.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]);
        }
 
        //蛇的身体长度通过length来控制
        for (int i = 1;i < lenth;i++){
            data.body.painticon(this,g,snakex[i],snakey[i]);
        }
        //画积分
        g.setcolor(color.white);
        g.setfont(new font("微软雅黑",font.bold,20));
        g.drawstring("长度"+lenth,700,45);
        g.drawstring("分数"+score,780,45);
        //画食物
        data.food.painticon(this,g,foodx,foody);
        //游戏提示:是否开始
        if (isstart == false){
            //画一个文字string来提示开始
            g.setcolor(color.white);    //定义颜色
            g.setfont(new font("微软雅黑",font.bold,40));   //设置字体
            g.drawstring("按下空格开始游戏",300,300);
        }
        //失败提醒
        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_up) {
            fx = "u";
        }else if (keycode==keyevent.vk_down) {
            fx = "d";
        }
    }
 
    //定时器,监听时间,帧,执行定时操作
    @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){ snakex[0] = 25; }//右边界判断
            }else if (fx.equals("l")){
                snakex[0] =snakex[0] - 25;//头部移动
                if (snakex[0]<25){ snakex[0] = 850;}//左边界判断
            }else if (fx.equals("u")){
                snakey[0] =snakey[0] - 25;//头部移动
                if (snakey[0]<75){ snakey[0] = 650;}//上边界判断
            }else if (fx.equals("d")){
                snakey[0] =snakey[0] + 25;//头部移动
                if (snakey[0]>650){ snakey[0] = 75;}//下边界判断
            }
 
            //如果食物和蛇头坐标重合
            if (snakex[0]==foodx && snakey[0]==foody){
                lenth ++;//蛇的身体长度加一
 
                score = score + 10;
 
                //重新生成食物
                foodx = 25 +25 * random.nextint(34);
                foody = 75 +25 * random.nextint(24);
            }
            //结束判断
            for (int i = 1; i<lenth;i++){
                if (snakex[0]==snakex[i] && snakey[0]==snakey[i]){
                    isfail = true;
                }
            }
 
 
            repaint();//刷新界面
        }
        timer.start();//让时间动起来
    }
 
    @override//键盘按下,弹起
    public void keytyped(keyevent e) {
 
    }
    @override//释放某个键
    public void keyreleased(keyevent e) {
 
    }
 
 
}

游戏数据(存放图片)

?
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
package snake;
 
import javax.swing.*;
import java.net.url;
 
//数据中心
public class data {
    //相对路径 tx.jpg 绝对路径  /相当于当前的项目
 
    //头部图片  url 定位图片地址
    public static url headerurl = data.class.getresource("/statics/header.png");
    public static imageicon header = new imageicon(headerurl);
 
     //上下左右移动
    public static url upurl = data.class.getresource("/statics/up.png");
    public static imageicon up = new imageicon(upurl);
    public static url downurl = data.class.getresource("/statics/down.png");
    public static imageicon down = new imageicon(downurl);
    public static url lefturl = data.class.getresource("/statics/left.png");
    public static imageicon left = new imageicon(lefturl);
    public static url righturl = data.class.getresource("/statics/right.png");
    public static imageicon right = new imageicon(righturl);
    //身体
    public static url bodyurl = data.class.getresource("/statics/body.png");
    public static imageicon body = new imageicon(bodyurl);
    //食物
    public static url foodurl = data.class.getresource("/statics/food.png");
    public static imageicon food = new imageicon(foodurl);
 
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wuhuizzj/article/details/108957947

延伸 · 阅读

精彩推荐