Главная страница Случайная страница КАТЕГОРИИ: АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника |
Adding More Features to the Sliding Puzzle GameThe previous tile game provides a nice minimal example of how to respond to user touch, and how to move graphics interactively on screen. In order to make a more playable game, a few additional features are essential. First, keeping track of the number of swap moves provides a means of keeping score: score = 0gr.open 255, 255, 255, 255gr.orientation 1gr.color 255, 0, 0, 255, 1 gr.rect rct1, 0, 0, 100, 100 gr.color 255, 0, 255, 0, 1 gr.rect rct2, 100, 0, 200, 100 gr.color 255, 255, 0, 0, 1 gr.rect rct3, 200, 0, 300, 100 gr.color 255, 255, 255, 0, 1 gr.rect rct4, 0, 100, 100, 200 gr.color 255, 0, 255, 255, 1 gr.rect rct5, 100, 100, 200, 200 gr.color 255, 128, 0, 0, 1 gr.rect rct6, 200, 100, 300, 200 gr.color 255, 0, 128, 0, 1 gr.rect rct7, 0, 200, 100, 300 gr.color 255, 0, 0, 0, 1 gr.rect rct8, 100, 200, 200, 300gr.color 255, 255, 255, 255, 1 gr.rect rct9, 200, 200, 300, 300gr.color 255, 0, 0, 0, 1 gr.text.draw scoreboard, 0, 350, " Move: " + str$(score)gr.rendergetInput: do gr.touch touched, x, y until touched if x < 100 x = 0 elseif x < 200 x = 100 else x = 200 endif if y < 100 y = 0 elseif y < 200 y = 100 else y = 200 endif gr.get.position rct1, x1, y1 gr.get.position rct2, x2, y2 gr.get.position rct3, x3, y3 gr.get.position rct4, x4, y4 gr.get.position rct5, x5, y5 gr.get.position rct6, x6, y6 gr.get.position rct7, x7, y7 gr.get.position rct8, x8, y8 gr.get.position rct9, x9, y9 if (x = x1) & (y = y1) then tilenum = rct1 if (x = x2) & (y = y2) then tilenum = rct2 if (x = x3) & (y = y3) then tilenum = rct3 if (x = x4) & (y = y4) then tilenum = rct4 if (x = x5) & (y = y5) then tilenum = rct5 if (x = x6) & (y = y6) then tilenum = rct6 if (x = x7) & (y = y7) then tilenum = rct7 if (x = x8) & (y = y8) then tilenum = rct8 if (x = x9) & (y = y9) then tilenum = rct9 gr.get.position rct9, blankx, blanky gr.modify tilenum, " left", blankx gr.modify tilenum, " top", blanky gr.modify tilenum, " right", blankx + 100 gr.modify tilenum, " bottom", blanky + 100 gr.modify rct9, " left", x gr.modify rct9, " top", y gr.modify rct9, " right", x + 100 gr.modify rct9, " bottom", y + 100 score = score + 1 gr.modify scoreboard, " text", " Moves: " + str$(score) gr.rendergoto getInputYou may have noticed that the above game allows for illegal moves (i.e., pieces that are not next to the blank space can be moved directly into the blank space). This next version compares the positions of the blank piece and the touched piece to eliminate that possibility: score = 0gr.open 255, 255, 255, 255gr.orientation 1gr.color 255, 0, 0, 255, 1 gr.rect rct1, 0, 0, 100, 100 gr.color 255, 0, 255, 0, 1 gr.rect rct2, 100, 0, 200, 100 gr.color 255, 255, 0, 0, 1 gr.rect rct3, 200, 0, 300, 100 gr.color 255, 255, 255, 0, 1 gr.rect rct4, 0, 100, 100, 200 gr.color 255, 0, 255, 255, 1 gr.rect rct5, 100, 100, 200, 200 gr.color 255, 128, 0, 0, 1 gr.rect rct6, 200, 100, 300, 200 gr.color 255, 0, 128, 0, 1 gr.rect rct7, 0, 200, 100, 300 gr.color 255, 0, 0, 0, 1 gr.rect rct8, 100, 200, 200, 300gr.color 255, 255, 255, 255, 1 gr.rect rct9, 200, 200, 300, 300gr.color 255, 0, 0, 0, 1 gr.text.draw scoreboard, 0, 350, " Move: " + str$(score)gr.rendergetInput: do gr.touch touched, x, y until touched if x < 100 x = 0 elseif x < 200 x = 100 else x = 200 endif if y < 100 y = 0 elseif y < 200 y = 100 else y = 200 endif gr.get.position rct1, x1, y1 gr.get.position rct2, x2, y2 gr.get.position rct3, x3, y3 gr.get.position rct4, x4, y4 gr.get.position rct5, x5, y5 gr.get.position rct6, x6, y6 gr.get.position rct7, x7, y7 gr.get.position rct8, x8, y8 gr.get.position rct9, x9, y9 if (x = x1) & (y = y1) then tilenum = rct1 if (x = x2) & (y = y2) then tilenum = rct2 if (x = x3) & (y = y3) then tilenum = rct3 if (x = x4) & (y = y4) then tilenum = rct4 if (x = x5) & (y = y5) then tilenum = rct5 if (x = x6) & (y = y6) then tilenum = rct6 if (x = x7) & (y = y7) then tilenum = rct7 if (x = x8) & (y = y8) then tilenum = rct8 if (x = x9) & (y = y9) then tilenum = rct9 gr.get.position rct9, blankx, blanky diffx = abs(x - blankx) diffy = abs(y - blanky) if ((diffx + diffy) < 101) gr.modify tilenum, " left", blankx gr.modify tilenum, " top", blanky gr.modify tilenum, " right", blankx + 100 gr.modify tilenum, " bottom", blanky + 100 gr.modify rct9, " left", x gr.modify rct9, " top", y gr.modify rct9, " right", x + 100 gr.modify rct9, " bottom", y + 100 score = score + 1 gr.modify scoreboard, " text", " Moves: " + str$(score) gr.render endifgoto getInputFinally, adding some text to each tile makes it easier to clarify the order in which the tiles should be rearranged. In the following example, numbers are added in a text field, placed at the center of each tile. The appropriate text objects are moved whenever their associated tile images are moved: Данная страница нарушает авторские права? |