java贪食蛇源代码分享
java
<code class="language-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; /** * @url http://download.csdn.net/detail/leasystu/4308246 * * @author Easy */ 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 = 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 + 15 * 35 - nodeTempNode.height) { nodeTempNode = new Node(nodeTempNode.x, 40 - nodeTempNode.height); } nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height)); break; } } }</node></node></code>
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
4 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
4 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处
