java贪吃蛇游戏代码doc.docx
《java贪吃蛇游戏代码doc.docx》由会员分享,可在线阅读,更多相关《java贪吃蛇游戏代码doc.docx(9页珍藏版)》请在冰豆网上搜索。
![java贪吃蛇游戏代码doc.docx](https://file1.bdocx.com/fileroot1/2023-1/7/29e4fe3b-db83-4b08-b5b1-f904d63bc1c1/29e4fe3b-db83-4b08-b5b1-f904d63bc1c11.gif)
java贪吃蛇游戏代码doc
java贪吃蛇游戏代码
思路:
主要是模拟蛇的运动。
只要对蛇头进行操作,然后让前一种状态先的蛇头位置复制给现在蛇身的第二个部分,让前一状态的蛇的第二个部分的位置复制给现在蛇身的第三个部分......以此类推,从而达到模拟蛇运动的状态。
GluttonousSnake.java
//贪吃蛇图形界面
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Observable;
importjava.util.Observer;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.border.TitledBorder;
importjavax.swing.event.CaretEvent;
importjavax.swing.event.CaretListener;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
publicclassGluttonousSnakeextendsJFrameimplementsActionListener{
privateJButtonupButton,downButton,leftButton,rightButton;//控制方向按钮
privateJTextFieldscore;//分数
privateSnakeCanvassnake;//蛇的模型
publicGluttonousSnake(){
super(贪吃蛇//设置标题
this.setSize(725,515);//设置大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置退出按键
this.setLocationRelativeTo(null);//设置窗口居中
JPanelp=newJPanel();//控制按钮和显示分数面板
p.setBorder(newTitledBorder(控制和显示区));//设置这个面板的标题
this.getContentPane().add(p,East//设置此面板的位置
p.setLayout(newGridLayout(4,1));//设置此面板的布局方式,为网格布局方式
JPanelp2=newJPanel();//在此面板中添加显示分数的面板
p2.setLayout(newFlowLayout());//设置为流布局方式
p2.add(newJLabel(得分:
));
score=newJTextField(0
score.setEditable(false);
p2.add(score);
p.add(p2);
//添加按钮,有图片的并添加事件监听
upButton=newJButton(,newImageIcon(up.png));
upButton.setActionCommand(up
upButton.addActionListener(this);
downButton=newJButton(,newImageIcon(down.png));
downButton.setActionCommand(down
downButton.addActionListener(this);
leftButton=newJButton(,newImageIcon(left.png));
leftButton.setActionCommand(left
leftButton.addActionListener(this);
rightButton=newJButton(,newImageIcon(right.png));
rightButton.setActionCommand(right
rightButton.addActionListener(this);
p.add(upButton);
JPanelp1=newJPanel();
p1.setLayout(newGridLayout(1,2));
p1.add(leftButton);
p1.add(rightButton);
p.add(p1);
p.add(downButton);
addMenu();//添加菜单
start();
this.setResizable(false);
this.setVisible(true);
privatevoidstart(){
snake=newSnakeCanvas();
this.getContentPane().add(snake);
privatevoidaddMenu(){
JMenuBarmenubar=newJMenuBar();
this.setJMenuBar(menubar);
JMenugame=newJMenu(游戏
JMenuhelp=newJMenu(帮助
JMenuItemjitemNew=newJMenuItem(新游戏
jitemNew.setActionCommand(new
jitemNew.addActionListener(this);
JMenuItemjitemPause=newJMenuItem(暂停
jitemPause.setActionCommand(pause
jitemPause.addActionListener(this);
JMenuItemjitemExit=newJMenuItem(退出
jitemExit.setActionCommand(exit
jitemExit.addActionListener(this);
game.add(jitemNew);
game.add(jitemPause);
game.addSeparator();//菜单里设置分隔线
game.add(jitemExit);
menubar.add(game);
menubar.add(help);
publicstaticvoidmain(String[]args){
newGluttonousSnake();
publicvoidactionPerformed(ActionEvente){
if(e.getActionCommand().equalsIgnoreCase(exit)){
System.exit(EXIT_ON_CLOSE);
if(e.getSource()instanceofJButton){
if(e.getActionCommand().equalsIgnoreCase(up)){//响应向上按钮按下事件
snake.setDirect
(1);//设置蛇头运动方向
snake.repaint();//对蛇模型重新画
snake.timer.start();//定时器开始
return;
if(e.getActionCommand().equalsIgnoreCase(down)){
snake.setDirect
(2);
snake.repaint();
snake.timer.start();
return;
if(e.getActionCommand().equalsIgnoreCase(left)){
snake.setDirect(3);
snake.repaint();
snake.timer.start();
return;
if(e.getActionCommand().equalsIgnoreCase(right)){
snake.setDirect(4);
snake.repaint();
snake.timer.start();
return;
if(e.getSource()instanceofJMenuItem){
if(e.getActionCommand().equalsIgnoreCase(new)){
//this.getContentPane().remove(snake);
snake.init();
snake.repaint();
snake.timer.start();
if(e.getActionCommand().equalsIgnoreCase(pause)){
snake.timer.stop();
JOptionPane.showMessageDialog(this,继续请按“确定”
snake.timer.start();
publicclassSnakeCanvasextendsMapCanvasimplementsActionListener{
privateintnumber=5;//蛇身的初始长度
publicTimertimer;//定时器
privateBody[]bodys;//蛇身
privateBodyfood;//食物
publicintscore=0;//分数
privateintspeed=250;//定时器延时事件
intdirect=3;//蛇头初始方向
privateint[][]mapflag=newint[455][455];//地图标记
//初始化
publicvoidinit(){
this.direct=3;
this.number=5;
this.bodys=newBody[this.number];
mapflag=newint[455][455];
for(inti=0,x=300;ithis.number;i++,x+=30){
bodys[i]=newBody(x,180);
mapflag[bodys[i].x][bodys[i].y]=2;//如果是蛇体设标记为2
food=creatFood();//创建食物
publicSnakeCanvas(){
init();
timer=newTimer(speed,this);//创建定时器对象
timer.start();//定时器启动
privateclassBody{//蛇头成员
intx,y;
Body(intx,inty){
this.x=x;
this.y=y;
publicvoidpaint(Graphicsg){//画图
super.paint(g);
if(bodys==null){
return;
for(inti=0;ithis.number;i++){
if(i==0){//设置蛇头颜色不同
g.setColor(Color.blue);
}else{
g.setColor(Color.black);
mapflag[bodys[i].x][bodys[i].y]=2;
g.fillOval(bodys[i].x,bodys[i].y,30,30);//画蛇体
g.setColor(Color.red);
g.fillOval(food.x,food.y,30,30);//画食物
publicBodycreatFood(){
intx=0,y=0;
do{//随机生成食物位置
Randomr=newRandom();//创建随机数对象
x=r.nextInt(450);//在0到所给的数中随机产生一个伪随机数
y=r.nextInt(450);
//System.out.println(mapflag[x][y]+!
}while(x%30!
=0||y%30!
=0||mapflag[x][y]==2);//保证位置不是蛇身的部分,并且确保整齐性
//System.out.println(x++y);
mapflag[x][y]=1;//将食物标记为1,下面就可以通过标记的不同来判断是吃到食物还是自身,从而进行不同的操作
returnnewBody(x,y);
publicvoidactionPerformed(ActionEvente){
if(bodys==null){
return;
if(!
isMove()){//如果移动失败,游戏结束
JOptionPane.showMessageDialog(this,GameOver!
bodys=null;
timer.stop();
return;
repaint();
//蛇移动,原理:
仅需对蛇头进行操作,然后将蛇头之前的位置赋值给现在蛇身的第二个部分,
//让之前的第二个部分复制个现在的第三个部分。
。
。
。
以此类推,模拟蛇运动(这是关键)
publicvoidmove(intx,inty){
Body[]b=newBody[bodys.length];
for(inti=0;ithis.number;i++){
b[i]=newBody(bodys[i].x,bodys[i].y);
this.bodys[0].x=x;
this.bodys[0].y=y;
for(inti=1;ithis.number;i++){
this.bodys[i]=b[i-1];
setMapflag();
privatevoidsetMapflag(){//地图标记的刷新
mapflag=newint[455][455];
mapflag[food.x][food.y]=1;
for(inti=0;ithis.number;i++){
mapflag[bodys[i].x][bodys[i].y]=2;
//判断是否移动成功
publicbooleanisMove(){
if(bodys==null){
returnfalse;
intx=bodys[0].x;
inty=bodys[0].y;
switch(this.direct){//通过方向的不同对蛇头进行不同的操作
case1:
y-=30;
break;
case2:
y+=30;
break;
case3:
x-=30;
break;
case4:
x+=30;
break;
if(x0||y=450||x=450||y0){//越界问题
returnfalse;
if(mapflag[x][y]==1){//标记为1,则为蛇头,要添加蛇身长度
this.number++;
addSnake(x,y);//添加蛇长
//System.outSnake(intx,inty){
this.score++;
Body[]b=newBody[bodys.length];
for(inti=0;ithis.number-1;i++){
b[i]=newBody(bodys[i].x,bodys[i].y);
bodys=newBody[this.number];
this.bodys[0]=newBody(x,y);
for(inti=1;ithis.number;i++){
this.bodys[i]=b[i-1];
setMapflag();
food=creatFood();
publicvoidsetDirect(inti){//设置蛇头方向
this.direct=i;
}
来自: