book.luaer.cn
实习结束了,在这里总结下实习的相关事宜。
我去年十一月份入职的,刚入公司技术部人很少,我选择的是服务端开发,服务端就三四个人,客户端倒是有五六个人。第一天上班就是打开电脑,安装一些需要的工具。公司的做的产品是手游,主要使用的是脚本语言lua(不出名?魔兽就是用lua写的)。对于lua语言,我是很陌生的,所以安装完必备的工具后就开始学习lua了。我选择的教程是 http://book.luaer.cn/ (现在查api使用的是 http://HdhCmsTestcodingnow测试数据/2000/download/lua_manual.html )。大概花了1天时间把lua的语法了解了下。然后老大就安排了一个小东西给我实现,控制台版的小游戏(仅仅是模拟而已),实现主城,商店,战斗(很简易的,后面会给出源代码,不要喷哈)。
这款游戏是重0开始的,没有现成的engine。都是从底层架构开始的,使用c实现网络模块和内存数据库模块。前期我负责写网络协议的压包和解包,遇到的最大的困难是c和lua的数据交互,经过老大的指导和自己的钻研,把交互栈搞明白了(写过如何交互的技术博文,需要的给我发邮件)。加包就是序列化,解包就是反序列化,将序列化的数据用于网络传输,网络收发模块是一个同事写的(这两个模块最后都没有采纳,老大自己重写了,看了他写的代码后确实佩服,清晰易懂)。
在这每天晚上9点是有个例会的(21点?是的,游戏公司都会加班)。例会上主要是总结下今天做了什么,遇到了什么问题,完成了什么任务,什么任务没有完成,后续的计划等等。而且每个月经理都会和我们谈一次话,聊聊这个月做的事情啊,生活上的和工作上的都会聊聊。这方式不错,经理能够及时掌握员工的工作状态是很好的。
底层模块基本实现之后,接下来的就是实现一些逻辑模块,分给我的是背包系统,包括道具,装备和铁匠铺。由于我大学期间不玩网游,对游戏的不认识太多了,公司都会叫我们玩玩网游,了解下模块和功能,然后就试着去实现。我个人感觉游戏都不好玩,但是为了了解是怎么实现的,我还是会玩玩的,游戏的剧情我就不看了,专门挑系统的功能。
公司规定每天加班,单休。加班确实累了点,但是学到的东西还是挺多的,关于游戏逻辑,我实现了背包,铁匠铺,道具,装备,聊天系统,我是从一开始就跟着底层模块开始建立的,对底层模块还是比较了解的,所以实现逻辑模块比较简单。到现在快有4个月了,公司也给我提前转正了,在服务端我也算元老级人物了,陆陆续续换了好多人了。希望游戏能够取得成功。
HelloLua
1 function clear()
2 -- linux下为
3 -- os.execute("clear")
4 -- windows下为
5 os.execute ( " cls " )
6 end ;
7
8 -- 输入选项
9 function inputSelect(s,n)
10 io.write (s)
11 if n == nil then
12 n = 1
13 end ;
14 return io.read (n)
15 end ;
16
17 -- buy somthing
18 function buySomething(p,s,n)
19 if p.wallet > n then
20 if s == " ATK " then
21 p.ATK = p.ATK + n
22 elseif s == " DEF " then
23 p.DEF = p.DEF + n
24 elseif s == " HP " then
25 p.HP = p.HP + n
26 end ;
27 p.wallet = p.wallet - n
28 else
29 print ( " your haven't more money to buy it " )
30 end ;
31 end ;
32
33 -- show stat
34 function show_stat(p)
35 io.write ( " ATK:\t " ..p.ATK.. " \n " )
36 io.write ( " DEF:\t " ..p.DEF.. " \n " )
37 io.write ( " HP:\t " ..p.HP.. " \n " )
38 io.write ( " wallet:\t " ..p.wallet.. " \n " )
39 sleep( 3 )
40 end ;
41
42 -- 进入商店
43 function goto_shop()
44 clear()
45 print ( " you are in shop now. " )
46 print ( " 1. buy ATK " )
47 print ( " 2. buy DEF " )
48 print ( " 3. buy HP " )
49 print ( " 0. back " )
50 input = inputSelect( " please input number: " )
51 if input == " 1 " then
52 buySomething(P, " ATK " , 1 )
53 print ( " you buy ATK OK! " )
54 show_stat(P)
55 elseif input == " 2 " then
56 buySomething(P, " DEF " , 1 )
57 print ( " you buy ATK OK! " )
58 show_stat(P)
59 elseif input == " 3 " then
60 buySomething(P, " HP " , 1 )
61 print ( " you buy ATK OK! " )
62 show_stat(P)
63 elseif input == " 0 " then
64 show_welcome()
65 else
66 goto_shop()
67 end ;
68 goto_shop()
69 end ;
70
71 -- 显示战场
72 function show_battlefield()
73 clear()
74 print ( " PKing " )
75 io.write ( " Person:\tyou\tmonster\n " )
76 end ;
77
78 -- A hit B
79 function AhitB(A, B)
80 B.HP = B.HP - A.ATK / B.DEF
81 -- B.HP = B.HP - 30
82 end ;
83
84 function show_result(p,e)
85 io.write ( " ATK:\t " ..p.ATK.. " \t " ..e.ATK.. " \n " )
86 io.write ( " DEF:\t " ..p.DEF.. " \t " ..e.DEF.. " \n " )
87 io.write ( " HP:\t " ..p.HP.. " \t " ..e.HP.. " \n " )
88 end ;
89
90 -- 时间暂停n秒
91 function sleep(n)
92 t = os.time ()
93 while os.time () - t < n do
94 end ;
95 end ;
96
97 -- 战场,返回false则战败,返回true则胜利
98 function battlefield(p,e)
99 while true do
100 -- p hit e
101 if p.HP <= 0 then
102 return false
103 end ;
104 AhitB(p,e)
105 show_battlefield()
106 io.write ( " \tyou hit moster\n " )
107 show_result(p,e)
108 sleep( 1 )
109
110 -- e hit p
111 if e.HP <= 0 then
112 return true
113 end ;
114 AhitB(e,p)
115 show_battlefield()
116 io.write ( " \tmoster hit you\n " )
117 show_result(p,e)
118 sleep( 1 )
119 end ;
120 end ;
121
122
123 -- 进入战场选择菜单
124 function goto_battlefield()
125 clear()
126
127 print ( " you are in battlefield now. " )
128 print ( " 1. low level battlefield " )
129 print ( " 2. intermediate battlefield " )
130 print ( " 3. advanced battlefield " )
131 print ( " 0. back " )
132 input = inputSelect( " please choose battlefield: " )
133 if input == " 1 " then
134 print ( " in here " )
135 E = {
136 ATK = E1.ATK,
137 DEF = E1.DEF,
138 HP = E1.HP}
139 if battlefield(P,E) then
140 goto_battlefield()
141 print ( " you win " )
142 P.HP = P.HP + 10
143 sleep( 2 )
144 else
145 print ( " you die " )
146 print ( " GAME OVER " )
147 end ;
148 elseif input == " 2 " then
149 E = {
150 ATK = E2.ATK,
151 DEF = E2.DEF,
152 HP = E2.HP}
153 if battlefield(P,E) then
154 goto_battlefield()
155 print ( " you win " )
156 P.HP = P.HP + 10
157 P.wallet = P.wallet + 10
158 sleep( 2 )
159 else
160 print ( " you die " )
161 print ( " GAME OVER " )
162 end ;
163 elseif input == " 3 " then
164 E = {
165 ATK = E3.ATK,
166 DEF = E3.DEF,
167 HP = E3.HP}
168 if battlefield(P,E) then
169 goto_battlefield()
170 print ( " you win " )
171 P.HP = P.HP + 20
172 P.wallet = P.wallet + 10
173 P.ATK = P.ATK + 5
174 P.DEF = P.DEF + 5
175 sleep( 2 )
176 else
177 print ( " you die " )
178 print ( " GAME OVER " )
179 end ;
180 elseif input == " 0 " then
181 show_welcome()
182 else
183 goto_battlefield()
184 end ;
185 end ;
186
187 function show_welcome()
188 clear()
189 print ( " ======welcome to battle game====== " )
190 io.write ( " \tplease choose entrance\n " )
191 print ( " 1. shop " )
192 print ( " 2. battlefield entrance " )
193 print ( " 0. exit " )
194 input = inputSelect( " please input number of the entrance: " )
195 if input == " 1 " then
196 goto_shop()
197 elseif input == " 2 " then
198 goto_battlefield()
199 elseif input == " 0 " then
200 os.exit ()
201 else
202 show_welcome()
203 end ;
204 end ;
205
206 P = {
207 wallet = 100 ,
208 ATK = 100 ,
209 DEF = 100 ,
210 HP = 100 }
211 E1 = {
212 ATK = 10 ,
213 DEF = 10 ,
214 HP = 100 }
215
216 E2 = {
217 ATK = 20 ,
218 DEF = 20 ,
219 HP = 100 }
220 E3 = {
221 ATK = 30 ,
222 DEF = 30 ,
223 HP = 100 }
224
225 show_welcome()
226
227
228
229 -- 字符串相似度算法 lua 实现
230 function EditDistance( s, t, lim )
231 local s_len, t_len = #s, #t -- Calculate the sizes of the strings or arrays
232 if lim and math.abs ( s_len - t_len ) >= lim then -- If sizes differ by lim, we can stop here
233 return lim
234 end
235
236 -- Convert string arguments to arrays of ints (ASCII values)
237 if type ( s ) == " string " then
238 s = { string.byte ( s, 1 , s_len ) }
239 end
240
241 if type ( t ) == " string " then
242 t = { string.byte ( t, 1 , t_len ) }
243 end
244
245 local min = math.min -- Localize for performance
246 local num_columns = t_len + 1 -- We use this a lot
247
248 local d = {} -- (s_len+1) * (t_len+1) is going to be the size of this array
249 -- This is technically a 2D array, but we're treating it as 1D. Remember that 2D access in the
250 -- form my_2d_array[ i, j ] can be converted to my_1d_array[ i * num_columns + j ], where
251 -- num_columns is the number of columns you had in the 2D array assuming row-major order and
252 -- that row and column indices start at 0 (we're starting at 0).
253
254 for i= 0 , s_len do
255 d[ i * num_columns ] = i -- Initialize cost of deletion
256 end
257 for j= 0 , t_len do
258 d[ j ] = j -- Initialize cost of insertion
259 end
260
261 for i= 1 , s_len do
262 local i_pos = i * num_columns
263 local best = lim -- Check to make sure something in this row will be below the limit
264 for j= 1 , t_len do
265 local add_cost = (s[ i ] ~= t[ j ] and 1 or 0 )
266 local val = min(
267 d[ i_pos - num_columns + j ] + 1 , -- Cost of deletion
268 d[ i_pos + j - 1 ] + 1 , -- Cost of insertion
269 d[ i_pos - num_columns + j - 1 ] + add_cost -- Cost of substitution, it might not cost anything if it's the same
270 )
271 d[ i_pos + j ] = val
272
273 -- Is this eligible for tranposition?
274 if i > 1 and j > 1 and s[ i ] == t[ j - 1 ] and s[ i - 1 ] == t[ j ] then
275 d[ i_pos + j ] = min(
276 val, -- Current cost
277 d[ i_pos - num_columns - num_columns + j - 2 ] + add_cost -- Cost of transposition
278 )
279 end
280
281 if lim and val < best then
282 best = val
283 end
284 end
285
286 if lim and best >= lim then
287 return lim
288 end
289 end
290
291 return d[ # d ]
292 end
293 -- 判断字符串是否相似
294 function isStringLike(str1, str2)
295 local similarity = EditDistance(str1,str2, 1 )
296 -- 相似:相似度大于字符串长度的一半
297 if similarity > #str1/ 2 and similarity > #str2/ 2 then
298 return true
299 else
300 return false
301 end
302 end
1 function clear()
2 -- linux下为
3 -- os.execute("clear")
4 -- windows下为
5 os.execute ( " cls " )
6 end ;
7
8 -- 输入选项
9 function inputSelect(s,n)
10 io.write (s)
11 if n == nil then
12 n = 1
13 end ;
14 return io.read (n)
15 end ;
16
17 -- buy somthing
18 function buySomething(p,s,n)
19 if p.wallet > n then
20 if s == " ATK " then
21 p.ATK = p.ATK + n
22 elseif s == " DEF " then
23 p.DEF = p.DEF + n
24 elseif s == " HP " then
25 p.HP = p.HP + n
26 end ;
27 p.wallet = p.wallet - n
28 else
29 print ( " your haven't more money to buy it " )
30 end ;
31 end ;
32
33 -- show stat
34 function show_stat(p)
35 io.write ( " ATK:\t " ..p.ATK.. " \n " )
36 io.write ( " DEF:\t " ..p.DEF.. " \n " )
37 io.write ( " HP:\t " ..p.HP.. " \n " )
38 io.write ( " wallet:\t " ..p.wallet.. " \n " )
39 sleep( 3 )
40 end ;
41
42 -- 进入商店
43 function goto_shop()
44 clear()
45 print ( " you are in shop now. " )
46 print ( " 1. buy ATK " )
47 print ( " 2. buy DEF " )
48 print ( " 3. buy HP " )
49 print ( " 0. back " )
50 input = inputSelect( " please input number: " )
51 if input == " 1 " then
52 buySomething(P, " ATK " , 1 )
53 print ( " you buy ATK OK! " )
54 show_stat(P)
55 elseif input == " 2 " then
56 buySomething(P, " DEF " , 1 )
57 print ( " you buy ATK OK! " )
58 show_stat(P)
59 elseif input == " 3 " then
60 buySomething(P, " HP " , 1 )
61 print ( " you buy ATK OK! " )
62 show_stat(P)
63 elseif input == " 0 " then
64 show_welcome()
65 else
66 goto_shop()
67 end ;
68 goto_shop()
69 end ;
70
71 -- 显示战场
72 function show_battlefield()
73 clear()
74 print ( " PKing " )
75 io.write ( " Person:\tyou\tmonster\n " )
76 end ;
77
78 -- A hit B
79 function AhitB(A, B)
80 B.HP = B.HP - A.ATK / B.DEF
81 -- B.HP = B.HP - 30
82 end ;
83
84 function show_result(p,e)
85 io.write ( " ATK:\t " ..p.ATK.. " \t " ..e.ATK.. " \n " )
86 io.write ( " DEF:\t " ..p.DEF.. " \t " ..e.DEF.. " \n " )
87 io.write ( " HP:\t " ..p.HP.. " \t " ..e.HP.. " \n " )
88 end ;
89
90 -- 时间暂停n秒
91 function sleep(n)
92 t = os.time ()
93 while os.time () - t < n do
94 end ;
95 end ;
96
97 -- 战场,返回false则战败,返回true则胜利
98 function battlefield(p,e)
99 while true do
100 -- p hit e
101 if p.HP <= 0 then
102 return false
103 end ;
104 AhitB(p,e)
105 show_battlefield()
106 io.write ( " \tyou hit moster\n " )
107 show_result(p,e)
108 sleep( 1 )
109
110 -- e hit p
111 if e.HP <= 0 then
112 return true
113 end ;
114 AhitB(e,p)
115 show_battlefield()
116 io.write ( " \tmoster hit you\n " )
117 show_result(p,e)
118 sleep( 1 )
119 end ;
120 end ;
121
122
123 -- 进入战场选择菜单
124 function goto_battlefield()
125 clear()
126
127 print ( " you are in battlefield now. " )
128 print ( " 1. low level battlefield " )
129 print ( " 2. intermediate battlefield " )
130 print ( " 3. advanced battlefield " )
131 print ( " 0. back " )
132 input = inputSelect( " please choose battlefield: " )
133 if input == " 1 " then
134 print ( " in here " )
135 E = {
136 ATK = E1.ATK,
137 DEF = E1.DEF,
138 HP = E1.HP}
139 if battlefield(P,E) then
140 goto_battlefield()
141 print ( " you win " )
142 P.HP = P.HP + 10
143 sleep( 2 )
144 else
145 print ( " you die " )
146 print ( " GAME OVER " )
147 end ;
148 elseif input == " 2 " then
149 E = {
150 ATK = E2.ATK,
151 DEF = E2.DEF,
152 HP = E2.HP}
153 if battlefield(P,E) then
154 goto_battlefield()
155 print ( " you win " )
156 P.HP = P.HP + 10
157 P.wallet = P.wallet + 10
158 sleep( 2 )
159 else
160 print ( " you die " )
161 print ( " GAME OVER " )
162 end ;
163 elseif input == " 3 " then
164 E = {
165 ATK = E3.ATK,
166 DEF = E3.DEF,
167 HP = E3.HP}
168 if battlefield(P,E) then
169 goto_battlefield()
170 print ( " you win " )
171 P.HP = P.HP + 20
172 P.wallet = P.wallet + 10
173 P.ATK = P.ATK + 5
174 P.DEF = P.DEF + 5
175 sleep( 2 )
176 else
177 print ( " you die " )
178 print ( " GAME OVER " )
179 end ;
180 elseif input == " 0 " then
181 show_welcome()
182 else
183 goto_battlefield()
184 end ;
185 end ;
186
187 function show_welcome()
188 clear()
189 print ( " ======welcome to battle game====== " )
190 io.write ( " \tplease choose entrance\n " )
191 print ( " 1. shop " )
192 print ( " 2. battlefield entrance " )
193 print ( " 0. exit " )
194 input = inputSelect( " please input number of the entrance: " )
195 if input == " 1 " then
196 goto_shop()
197 elseif input == " 2 " then
198 goto_battlefield()
199 elseif input == " 0 " then
200 os.exit ()
201 else
202 show_welcome()
203 end ;
204 end ;
205
206 P = {
207 wallet = 100 ,
208 ATK = 100 ,
209 DEF = 100 ,
210 HP = 100 }
211 E1 = {
212 ATK = 10 ,
213 DEF = 10 ,
214 HP = 100 }
215
216 E2 = {
217 ATK = 20 ,
218 DEF = 20 ,
219 HP = 100 }
220 E3 = {
221 ATK = 30 ,
222 DEF = 30 ,
223 HP = 100 }
224
225 show_welcome()
226
227
228
229 -- 字符串相似度算法 lua 实现
230 function EditDistance( s, t, lim )
231 local s_len, t_len = #s, #t -- Calculate the sizes of the strings or arrays
232 if lim and math.abs ( s_len - t_len ) >= lim then -- If sizes differ by lim, we can stop here
233 return lim
234 end
235
236 -- Convert string arguments to arrays of ints (ASCII values)
237 if type ( s ) == " string " then
238 s = { string.byte ( s, 1 , s_len ) }
239 end
240
241 if type ( t ) == " string " then
242 t = { string.byte ( t, 1 , t_len ) }
243 end
244
245 local min = math.min -- Localize for performance
246 local num_columns = t_len + 1 -- We use this a lot
247
248 local d = {} -- (s_len+1) * (t_len+1) is going to be the size of this array
249 -- This is technically a 2D array, but we're treating it as 1D. Remember that 2D access in the
250 -- form my_2d_array[ i, j ] can be converted to my_1d_array[ i * num_columns + j ], where
251 -- num_columns is the number of columns you had in the 2D array assuming row-major order and
252 -- that row and column indices start at 0 (we're starting at 0).
253
254 for i= 0 , s_len do
255 d[ i * num_columns ] = i -- Initialize cost of deletion
256 end
257 for j= 0 , t_len do
258 d[ j ] = j -- Initialize cost of insertion
259 end
260
261 for i= 1 , s_len do
262 local i_pos = i * num_columns
263 local best = lim -- Check to make sure something in this row will be below the limit
264 for j= 1 , t_len do
265 local add_cost = (s[ i ] ~= t[ j ] and 1 or 0 )
266 local val = min(
267 d[ i_pos - num_columns + j ] + 1 , -- Cost of deletion
268 d[ i_pos + j - 1 ] + 1 , -- Cost of insertion
269 d[ i_pos - num_columns + j - 1 ] + add_cost -- Cost of substitution, it might not cost anything if it's the same
270 )
271 d[ i_pos + j ] = val
272
273 -- Is this eligible for tranposition?
274 if i > 1 and j > 1 and s[ i ] == t[ j - 1 ] and s[ i - 1 ] == t[ j ] then
275 d[ i_pos + j ] = min(
276 val, -- Current cost
277 d[ i_pos - num_columns - num_columns + j - 2 ] + add_cost -- Cost of transposition
278 )
279 end
280
281 if lim and val < best then
282 best = val
283 end
284 end
285
286 if lim and best >= lim then
287 return lim
288 end
289 end
290
291 return d[ # d ]
292 end
293 -- 判断字符串是否相似
294 function isStringLike(str1, str2)
295 local similarity = EditDistance(str1,str2, 1 )
296 -- 相似:相似度大于字符串长度的一半
297 if similarity > #str1/ 2 and similarity > #str2/ 2 then
298 return true
299 else
300 return false
301 end
302 end
作者: 涵曦 ( 涵曦的技术博客 - 博客园 )
微博: t.qq测试数据/hanxi1203
出处: hanxi.cnblogs测试数据
GitHub: github测试数据/hanxi
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: 随感
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息