java贪吃蛇求一个java的贪吃蛇程序(打包),发我

java贪吃蛇  时间:2021-07-10  阅读:()

求贪吃蛇JAVA代码

import?java.awt.Color;?? import?java.awt.Graphics;?? import?java.awt.Graphics2D;?? import?java.awt.Rectangle;?? import?java.awt.event.KeyAdapter;?? import?java.awt.event.KeyEvent;?? import?java.awt.image.BufferedImage;?? import?java.util.ArrayList;?? import?java.util.List;?? import?javax.swing.JFrame;?? public?class?InterFace?extends?JFrame?{?? ????public?static?final?int?WIDTH?=?800,?HEIGHT?=?600,?SLEEPTIME?=?20,?L?=?1,R?=?2,?U?=?3,?D?=?4;?? ????BufferedImage?offersetImage=?new?BufferedImage(WIDTH,?HEIGHT,BufferedImage.TYPE_3BYTE_BGR);;?? ????Rectangle?rect?=?new?Rectangle(20,?40,?15?*?50,?15?*?35);?? ????Snake?snake;?? ????Node?node;?? ????public?InterFace()?{?? ????????snake?=?new?Snake(this);?? ????????createNode();?? ????????this.setBounds(100,?100,?WIDTH,?HEIGHT);?? ????????this.addKeyListener(new?KeyAdapter()?{?? ????????????public?void?keyPressed(KeyEvent?arg0)?{?? ????????????????System.out.println(arg0.getKeyCode());?? ????????????????switch?(arg0.getKeyCode())?{?? ????????????????case?KeyEvent.VK_LEFT:?? ????????????????????snake.dir?=?L;?? ????????????????????break;?? ????????????????case?KeyEvent.VK_RIGHT:?? ????????????????????snake.dir?=?R;?? ????????????????????break;?? ????????????????case?KeyEvent.VK_UP:?? ????????????????????snake.dir?=?U;?? ????????????????????break;?? ????????????????case?KeyEvent.VK_DOWN:?? ????????????????????snake.dir?=?D;?? ????????????????}?? ????????????}?? ????????});?? ????????this.setTitle("贪吃蛇?0.1???By?:?Easy");?? ????????this.setDefaultCloseOperation(EXIT_ON_CLOSE);?? ????????this.setVisible(true);?? ????????new?Thread(new?ThreadUpadte()).start();?? ????}?? ????public?void?paint(Graphics?g)?{?? ????????Graphics2D?g2d?=?(Graphics2D)?offersetImage.getGraphics();?? ????????g2d.setColor(Color.white);?? ????????g2d.fillRect(0,?0,?WIDTH,?HEIGHT);?? ????????g2d.setColor(Color.black);?? ????????g2d.drawRect(rect.x,?rect.y,?rect.width,?rect.height);?? ????????if?(snake.hit(node))?{?? ????????????createNode();?? ????????}?? ????????snake.draw(g2d);?? ????????node.draw(g2d);?? ????????g.drawImage(offersetImage,?0,?0,?null);?? ????}?? ????class?ThreadUpadte?implements?Runnable?{?? ????????public?void?run()?{?? ????????????while?(true)?{?? ????????????????try?{?? ????????????????????Thread.sleep(SLEEPTIME);?? ????????????????????repaint();?? ????????????????}?catch?(InterruptedException?e)?{?? ????????????????????e.printStackTrace();?? ????????????????}?? ????????????}?? ????????}?? ????}?? ????public?void?createNode()?{?? ????????int?x?=?(int)?(Math.random()?*?650)?+?50,y?=?(int)?(Math.random()?*?500)?+?50;?? ????????Color?color?=?Color.blue;?? ????????node?=?new?Node(x,?y,?color);?? ????}?? ????public?static?void?main(String?args[])?{?? ????????new?InterFace();?? ????}?? }?? class?Node?{?? ????int?x,?y,?width?=?15,?height?=?15;?? ????Color?color;?? ????public?Node(int?x,?int?y,?Color?color)?{?? ????????this(x,?y);?? ????????this.color?=?color;?? ????}?? ????public?Node(int?x,?int?y)?{?? ????????this.x?=?x;?? ????????this.y?=?y;?? ????????this.color?=?color.black;?? ????}?? ????public?void?draw(Graphics2D?g2d)?{?? ????????g2d.setColor(color);?? ????????g2d.drawRect(x,?y,?width,?height);?? ????}?? ????public?Rectangle?getRect()?{?? ????????return?new?Rectangle(x,?y,?width,?height);?? ????}?? }?? class?Snake?{?? ????public?List<Node>?nodes?=?new?ArrayList<Node>();?? ????InterFace?interFace;?? ????int?dir=InterFace.R;?? ????public?Snake(InterFace?interFace)?{?? ????????this.interFace?=?interFace;?? ????????nodes.add(new?Node(20?+?150,?40?+?150));?? ????????addNode();?? ????}?? ????public?boolean?hit(Node?node)?{?? ????????for?(int?i?=?0;?i?<?nodes.size();?i++)?{?? ????????????if?(nodes.get(i).getRect().intersects(node.getRect()))?{?? ????????????????addNode();?? ????????????????return?true;?? ????????????}?? ????????}?? ????????return?false;?? ????}?? ????public?void?draw(Graphics2D?g2d)?{?? ????????for?(int?i?=?0;?i?<?nodes.size();?i++)?{?? ????????????nodes.get(i).draw(g2d);?? ????????}?? ????????move();?? ????}?? ????public?void?move()?{?? ????????nodes.remove((nodes.size()?-?1));?? ????????addNode();?? ????}?? ????public?synchronized?void?addNode()?{?? ????????Node?nodeTempNode?=?nodes.get(0);?? ????????switch?(dir)?{?? ????????case?InterFace.L:?? ????????????if?(nodeTempNode.x?<=?20)?{?? ????????????????nodeTempNode?=?new?Node(20?+?15?*?50,?nodeTempNode.y);?? ????????????}?? ????????????nodes.add(0,?new?Node(nodeTempNode.x?-?nodeTempNode.width,?? ????????????????????nodeTempNode.y));?? ????????????break;?? ????????case?InterFace.R:?? ????????????if?(nodeTempNode.x?>=?20?+?15?*?50?-?nodeTempNode.width)?{?? ????????????????nodeTempNode?=?new?Node(20?-?nodeTempNode.width,?nodeTempNode.y);?? ????????????}?? ????????????nodes.add(0,?new?Node(nodeTempNode.x?+?nodeTempNode.width,?? ????????????????????nodeTempNode.y));?? ????????????break;?? ????????case?InterFace.U:?? ????????????if?(nodeTempNode.y?<=?40)?{?? ????????????????nodeTempNode?=?new?Node(nodeTempNode.x,?40?+?15?*?35);?? ????????????}?? ????????????nodes.add(0,?new?Node(nodeTempNode.x,?nodeTempNode.y?-?nodeTempNode.height));?? ????????????break;?? ????????case?InterFace.D:?? ????????????if?(nodeTempNode.y?>=?40?+?15?*?35?-?nodeTempNode.height)?{?? ????????????????nodeTempNode?=?new?Node(nodeTempNode.x,40?-?nodeTempNode.height);?? ????????????}?? ????????????nodes.add(0,?new?Node(nodeTempNode.x,?nodeTempNode.y?+?nodeTempNode.height));?? ????????????break;?? ????????}?? ????}?? }

用Java语言写一个约1500行代码的贪吃蛇游戏

Runnable } if (i.util.Date.start();args) { Thread new Thread(new Thread1());0;one = new Thread(new Thread2()).printStackTrace(); public if (i % one.start(); two;class Thread2 Thread implements class Thread1 { while(true){ i++; System.out.println(new i = run() { while (true) { two = Date().toLocaleString()); } catch try { Thread.sleep(10000);Runnable { break; } } } } import java;Client { public static void main(String[] ); public void run() } } } }<pre t="code" l="java">public class 4 == 0) { System.out.println(;*******<pre t="code" l="java">public implements { private int (InterruptedException e) { e;{ public void 100)nbsp

求一个java的贪吃蛇程序(打包),发我

.tarena.elts.test; import java.awt.Color; import java.awt.Font; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class WormFrame extends JFrame{ private static final long serialVersionUID = 1L; public static final int UP = -10; public static final int DOWN = 10; public static final int LEFT = -200; public static final int RIGHT = 200; private int speed = 200;//蛇的移动速度,越小越快 private JPanel jPanel=null;//游戏面板 private JLabel jLabel=null;//显示游戏结束的标签 private JButton reset = null;//从新开始游戏的按钮 private JButton control = null;//控制方向的按钮 private List worm = new ArrayList();//贪吃蛇 //将整个面板划分成节点,蛇走到那里,就那整个节点点亮 private Map nodes = new HashMap(); private int dir= LEFT;//方向 private Point food;//食物 private boolean isContinue=false;//判断蛇是否行动的标记 public static void main(String[] args) { new WormFrame(); } public WormFrame() { initialize(); start(); } //游戏初始化 private void initialize() { this.setSize(500, 500); this.setLocation(250, 100); this.setResizable(false); this.add(getJPanel());//添加面板 this.setTitle("贪吃蛇,空格键暂停,回车开始"); this.setVisible(true); } //游戏开始 private void start() { isContinue = true; while (true) { while (isContinue) { try { Point p = worm.get(0); int x = (int) p.getX() + dir / 20; int y = (int) p.getY() + dir % 100; if (food.equals(new Point(x, y))) { worm.add(0, food); if(worm.size()%1==0){ speed-=10; } getFood(); continue; } p = new Point(x, y); if(worm.contains(p)){ throw new Exception(); } nodes.get(p).setVisible(false); worm.add(0, p); nodes.get(worm.remove(worm.size() - 1)).setVisible(true); Thread.sleep(speed); } catch (Exception e) { jLabel.setVisible(true); reset.setVisible(true); //不停止内循环,jLabel和reset不消失 isContinue = false; } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } //游戏从新开始 private void reset(){ worm = new ArrayList(); for(Point p :nodes.keySet()){ nodes.get(p).setVisible(false); } addWorm(); dir = LEFT; getFood(); isContinue =true; } //获取游戏面板的方法,面板中有记录游戏时间的标签, //代表游戏角色的按钮和 重新开始游戏的按钮 private JPanel getJPanel() { if (jPanel == null) { //显示游戏结束的标签 getOver(); //从新开始的按钮 getReset(); //控制按钮 getControl(); //游戏面板 jPanel = new JPanel(); jPanel.setLayout(null);//设置面板布局为空 //jPanel.setForeground(new Color(255,255,255));//设置面板前景色 jPanel.setBackground(new Color(0,0,0));//设置面板背景色 jPanel.add(jLabel,null);//面板中添加 显示游戏结束的标签 jPanel.add(reset,null);//面板中添加 从新开始 的按钮 jPanel.add(control,null); for(int i = 0;i<490;i+=10){ for (int j = 0; j < 470; j+=10) { JButton jb = new JButton(); Point p = new Point(i,j); jb.setBounds(i,j, 10, 10); jb.setBackground(new Color(255,255,255)); jb.setEnabled(false); //jb.setVisible(true); nodes.put(p, jb); jPanel.add(jb,null); } } addWorm();//添加一条蛇 getFood();//食物按钮 jPanel.setVisible(true);//设置面板可见 } return jPanel; } //游戏结束标签 private void getOver() { jLabel = new JLabel(); jLabel.setBounds(170, 200, 200, 50);//设置标签大小和位置 jLabel.setFont(new Font("Dialog", Font.BOLD, 24));//设置标签字体 jLabel.setForeground(new Color(250, 2, 2));//设置标签前景颜色 jLabel.setText(" 游戏结束"); jLabel.setEnabled(true);//设置此标签可用 jLabel.setVisible(false);//设置此标签不可见 } //从新开始按钮 private void getReset() { if (reset == null) { reset = new JButton();//新建一个按钮 reset.setBounds(170, 300, 164, 51);//设置按钮的大小 reset.setText("重新开始");//设置按钮的内容 reset.setVisible(false);//设置按钮不可见 //添加按钮的时间监听器 reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset.setVisible(false);// 点击重新开始按钮后,按钮消失 jLabel.setVisible(false);// 记录游戏时间的按钮也消失 try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } reset(); } }); } } //控制方向的按钮 private void getControl(){ if(control == null){ control = new JButton(); control.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()){ case KeyEvent.VK_LEFT :chDir(LEFT);break; case KeyEvent.VK_RIGHT :chDir(RIGHT);break; case KeyEvent.VK_UP :chDir(UP);break; case KeyEvent.VK_DOWN :chDir(DOWN);break; case KeyEvent.VK_ENTER:isContinue = true;break; case KeyEvent.VK_SPACE:isContinue = false;break; } } }); } } //生成食物 private void getFood(){ Random ran = new Random(); //横坐标最大480,纵坐标最大460 int x = ran.nextInt(49)*10; int y = ran.nextInt(47)*10; food = new Point(x,y); Set set = new HashSet(worm); while(set.contains(food)){ x = ran.nextInt(50)*10; y = ran.nextInt(50)*10; food = new Point(x,y); } nodes.get(food).setVisible(false); } //改变方向 private void chDir(int dir){ if(this.dir+dir!=0){ this.dir = dir; } } //添加Worm的方法 private void addWorm(){ for(int i = 250;i<300;i+=10){ Point p = new Point(i,250); worm.add(p); nodes.get(p).setVisible(true); } } } //给分

Dynadot多种后缀优惠域名优惠码 ,.COM域名注册$6.99

Dynadot 是一家非常靠谱的域名注册商家,老唐也从来不会掩饰对其的喜爱,目前我个人大部分域名都在 Dynadot,还有一小部分在 NameCheap 和腾讯云。本文分享一下 Dynadot 最新域名优惠码,包括 .COM,.NET 等主流后缀的优惠码,以及一些新顶级后缀的优惠。对于域名优惠,NameCheap 的新后缀促销比较多,而 Dynadot 则是对于主流后缀的促销比较多,所以可以各取所...

弘速云20.8元/月 ,香港云服务器 2核 1g 10M

弘速云元旦活动本公司所销售的弹性云服务器、虚拟专用服务器(VPS)、虚拟主机等涉及网站接入服务的云产品由具备相关资质的第三方合作服务商提供官方网站:https://www.hosuyun.com公司名:弘速科技有限公司香港沙田直营机房采用CTGNET高速回国线路弹性款8折起优惠码:hosu1-1 测试ip:69.165.77.50​地区CPU内存硬盘带宽价格购买地址香港沙田2-8核1-16G20-...

Krypt($120/年),2vCPU/2GB/60GB SSD/3TB

Krypt这两天发布了ION平台9月份优惠信息,提供一款特选套餐年付120美元(原价$162/年),开设在洛杉矶或者圣何塞机房,支持Windows或者Linux操作系统。ion.kryptcloud.com是Krypt机房上线的云主机平台,主要提供基于KVM架构云主机产品,相对于KT主站云服务器要便宜很多,产品可选洛杉矶、圣何塞或者新加坡等地机房。洛杉矶机房CPU:2 cores内存:2GB硬盘:...

java贪吃蛇为你推荐
showwindowShowWindow和EnableWindow区别oracle索引如何在ORACLE数据库的字段上建立索引layout_gravityandroid 布局中 为什么能够通过android:layout_above 、android:layout_alignTop 、等 还要在之前加入jdk6JDK6和JDK7两个版本有什么区别,初学者选那个好?调度系统配送调度系统是干嘛的?是手机还是电脑的系统?radius认证电信或网通的RADIUS认证都记录些什么?谁能说说ISP的宽带帐号检查流程民生电商民生电商是民生银行吗?smartupload为什么使用smartupload执行上传保存操作时用这句smart.save("upload")失败用smart.save("/upload")成功弹幕网站A站B站网址是什么,国内很出名嗎?有什么网站特点..cf加速器玩cf ping高用什么加速器比较好
国内最好的虚拟主机 godaddy域名注册 3322动态域名注册 国外vps租用 本网站服务器在美国维护 liquidweb 主机 美国主机评论 服务器日志分析 web服务器架设软件 免费ftp空间申请 美国十次啦服务器 腾讯云分析 服务器维护方案 cdn联盟 个人免费主页 1元域名 空间登陆首页 空间登入 shuang12 更多