Day02 骚年,玩蛇吗?(制作小游戏贪吃蛇)
来源:互联网 发布:彩票虚拟关注软件 编辑:程序博客网 时间:2024/04/30 13:33
花了两天时间,终于让我的蛇跑起来了,第一次自己独立做成功一个小游戏,感觉很有成就感。嘻嘻嘻嘻嘻嘻~~~
附上实现后的图,可以玩,但还有些细节需要改进,比如记分,暂停,重玩等功能暂时还没实现,以后会改进的
我说一下思路和步骤
第一步:JFrame画出窗口
第二步:重写paint方法,用来画出地图界限、球和蛇
第三步:初始化 init() 设置球和蛇的初始位置坐标
第四部:写内部类move,实现线程。让蛇跑起来
第五步:游戏规则,蛇不能撞墙,不能缠着自己。否则游戏结束GG。蛇吃到球变长,同时会在随机位置产生新的球。
第六步:switch实现键盘操作 addKeyListenner()
这里最重要的一个核心思想就是 线程
写一个死循环while(true),然后把画的操作都放在循环里面,这样游戏就可以一直玩。
但是每循环一次,要让线程歇一会再继续跑,这里就用到了Thread.sleep(time);
这样就实现了让蛇以稳定速度前行。
package com.java1997;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Point;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.LinkedList;import javax.swing.JFrame;import javax.swing.JOptionPane;public class Snake extends JFrame{private Point point=new Point();private LinkedList<Point> list=new LinkedList<Point>();private int time;private int key;//初始化private void init(){//食物初始位置point.setLocation(100, 100);//蛇初始位置list.add(new Point(300,300));list.add(new Point(310,300));list.add(new Point(320,300));list.add(new Point(330,300));list.add(new Point(340,300));list.add(new Point(350,300));list.add(new Point(360,300));list.add(new Point(370,300));list.add(new Point(380,300));time=100;key=37;}@Overridepublic void paint(Graphics g) {Image img = createImage(500, 500);Graphics g1=img.getGraphics();//画背景g1.setColor(Color.BLACK);g1.fillRect(0, 0, 500, 500);//线框g1.translate(50, 50);g1.setColor(Color.WHITE);g1.drawRect(0, 0, 400, 400); //线条用draw,填满用fill//食物g1.setColor(Color.CYAN);g1.fillOval(point.x, point.y, 10, 10);//蛇g1.setColor(Color.PINK);for(Point p:list){g1.fillRect(p.x, p.y, 10, 10);}g.drawImage(img, 0, 0, 500, 500, this);}public Snake(){this.setTitle("玩蛇骚年"); //标题this.setSize(500,500); //大小this.setVisible(true); //可见this.setLocationRelativeTo(null); //居中this.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e){if(e.getKeyCode()>=37 && e.getKeyCode()<=40){if(Math.abs(e.getKeyCode()-key)!=2){key=e.getKeyCode();}}}});init();new Thread(new Move()).start();}//实现线程class Move implements Runnable{public void run() {int i=0;//记步数while(true){try {System.out.println(i+"~~~");Thread.sleep(time);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//获取蛇头Point p = list.getFirst().getLocation();switch (key) {case 37:p.x=p.x-10;break;case 38:p.y=p.y-10;break;case 39:p.x=p.x+10;break;case 40:p.y=p.y+10;break;}//蛇不能撞墙,不能撞自己System.out.println(p.x+","+p.y);if(p.x<0||p.x>390||p.y<0||p.y>390||list.contains(p)){JOptionPane.showMessageDialog(null, "游戏结束 GG");break;}list.addFirst(p);//吃球,更新球坐标if(p.equals(point)){int x=(int)(Math.random()*40)*10;int y=(int)(Math.random()*40)*10;point.setLocation(x, y);}else{list.removeLast();}Snake.this.repaint();i++;}}}public static void main(String[] args) {new Snake();}}
阅读全文
0 0
- Day02 骚年,玩蛇吗?(制作小游戏贪吃蛇)
- 【小游戏】C++手工制作贪吃蛇
- 利用unity 制作贪吃蛇小游戏
- html画布制作贪吃蛇小游戏
- 贪吃蛇小游戏(JAVA)
- js--贪吃蛇小游戏(加墙)
- JS小游戏:贪吃蛇(附源码)
- Arduion小游戏集合(贪吃蛇)
- 小游戏 贪吃蛇
- 贪吃蛇小游戏
- 贪吃蛇小游戏
- 贪吃蛇小游戏
- Applet贪吃蛇小游戏
- 贪吃蛇小游戏记录:
- 贪吃蛇小游戏笔记
- [小游戏]贪吃蛇
- 经典贪吃蛇小游戏
- 贪吃蛇小游戏~
- Broadlink :智能家居的下一步是人工智能
- centos6.5 安装 mysql
- 机器学习kaggle入门——手写体数字识别
- FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks 代码及环境配置
- JDBC公共类抽取
- Day02 骚年,玩蛇吗?(制作小游戏贪吃蛇)
- 期望dp 入入门
- Android热修复技术选型——三大流派解析
- rpm安装与yum安装的区别与特点
- Referenced file contains errors (http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd)
- ubuntu上联系Unix环境高级编程-apue.h的配置与安装
- 聊聊微服务架构
- Git创建本地分支并关联远程分支
- *TEST 10 for NOIP 迷幻测试 (230/300)