Студопедия

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

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

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






A Simple Sliding Puzzle Game




The following program is the classic 8-puzzle, in which you have 8 moveable tiles and 1 empty space. Touch the colored tile that you want to move into the empty space. This code uses many of the graphics features, conditional evaluations and flow control structures you've seen so far:

! Set up the screen: gr.open 255, 255, 255, 255 % white backgroundgr.orientation 1 % portraid mode! We'll create 9 different colored tiles, each 100 pixels across, laid! out in a square grid. The first row of 3 tiles will be placed at height! 0-100, the next row at height 100-200, and the last row at height! 200-300:! 1st tile (rct1): gr.color 255, 0, 0, 255, 1 % COLOR (transp, red, green, blue, fill)gr.rect rct1, 0, 0, 100, 100 % POSITION (left, top, right bottom)! 2nd tile (rct2): gr.color 255, 0, 255, 0, 1 % (change color for every new box...)gr.rect rct2, 100, 0, 200, 100 % (change position for every new box...)! 3rd tile (rct3): gr.color 255, 255, 0, 0, 1 % colorgr.rect rct3, 200, 0, 300, 100 % create and position! 4th tile: gr.color 255, 255, 255, 0, 1 % colorgr.rect rct4, 0, 100, 100, 200 % position! 5th tile: gr.color 255, 0, 255, 255, 1 % colorgr.rect rct5, 100, 100, 200, 200 % position! 6th tile: gr.color 255, 128, 0, 0, 1 % colorgr.rect rct6, 200, 100, 300, 200 % position! 7th tile: gr.color 255, 0, 128, 0, 1 % colorgr.rect rct7, 0, 200, 100, 300 % position! 8th tile: gr.color 255, 0, 0, 0, 1 % colorgr.rect rct8, 100, 200, 200, 300 % position! 9th tile (this one represents an empty space, so it's color is white): gr.color 255, 255, 255, 255, 1 % colorgr.rect rct9, 200, 200, 300, 300 % position! Show the screen gr.render! Now start an endless loop to constantly get user input, and then do! something with that input: getInput:! Get a touch event: do gr.touch touched, x, y until touched! Set the X and Y coordinate values to the CORNER of the touched! tile's position (we want to deal with the specific position values! of the touched tile, NOT with the random position at which the! user's finger touched, somewhere within the tile): if x < 100 % if X is somewhere between 0-100, set X to 0 x = 0 elseif x < 200 % bewtween 101-200, set X to 100 x = 100 else % bewtween 201-300, set X to 200 x = 200 endif if y < 100 % Do the same thing with all potential Y values y = 0 elseif y < 200 y = 100 else y = 200 endif! Now we need to get the current positions of each tile, to determine! which one was touched: 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! Compare each of the positions above to the touched corner! coordinate, and marked it as the touched tile: 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! Get the position of tile #9 (the " blank" space): gr.get.position rct9, blankx, blanky! Swap the position of the touched piece, and the blank piece.! This effectively moves the touched piece into the blank space: gr.modify tilenum, " left", blankx % move the touched piece gr.modify tilenum, " top", blanky % to the former position gr.modify tilenum, " right", blankx + 100 % of the blank piece gr.modify tilenum, " bottom", blanky + 100 gr.modify rct9, " left", x % move the black piece gr.modify rct9, " top", y % to the former position gr.modify rct9, " right", x + 100 % of the touched piece gr.modify rct9, " bottom", y + 100 gr.render! Do the entire process again and again, until the puzzle is solved: goto getInput

Here's the complete program without comments:

gr.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, 300 gr.color 255, 255, 255, 255, 1 gr.rect rct9, 200, 200, 300, 300 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 gr.rendergoto getInput

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





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