当前位置:   article > 正文

利用Java实现贪吃蛇项目(附带源码和图片素材)_使用java进行贪吃蛇游戏的方法

使用java进行贪吃蛇游戏的方法

目录

程序的主类GameWin

工具类GameUtils

游戏父类GameObj

身体类BodyObj

食物类FoodObj

蛇头部HeadObj

图片资源


大家好,我是小刘!  

本篇文章将向各位介绍一个Java游戏项目,本次项目可以应用与毕业设计以及简历等!!!

运行结果如下

接下来就让我们进入代码的实现吧

首先新建一个项目,然后在项目里面新建一个存放图片的文件夹,将本次项目所需要的图片存放进去,图片资源在文章最后。(按照小刘下面这个方式去创建就可以啦!!一定要在项目中去创建!!)创建包的时候也记得更改一下名字哦!

程序的主类GameWin

  1. package Game;
  2. import Game.utils.GameUtils;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class GameWin extends JFrame {
  10. public static int state=0;
  11. public int score=0;//分数
  12. //定义双缓存图片
  13. Image offScreeImage=null;
  14. //分数宽高
  15. int winWidth=800;
  16. int winHeight=600;
  17. public HeadObj headObj=new HeadObj(GameUtils.image3,60,570,this);
  18. public List<BodyObj> bodyObjList=new ArrayList<>();
  19. public FoodObj foodObj=new FoodObj().getFood();
  20. public void launch(){
  21. this.setVisible(true);
  22. this.setSize(winWidth,winHeight);
  23. this.setLocationRelativeTo(null);
  24. this.setTitle("好七蛇");
  25. bodyObjList.add(new BodyObj(GameUtils.imagebody,30,570,this));
  26. bodyObjList.add(new BodyObj(GameUtils.imagebody,0,570,this));
  27. //键盘事件
  28. this.addKeyListener(new KeyAdapter() {
  29. @Override
  30. public void keyPressed(KeyEvent e) {
  31. if (e.getKeyCode()==KeyEvent.VK_SPACE){
  32. switch (state){
  33. //未开始
  34. case 0:
  35. state=1;
  36. break;
  37. case 1:
  38. //游戏中
  39. state=2;
  40. repaint();
  41. break;
  42. case 2:
  43. //游戏暂停
  44. state=1;
  45. break;
  46. case 3:
  47. //失败后重新开始
  48. state=5;
  49. break;
  50. default:break;
  51. }
  52. }
  53. }
  54. });
  55. while (true){
  56. if (state==1) {
  57. repaint();
  58. }
  59. //失败重启
  60. if (state==5){
  61. state=0;
  62. resetGame();
  63. }
  64. try {
  65. //1秒1000毫秒
  66. Thread.sleep(200);
  67. } catch (InterruptedException e) {
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. }
  72. @Override
  73. public void paint(Graphics g) {
  74. //双缓存图片初始化
  75. if (offScreeImage==null){
  76. offScreeImage=this.createImage(winWidth,winHeight);
  77. }
  78. Graphics gImage=offScreeImage.getGraphics();
  79. //灰色背景
  80. gImage.setColor(Color.gray);
  81. gImage.fillRect(0,0,winWidth,winHeight);
  82. gImage.setColor(Color.black);
  83. //横线x相同,y不同
  84. for (int i = 0; i <=20; i++) {
  85. //横线
  86. gImage.drawLine(0,i*30,600,i*30);
  87. //竖线
  88. gImage.drawLine(i*30,0,i*30,600);
  89. }
  90. for (int i =bodyObjList.size()-1;i>=0 ; i--) {
  91. bodyObjList.get(i).paintSelf(gImage);
  92. }
  93. headObj.paintSelf(gImage);
  94. foodObj.paintSelf(gImage);
  95. GameUtils.drawWork(gImage,score+" 分",Color.BLUE,50,650,300);
  96. gImage.setColor(Color.gray);
  97. prompt(gImage);
  98. g.drawImage(offScreeImage,0,0,null);
  99. }
  100. //绘制提示语
  101. void prompt(Graphics g){
  102. if (state==0){
  103. g.fillRect(120,240,400,70);
  104. GameUtils.drawWork(g,"按下空格健开始游戏",Color.yellow,35,150,290);
  105. }
  106. //暂停
  107. if (state==2){
  108. g.fillRect(120,240,400,70);
  109. GameUtils.drawWork(g,"游戏暂停,按空格继续",Color.yellow,35,150,290);
  110. }
  111. if (state==3){
  112. g.fillRect(120,240,400,70);
  113. GameUtils.drawWork(g,"游戏失败,按空格键重新开始 ",Color.red,35,150,290);
  114. }
  115. //通关
  116. if (state==4){
  117. g.fillRect(120,240,400,70);
  118. GameUtils.drawWork(g,"达成条件游戏通关",Color.green,35,150,290);
  119. }
  120. }
  121. //未开始
  122. //游戏重置
  123. void resetGame(){
  124. //关闭当前窗口
  125. this.dispose();
  126. //开启一个新窗口
  127. String[] args={};
  128. main(args);
  129. }
  130. public static void main(String[] args) {
  131. GameWin gameWin=new GameWin();
  132. gameWin.launch();
  133. }
  134. }

工具类GameUtils

  1. package Game.utils;
  2. import java.awt.*;
  3. public class GameUtils {
  4. public static Image image=Toolkit.getDefaultToolkit().getImage("img/img.png");
  5. public static Image image1=Toolkit.getDefaultToolkit().getImage("img/img_1.png");
  6. public static Image image2=Toolkit.getDefaultToolkit().getImage("img/img_2.png");
  7. public static Image image3=Toolkit.getDefaultToolkit().getImage("img/img_3.png");
  8. public static Image imagebody=Toolkit.getDefaultToolkit().getImage("img/img_5.png");
  9. public static Image imagefood=Toolkit.getDefaultToolkit().getImage("img/img_4.png");
  10. //绘制文字
  11. public static void drawWork(Graphics g,String str,Color color,int size,int x,int y){
  12. g.setColor(color);
  13. g.setFont(new Font("仿宋",Font.BOLD,size));
  14. g.drawString(str,x,y);
  15. }
  16. }

游戏父类GameObj

  1. package Game;
  2. import java.awt.*;
  3. public class GameObject {
  4. //图片
  5. Image img;
  6. //坐标
  7. int x;
  8. int y;
  9. //宽高
  10. int width=30;
  11. int height=30;
  12. //窗口类的引用
  13. GameWin frame;
  14. public Image getImg() {
  15. return img;
  16. }
  17. public void setImg(Image img) {
  18. this.img = img;
  19. }
  20. public int getX() {
  21. return x;
  22. }
  23. public void setX(int x) {
  24. this.x = x;
  25. }
  26. public int getY() {
  27. return y;
  28. }
  29. public void setY(int y) {
  30. this.y = y;
  31. }
  32. public int getWidth() {
  33. return width;
  34. }
  35. public void setWidth(int width) {
  36. this.width = width;
  37. }
  38. public int getHeight() {
  39. return height;
  40. }
  41. public void setHeight(int height) {
  42. this.height = height;
  43. }
  44. public GameWin getFrame() {
  45. return frame;
  46. }
  47. public void setFrame(GameWin frame) {
  48. this.frame = frame;
  49. }
  50. public GameObject(){
  51. }
  52. public GameObject(Image img, int x, int y, GameWin frame) {
  53. this.img = img;
  54. this.x = x;
  55. this.y = y;
  56. this.frame = frame;
  57. }
  58. public GameObject(Image img, int x, int y, int width, int height, GameWin frame) {
  59. this.img = img;
  60. this.x = x;
  61. this.y = y;
  62. this.width = width;
  63. this.height = height;
  64. this.frame = frame;
  65. }
  66. //绘制自身
  67. public void paintSelf(Graphics g){
  68. g.drawImage(img,x,y,null);
  69. }
  70. }

身体类BodyObj

  1. package Game;
  2. import java.awt.*;
  3. public class BodyObj extends GameObject{
  4. public BodyObj(Image image,int x,int y,GameWin frame){
  5. super(image,x,y,frame);
  6. }
  7. @Override
  8. public void paintSelf(Graphics g) {
  9. super.paintSelf(g);
  10. }
  11. }

食物类FoodObj

  1. package Game;
  2. import Game.utils.GameUtils;
  3. import java.awt.*;
  4. import java.util.Random;
  5. public class FoodObj extends GameObject{
  6. //随机函数
  7. Random r=new Random();
  8. public FoodObj(){super();}
  9. public FoodObj(Image image,int x,int y,GameWin frame){
  10. super(image,x,y,frame);
  11. }
  12. //获取食物
  13. public FoodObj getFood(){
  14. return new FoodObj(GameUtils.imagefood,r.nextInt(20)*30,(r.nextInt(19) +1)*30,this.frame);
  15. }
  16. @Override
  17. public void paintSelf(Graphics g) {
  18. super.paintSelf(g);
  19. }
  20. }

蛇头部HeadObj

  1. package Game;
  2. import Game.utils.GameUtils;
  3. import java.awt.*;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import java.util.List;
  7. public class HeadObj extends GameObject{
  8. //方向 up down left right
  9. private String direction="right";
  10. public String getDirection() {
  11. return direction;
  12. }
  13. public void setDirection(String direction) {
  14. this.direction = direction;
  15. }
  16. public void move(){
  17. //蛇身移动
  18. java.util.List<BodyObj> bodyObjList=this.frame.bodyObjList;
  19. for (int i = bodyObjList.size()-1;i>=1;i--) {
  20. bodyObjList.get(i).x=bodyObjList.get(i-1).x;
  21. bodyObjList.get(i).y=bodyObjList.get(i-1).y;
  22. //蛇头与身体的碰撞判断
  23. if (this.x==bodyObjList.get(i).x&&this.y==bodyObjList.get(i).y){
  24. //失败
  25. GameWin.state=3;
  26. }
  27. }
  28. bodyObjList.get(0).x=this.x;
  29. bodyObjList.get(0).y=this.y;
  30. //蛇头移动
  31. switch (direction){
  32. case "up":
  33. y-=height;
  34. break;
  35. case "down":
  36. y+=height;
  37. break;
  38. case "left":
  39. x-=width;
  40. break;
  41. case "right":
  42. x+=width;
  43. break;
  44. }
  45. }
  46. public HeadObj(Image img, int x, int y, GameWin frame) {
  47. super(img, x, y, frame);
  48. this.frame.addKeyListener(new KeyAdapter() {
  49. @Override
  50. public void keyPressed(KeyEvent e) {
  51. changeDirection(e);
  52. }
  53. });
  54. }
  55. public void changeDirection(KeyEvent e){
  56. switch (e.getKeyCode()){
  57. case KeyEvent.VK_A:
  58. if (!"right".equals(direction)){
  59. direction="left";
  60. img= GameUtils.image2;
  61. }
  62. break;
  63. case KeyEvent.VK_D:
  64. if (!"left".equals(direction)){
  65. direction="right";
  66. img= GameUtils.image3;
  67. }
  68. break;
  69. case KeyEvent.VK_W:
  70. if (!"down".equals(direction)){
  71. direction="up";
  72. img= GameUtils.image;
  73. }
  74. break;
  75. case KeyEvent.VK_S:
  76. if (!"up".equals(direction)){
  77. direction="down";
  78. img= GameUtils.image1;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. @Override
  86. public void paintSelf(Graphics g) {
  87. super.paintSelf(g);
  88. //蛇吃食物
  89. FoodObj food=this.frame.foodObj;
  90. Integer newX=null;
  91. Integer newY=null;
  92. if (this.x==food.x&&this.y==food.y){
  93. this.frame.foodObj=food.getFood();
  94. BodyObj lastBody=this.frame.bodyObjList.get(this.frame.bodyObjList.size()-1);
  95. newX= lastBody.x;
  96. newY= lastBody.y;
  97. //分数+1
  98. this.frame.score++;
  99. }
  100. //通关判断
  101. if (this.frame.score>=15){
  102. //通过
  103. GameWin.state=4;
  104. }
  105. move();
  106. if (newX!=null&& newY!=null){
  107. this.frame.bodyObjList.add(new BodyObj(GameUtils.imagebody,newX,newY,this.frame));
  108. }
  109. //越界
  110. if (x<0){
  111. x=570;
  112. }else if (x>570){
  113. x=0;
  114. }else if (y<30){
  115. y=570;
  116. } else if (y>570) {
  117. y=30;
  118. }
  119. }
  120. }

图片资源

最后希望大家多多点赞支持小刘

img.png img_1.png-----img_5png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/699426
推荐阅读
相关标签
  

闽ICP备14008679号