news 2026/2/24 23:37:09

贪吃蛇的java代码实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
贪吃蛇的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

运行结果

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/23 19:27:45

燕麦矮砧密植:水肥一体化系统的铺设要点

燕麦地里&#xff0c;老刘的燕麦长势整齐&#xff0c;穗大粒饱。"这套水肥系统真是帮了大忙&#xff0c;"他指着田间的滴灌设备说&#xff0c;"不仅省水省肥&#xff0c;产量还提高了三成。"认识燕麦矮砧密植燕麦矮砧密植&#xff0c;简单来说就是选用矮秆…

作者头像 李华
网站建设 2026/2/20 22:50:25

云南昆明/南宁/海南海口品牌快闪店设计搭建公司哪家好?

在消费升级与商业创新双重驱动下&#xff0c;国内城市核心商圈正涌现出一批以短期空间运营为特色的新型商业实践。这类空间通过主题化场景构建、限时性体验设计以及社交化互动机制&#xff0c;形成了独特的商业空间运营模式。其凭借差异化的内容呈现与精准的受众定位&#xff0…

作者头像 李华
网站建设 2026/2/21 21:02:33

外设与接口:基于内核 gpio-keys 子系统的按键处理

1 基本原理 在 Linux 中&#xff0c;gpio-keys 是一个平台驱动&#xff08;Platform Driver&#xff09;&#xff0c;它充当了物理 GPIO 硬件与 Linux 标准输入子系统&#xff08;Input Subsystem&#xff09;之间的“翻译官”。 整个处理流程自下而上分为四层&#xff1a; 硬件…

作者头像 李华
网站建设 2026/2/22 10:45:00

测试的“元认知”:智能体如何评估自身可靠性?

在软件测试领域&#xff0c;自动化与智能化正以前所未有的速度重塑工作流程。随着人工智能代理&#xff08;智能体&#xff09;广泛应用于测试用例生成、缺陷预测和持续集成&#xff0c;一个关键问题浮出水面&#xff1a;这些智能体如何像人类测试专家一样&#xff0c;对自身行…

作者头像 李华
网站建设 2026/2/18 7:42:13

本凡码农引领杭州小程序开发解决方案赋能企业创新与发展

本凡码农的杭州小程序开发解决方案为企业提供了一种高效的数字化转型工具。我们的目标是帮助品牌快速适应市场变化&#xff0c;提升用户体验。通过定制化的小程序&#xff0c;企业能够实现从线上到线下的无缝连接&#xff0c;简化业务流程&#xff0c;从而更好地满足用户需求。…

作者头像 李华
网站建设 2026/2/23 11:36:48

Windows11系统文件wer.dll丢失或损坏问题 下载修复

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华