当前位置: 首页 > news >正文

贪吃蛇的java代码实现

实验六:贪吃蛇


bodyObj

package snake; import java.awt.*; public class bodyObj extends GameObj { public bodyObj(Image imd, int x, int y, GameWin frame) { super(imd, x, y, frame); } public void paintSelf(Graphics g) { super.paintSelf(g); } }

FoodObj

package snake; import java.awt.*; import java.util.Random; public class FoodObj extends GameObj { Random r = new Random(); public FoodObj() { super(); } public FoodObj(Image img,int x,int y,GameWin frame) { super(img,x,y,frame); } public FoodObj getFood(){ return new FoodObj(GameUtils.Fimg,r.nextInt(20)*30,(r.nextInt(19)+1)*30,this.frame); } public void paintSelf(Graphics g) { super.paintSelf(g); } }

GameObj

package snake; import java.awt.*; public class GameObj { Image img; int x; int y; int w=30; int h=30; GameWin frame; public Image getImg(){ return img; } public void setImg(Image img){ this.img = img; } public int getX(){ return x; } public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } public int getY(){ return y; } public void setW(int w){ this.w = w; } public int getW(){ return w; } public void setH(int h){ this.h = h; } public int getH(){ return h; } public void setFrame(GameWin frame){ this.frame = frame; } public GameWin getFrame(){ return frame; } public GameObj(){ } public GameObj(Image img,int x, int y, GameWin frame){ this.img = img; this.x = x; this.y = y; this.frame = frame; } public GameObj(Image img,int x, int y, int w, int h, GameWin frame){ this.img = img; this.x = x; this.y = y; this.w = w; this.h = h; this.frame = frame; } public void paintSelf(Graphics g){ g.drawImage(img,x,y,frame); } }

GameUtils

e snake; import java.awt.*; public class GameUtils { public static Image Uimg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\up.png"); public static Image Dimg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\down.png"); public static Image Limg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\left.png"); public static Image Rimg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\right.png"); public static Image Bimg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\body.png"); public static Image Fimg=Toolkit.getDefaultToolkit().getImage("C:\\Users\\86151\\Desktop\\snakeT\\food.png");

GameWin

public class GameWin extends Frame { Image offScreenImage=null; public static int state=0;//0未开始,1游戏中,2暂停 HeadObj headObj=new HeadObj(GameUtils.Rimg,60,60,this); public List<bodyObj> bodyObjList=new ArrayList<>(); //Random r = new Random(); FoodObj foodObj=new FoodObj().getFood(); public void Launch(){ this.setVisible(true); this.setSize(600,600); this.setLocationRelativeTo(null); this.setTitle("贪吃蛇"); bodyObjList.add(new bodyObj(GameUtils.Bimg,30,60,this)); bodyObjList.add(new bodyObj(GameUtils.Bimg,0,60,this)); this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_SPACE){ switch(state) { case 0://未开始 state = 1; break; case 1://游戏中 state = 2;//暂停 break; case 2://暂停 state = 1; break; case 3: state = 5; break; default: break; } } } }); while(true){ if (state==1){ repaint(); } if (state==5){ state=0; resetGame(); } try{ Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public void drawWord(Graphics g,String str,Color color,int size,int x,int y){ g.setColor(color); g.setFont(new Font("宋体",Font.BOLD,size)); g.drawString(str,x,y); } void prompt(Graphics g) { if (state == 0) { g.fillRect(120, 240,400, 70); drawWord(g, "按空格开始游戏", Color.BLUE, 35, 150, 290); } if (state == 3) { drawWord(g, "蛇头和身体碰撞, 失败", Color.RED, 35, 150, 290); } } void resetGame(){ this.dispose(); String[] args = {}; main(args); } public void paint(Graphics g){ if(offScreenImage==null){ offScreenImage=this.createImage(600,600); } Graphics gImage=offScreenImage.getGraphics(); //绘制网格 gImage.setColor(Color.gray); gImage.fillRect(0, 0, this.getWidth(), this.getHeight());//灰色背景 gImage.setColor(Color.black); //g.drawLine(0,60,600,60); for(int i=0;i<=20;i++){ gImage.drawLine(0,i*30,600,i*30);//横线 gImage.drawLine(i*30,0,i*30,600);//竖线 } for(int i=bodyObjList.size()-1;i>=0;i--){ bodyObjList.get(i).paintSelf(gImage); } headObj.paintSelf(gImage); foodObj.paintSelf(gImage); g.drawImage(offScreenImage,0,0,this); g.setColor(Color.YELLOW); prompt(g); } public static void main(String[] args) { GameWin gamewin=new GameWin(); gamewin.Launch(); } }

HeadObj

public class HeadObj extends GameObj { private String direction="right"; public String getDirection(){ return direction; } public void setDirection(String direction){ this.direction = direction; } public HeadObj(Image image, int x, int y, GameWin frame){ super(image, x, y, frame); this.frame.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ changeDirection(e); } }); } public void changeDirection(KeyEvent e){ switch(e.getKeyCode()) { case KeyEvent.VK_RIGHT: direction = "right"; img = GameUtils.Rimg; break; case KeyEvent.VK_LEFT: direction = "left"; img = GameUtils.Limg; break; case KeyEvent.VK_UP: direction = "up"; img = GameUtils.Uimg; break; case KeyEvent.VK_DOWN: direction = "down"; img = GameUtils.Dimg; break; default: break; } } public void move(){ List<bodyObj> bodyObjList=this.frame.bodyObjList; for(int i=bodyObjList.size()-1;i>=1;i--){ bodyObjList.get(i).x=bodyObjList.get(i-1).x; bodyObjList.get(i).y=bodyObjList.get(i-1).y; if(this.x==bodyObjList.get(i).x && this.y==bodyObjList.get(i).y){ GameWin.state=3; } } bodyObjList.get(0).x=this.x; bodyObjList.get(0).y=this.y; switch(direction) { case "right": x = x + w; break; case "left": x = x - w; break; case "up": y = y - w; break; case "down": y = y + w; break; default: break; } } @Override

运行结果

http://www.jsqmd.com/news/89138/

相关文章:

  • 随机抽奖算法实现与对比:聚焦洗牌算法(Fisher-Yates)
  • 打开软件出现找不到vcruntime140d.dll文件的情况 下载修复解决
  • 外卖订餐管理系统
  • Level 2 → Level 3
  • 软件缺少vbschs.dll文件 无法启动运行的情况 下载修复
  • Flutter 混合开发实战:从 Add-to-App 到高性能双向通信的全栈集成方案
  • 外设与接口:基于内核 gpio-keys 子系统的按键处理
  • sglang 大模型推理框架支持的EAGLE 1,2,3
  • 延凡科技 EMS 智慧云平台:3 万起订阅,中小用能单位的 “云端全能源管家”
  • 拦截器注册InterceptorRegistry 实现讲解
  • 汇编语言全接触-27.工具提示控件
  • 汇编语言全接触-26.启动画面
  • 我不是这样
  • 验证IP地址(一)
  • 医院管理|基于springboot 医院管理系统(源码+数据库+文档)
  • 浅谈:算法中的斐波那契数(一)
  • 测试的“元认知”:智能体如何评估自身可靠性?
  • 10.8 总结
  • 【Hadoop+Spark+python毕设】物联网网络安全威胁数据分析系统、计算机毕业设计、包括数据爬取、数据分析、数据可视化、Hadoop、实战教学
  • 9.28总结
  • 零基础学JAVA--Day34(Map接口+HashTable+HashMap+TreeSet+TreeMap+开发中如何选择集合实现类?(重要)) - 指南
  • 电影院购票|基于springboot 电影院购票系统(源码+数据库+文档)
  • C#+VisionMaster联合开发(二)_操作流程
  • 本地部署DeepSeek
  • AI驱动的手动测试变革:赋能而非替代
  • 航空机票预定系统|基于springboot 航空机票预定系统(源码+数据库+文档)
  • [Windows] 剪映自动预合成v1.0
  • 低代码平台的测试挑战:测试从业者的新战场
  • Go项目发布到Go官方仓库完整指南
  • 智能测试用例生成技术探秘