[工具] 学作PSP自制程序-LUA中级教程-3(英文原文+翻译)

本主题由 艾达·王 于 2009-6-7 18:55 分类

学作PSP自制程序-LUA中级教程-3(英文原文+翻译)

-----------------------写在前面-----------------------
对于其他的教程,请在论坛中,用“LUA中级教程”作为关键字,进行检索。
-------------------------------------------------------


PSP Lua Snippets - Drawing A Healthbar
》》描绘生命条(HP计算,血条)

Drawing a healthbar to the screen is pretty much a standard in most games. This can be a very confusing thing to a new programmer who doesn't know how to approach this. But it actually is a very simple process once you see some ways it can be done. Let's start coding.
》》在游戏中描绘一个“生命条”,在大多数游戏中的都是很有用的。这是一件很麻烦的事情,对于那些不知道如何模拟它的初级程序员来说。
》》但是,如果你看过如何做,它是一件很简单的处理。让我们开始写代码吧。

First, let's create two colors. We will use red for the healthbar and white to print to the screen in text.
》》首先,创建两个颜色。红色是为描绘生命条准备的,白色是显示文字用的。

red = Color.new(255,0,0)
white = Color.new(255,255,255)


Next, we will create some arrays to store some healthbar and player information. This could be done with simple variables but we will use arrays since in a real game it would be easier to add more data types for the player and healthbar.
》》接下来,我们创建一些数组,用来存储生命条和角色的信息。可以用一些简单的变量就能办到,但是我们会使用数组,一旦我们在开发一个真的游戏,
》》使用数组就更容易为生命条和角色信息增加将会使用的类型。
Our first array is for the healthbar. We will create the array and make a type to refer to the healthbar's y position.
》》我们的第一个数组是为了生命条准备的。目的是,标志生命条的位置。(这里指生命条的高度)

healthbar = {}
healthbar.y = 10


Now, we will make an array for our player. In our example game all we need to store in the array is the player's health.
》》现在,为角色创建一个数组。在这里用来存储角色的生命值。(生命值和生命条并不是1:1的关系,更多的时候是n:n。本文是1:1)。

player = {}
player.health = 100


Now, we start our main loop, screen clearing, and button input code:
》》现在,我们开始我们以清空屏幕和监控按钮状态两步来开始主循环。

while true do
screen:clear()
pad = Controls.read()


Now we have started our main program loop, the part of our program which will repeat over and over checking for updates to the game.
》》现在开始我们程序的主循环,这一部分将反复的更新信息和检查按钮状态等。
Our next command will draw the healthbar to the screen. We will use the drawRect command to do this. This command simply draws a filled rectangle. Here's the code. Take a look and I will explain it.
》》下一个命令,将会在屏幕上描绘一个生命条。我们将会用到drawRect命令来做。这个命令会填充一个矩形(画一个矩形,并填充颜色)。
》》看看吧,我会解释它。

screen:fillRect(10,10,player.health,healthbar.y,red)

This command can be used to draw a rectangle to an image. But instead we are drawing it to the screen instead.
》》这个命令可以用来绘制矩形的图像。But instead we are drawing it to the screen instead.
The two 10's are the x and y position that we will draw this rectangle at.
》》前面的两个10,是标志了描绘这个矩形的开始坐标(矩形左上角的坐标)。
The next two arguments are the arrays we created earlier.
》》接下来的参数是我们刚刚建立的。
player.health will be the width of the rectangle. So as our players health increases or decreases so will the bar.
》》player.health将标志矩形的宽,角色生命值的增加和减少就会反映在这个矩形上。
healthbar.y will be the height of the rectangle, which we set to 10 earlier. This value will never change.
》》healthbar.y是这个矩形的高,我们早先设置的10.这个值将不会改变。
Although, you could change the y instead of x value if you wanted a vertical healthbar.
》》你可以把x和y互换,如果你想要得到一个竖着的生命条。
Next, we will print the players health to the screen underneath the healthbar, just so you can see the health visually with text and graphics as well.
》》接下来,我们将在生命条的下面,显示出生命值,你就可以用文字和图形来观察生命值的变化了。

screen:print(10,30,player.health,white)

Now, for this example game we are going to increase the health if the up button is held, and decrease the health if the down button is held. Here's the up button code:
》》现在,编写的代码,在上(up)键按下时,生命值会增加;当下(down)键按住时,生命值会减小。这是上键的代码。

if pad:up() and player.health < 100 then
player.health = player.health + 0.5
end


This code will increase the player's health by 0.5 if the up button is pressed, and if the player's health is less than 100 as well, since our health only goes to 100.
》》这个代码的意思,当生命值小于100,并且上键被按住时,生命值会已0.5的增加量而增加。但是只能增加到100(最大值)。
Now, let's do the down button.
》》现在,是下键的代码。

if pad:down() and player.health > 0 then
player.health = player.health - 0.5 end


This code is the same as up except that the health will decrease if down is pressed, considering our health is greater than zero. Without the check for zero our health could go in to negative numbers!
》》这部分代码和上键的代码功能类似,同样是不能减少到0以下,要不然角色的生命值就成了负数了啊。
Let's end our loop and wrap up this tutorial.
》》让我们用一贯的做法来结束主循环。

screen.flip()
screen.waitVblankStart()
end


》》(结束)我在代码里增加了竖直显示的生命条

[ 本帖最后由 zenanswer 于 2009-5-11 00:07 编辑 ]
附件: 您所在的用户组无法下载或查看附件
本帖最近评分记录
  • 艾达·王 金钱 +150 支持这样的好帖~ 原来生命条是这样做的~ 加 ... 2009-5-11 00:02
  • 艾达·王 多玩草 +30 支持这样的好帖~ 原来生命条是这样做的~ 加 ... 2009-5-11 00:02

TOP

这种东西怎么就是没有人看那?

TOP

我看了.虽然看不太懂~
不过LZ这样的技术贴.应该顶.

其实在这里的大部份的朋友都不会编程的~  不过.学无止境!

TOP

MS挺复杂的。不懂,纯飘过……

TOP

我感觉这比需要配置复杂的编译环境的C简单多了
而且windows也能运行,不需要lunix系统。

TOP

支持这样的好帖~ 原来生命条是这样做的~ 加分加草~
另外,建议标题通俗一些,这样可能更容易被人接受

TOP

頂一下......謝謝樓主分享

TOP

提示: 作者被禁止或删除 内容自动屏蔽

TOP

留名再看。

TOP

引用:
原帖由 lddlyz83 于 2009-5-11 00:05 发表
感觉很原始的做法,不用那么麻烦的了。
我也是刚学,找到一些简单的资料学习一下。
我觉得很适合初学者啊。

---------------
PS:你的砖头怎么样了?
      关心而已……没有恶意……
---------------

TOP

伸手党们被DA惯坏了,看来以后想吃饱,想玩好就要自己动手,艰苦奋斗……

TOP

回复 10# 的帖子

提示: 作者被禁止或删除 内容自动屏蔽

TOP

提示: 作者被禁止或删除 内容自动屏蔽

TOP

开发平台 编译工具
??
我没用过lua = =+
请lz写的明白点吧
呵呵

TOP

引用:
原帖由 NuNuPig 于 2009-5-11 00:34 发表
开发平台 编译工具
??
我没用过lua = =+
请lz写的明白点吧
呵呵
我这是 第3贴了 还有 1和2 自己在论坛里搜搜看。

TOP