博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪吃蛇
阅读量:5820 次
发布时间:2019-06-18

本文共 4130 字,大约阅读时间需要 13 分钟。

@SuppressWarnings("serial")public class MainClass extends JFrame { ControlSnake control; Toolkit kit; Dimension dimen; public static void main(String[] args) {  new MainClass("my snake"); } public MainClass(String s) {  super(s);  control = new ControlSnake();  control.setFocusable(true);  kit = Toolkit.getDefaultToolkit();  dimen = kit.getScreenSize();  add(control);  setLayout(new BorderLayout());  setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3  setSize(FWIDTH, FHEIGHT);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  setResizable(false);  setVisible(true); } public static final int FWIDTH = 315; public static final int FHEIGHT = 380;}import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.Timer;import java.util.Random;@SuppressWarnings("serial")public class ControlSnake extends JPanel implements ActionListener { Random rand; ArrayList
list, listBody; String str, str1; static boolean key; int x, y, dx, dy, fx, fy, flag; int snakeBody; int speed; public ControlSnake() { snakeBody = 1; str = "上下左右方向键控制 P键暂停..."; str1 = "现在的长度为:" + snakeBody; key = true; flag = 1; speed = 700; rand = new Random(); list = new ArrayList
(); listBody = new ArrayList
(); x = 5; y = 5; list.add(new Point(x, y)); listBody.add(list.get(0)); dx = 10; dy = 0; fx = rand.nextInt(30) * 10 + 5;// 2 fy = rand.nextInt(30) * 10 + 5;// 2 setBackground(Color.WHITE); setSize(new Dimension(318, 380)); final Timer time = new Timer(speed, this); time.start(); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 37) { dx = -10; dy = 0; } else if (e.getKeyCode() == 38) { dx = 0; dy = -10; } else if (e.getKeyCode() == 39) { dx = 10; dy = 0; } else if (e.getKeyCode() == 40) { dx = 0; dy = 10; } else if (e.getKeyCode() == 80) { if (flag % 2 == 1) { time.stop(); } if (flag % 2 == 0) { time.start(); } flag++; } } }); } public void paint(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, 400, 400); g.setColor(Color.DARK_GRAY); g.drawLine(3, 3, 305, 3); g.drawLine(3, 3, 3, 305); g.drawLine(305, 3, 305, 305); g.drawLine(3, 305, 305, 305); g.setColor(Color.PINK); for (int i = 0; i < listBody.size(); i++) { g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9); } g.fillRect(x, y, 9, 9); g.setColor(Color.ORANGE); g.fillRect(fx, fy, 9, 9); g.setColor(Color.DARK_GRAY); str1 = "现在的长度为:" + snakeBody; g.drawString(str, 10, 320); g.drawString(str1, 10, 335); } public void actionPerformed(ActionEvent e) { x += dx; y += dy; if (makeOut() == false) { JOptionPane.showMessageDialog(null, "重新开始......"); speed = 700; snakeBody = 1; x = 5; y = 5; list.clear(); list.add(new Point(x, y)); listBody.clear(); listBody.add(list.get(0)); dx = 10; dy = 0; } addPoint(x, y); if (x == fx && y == fy) { speed = (int) (speed * 0.8);//速度增加参数 if (speed < 200) { speed = 100; } fx = rand.nextInt(30) * 10 + 5;// 2 fy = rand.nextInt(30) * 10 + 5;// 2 snakeBody++;// 2 } // 2 repaint(); } public void addPoint(int xx, int yy) { // 动态的记录最新发生的50步以内的移动过的坐标 // 并画出最新的snakeBody if (list.size() < 100) {
//蛇身长度最长为100 list.add(new Point(xx, yy)); } else { list.remove(0); list.add(new Point(xx, yy)); } if (snakeBody == 1) { listBody.remove(0); listBody.add(0, list.get(list.size() - 1)); } else { listBody.clear(); if (list.size() < snakeBody) { for (int i = list.size() - 1; i > 0; i--) { listBody.add(list.get(i)); } } else { for (int i = list.size() - 1; listBody.size() < snakeBody; i--) { listBody.add(list.get(i)); } } } } public boolean makeOut() { if ((x < 3 || y < 3) || (x > 305 || y > 305)) { return false; } for (int i = 0; i < listBody.size() - 1; i++) { for (int j = i + 1; j < listBody.size(); j++) { if (listBody.get(i).equals(listBody.get(j))) { return false; } } } return true; }}

 

转载于:https://www.cnblogs.com/cczzww/p/5782053.html

你可能感兴趣的文章
CodeIgniter 3.0 新手捣鼓源码(一) base_url()
查看>>
Chrome 广告屏蔽功能不影响浏览器性能
查看>>
vSphere 6将于2月2日全球同步发表
查看>>
Android状态栏实现沉浸式模式
查看>>
让你的APP实现即时聊天功能
查看>>
iOS 绝对路径和相对路径
查看>>
使用Openfiler搭建ISCSI网络存储
查看>>
学生名单
查看>>
(转) 多模态机器翻译
查看>>
【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
查看>>
矩阵常用归一化
查看>>
Oracle常用函数总结
查看>>
【聚能聊有奖话题】Boring隧道掘进机完成首段挖掘,离未来交通还有多远?
查看>>
考研太苦逼没坚持下来!看苑老师视频有点上头
查看>>
HCNA——RIP的路由汇总
查看>>
zabbix监控php状态(四)
查看>>
实战Django:小型CMS Part2
查看>>
原创]windows server 2012 AD架构试验系列 – 16更改DC计算机名
查看>>
统治世界的十大算法
查看>>
linux svn安装和配置
查看>>