博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
libgdx游戏引擎开发笔记(九)SuperJumper游戏例子的讲解(篇三)---- 主游戏界面显示框架...
阅读量:6607 次
发布时间:2019-06-24

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

 在完成前面的基础性工作之后,接下来就到了重头戏:游戏主界面和游戏逻辑的搭建。


1.游戏主界面的搭建


   今天我们要完成的工作是:点击play进入游戏界面,对应的在游戏界面显示一些东西.

效果:


   看到以上四张图相信大家应该很明白要干什么了,下面我们上代码


GameScreen类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package 
com.zhf.mylibgdx;
import 
com.badlogic.gdx.Game;
import 
com.badlogic.gdx.Gdx;
import 
com.badlogic.gdx.Screen;
import 
com.badlogic.gdx.graphics.GL10;
import 
com.badlogic.gdx.graphics.GLCommon;
import 
com.badlogic.gdx.graphics.OrthographicCamera;
import 
com.badlogic.gdx.graphics.g2d.SpriteBatch;
import 
com.badlogic.gdx.math.Rectangle;
import 
com.badlogic.gdx.math.Vector3;
/**
 
* 游戏主界面
 
* @author ZHF
 
*
 
*/
public 
class 
GameScreen 
implements 
Screen {
    
//状态
    
static 
final 
int 
GAME_READY = 
0
;   
//准备
    
static 
final 
int 
GAME_RUNNING = 
1
;  
//运行
    
static 
final 
int 
GAME_PAUSED = 
2
;  
//暂停
    
static 
final 
int 
GAME_LEVEL_END = 
3
;  
//本关结束
    
static 
final 
int 
GAME_OVER = 
4
;  
//游戏结束
                                                                                                                                                                                                                                                                                                                                                                                   
    
Game game;
                                                                                                                                                                                                                                                                                                                                                                                   
    
int 
state; 
//状态
    
OrthographicCamera guiCam;  
//相机
    
Vector3 touchPoint;  
//触点
    
SpriteBatch batcher;  
//用于绘画的
                                                                                                                                                                                                                                                                                                                                                                                   
    
Rectangle pauseBounds;  
//暂停按钮
    
Rectangle resumeBounds; 
//继续游戏按钮
    
Rectangle quitBounds;  
//离开按钮
                                                                                                                                                                                                                                                                                                                                                                                   
    
int 
lastScore;   
//最后得分
    
String scoreString;  
//显示分数
                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                   
    
public 
GameScreen(Game game) {
        
this
.game = game;
        
//初始化工作
        
state = GAME_READY;
        
guiCam = 
new 
OrthographicCamera(
320
480
);
        
guiCam.position.set(
320 
2
480 
2
0
);  
//将相机的视角设为中间
        
touchPoint = 
new 
Vector3();
        
batcher = 
new 
SpriteBatch();
                                                                                                                                                                                                                                                                                                                                                                                       
        
pauseBounds = 
new 
Rectangle(
320 
64
480 
64
64
64
);
        
resumeBounds = 
new 
Rectangle(
160 
96
240
192
36
);
        
quitBounds = 
new 
Rectangle(
160 
96
240 
36
192
36
);
                                                                                                                                                                                                                                                                                                                                                                                       
        
lastScore = 
0
;
        
scoreString = 
"SCORE: 0"
;
    
}
                                                                                                                                                                                                                                                                                                                                                                                   
    
/**刷新状态**/
    
public 
void 
update (
float 
deltaTime) {
        
if 
(deltaTime > 
0
.1f) deltaTime = 
0
.1f;
        
//根据游戏动向,刷新状态
        
switch 
(state) {
        
case 
GAME_READY:
            
updateReady();  
//准备状态
            
break
;
        
case 
GAME_RUNNING:
            
updateRunning(deltaTime);  
//游戏运行状态
            
break
;
        
case 
GAME_PAUSED:
            
updatePaused();  
//游戏暂停状态
            
break
;
        
case 
GAME_LEVEL_END:  
//本关结束
            
updateLevelEnd();
            
break
;
        
case 
GAME_OVER:
            
updateGameOver();  
//游戏结束
            
break
;
        
}
    
}
                                                                                                                                                                                                                                                                                                                                                                                   
    
/**准备状态**/
    
private 
void 
updateReady () {
        
if 
(Gdx.input.justTouched()) {
            
state = GAME_RUNNING;
        
}
    
}
    
/**游戏运行状态**/
    
private 
void 
updateRunning (
float 
deltaTime) {
        
if 
(Gdx.input.justTouched()) {
            
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 
0
));
            
//点击暂停
            
if 
(OverlapTester.pointInRectangle(pauseBounds, touchPoint.x, touchPoint.y)) {
                
Assets.playSound(Assets.clickSound);
                
state = GAME_PAUSED;
                
return
;
            
}
        
}
    
}
    
/**游戏暂停状态**/
    
private 
void 
updatePaused () {
        
if 
(Gdx.input.justTouched()) {
            
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 
0
));
            
//点击继续游戏
            
if 
(OverlapTester.pointInRectangle(resumeBounds, touchPoint.x, touchPoint.y)) {
                
Assets.playSound(Assets.clickSound);
                
state = GAME_RUNNING;
                
return
;
            
}
            
//点击离开,返回到主菜单界面
            
if 
(OverlapTester.pointInRectangle(quitBounds, touchPoint.x, touchPoint.y)) {
                
Assets.playSound(Assets.clickSound);
                
game.setScreen(
new 
MainMenuScreen(game));
                
return
;
            
}
        
}
    
}
    
/**本关结束**/
    
private 
void 
updateLevelEnd () {
        
if 
(Gdx.input.justTouched()) {
            
state = GAME_READY;
        
}
    
}
    
/**游戏结束**/
    
private 
void 
updateGameOver () {
        
if 
(Gdx.input.justTouched()) {
            
game.setScreen(
new 
MainMenuScreen(game));
        
}
    
}
                                                                                                                                                                                                                                                                                                                                                                                   
    
/**绘画**/
    
public 
void 
draw (
float 
deltaTime) {
        
GLCommon gl = Gdx.gl;
        
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        
gl.glClearColor(0f, 0f, 0f, 0f);
        
guiCam.update();
        
batcher.setProjectionMatrix(guiCam.combined);
        
batcher.enableBlending();
                                                                                                                                                                                                                                                                                                                                                                                       
        
batcher.begin();
        
//根据游戏状态刷新界面
        
switch 
(state) {
        
case 
GAME_READY:
            
presentReady();  
//绘制准备界面
            
break
;
        
case 
GAME_RUNNING:
            
presentRunning();  
//绘制暂停和分数显示
            
break
;
        
case 
GAME_PAUSED:
            
presentPaused();  
//绘制暂停菜单和分数显示
            
break
;
        
case 
GAME_LEVEL_END:
            
presentLevelEnd();  
//绘制本关结束
            
break
;
        
case 
GAME_OVER:
            
presentGameOver();  
//绘制游戏结束
            
break
;
        
}
        
batcher.end();
    
}
                                                                                                                                                                                                                                                                                                                                                                                   
    
/**绘制准备**/
    
private 
void 
presentReady () {
        
batcher.draw(Assets.ready, 
160 
192 
2
240 
32 
2
192
32
);
    
}
    
/**绘制暂停和分数显示**/
    
private 
void 
presentRunning () {
        
batcher.draw(Assets.pause, 
320 
64
480 
64
64
64
);
        
Assets.font.draw(batcher, scoreString, 
16
480 
20
);
    
}
    
/**绘制暂停菜单和分数显示**/
    
private 
void 
presentPaused () {
        
batcher.draw(Assets.pauseMenu, 
160 
192 
2
240 
96 
2
192
96
);
        
Assets.font.draw(batcher, scoreString, 
16
480 
20
);
    
}
    
/**绘制本关结束**/
    
private 
void 
presentLevelEnd () {
        
String topText = 
"the princess is ..."
;
        
String bottomText = 
"in another castle!"
;
        
float 
topWidth = Assets.font.getBounds(topText).width;
        
float 
bottomWidth = Assets.font.getBounds(bottomText).width;
        
Assets.font.draw(batcher, topText, 
160 
- topWidth / 
2
480 
40
);
        
Assets.font.draw(batcher, bottomText, 
160 
- bottomWidth / 
2
40
);
    
}
    
/**绘制游戏结束**/
    
private 
void 
presentGameOver () {
        
batcher.draw(Assets.gameOver, 
160 
160 
2
240 
96 
2
160
96
);
        
float 
scoreWidth = Assets.font.getBounds(scoreString).width;
        
Assets.font.draw(batcher, scoreString, 
160 
- scoreWidth / 
2
480 
20
);
    
}
    
@Override
    
public 
void 
render(
float 
delta) {
        
update(delta);
        
draw(delta);
    
}
    
@Override
    
public 
void 
resize(
int 
width, 
int 
height) {
        
// TODO Auto-generated method stub
    
}
    
@Override
    
public 
void 
show() {
        
// TODO Auto-generated method stub
    
}
    
@Override
    
public 
void 
hide() {
        
// TODO Auto-generated method stub
    
}
    
@Override
    
public 
void 
pause() {
        
if 
(state == GAME_RUNNING) state = GAME_PAUSED;
    
}
    
@Override
    
public 
void 
resume() {
        
// TODO Auto-generated method stub
    
}
    
@Override
    
public 
void 
dispose() {
        
// TODO Auto-generated method stub
    
}
}

分析: 

       这里代码里的注释已经写得很清楚,这里我再讲一下框架,方便初学者学习,在构造方法里我们初始化了一些变量,接下来很重要的一步就是在render()方法里写了两个重要的方法:update() 和 draw(); 即:游戏的逻辑


update(float deltaTime): 根据游戏动向不断地刷新状态(准备状态、游戏运行状态、游戏暂停状态、本关结束、本关结束


draw(float deltaTime): 根据游戏状态刷新界面(

                       presentReady();  //绘制准备界面

presentRunning();  //绘制暂停和分数显示

presentPaused(); //绘制暂停菜单和分数显示

presentLevelEnd(); //绘制本关结束

presentGameOver(); //绘制游戏结束  )


 我们别忘了和前面一样,在MainMneuScreen中将条状那行注释去掉:

1
game.setScreen(
new 
GameScreen(game));

 同样,游戏界面中包含一些元素(如暂停按钮,菜单等),自然要在Asset类中对应的加载资源进去。

声明:

1
2
3
4
5
//游戏界面
public 
static 
TextureRegion pause;
//暂停按钮
public 
static 
TextureRegion pauseMenu;  
//暂停弹出菜单
public 
static 
TextureRegion ready;  
//准备
public 
static 
TextureRegion gameOver; 
//游戏结束

实例化:

1
2
3
4
5
//游戏界面界面
pause = 
new 
TextureRegion(items, 
64
64
64
64
);
pauseMenu = 
new 
TextureRegion(items, 
224
128
192
96
);
ready = 
new 
TextureRegion(items, 
320
224
192
32
);
gameOver = 
new 
TextureRegion(items, 
352
256
160
96
);

 ok!这一讲可能代码比较少,这里我们主要先将框架搭建起来,下一讲我们我涉及到游戏场景中各个元素以及世界的编写。


 源码下载

     本文转自zhf651555765 51CTO博客,原文链接:http://blog.51cto.com/smallwoniu/1262906,如需转载请自行联系原作者

你可能感兴趣的文章
FTP服务
查看>>
我的友情链接
查看>>
xcode8运行后后台打印网络相关的日志
查看>>
python语言中函数的传参与基本练习
查看>>
Java集合框架面试题
查看>>
Django1.7中注册、登陆、以及cookie的使用
查看>>
实现Lync Server 2010企业版前端服务器部署
查看>>
Java的主要就业方向
查看>>
关于使用mac进行文件远程操作
查看>>
Ubuntu Server 14.04 配置VNC
查看>>
我的友情链接
查看>>
Tcl命令操作实验-----(6)---case foreach
查看>>
Tcl命令操作实验-----(12) ---字典
查看>>
akka的Hello
查看>>
scala的视图界定
查看>>
PHP扩展之文件系统
查看>>
智能手机将替代PC成大众最常用终端 智能手机前景无限
查看>>
解决linux下用ntpdate同步时间
查看>>
我的友情链接
查看>>
学习开源框架WebX的总结
查看>>