Create a Brick Breaker Game with the Corona SDK: Application Setup

原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Application Setup

这是系列教程的第一部分,完整内容点击下页的链接:

 

在这个系列教程中,我们将使用Corona SDK创建一个Brick Breaker游戏,这个游戏的目标很简单,你需要控制一个木板,像打乒乓球一样控制球来将对面的砖块都打掉。

好吧,让我们开始吧。

 

Step 1: Application Overview

再次介绍一下游戏规则,你需要将所有砖块都打掉,同时不能让球掉下来。如果达成目标,游戏将会进入下一关,否则游戏就结束。

 

Step 2: Target Device

首先我们需要确定游戏最终发布到的平台,这样我们才能根据目标平台制作合适大小的图片。

ios平台下有三种格式:iPad, iPhone3/iTouch2以及iPhone4,分别对应1024*768,320*480,640*960的分辨率。

Android的情况要复杂一些,因为各个手机制造厂商的标准都不统一,对于较大的屏幕来说就有480*800和480*854两种大小,其他小屏幕也一样不统一,Pad的大小更加不确定。

在这个教程中,我们将只关注于iPhone/iTouch平台,但是Corona SDK本身是跨平台的,制作的app可以很容易移植到Android平台以及ios的其他分辨率下。

 

Step 3: Interface

如上所示,界面包括多个不同的形状,按钮以及背景图片等资源,可以在这里下载。

 

Step 4: Export Graphics

根据你要发布的平台将图片缩放到合适的尺寸并保存。

 

Step 5: Main.lua

好了,要开始写代码了。

使用你最喜欢的文本编辑器,当然,如果能够支持Lua语法高亮那就更好了。注意,项目必须要有一个Main.lua文件,先从这个开始吧。

 

Step 6: Code Structure

我们会按下面描述的方式来组织我们的代码结构:

Necesary Classes
Variables and Constants
Declare Functions
contructor (Main function)
class methods (other functions)
call Main function

 

 

Step 7: Hide Status Bar

display.setStatusBar(display.HiddenStatusBar)

使用这段代码即可隐藏屏幕顶部的状态栏。

 

Step 8: Load Physics Engine

Corona SDK集成了Box2D物理引擎,可以使用下面的代码来将物理引擎集成到项目中。

--Physics Engine
local physics = require 'physics'
physics.start()
physics.setGravity(0, 0) -- Set gravity to 0 as we won't be needing it

Step 9: Game Variables & Constants

下面定义了一些基本变量以及常量,可以通过代码注释来了解它们的作用。

8
9
10
11
12
13
14
15
16
17
18
19
20
local BRICK_W = 41 --brick width
local BRICK_H = 21 --brick height
local OFFSET = 23 --an offset used to center the bricks
local W_LEN = 8 --the length of the levels, only 8 horizontal bricks should be created on stage
local SCORE_CONST = 100 --the amount to add to the score when a brick is hit
local score = 0 --stores the current score
local bricks = display.newGroup() --holds all the level bricks
local xSpeed = 5
local ySpeed = -5
local xDir = 1 -- x direction
local yDir = 1
local gameEvent = '' --stores game events (win, lose, finished)
local currentLevel = 1

Step 10: Menu Screen

 

这就是游戏的主界面,也是游戏中出现的第一个界面。下面定义的这几个变量用于保存背景图片和菜单界面的组件。

22
23
24
25
26
local background =  display.newImage('bg.png')
local menuScreen
local mScreen
local startB
local aboutB

 

Step 11: About Screen

这个界面用于显示作者以及版权信息,使用下面这个变量来保存该视图信息

28
local aboutScreen

 

Step 12: Game Screen

这是游戏界面,当玩家按下start按钮的时候创建。

30
31
32
local paddle
local brick
local ball

 

Step 13: Score & Level Text

下面几个变量用于保存当前关卡和分数

34
35
36
37
local scoreText
local scoreNum
local levelText
local levelNum

 

Step 14: Alert Screen

用于显示玩家胜利或者失败。

39
40
41
42
43
local alertScreen
local alertBg
local box
local titleTF
local msgTF

 

Step 15: Levels

我们使用Lua table来保存关卡数据

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
local levels = {}
levels[1] =             {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[2] =             {{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,1,0,0,1,0,0},
{0,0,0,0,0,1,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,1,1,1,0,0},}
levels[3] =             {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,1,1,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[4] =             {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1},}

在上面的表格中,1表示该位置有一个砖块,0表示没有。后面的程序会读取这个表格并生成对应的关卡。

 

Next Time. . .

在接下来的两节里,我们将添加与界面交互以及碰撞检测的代码。

 

 

原文地址: http://mobile.tutsplus.com/tutorials/corona/corona-sdk_brick-breaker/

原创文章,转载请注明: 转载自All-iPad.net

本文链接地址: Create a Brick Breaker Game with the Corona SDK: Application Setup

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值