news 2026/6/21 14:05:33

贪吃蛇的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/6/15 14:22:20

事务操作流程

1.开启事务&#xff1a;明确事物的起始点&#xff08;begin或者start transaction&#xff09;2.执行操作&#xff1a;执行SQL语句3.判断结果&#xff1a;若操作都执行成功&#xff0c;提交事务&#xff0c;修改生成若任意操作失败&#xff0c;回滚事务&#xff0c;撤销所有已经…

作者头像 李华
网站建设 2026/6/20 11:27:43

打开软件出现找不到vcomp140.dll文件 无法运行的情况 下载修复解决

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

作者头像 李华
网站建设 2026/6/19 21:50:23

汇编语言全接触-27.工具提示控件

我们将学习工具提示控件:它是什么如何创建和使用.下载例子理论:工具提示是当鼠标在某特定区域上停留时显示的一个矩形窗口.工具提示窗口包含一些编程者想要显示的文本.在这点上,工具提示同状态栏的作用是一样的,所不同的是工具提示当单击或者远离指定区域的时候就会消逝,你可能…

作者头像 李华
网站建设 2026/6/18 18:26:04

测试左移:构建软件质量的早期防线

在快速迭代的现代软件开发周期中&#xff0c;缺陷发现的时机直接影响项目成本、发布节奏与最终用户体验。传统软件测试模式中&#xff0c;测试活动往往集中于开发后期&#xff0c;导致缺陷修复成本高昂、返工风险加剧。测试左移作为一种前瞻性质量保障策略&#xff0c;通过将测…

作者头像 李华
网站建设 2026/6/17 8:27:19

串口通讯的android 封装开箱即用!提供源代码!

功能概述 本文档总结了在Android应用中使用serialportlibrary实现串口通讯功能的完整过程。通过本次开发&#xff0c;成功添加了以下核心功能&#xff1a; 串口设备的打开与关闭 数据的发送与接收 用户友好的操作界面 实现细节 1. UI界面修改 在activity_main.xml中添加…

作者头像 李华