贪吃蛇游戏的设计步骤:
Part 1:
设计游戏图纸
画出900*700的白色窗口
在窗口上添加画布
在画布上添加标题
在画布上添加黑色游戏区
Part 2:
放置静态的蛇:一个头、两个身体
加上开始提示:按空格键开始游戏
让蛇动起来:监听Timer事件,平移数据
实现游戏暂停
实现转向功能
Part 3:
添加食物
吃掉食物
添加死亡条件
实现“重新开始”功能
添加分数和长度
游戏图纸如下:
蛇及游戏框的素材如下:
packagesnake; importjavax.swing.JFrame;importjavax.swing.JPanel; publicclassSnake{ publicstaticvoidmain(String[] args){ JFrame frame =newJFrame();// 创建一个游戏界面的框架frame.setBounds(10,10,900,720);// 设置框架的大小frame.setResizable(false);// 设置框架大小为不能改变// 点击关闭按钮 关闭游戏界面 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SnakePanel panel =newSnakePanel();//添加画布frame.add(panel);// 刚添加时画布是空的看不到 frame.setVisible(true);// 允许显示游戏界面 }}
SnakePanel类:
packagesnake; importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Random;importjavax.swing.ImageIcon;importjavax.swing.JPanel;importjavax.swing.Timer; publicclassSnakePanelextendsJPanelimplementsKeyListener,ActionListener{// 定义七个图片变量,代表七张图片ImageIcon up =newImageIcon("up.png");// 向上的蛇头ImageIcon down =newImageIcon("down.png");// 向下的蛇头ImageIcon left =newImageIcon("left.png");// 向左的蛇头ImageIcon right =newImageIcon("right.png");// 向右的蛇头ImageIcon food =newImageIcon("food.png");// 食物ImageIcon body =newImageIcon("body.png");// 蛇的身体ImageIcon title =newImageIcon("title.jpg");// 游戏界面的主题 // 蛇的每一部分int[] snakex =newint[750];int[] snakey =newint[750]; // 随机生成食物Random rand =newRandom();intfoodx = rand.nextInt(34)*25+25;//此处的数值根据自己设计的游戏界面的大小来确定intfoody = rand.nextInt(24)*25+75; // 设置游戏的默认属性intlen =3;intscore =0;String direction ="R";// U上 D下 L左 R右 booleanisStarted =false;// 判断游戏是否开始booleanisFailed =false;// 判断游戏是否结束Timer timer =newTimer(100,this);// 每100毫秒调用一次ActionPerformed publicSnakePanel(){// 建造画布的构造函数this.setFocusable(true);// 获取焦点this.addKeyListener(this);// 监听键盘事件 setup(); timer.start(); } publicvoidpaint(Graphics g){// Graphics 画笔 this.setBackground(Color.BLACK);// 设置画布背景颜色title.paintIcon(this, g,25,11);// 放置主题图片g.fillRect(25,75,850,650);// 用画笔设置游戏方框 // 画蛇头(注意判断蛇头的方向)if(direction.equals("R"))right.paintIcon(this, g, snakex[0], snakey[0]);elseif(direction.equals("L"))left.paintIcon(this, g, snakex[0], snakey[0]);elseif(direction.equals("U"))up.paintIcon(this, g, snakex[0], snakey[0]);elseif(direction.equals("D"))down.paintIcon(this, g, snakex[0], snakey[0]); // 画蛇的身体for(inti =1; i < len; i ++)body.paintIcon(this, g, snakex[i], snakey[i]); // 判断如果游戏没开始显示。。。if(!isStarted){ g.setColor(Color.WHITE);g.setFont(newFont("arial",Font.BOLD,30));g.drawString("Press Space to start / pause",200,300); } // 判断如果游戏结束显示。。。if(isFailed){ g.setColor(Color.WHITE); g.setFont(newFont("arial",Font.BOLD,30));g.drawString("Game Over ! Press space to restart",200,300); } // 显示食物food.paintIcon(this, g, foodx, foody); // 设置分数和蛇的长度 g.setColor(Color.WHITE);g.setFont(newFont("arial",Font.PLAIN,15));g.drawString("Score : "+score,650,37);g.drawString("Len :"+len,650,57); } publicvoidsetup(){// 游戏初始化isStarted =false;isFailed =false;len =3;score =0;snakex[0] =100; snakex[1] =75; snakex[2] =50;snakey[0] =100; snakey[1] =100; snakey[2] =100; } @OverridepublicvoidkeyPressed(KeyEvent e){ //实现键盘响应 intKeyCode = e.getKeyCode();if(KeyCode == KeyEvent.VK_SPACE){// 敲击空格现实/消除提示信息if(isFailed){// isStarted = false; // 可以将这两行放入setup中// isFailed = false; setup();}else isStarted = !isStarted;}elseif(KeyCode == KeyEvent.VK_UP && direction !="D")direction ="U";elseif(KeyCode == KeyEvent.VK_DOWN && direction !="U")direction ="D";elseif(KeyCode == KeyEvent.VK_RIGHT && direction !="L")direction ="R";elseif(KeyCode == KeyEvent.VK_LEFT && direction !="R")direction ="L"; }@OverridepublicvoidactionPerformed(ActionEvent e){// 1. 再定义一个闹钟 timer.start(); // 2. 移动数据if(isStarted && !isFailed){// 移动身体for(inti = len; i>0; i--){snakex[i] = snakex[i-1];snakey[i] = snakey[i-1]; }// 移动头if(direction.equals("R")){snakex[0] = snakex[0] +25;if(snakex[0] >850) snakex[0] =25;}elseif(direction.equals("L")){snakex[0] = snakex[0] -25;if(snakex[0] <25) snakex[0] =850;}elseif(direction.equals("U")){snakey[0] = snakey[0] -25;if(snakey[0] <75) snakey[0] =650;}elseif(direction.equals("D")){snakey[0] = snakey[0] +25;if(snakey[0] >650) snakey[0] =75; } if(snakex[0] == foodx && snakey[0] == foody){// 吃食物 len ++; score ++;foodx = rand.nextInt(34)*25+25;foody = rand.nextInt(24)*25+75; } for(inti =1; i < len; i ++){// 如果蛇头碰到自己的身体游戏结束if(snakex[0] == snakex[i] && snakey[0] == snakey[i]){isFailed =true; } } }// 3. repaint() repaint(); } @OverridepublicvoidkeyTyped(KeyEvent e){ }@OverridepublicvoidkeyReleased(KeyEvent e){ } }
游戏效果如下:
作者:Geoooo
原文:https://blog.csdn.net/geoooo/article/details/79457301