The ninth day_打飞机项目
开发打飞机小游戏
7点半起床:
8点半学习:
11点半吃饭 3h
12点半学习
5点半散步: 5h
6点半学习
7点半吃饭 1h
8点学习
11点半洗漱 3.5h
12点睡觉
sum = 12.5h
基本功能实现
AWT和Swing是Java中常见的GUI(图形用户界面)技术,不需要花时间掌握
1 package cn.zqf.game;2 import java.awt.Window;3 import java.awt.event.WindowAdapter;4 import javax.swing.JFrame;5 import com.sun.glass.events.WindowEvent;6 //飞机游戏的主窗口7 public class MyGameFrame extends JFrame {8 //下面的这个方法用于初始化窗口9 public void launchFrame(){
10 this.setTitle("邹奇方的游戏");
11 this.setVisible(true);
12 this.setSize(500, 500); //设置大小
13 this.setLocation(500,100); //设置出现在屏幕的位置,以左上角为标准
14
15 this.addWindowListener(new WindowAdapter(){
16 public void windowClosing(WindowEvent e){
17 System.exit(0);
18 }
19 });
20 }
21 public static void main(String[] args) {
22 MyGameFrame f= new MyGameFrame();
23 f.launchFrame();
24 }
25 }
图形绘制_文本绘制_颜色改变_图像对象的加载
paint 方法 以下是打飞机项目的7个模块:都由包名开头:
1 package cn.sxt.game;
2 public class Constant {
3 public static final int GAME_WIDTH = 500;
4 public static final int GAME_HEIGHT = 500;
1 package cn.sxt.game;2 import java.awt.Graphics;3 import java.awt.Image;4 // 爆炸类5 6 public class Explode {7 double x, y;8 9 static Image[] imgs = new Image[16];
10 static {
11 for (int i = 0; i < 16; i++) {
12 imgs[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");
13 imgs[i].getWidth(null);
14 }
15 }
16 int count;
17 public void draw(Graphics g) {
18 if (count <= 15) {
19 g.drawImage(imgs[count], (int) x, (int) y, null);
20 count++;
21 }
22 }
23 public Explode(double x, double y) {
24 this.x = x;
25 this.y = y;
26 }
27 }
1 package cn.sxt.game;2 import java.awt.Graphics;3 import java.awt.Image;4 import java.awt.Rectangle;5 //游戏物体的父类6 public class GameObject {7 Image img;8 double x,y;9 int speed;
10 int width, height;
11
12 public void drawSelf(Graphics g){
13 g.drawImage(img, (int)x,(int) y, null);
14 }
15 public GameObject(Image img, double x, double y, int speed, int width, int height) {
16 super();
17 this.img = img;
18 this.x = x;
19 this.y = y;
20 this.speed = speed;
21 this.width = width;
22 this.height = height;
23 }
24 public GameObject(Image img, double x, double y) {
25 super();
26 this.img = img;
27 this.x = x;
28 this.y = y;
29 }
30
31 public GameObject() {
32 }
33
34 //返回物体所在的矩形。便于后续的碰撞检测
35 public Rectangle getRect(){
36 return new Rectangle((int)x, (int)y, width, height);
37 }
38 }
package cn.sxt.game;import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;public class GameUtil {// 工具类最好将构造器私有化。private GameUtil() {} //返回指定路径文件的图片对象public static Image getImage(String path) {BufferedImage bi = null;try {URL u = GameUtil.class.getClassLoader().getResource(path);bi = ImageIO.read(u);} catch (IOException e) {e.printStackTrace();}return bi;}
}
1 package cn.sxt.game;2 import java.awt.Color;3 import java.awt.Font;4 import java.awt.Frame;5 import java.awt.Graphics;6 import java.awt.Image;7 import java.awt.event.KeyAdapter;8 import java.awt.event.KeyEvent;9 import java.awt.event.WindowAdapter;10 import java.awt.event.WindowEvent;11 import java.util.Date;12 // 飞机游戏的主窗口13 14 public class MyGameFrame extends Frame {15 16 Image planeImg = GameUtil.getImage("images/plane.png");17 Image bg = GameUtil.getImage("images/bg.jpg");18 19 Plane plane = new Plane(planeImg,250,250);20 Shell[] shells = new Shell[50];21 22 Explode bao ;23 Date startTime = new Date();24 Date endTime;25 int period; //游戏持续的时间26 27 @Override28 public void paint(Graphics g) { //自动被调用。 g相当于一只画笔29 Color c = g.getColor();30 g.drawImage(bg, 0, 0, null);31 32 plane.drawSelf(g); //画飞机33 34 //画出所有的炮弹35 for(int i=0;i<shells.length;i++){36 shells[i].draw(g);37 38 //飞机和炮弹的碰撞检测!!!39 boolean peng = shells[i].getRect().intersects(plane.getRect());40 if(peng){41 plane.live = false;42 if(bao ==null){43 bao = new Explode(plane.x, plane.y);44 45 endTime = new Date();46 period = (int)((endTime.getTime()-startTime.getTime())/1000);47 }48 bao.draw(g);49 }50 51 //计时功能,给出提示52 if(!plane.live){53 g.setColor(Color.red);54 Font f = new Font("宋体", Font.BOLD, 50);55 g.setFont(f);56 g.drawString("时间:"+period+"秒", (int)plane.x, (int)plane.y);57 }58 59 }60 61 g.setColor(c);62 }63 64 65 //帮助我们反复的重画窗口!66 class PaintThread extends Thread {67 @Override68 public void run() {69 while(true){70 repaint(); //重画71 72 try {73 Thread.sleep(40); //1s=1000ms74 } catch (InterruptedException e) {75 e.printStackTrace();76 } 77 }78 }79 80 }81 82 //定义键盘监听的内部类83 class KeyMonitor extends KeyAdapter {84 @Override85 public void keyPressed(KeyEvent e) {86 plane.addDirection(e);87 }88 @Override89 public void keyReleased(KeyEvent e) {90 plane.minusDirection(e);91 }92 }93 // 初始化窗口94 public void launchFrame(){95 this.setTitle("尚学堂学员_程序猿作品");96 this.setVisible(true);97 this.setSize(Constant.GAME_WIDTH , Constant.GAME_HEIGHT);98 this.setLocation(300, 300);99
100 this.addWindowListener(new WindowAdapter() {
101 @Override
102 public void windowClosing(WindowEvent e) {
103 System.exit(0);
104 }
105 });
106
107 new PaintThread().start(); //启动重画窗口的线程
108 addKeyListener(new KeyMonitor()); //给窗口增加键盘的监听
109
110
111 //初始化50个炮弹
112 for(int i=0;i<shells.length;i++){
113 shells[i] = new Shell();
114 }
115
116 }
117
118 public static void main(String[] args) {
119 MyGameFrame f = new MyGameFrame();
120 f.launchFrame();
121 }
122
123 private Image offScreenImage = null;
124
125 public void update(Graphics g) {
126 if(offScreenImage == null)
127 offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度
128
129 Graphics gOff = offScreenImage.getGraphics();
130 paint(gOff);
131 g.drawImage(offScreenImage, 0, 0, null);
132 }
133
134 }
1 package cn.sxt.game;2 import java.awt.Graphics;3 import java.awt.Image;4 import java.awt.event.KeyEvent;5 //飞机类6 public class Plane extends GameObject {7 boolean left,up,right,down;8 9 boolean live = true;
10
11 public void drawSelf(Graphics g){
12 if(live){
13 g.drawImage(img, (int)x,(int) y, null);
14
15 if(left){
16 x -=speed;
17 }
18 if(right){
19 x += speed;
20 }
21 if(up){
22 y -=speed; //y = y-speed;
23 }
24 if(down){
25 y += speed;
26 }
27 }else{
28 }
29 }
30 public Plane(Image img, double x, double y){
31 this.img = img;
32 this.x = x;
33 this.y = y;
34 this.speed = 3;
35 this.width = img.getWidth(null) ;
36 this.height = img.getHeight(null);
37 }
38 //按下某个键,增加相应的方向
39 public void addDirection(KeyEvent e){
40 switch (e.getKeyCode()) {
41 case KeyEvent.VK_LEFT:
42 left = true;
43 break;
44 case KeyEvent.VK_UP:
45 up = true;
46 break;
47 case KeyEvent.VK_RIGHT:
48 right = true;
49 break;
50 case KeyEvent.VK_DOWN:
51 down = true;
52 break;
53 }
54 }
55 //按下某个键,取消相应的方向
56 public void minusDirection(KeyEvent e){
57 switch (e.getKeyCode()) {
58 case KeyEvent.VK_LEFT:
59 left = false;
60 break;
61 case KeyEvent.VK_UP:
62 up = false;
63 break;
64 case KeyEvent.VK_RIGHT:
65 right = false;
66 break;
67 case KeyEvent.VK_DOWN:
68 down = false;
69 break;
70 }
71 }
72 }
1 package cn.sxt.game;2 import java.awt.Color;3 import java.awt.Graphics;4 //炮弹类5 public class Shell extends GameObject {6 7 double degree;8 9 public Shell(){
10 x = 200;
11 y = 200;
12 width=10;
13 height = 10;
14 speed = 3;
15 degree = Math.random()*Math.PI*2;
16 }
17 public void draw(Graphics g){
18 Color c = g.getColor();
19 g.setColor(Color.YELLOW);
20
21 g.fillOval((int)x,(int) y, width, height);
22
23 //炮弹沿着任意角度去飞
24 x += speed*Math.cos(degree);
25 y += speed*Math.sin(degree);
26
27 if(x<0||x>Constant.GAME_WIDTH-width){
28 degree = Math.PI - degree;
29 }
30 if(y<30||y>Constant.GAME_HEIGHT-height){
31 degree = - degree;
32 }
33 g.setColor(c);
34 }
35 }
The ninth day_打飞机项目相关推荐
- 2022-2028年中国大飞机项目产业链分析及投资咨询报告
[报告类型]产业研究 [报告价格]¥4500起 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了中国大飞机项目行业市场行业相关概述.中国大 ...
- cocos2dx 3.2之Lua打飞机项目
1 创建lua打飞机项目 cocos new T32Lua -dE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\projects -l lua 2 ...
- cocos2dx打飞机项目笔记三:HeroLayer类和坐标系
HeroLayer类主要是处理hero的一些相关东西,以及调用bulletLayer的一些方法,因为子弹是附属于hero的~~ HeroLayer 类的成员如下: 1 class HeroLayer ...
- cocos2dx打飞机项目笔记五:CCSpriteBatchNode 的使用
在上一节里,在头文件看到 定义了一个 CCSpriteBatchNode* batchNode;,在addEnemy方法里看到 batchNode->addChild(enemy); 新建的敌机 ...
- cocos2dx打飞机项目笔记六:GameScene类和碰撞检测 boundingbox
GameScene类虽然是占用游戏最多时间的类,但是里面的东西不是很多,最重要的就是碰撞检测了,碰撞检测代码如下: 1 void GameScene::detectionCrash() 2 { 3 4 ...
- cocos2dx打飞机项目笔记二:BulletLayer类
BulletLayer.h 内容如下 1 class BulletLayer : public cocos2d::CCLayer 2 { 3 4 public: 5 6 CC_SYNTHESIZE(b ...
- 中国飞机制造及修理行业未来需求预测及发展态势研究报告2021版
中国飞机制造及修理行业未来需求预测及发展态势研究报告2021版 HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS-- [修订日期]:2021年11月 [搜索 ...
- 【课程作业】Pygame游戏开发之三个小游戏 容易简单 附项目代码和游戏介绍
主要知识点: 精准碰撞检测 用户自定义事件 敌人生成 一.躲避汉堡游戏(参考小甲鱼微信打飞机项目)(单人) 游戏规则: 1.操作人物躲避天空掉落的汉堡
- 袁萌浅谈C919大飞机(四)
袁萌浅谈C919大飞机(四) 袁萌不是大飞机专家,只是略懂大飞机飞行的数学原理(多元微积分应用).大飞机外壳的每一平方厘米面积上的受力情况都必需事先经过模拟计算,计算量十分巨大. 大飞机是一个特大系统 ...
最新文章
- 2021年大数据ELK(二):Elasticsearch简单介绍
- 让字体图标代替雪碧图,减少请求带宽
- MySQL DBA面试全揭秘
- Golang切片的三种简单使用方式及区别
- 海底光缆,到底是怎么安装和维护的?
- CCNA之网络地址转换(NAT)简介
- 操作系统笔记(王道考研) 第一章:计算机系统概述
- 利用win10自带的工具测硬盘读写速度
- 5S定置管理协调现场的三大元素
- excel转置怎么操作_EXCEL的矩阵运算
- pdf转换html表格没了,PDF转HTML后,表格排版全乱了是怎么回事?
- 计算机桌面文件能单独设密码吗,告诉你怎么给文件夹设置密码
- (转)罗振宇跨年演讲:哪来直接登顶的人生,只有不断迭代的历程
- C++语法(二十)常函数、常对象
- 第七章·【第一次上岗:核桃编程】
- JS自写带描述标签云
- Unity-odin-npbehave结合烟雨大佬的示例
- 程序员可选择的个博客论坛网站
- Linux source文件后提示 export:command not found
- BZOJ 3036 绿豆蛙的归宿
热门文章
- 微信点击链接直接下载安装包功能实现方式
- 【机器学习】机器学习在爱奇艺视频分析理解中的实践
- 【Git】团队协作(终于学会了)
- 使用Git对Word或Excel文件进行版本管理
- lightroom 闪退_【HTTP网球iOS】Lightroom解锁
- Swift CoreAnimation ---- CALayer的呈现层和模型层
- 多的是,你不知道的“浮点数”
- s60v5全屏幕java_【转】 最新消息 ● S60v5官方华丽升级塞班^3系统java v2.1(亲测有效)...
- HDU 1814 Peaceful Commission(2-SAT)
- IDL图像可视化(八)