Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Adding More Features to the Sliding Puzzle Game




The 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 getInput

You 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 getInput

Finally, 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:

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.color 255, 255, 255, 255, 1 gr.text.draw text1, 50, 50, " 1" gr.text.draw text2, 150, 50, " 2" gr.text.draw text3, 250, 50, " 3" gr.text.draw text4, 50, 150, " 4" gr.text.draw text5, 150, 150, " 5" gr.text.draw text6, 250, 150, " 6" gr.text.draw text7, 50, 250, " 7" gr.text.draw text8, 150, 250, " 8" gr.text.draw text9, 250, 250, " 9" 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) tilenum = rct1 textnum = text1 endif if (x = x2) & (y = y2) tilenum = rct2 textnum = text2 endif if (x = x3) & (y = y3) tilenum = rct3 textnum = text3 endif if (x = x4) & (y = y4) tilenum = rct4 textnum = text4 endif if (x = x5) & (y = y5) tilenum = rct5 textnum = text5 endif if (x = x6) & (y = y6) tilenum = rct6 textnum = text6 endif if (x = x7) & (y = y7) tilenum = rct7 textnum = text7 endif if (x = x8) & (y = y8) tilenum = rct8 textnum = text8 endif if (x = x9) & (y = y9) tilenum = rct9 textnum = text9 endif 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 textnum, " x", blankx + 50 gr.modify textnum, " y", blanky + 50 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 getInput

Данная страница нарушает авторские права?





© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.