本文实例为大家分享了java实现简单的贪吃蛇小游戏的具体代码,供大家参考,具体内容如下
1. 程序结构
程序结构图如图:
2. 程序设计思路
2.1 data类
作用:连接statics文件夹,将静态资源包中的图片转化为图标 方便在面板上绘制。
实现:使用class.getresource(string path)方法。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.snake; import javax.swing.*; import java.net.url; public class data { //贪吃蛇头部 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); } |
2.2 startgame类
作用:创建游戏窗口,在窗口中添加一个游戏面板。
实现:使用jframe类创建游戏窗口,利用其add()方法添加一个gamepanel类实例化对象。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.snake; import javax.swing.*; import java.awt.*; public class startgame { public static void main(string[] args){ //建立游戏窗口 jframe frame = new jframe( "java-贪吃蛇小游戏" ); //标题 frame.setsize( 900 , 720 ); //窗口大小 frame.setlocationrelativeto( null ); //窗口显示屏幕中间 frame.setresizable( false ); //固定窗口大小 frame.setdefaultcloseoperation(jframe.exit_on_close); //设置窗体关闭事件 frame.add( new gamepanel()); //添加游戏内容 frame.setvisible( true ); //设置窗体可见 } } |
2.3 gamepanel类
作用: 实现游戏的动态页面。
实现:
(1)init()方法:初始化小蛇位置;
(2)eat()方法:用随机种子随机食物的位置,并进行判定,食物位置不能和小蛇位置重合;
(3)继承jpanel类,重写paintcomponent(graphics g)方法,在方法中绘制标题栏、小蛇的位置(根据direction小蛇头部方向变量绘制小蛇头部)、小蛇身体、积分栏、游戏提醒项与失败判断项;
(4)实现keylistener 接口中的keypressed(keyevent e)方法,获取键盘输入,根据键盘输入对游戏状态或者小蛇头部方向direction变量进行更改;
(5)实现actionlistener接口中的actionperformed(actionevent e)方法,根据游戏状态和direction变量进行小蛇移动操作(注意禁用直接回头操作),进行吃食物判定和死亡判定。使用timer计时器让游戏动态变化,用repaint()方法实时更新界面。
代码如下:
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 |
package com.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 [] snakex = new int [ 500 ]; //贪吃蛇横坐标 int [] snakey = new int [ 500 ]; //贪吃蛇纵坐标 int foodx; //食物横坐标 int foody; //食物蛇纵坐标 int length; //贪吃蛇的长度 string direction; //贪吃蛇头方向 int score; //积分 random r = new random(); timer timer = new timer( 100 , this ); boolean isstart; boolean isfail; //构造函数 public gamepanel(){ init(); this .setfocusable( true ); this .addkeylistener( this ); timer.start(); } private void init(){ length= 3 ; snakex[ 0 ]= 100 ;snakey[ 0 ]= 100 ; snakex[ 1 ]= 75 ;snakey[ 1 ]= 100 ; snakex[ 2 ]= 50 ;snakey[ 2 ]= 100 ; direction = "r" ; eat(foodx,foody); isstart = false ; isfail = false ; score = 0 ;
} private void eat( int x, int y){ x= 25 + 25 *r.nextint( 34 ); y= 75 + 25 *r.nextint( 24 ); for ( int i = 0 ; i < length; i++) { if (snakex[i]==x&&snakey[i]==y){ x = 25 + 25 *r.nextint( 34 ); y = 75 + 25 *r.nextint( 24 ); } } foodx = x;foody = y; } protected void paintcomponent(graphics g) { super .paintcomponent(g); this .setbackground(color.white); //设置背景板为白色 //画标题 g.setcolor(color.green); g.setfont( new font( "幼圆" ,font.bold, 50 )); g.drawstring( "贪吃蛇游戏" , 300 , 60 ); //绘制游戏区域 g.setcolor(color.gray); g.fillrect( 25 , 75 , 850 , 600 ); //画贪吃蛇头部 if (direction== "r" ){ data.right.painticon( this ,g,snakex[ 0 ],snakey[ 0 ]); } else if (direction== "l" ){ data.left.painticon( this ,g,snakex[ 0 ],snakey[ 0 ]); } if (direction== "u" ){ data.up.painticon( this ,g,snakex[ 0 ],snakey[ 0 ]); } else if (direction== "d" ){ data.down.painticon( this ,g,snakex[ 0 ],snakey[ 0 ]); } //画身体 for ( int i = 1 ; i < length ; i++) { data.body.painticon( this ,g,snakex[i],snakey[i]); } //画食物 data.food.painticon( this ,g,foodx,foody); //绘制积分栏 g.setcolor(color.black); g.setfont( new font( "幼圆" ,font.bold, 20 )); g.drawstring( "长度:" +length, 730 , 30 ); g.drawstring( "得分:" +score, 730 , 60 ); //游戏开始提醒 if (isstart== false ){ g.setcolor(color.black); g.setfont( new font( "幼圆" ,font.bold, 40 )); g.drawstring( "按空格键开始游戏" , 300 , 300 ); } //失败判断 if (isfail){ g.setcolor(color.red); g.setfont( new font( "幼圆" ,font.bold, 40 )); g.drawstring( "游戏失败,按空格键重新开始" , 300 , 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&&direction!= "r" ){ direction = "l" ; } else if (keycode==keyevent.vk_right&&direction!= "l" ){ direction = "r" ; } else if (keycode==keyevent.vk_up&&direction!= "d" ){ direction = "u" ; } else if (keycode==keyevent.vk_down&&direction!= "u" ){ direction = "d" ; } } @override public void keyreleased(keyevent e) {
} @override public void keytyped(keyevent e) { }
@override public void actionperformed(actionevent e) { //判断游戏状态 if (isstart&&!isfail){ //移动身体 for ( int i = length- 1 ; i > 0 ; i--) { snakex[i] = snakex[i- 1 ]; snakey[i] = snakey[i- 1 ]; } //移动头部 if (direction== "r" ){ snakex[ 0 ] += 25 ; if (snakex[ 0 ]> 850 ){ snakex[ 0 ] = 25 ; } } else if (direction== "l" ){ snakex[ 0 ] -= 25 ; if (snakex[ 0 ]< 25 ){ snakex[ 0 ] = 850 ; } } else if (direction== "u" ){ snakey[ 0 ] -= 25 ; if (snakey[ 0 ]< 75 ){ snakey[ 0 ] = 650 ; } } else if (direction== "d" ){ snakey[ 0 ] += 25 ; if (snakey[ 0 ]> 650 ){ snakey[ 0 ] = 75 ; } } //吃食物 if (snakex[ 0 ]==foodx&&snakey[ 0 ]==foody){ length++; score += 10 ; eat(foodx,foody); } //死亡判定 for ( int i = 1 ; i < length; i++) { if (snakex[ 0 ]==snakex[i]&&snakey[ 0 ]==snakey[i]){ isfail= true ; } } repaint(); } timer.start(); } } |
3. 游戏展示
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/xfjssaw/article/details/115708947