使用RingAllegro的方块拼图(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/squares-puzzle-using-ringallegro-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 9 分钟阅读 - 4033 个词 阅读量 0使用RingAllegro的方块拼图(译文)
原文地址:https://www.codeproject.com/Articles/1096942/Squares-Puzzle-using-RingAllegro
原文作者:Majdi Sobain
译文由本站 robot-v1.0 翻译
前言
This Article is about 2D games programming simplicity using RingAllegro Library.
本文是有关使用RingAllegro库简化2D游戏编程的文章.
源代码(The Source Code)
介绍(Introduction)
Squares益智游戏是一个非常著名和流行的游戏,几乎所有人都使用不同的平台或工具进行了游戏.我最近使用的示例是"(Squares puzzle game is a very known and popular game that almost all of you have played using different platforms or tools. The very recent sample that I used was “) 随机滑动大脑游戏(Shuffle ‘n Slide Brain Game) ")"( 在android上.该游戏的主要目的是将图像切成9张,然后随机绘制在板上,因此您应该重新组织它们以获得正确的图像.(on android. This game is all about slicing an image into 9 pieces that plotted randomly on a board so you are supposed to reorganize them to get the correct image.)
背景(Background)
RingAllegro是基于以下内容的Ring编程语言的非常强大且简单的扩展:(RingAllegro is a very powerful and simple extension of the Ring Programming Language that is based on) 快板图书馆(Allegro library) 致力于以合理的2D游戏开发思路轻松,流畅地创建具有高交互能力的2D游戏.(specialized in creating 2D games easly and fluently with high interaction capacity in a reasonable way of 2D game development ideas.) 环形编程语言(The Ring Programming Language) 是最近发布的创新实用的通用多范式脚本语言,可以嵌入C/C ++项目中,使用C/C ++代码扩展和/或用作独立语言.支持的编程范例是命令式,过程式,面向对象,函数式,元编程,使用嵌套结构的声明式编程以及自然编程.该语言是可移植的(Windows,Linux,Mac OS X,Android等),可用于创建控制台,GUI,Web,游戏和移动应用程序.该语言被设计为简单,小型,灵活和快速.它的动态语言(动态键入和弱类型)将源代码编译为字节码,然后由Ring虚拟机执行,该虚拟机与Ring Compiler集成在一个程序中.(is a recently released innovative and practical general-purpose multi-paradigm scripting language that can be embedded in C/C++ projects, extended using C/C++ code and/or used as standalone language. The supported programming paradigms are Imperative, Procedural, Object-Oriented, Functional, Meta programming, Declarative programming using nested structures, and Natural programming. The language is portable (Windows, Linux, Mac OS X, Android, etc.) and can be used to create Console, GUI, Web, Games and Mobile applications. The language is designed to be Simple, Small, Flexible and Fast. Its Dynamic Language (Dynamic Typing and Weakly Typed) that compile the source code to byte code then execute it by the Ring Virtual Machine, which is integrated with the Ring Compiler in one program.)
环中的编码原理(Coding Principles in Ring)
Ring编程语言中的编码过程具有必须遵循的特殊原则,这些特殊原则是为了在不同的程序员和开发人员之间实现较高的编程便利性而构建的,并且易于与其多范式哲学一起使用.就目前而言,我必须向其中的一些人展示必要的点,以披露对使阅读变得更加流畅至关重要的要点.(The coding process in Ring Programming Language has special principles, that we have to follow, which have been built to achieve high range of programing convenience between different programmers and developers, and to be easly used with its multi-paradigm philosophy. For now, I have to show some of them as necessary to disclose points that are important for reading to become smoother.) 环形编程语言并不认为Main方法是必不可少的,因此可以从第一行到最后执行编写的代码.在这种情况下,应按照下一个顺序以特殊方式组织所有代码部分:(Ring Programming Language doesn’t consider Main method essential so that the written code will be executed from the first line to the end. In this case all of code parts should be organized in a special manner following the next sequence:) 1-文件加载.(1- Files Loading.) 2-语句和全局变量.(2- Statements and global variables.) 3-功能.(3- Functions.) 4-包和类.(4- Packages and classes.) 关于环形编码,我们应考虑的其他事项包括:(* Other things that we should consider about Ring coding are:*) 1-不区分大小写.(*1- Not case sensitive.*) 2-语句没有明确的结尾(否;或ENTER是必需的).(*2- No explicit end for statements (No ; or ENTER is required).*) 3-定义前调用功能.(*3- Call Function before definition.*) 4-没有关键字结束功能/类/包.(*4- No keyword to end Functions/Classes/Packages.*)
代码概述(Code Overview)
如果您对以下方面有所了解,则该代码可以自我解释(The code is kind of self explanatory if you have some knowledge about) RingAllegro(RingAllegro) 和(and) Allegro图书馆功能(Allegro Library functions) ,因此我将尝试在其主要部分中进行一些评论.(, so that I will try to walk through its main sections with some comments.)
初始化和董事会准备:(Initialization and board preparing:)
作为许多编程语言,Ring编程语言从包含内容然后定义变量的代码开始.(As a lot of programming language, Ring programming language starts its code with inclusions then variables definition.)
Load "gamelib.ring"
BoardDim = 398 # Board dimension
SquarePostions = [[0,0],2,3,4,5,6,7,8,9] # Each position contain x,y
SquareDim = 0 # Square dimensoin
Moves = 0 # To count number of moves as to estimate the level of solving strategies
Squares = ["square",1] # Each square combined with its current position during playing
然后,我们必须初始化RingAllegro库,并创建/准备主窗口,该主窗口将包含游戏板,切片的图像以及板上和下方的标签.(Then we have to initialize RingAllegro library and create/prepare the main window that will contain playing board, sliced images and labels on and below the board.)
# Initialization ===========
al_init()
al_init_image_addon()
al_init_font_addon()
al_init_ttf_addon()
font = al_load_ttf_font("pirulen.ttf",14,0 )
display = al_create_display(406,500)
al_clear_to_color(al_map_rgb(255,255,255))
Board = al_create_bitmap(boarddim,boarddim)
al_set_target_bitmap(board)
al_clear_to_color(al_map_rgb(220,220,220))
al_set_target_bitmap(al_get_backbuffer(display))
al_draw_bitmap(board,al_get_display_width(display) / 2 - boarddim /2,al_get_display_height(display) / 2 - boarddim / 2,0)
Image = al_create_sub_bitmap(al_load_bitmap("palace.jpg"),0,0,boarddim - 8 ,boarddim - 8)
MoveLabel = al_create_bitmap(al_get_display_width(display),30)
al_set_target_bitmap(movelabel)
al_clear_to_color(al_map_rgb(220,220,220))
al_set_target_bitmap(al_get_backbuffer(display))
al_draw_text(font, al_map_rgb(0,0,0), 150, 20,ALLEGRO_ALIGN_LEFT, "Squares :)")
al_flip_display() # This function is very important after each change to be displayed
然后我们必须初始化Allegro库的事件监听机制,该机制由事件队列和输入注册组成,就像这样;(Then We have to initialize event listening mechanism of Allegro library which composed of event queue and input registration like this;)
# Events Initialization ===============================
event_queue = al_create_event_queue()
ev = al_new_allegro_event()
al_register_event_source(event_queue, al_get_display_event_source(display))
al_install_mouse()
al_register_event_source(event_queue, al_get_mouse_event_source())
al_install_keyboard()
al_register_event_source(event_queue, al_get_keyboard_event_source())
然后,我们必须确定正方形假设的每个位置的确切x,y,然后从整个图片中切片正方形.(Then we have to determine the exact x,y of each position should squares assume, followed by slicing squares from all over the picture.)
# SetSquaresPositionsAndDimensions =======================
squaredim = (boarddim - 8) / 3
boardx = al_get_display_width(display) /2 - boarddim /2
boardy = al_get_display_height(display) /2 - boarddim /2
SquarePostions[1] = [boardx +2 , boardy +2]
SquarePostions[2] = [boardx + squaredim + 4, boardy +2]
SquarePostions[3] = [boardx + squaredim * 2 + 6, boardy +2]
SquarePostions[4] = [boardx +2 , boardy + squaredim + 4]
SquarePostions[5] = [boardx + squaredim + 4, boardy + squaredim + 4]
SquarePostions[6] = [boardx + squaredim * 2 + 6, boardy + squaredim + 4]
SquarePostions[7] = [boardx +2 , boardy + squaredim * 2 + 6]
SquarePostions[8] = [boardx + squaredim + 4, boardy + squaredim * 2 + 6]
SquarePostions[9] = [boardx + squaredim * 2 + 6, boardy + squaredim * 2 + 6]
# CreateSquares ==========================
squares = list(9)
ind = 1
for j = 0 to 2
for i = 0 to 2
squares[ind] = [al_create_sub_bitmap(image,i * squaredim, j * squaredim,squaredim,squaredim),-1]
ind += 1
next
next
squares[9] = [al_create_sub_bitmap(board,1,1,squaredim,squaredim),-1]
游戏与互动:(Playing and Interaction:)
在开始游戏挑战之前,我们必须给玩家一个机会,以简化解决策略的过程.(Before starting the game challenge we have to give the player a shot to make solving strategy easer.)
# Draw full Pic with timer ===========
al_draw_bitmap(image,al_get_display_width(display) /2 - boarddim /2 + 4,al_get_display_height(display) /2 - boarddim /2 + 4,0)
al_flip_display()
timer = al_create_timer(1)
al_register_event_source(event_queue, al_get_timer_event_source(timer))
al_start_timer(timer)
moves = 3
while true
al_wait_for_event(event_queue, ev)
al_draw_bitmap(movelabel,0,al_get_display_height(display) - 30,0)
al_draw_text(font, al_map_rgb(0,0,0), 200, al_get_display_height(display) - 20,ALLEGRO_ALIGN_LEFT, string(moves))
moves -= 1
al_flip_display()
if moves = -1 moves = 0 exit ok
end
al_destroy_timer(timer)
al_rest(1)
al_draw_bitmap(movelabel,0,al_get_display_height(display) - 30,0)
al_draw_text(font, al_map_rgb(0,0,0), 150, al_get_display_height(display) - 20,ALLEGRO_ALIGN_LEFT, "Moves : " + moves)
al_flip_display()
现在是时候让玩家随机进入方块位置并将其绘制在棋盘上,从而带给他们挑战.(Now its time to bring the player into the challenge by randomizing squares positions and draw them on the board.)
# RandomizePositions ================================
for i = 1 to 9
while true
Ind = LimitRandom(1,9)
found = false
for s in squares
if ind = s[2] found = true ok
next
if found = false squares[i][2] = ind exit ok
end
next
# DrawRandomizedSquares ===============
al_set_target_bitmap(al_get_backbuffer(display))
al_draw_bitmap(board,al_get_display_width(display) / 2 - boarddim /2,al_get_display_height(display) / 2 - boarddim / 2,0)
for s in squares
al_draw_bitmap(s[1],SquarePostions[s[2]][1],SquarePostions[s[2]][2],0)
next
al_flip_display()
现在是时候把球放到球员面前,让他/她展示自己的最佳状态:)(Now its time to put the ball in front of the player to make him/her show his/her best :)) 这可以使用while循环来完成,其中包括听玩家互动,应用他/她选择的动作然后检查整个谜题的正确解决方案.(This could be done using while loop which include listening for the player interaction, applying his/her chosen move then checking for correct solution of the whole puzzle.) 当玩家成功完成拼图时,我们必须根据他/她使用的动作数来估算并给出有关他/她解决拼图的努力的意见.(When the player completed the puzzle seccessfully we have to estimate and give opinion about his/her effort solving the puzzle depending on the number of moves he/she used.)
while true
al_wait_for_event(event_queue, ev)
switch al_get_allegro_event_type(ev)
on ALLEGRO_EVENT_DISPLAY_CLOSE
exit
on ALLEGRO_EVENT_MOUSE_BUTTON_UP
mouse_x = al_get_allegro_event_mouse_x(ev)
mouse_y = al_get_allegro_event_mouse_y(ev)
for cursqr = 1 to 9
dx = mouse_x - SquarePostions[cursqr][1]
dy = mouse_y - SquarePostions[cursqr][2]
if dx < squaredim and dx > 0
if dy < squaredim and dy > 0
ns = CheckNearSpace(cursqr)
if ns != 0 exsquareposition(cursqr,ns)
ok
ok
ok
next
on ALLEGRO_EVENT_KEY_UP
switch al_get_allegro_event_keyboard_keycode(ev)
on ALLEGRO_KEY_UP
ps = GiveSelectedSqrInd(1)
if ps != 0 exsquareposition(ps,squares[9][2]) ok
on ALLEGRO_KEY_DOWN
ps = GiveSelectedSqrInd(3)
if ps != 0 exsquareposition(ps,squares[9][2]) ok
on ALLEGRO_KEY_LEFT
ps = GiveSelectedSqrInd(4)
if ps != 0 exsquareposition(ps,squares[9][2]) ok
on ALLEGRO_KEY_RIGHT
ps = GiveSelectedSqrInd(2)
if ps != 0 exsquareposition(ps,squares[9][2]) ok
off
off
if Solved() = true
ft = al_load_ttf_font("pirulen.ttf",20,0 )
lastpanel = al_create_bitmap(350,70)
al_set_target_bitmap(lastpanel)
al_clear_to_color(al_map_rgb(255,255,255))
al_set_target_bitmap(al_get_backbuffer(display))
al_draw_bitmap(lastpanel,al_get_display_width(display) /2 - boarddim /2 + 24,al_get_display_height(display) /2 -30,0)
if moves < 50
al_draw_text(ft, al_map_rgb(0,0,0), 40, al_get_display_height(display) /2 - 20,ALLEGRO_ALIGN_LEFT, "Congratulations ^_^")
al_draw_text(font, al_map_rgb(0,0,0), 80, al_get_display_height(display) /2,ALLEGRO_ALIGN_LEFT, "You Are Really Genius")
but moves >= 50 and moves < 100
al_draw_text(ft, al_map_rgb(0,0,0), 40, al_get_display_height(display) /2 - 20,ALLEGRO_ALIGN_LEFT, "Congratulations ^_^")
al_draw_text(font, al_map_rgb(0,0,0), 100, al_get_display_height(display) /2,ALLEGRO_ALIGN_LEFT, "You Are Really Smart")
but moves >= 100 and moves < 200
al_draw_text(ft, al_map_rgb(0,0,0), 40, al_get_display_height(display) /2 - 20,ALLEGRO_ALIGN_LEFT, "Congratulations ^_^")
al_draw_text(font, al_map_rgb(0,0,0), 100, al_get_display_height(display) /2,ALLEGRO_ALIGN_LEFT, "You Are Smart")
but moves >= 200
al_draw_text(ft, al_map_rgb(0,0,0), 40, al_get_display_height(display) /2 - 20,ALLEGRO_ALIGN_LEFT, "Congratulations ^_^")
al_draw_text(font, al_map_rgb(0,0,0), 90, al_get_display_height(display) /2,ALLEGRO_ALIGN_LEFT, "But Slowly Solved")
ok
al_flip_display()
al_rest(15)
exit
ok
end
终结和功能:(Finalization and Functions:)
解决这个难题之后,我们必须最终确定并销毁以前使用过的RingAllegro组件,如下所示:(After solving the puzzle we have to finalize and destroy previously used RingAllegro components like this;)
# Finalization =================
al_destroy_event_queue(event_queue)
al_destroy_allegro_event(ev)
for s in squares
al_destroy_bitmap(s[1])
next
al_destroy_bitmap(image)
al_destroy_bitmap(board)
al_destroy_display(display)
al_exit()
最后,我们已经完成了执行此简单游戏的主要代码,但是我们没有谈论的其余代码行是针对在此游戏中本地定义和使用的功能的,因为这是Ring中代码组织的规则在代码底部声明类和函数的语言.(Finally we have finished the major code that executes this simple game but the rest of lines of code that we didn’t talk about are for the functions that have been defined and used locally in this game because this is the rule of code organization in Ring Language to declare classes and functions at the bottom of the code.)
func LimitRandom S,E
while true
r = random(e)
if r >= s return r ok
end
func ExSquarePosition filled,spaced
al_set_target_bitmap(al_get_backbuffer(display))
al_draw_bitmap(squares[find(squares,filled,2)][1],SquarePostions[squares[9][2]][1],SquarePostions[squares[9][2]][2],0)
al_draw_bitmap(squares[9][1],SquarePostions[filled][1],SquarePostions[filled][2],0)
squares[9][2] = filled
squares[find(squares,filled,2)][2] = spaced
moves += 1
al_draw_bitmap(movelabel,0,al_get_display_height(display) - 30,0)
al_draw_text(font, al_map_rgb(0,0,0), 150, al_get_display_height(display) - 20,ALLEGRO_ALIGN_LEFT, "Moves : " + moves)
al_flip_display()
func CheckNearSpace i
S = squares[9][2]
switch i
on 1
switch s on 2 return s on 4 return s off
on 2
switch s on 1 return s on 3 return s on 5 return s off
on 3
switch s on 2 return s on 6 return s off
on 4
switch s on 1 return s on 5 return s on 7 return s off
on 5
switch s on 2 return s on 4 return s on 6 return s on 8 return s off
on 6
switch s on 3 return s on 5 return s on 9 return s off
on 7
switch s on 4 return s on 8 return s off
on 8
switch s on 5 return s on 7 return s on 9 return s off
on 9
switch s on 6 return s on 8 return s off
off
return 0
func GiveSelectedSqrInd Dir # Up 1, Right 2, Down 3, Left 4
S = squares[9][2]
switch s
on 1
switch dir on 1 return 4 on 4 return 2 off
on 2
switch dir on 1 return 5 on 2 return 1 on 4 return 3 off
on 3
switch dir on 1 return 6 on 2 return 2 off
on 4
switch dir on 1 return 7 on 4 return 5 on 3 return 1 off
on 5
switch dir on 1 return 8 on 2 return 4 on 3 return 2 on 4 return 6 off
on 6
switch dir on 1 return 9 on 2 return 5 on 3 return 3 off
on 7
switch dir on 3 return 4 on 4 return 8 off
on 8
switch dir on 2 return 7 on 3 return 5 on 4 return 9 off
on 9
switch dir on 2 return 8 on 3 return 6 off
off
return 0
func Solved
for i = 1 to 9
if squares[i][2] != i return false ok
next
return true
兴趣点(Points of Interest)
我必须在这里声明的主要兴趣点是使用Ring Programming Language进行编码过程的简便性,因为我不必担心应该使用的变量类型或数组的尺寸和大小,甚至可以用过的.(The major point of interest that I have to declare here is the easiness and simplicity of coding journey using Ring Programming Language as I’m not supposed to worry about type of variables that should I use or type and size even dimension of arrays that can be used.) 还有一点是,该游戏可以跨平台玩,因此我可以轻松地将其轻松移植到其他操作系统.(Other point is that this game could be played cross-platformly so that I could comfortably porting it to other OS with a large ease.) 我要强调的最后一点是,Allegro库具有一种非常好的创新方式,可以通过简单的预定义功能对简单的游戏进行编程,因此,我认为很幸运地发现它已与Ring Programming Language集成在一起.(The last thing I wanna highlight is that, Allegro library has a very nice innovative way of programming simple games with already clear predefined functions so that I think I’m really lucky to find it already integrated with Ring Programming Language.)
资源资源(Resources)
1-(1-) Ring编程语言网站.(Ring Programming Language Website.) 2(2-) Allegro图书馆网站.(Allegro Library Website.) 3(3-) 该游戏的Github存储库.(This game’s Github repository.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
2D game 新闻 翻译