Guest User

Untitled

a guest
May 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.00 KB | None | 0 0
  1. GraphicsWindow.KeyDown = HandleKey
  2. GraphicsWindow.BackgroundColor = GraphicsWindow.GetColorFromRGB( 253, 252, 251 )
  3.  
  4. While "True"
  5. BOXES = 4 ' number of boxes per piece
  6. BWIDTH = 25 ' box width in pixels
  7. XOFFSET = 40 ' Screen X offset in pixels of where the board starts
  8. YOFFSET = 40 ' Screen Y offset in pixels of where the board starts
  9. CWIDTH = 10 ' Canvas Width, in number of boxes
  10. CHEIGHT = 20 ' Canvas Height, in number of boxes.
  11. STARTDELAY = 800
  12. ENDDELAY = 175
  13. PREVIEW_xpos = 13
  14. PREVIEW_ypos = 2
  15.  
  16. GraphicsWindow.Clear()
  17. GraphicsWindow.Title = "Small Basic Tetris"
  18. GraphicsWindow.Height = 580
  19. GraphicsWindow.Width = 700
  20. GraphicsWindow.Show()
  21.  
  22. SetupTemplates()
  23. SetupCanvas()
  24. MainLoop()
  25.  
  26. GraphicsWindow.ShowMessage( "Game Over", "Small Basic Tetris" )
  27. EndWhile
  28.  
  29. Sub MainLoop
  30. template = Text.Append("template", Math.GetRandomNumber(7))
  31.  
  32. CreatePiece() ' in: template ret: h
  33. nextPiece = h
  34.  
  35. end = 0
  36. sessionDelay = STARTDELAY
  37. While end = 0
  38. If sessionDelay > ENDDELAY Then
  39. sessionDelay = sessionDelay - 1
  40. EndIf
  41.  
  42. delay = sessionDelay
  43. thisPiece = nextPiece
  44. template = Text.Append("template", Math.GetRandomNumber(7))
  45.  
  46. CreatePiece() ' in: template ret: h
  47. nextPiece = h
  48. DrawPreviewPiece()
  49.  
  50. h = thisPiece
  51.  
  52. ypos = 0
  53. done = 0
  54. xpos = 3 ' always drop from column 3
  55. CheckStop() ' in: ypos, xpos, h ret: done
  56. If done = 1 Then
  57. ypos = ypos - 1
  58. MovePiece() 'in: ypos, xpos, h
  59. end = 1
  60. EndIf
  61.  
  62. yposdelta = 0
  63. While done = 0 Or yposdelta > 0
  64. MovePiece() 'in: ypos, xpos, h
  65.  
  66. ' Delay, but break if the delay get set to 0 if the piece gets dropped
  67. delayIndex = delay
  68. While delayIndex > 0 And delay > 0
  69. Program.Delay(10)
  70. delayIndex = delayIndex - 10
  71. EndWhile
  72.  
  73. If yposdelta > 0 Then
  74. yposdelta = yposdelta - 1 ' used to create freespin, when the piece is rotated
  75. Else
  76. ypos = ypos + 1 ' otherwise, move the piece down.
  77. EndIf
  78.  
  79. ' Check if the piece should stop.
  80. CheckStop() ' in: ypos, xpos, h ret: done
  81. EndWhile
  82. EndWhile
  83. EndSub
  84.  
  85. Sub HandleKey
  86. ' Stop game
  87. If GraphicsWindow.LastKey = "Escape" Then
  88. Program.End()
  89. EndIf
  90.  
  91. ' Move piece left
  92. If GraphicsWindow.LastKey = "Left" Then
  93. moveDirection = -1
  94. ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0
  95. If invalidMove = 0 Then
  96. xpos = xpos + moveDirection
  97. EndIf
  98. MovePiece() 'in: ypos, xpos, h
  99. EndIf
  100.  
  101. ' Move piece right
  102. If GraphicsWindow.LastKey = "Right" Then
  103. moveDirection = 1
  104. ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0
  105. If invalidMove = 0 Then
  106. xpos = xpos + moveDirection
  107. EndIf
  108. MovePiece() 'in: ypos, xpos, h
  109. EndIf
  110.  
  111. ' Move piece down
  112. If GraphicsWindow.LastKey = "Down" or GraphicsWindow.LastKey = "Space" Then
  113. delay = 0
  114. EndIf
  115.  
  116. ' Rotate piece
  117. If GraphicsWindow.LastKey = "Up" Then
  118. basetemplate = Array.GetValue(h, -1) ' Array.GetValue(h, -1) = the template name
  119. template = "temptemplate"
  120. rotation = "CW"
  121. CopyPiece() 'in basetemplate, template, rotation
  122.  
  123. Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name
  124. moveDirection = 0
  125. ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0
  126.  
  127. ' See if it can be moved so that it will rotate.
  128. xposbk = xpos
  129. yposdelta = 0
  130. While yposdelta = 0 And Math.Abs(xposbk - xpos) < 3 ' move up to 3 times only
  131. ' if the rotation move worked, copy the temp to "rotatedtemplate" and use that from now on
  132. If invalidMove = 0 Then
  133. basetemplate = template
  134. template = "rotatedtemplate"
  135. Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name
  136. rotation = "COPY"
  137. CopyPiece() 'in basetemplate, template, rotation
  138. yposdelta = 1 ' Don't move down if we rotate
  139. MovePiece() 'in: ypos, xpos, h
  140. ElseIf invalidMove = 2 Then
  141. ' Don't support shifting piece when hitting another piece to the right or left.
  142. xpos = 99 ' exit the loop
  143. Else
  144. ' if the rotated piece can't be placed, move it left or right and try again.
  145. xpos = xpos - invalidMove
  146. ValidateMove() ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0
  147. EndIf
  148. EndWhile
  149.  
  150. If invalidMove <> 0 Then
  151. xpos = xposbk
  152. Array.SetValue(h, -1, basetemplate) ' Array.GetValue(h, -1) = the template name
  153. template = ""
  154. EndIf
  155. EndIf
  156. EndSub
  157.  
  158.  
  159. Sub DrawPreviewPiece
  160. xpos = PREVIEW_xpos
  161. ypos = PREVIEW_ypos
  162. h = nextPiece
  163.  
  164. XOFFSETBK = XOFFSET
  165. YOFFSETBK = YOFFSET
  166. XOFFSET = XOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewx") ' Array.GetValue(h, -1) = the template name
  167. YOFFSET = YOFFSET + Array.GetValue(Array.GetValue(h, -1), "pviewy") ' Array.GetValue(h, -1) = the template name
  168. MovePiece() 'in: ypos, xpos, h
  169.  
  170. XOFFSET = XOFFSETBK
  171. YOFFSET = YOFFSETBK
  172. EndSub
  173.  
  174. ' creates template that's a rotated basetemplate
  175. Sub CopyPiece 'in basetemplate, template, rotation
  176. L = Array.GetValue(basetemplate, "dim")
  177.  
  178. If rotation = "CW" Then
  179. For i = 0 to BOXES - 1 ' x' = y y' = L - 1 - x
  180. v = Array.GetValue(basetemplate, i)
  181.  
  182. 'x = Math.Floor(v/10)
  183. 'y = Math.Remainder(v, 10)
  184.  
  185. ' new x and y
  186. x = (Math.Remainder(v, 10))
  187. y = (L - 1 - Math.Floor(v/10))
  188. Array.SetValue(template, i, x * 10 + y)
  189. EndFor
  190. ' Count-Cockwise is not currently used
  191. ElseIf rotation = "CCW" Then
  192. For i = 0 to BOXES - 1 ' x' = L - 1 - y y' = x
  193. v = Array.GetValue(basetemplate, i)
  194. 'x = Math.Floor(v/10)
  195. 'y = Math.Remainder(v, 10)
  196.  
  197. ' new x and y
  198. x = (L - 1 - Math.Remainder(v, 10))
  199. y = Math.Floor(v/10)
  200. Array.SetValue(template, i, x * 10 + y)
  201. EndFor
  202. ElseIf rotation = "COPY" Then
  203. For i = 0 to BOXES - 1
  204. Array.SetValue(template, i, Array.GetValue(basetemplate, i))
  205. EndFor
  206. Else
  207. GraphicsWindow.ShowMessage("invalid parameter", "Error")
  208. Program.End()
  209. EndIf
  210.  
  211. ' Copy the remain properties from basetemplate to template.
  212. Array.SetValue(template, "color", Array.GetValue(basetemplate, "color"))
  213. Array.SetValue(template, "dim", Array.GetValue(basetemplate, "dim"))
  214. Array.SetValue(template, "pviewx", Array.GetValue(basetemplate, "pviewx"))
  215. Array.SetValue(template, "pviewy", Array.GetValue(basetemplate, "pviewy"))
  216. EndSub
  217.  
  218. Sub CreatePiece ' in: template ret: h
  219. ' Create a new handle, representing an arrayName, that will represent the piece
  220. hcount = hcount + 1
  221. h = Text.Append("piece", hcount)
  222.  
  223. Array.SetValue(h, -1, template) ' Array.GetValue(h, -1) = the template name
  224.  
  225. GraphicsWindow.PenWidth = 1
  226. GraphicsWindow.PenColor = "Black"
  227. GraphicsWindow.BrushColor = Array.GetValue(template, "color")
  228.  
  229. For i = 0 to BOXES - 1
  230. s = Shapes.AddRectangle(BWIDTH, BWIDTH)
  231. Shapes.Move(s, -BWIDTH, -BWIDTH) ' move off screen
  232. Array.SetValue(h, i, s)
  233. EndFor
  234. EndSub
  235.  
  236. Sub MovePiece 'in: ypos, xpos, h. ypos/xpos is 0-19, representing the top/left box coordinate of the piece on the canvas. h returned by CreatePiece
  237. For i = 0 to BOXES - 1
  238. v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name
  239. x = Math.Floor(v/10)
  240. y = Math.Remainder(v, 10)
  241.  
  242. ' Array.GetValue(h, i) = box for piece h.
  243. ' xpos/ypos = are topleft of shape. x/y is the box offset within the shape.
  244. Shapes.Move(Array.GetValue(h, i), XOFFSET + xpos * BWIDTH + x * BWIDTH, YOFFSET + ypos * BWIDTH + y * BWIDTH)
  245. EndFor
  246. EndSub
  247.  
  248. Sub ValidateMove ' in: ypos, xpos, h, moveDirection ret: invalidMove = 1 or -1 or 2 if move is invalid, otherwise 0
  249. i = 0
  250. invalidMove = 0
  251. While i < BOXES
  252. v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name
  253.  
  254. 'x/y is the box offset within the shape.
  255. x = Math.Floor(v/10)
  256. y = Math.Remainder(v, 10)
  257.  
  258. If (x + xpos + moveDirection) < 0 Then
  259. invalidMove = -1
  260. i = BOXES ' force getting out of the loop
  261. EndIf
  262.  
  263. If (x + xpos + moveDirection) >= CWIDTH Then
  264. invalidMove = 1
  265. i = BOXES ' force getting out of the loop
  266. EndIf
  267.  
  268. If Array.GetValue("c", (x + xpos + moveDirection) + (y + ypos) * CWIDTH) <> "." Then
  269. invalidMove = 2
  270. i = BOXES ' force getting out of the loop
  271. EndIf
  272.  
  273. i = i + 1
  274. EndWhile
  275. EndSub
  276.  
  277.  
  278. Sub CheckStop ' in: ypos, xpos, h ret: done
  279. done = 0
  280. i = 0
  281. While i < BOXES
  282. v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name
  283.  
  284. 'x/y is the box offset within the shape.
  285. x = Math.Floor(v/10)
  286. y = Math.Remainder(v, 10)
  287.  
  288. If y + ypos > CHEIGHT Or Array.GetValue("c", (x + xpos) + (y + ypos) * CWIDTH) <> "." Then
  289. done = 1
  290. i = BOXES ' force getting out of the loop
  291. EndIf
  292.  
  293. i = i + 1
  294. EndWhile
  295.  
  296. ' If we need to stop the piece, move the box handles to the canvas
  297. If done = 1 Then
  298. For i = 0 to BOXES - 1
  299. v = Array.GetValue(Array.GetValue(h, -1), i) ' Array.GetValue(h, -1) = the template name
  300. 'x = Math.Floor(v/10)
  301. 'y = Math.Remainder(v, 10)
  302. Array.SetValue("c", (Math.Floor(v/10) + xpos) + (Math.Remainder(v, 10) + ypos - 1) * CWIDTH, Array.GetValue(h, i))
  303. EndFor
  304.  
  305. ' 1 points for every piece successfully dropped
  306. score = score + 1
  307. PrintScore()
  308.  
  309. ' Delete clared lines
  310. DeleteLines()
  311. EndIf
  312. EndSub
  313.  
  314.  
  315. Sub DeleteLines
  316. linesCleared = 0
  317.  
  318. ' Iterate over each row, starting from the bottom
  319. For y = CHEIGHT - 1 to 0 Step -1
  320.  
  321. ' Check to see if the whole row is filled
  322. x = CWIDTH
  323. While x = CWIDTH
  324. x = 0
  325. While x < CWIDTH
  326. piece = Array.GetValue("c", x + y * CWIDTH)
  327. If piece = "." then
  328. x = CWIDTH
  329. EndIf
  330. x = x + 1
  331. EndWhile
  332.  
  333. ' if non of them were empty (i.e "."), then remove the line.
  334. If x = CWIDTH Then
  335.  
  336. ' Delete the line
  337. For x1 = 0 to CWIDTH - 1
  338. Shapes.Remove(Array.GetValue("c", x1 + y * CWIDTH))
  339. EndFor
  340. linesCleared = linesCleared + 1
  341.  
  342. ' Move everything else down one.
  343. For y1 = y To 1 Step -1
  344. For x1 = 0 to CWIDTH - 1
  345. piece = Array.GetValue("c", x1 + (y1 - 1) * CWIDTH)
  346. Array.SetValue("c", x1 + y1 * CWIDTH, piece)
  347. Shapes.Move(piece, Shapes.GetLeft(piece), Shapes.GetTop(piece) + BWIDTH)
  348. EndFor
  349. EndFor
  350. EndIf
  351. EndWhile
  352. EndFor
  353.  
  354. If linesCleared > 0 Then
  355. score = score + 100 * Math.Round(linesCleared * 2.15 - 1)
  356. PrintScore()
  357. EndIf
  358. EndSub
  359.  
  360. Sub SetupCanvas
  361. ' GraphicsWindow.DrawResizedImage( Flickr.GetRandomPicture( "bricks" ), 0, 0, GraphicsWindow.Width, GraphicsWindow.Height)
  362.  
  363.  
  364. GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
  365. GraphicsWindow.FillRectangle(XOFFSET, YOFFSET, CWIDTH*BWIDTH, CHEIGHT*BWIDTH)
  366.  
  367. Program.Delay(200)
  368. GraphicsWindow.PenWidth = 1
  369. GraphicsWindow.PenColor = "Pink"
  370. For x = 0 To CWIDTH-1
  371. For y = 0 To CHEIGHT-1
  372. Array.SetValue("c", x + y * CWIDTH, ".") ' "." indicates spot is free
  373. GraphicsWindow.DrawRectangle(XOFFSET + x * BWIDTH, YOFFSET + y * BWIDTH, BWIDTH, BWIDTH)
  374. EndFor
  375. EndFor
  376.  
  377. GraphicsWindow.PenWidth = 4
  378. GraphicsWindow.PenColor = "Black"
  379. GraphicsWindow.DrawLine(XOFFSET, YOFFSET, XOFFSET, YOFFSET + CHEIGHT*BWIDTH)
  380. GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH, YOFFSET, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH)
  381. GraphicsWindow.DrawLine(XOFFSET, YOFFSET + CHEIGHT*BWIDTH, XOFFSET + CWIDTH*BWIDTH, YOFFSET + CHEIGHT*BWIDTH)
  382.  
  383. GraphicsWindow.PenColor = "Lime"
  384. GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET, XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 6)
  385. GraphicsWindow.DrawLine(XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 6)
  386. GraphicsWindow.DrawLine(XOFFSET - 4, YOFFSET + CHEIGHT*BWIDTH + 4, XOFFSET + CWIDTH*BWIDTH + 4, YOFFSET + CHEIGHT*BWIDTH + 4)
  387.  
  388. GraphicsWindow.PenColor = "Black"
  389. GraphicsWindow.BrushColor = "Pink"
  390. x = XOFFSET + PREVIEW_xpos * BWIDTH - BWIDTH
  391. y = YOFFSET + PREVIEW_ypos * BWIDTH - BWIDTH
  392. GraphicsWindow.FillRectangle(x, y, BWIDTH * 5, BWIDTH * 6)
  393. GraphicsWindow.DrawRectangle(x, y, BWIDTH * 5, BWIDTH * 6)
  394.  
  395. GraphicsWindow.FillRectangle(x - 20, y + 190, 310, 170)
  396. GraphicsWindow.DrawRectangle(x - 20, y + 190, 310, 170)
  397.  
  398. GraphicsWindow.BrushColor = "Black"
  399. GraphicsWindow.FontItalic = "False"
  400. GraphicsWindow.FontName = "Comic Sans MS"
  401. GraphicsWindow.FontSize = 16
  402. GraphicsWindow.DrawText(x, y + 200, "Game control keys:")
  403. GraphicsWindow.DrawText(x + 25, y + 220, "Left Arrow = Move piece left")
  404. GraphicsWindow.DrawText(x + 25, y + 240, "Right Arrow = Move piece right")
  405. GraphicsWindow.DrawText(x + 25, y + 260, "Up Arrow = Rotate piece")
  406. GraphicsWindow.DrawText(x + 25, y + 280, "Down Arrow = Drop piece")
  407. GraphicsWindow.DrawText(x, y + 320, "Press to stop game")
  408.  
  409. Program.Delay(200) ' without this delay, the above text will use the fontsize of the score
  410.  
  411. GraphicsWindow.BrushColor = "Black"
  412. GraphicsWindow.FontName = "Georgia"
  413. GraphicsWindow.FontItalic = "True"
  414. GraphicsWindow.FontSize = 36
  415. GraphicsWindow.DrawText(x - 20, y + 400, "Small Basic Tetris")
  416. Program.Delay(200) ' without this delay, the above text will use the fontsize of the score
  417. GraphicsWindow.FontSize = 16
  418. GraphicsWindow.DrawText(x - 20, y + 440, "ver.0.1")
  419.  
  420. Program.Delay(200) ' without this delay, the above text will use the fontsize of the score
  421. score = 0
  422. PrintScore()
  423. EndSub
  424.  
  425.  
  426. Sub PrintScore
  427. GraphicsWindow.PenWidth = 4
  428. GraphicsWindow.BrushColor = "Pink"
  429. GraphicsWindow.FillRectangle(500, 65, 153, 50)
  430. GraphicsWindow.BrushColor = "Black"
  431. GraphicsWindow.DrawRectangle(500, 65, 153, 50)
  432. GraphicsWindow.FontItalic = "False"
  433. GraphicsWindow.FontSize = 32
  434. GraphicsWindow.FontName = "Impact"
  435. GraphicsWindow.BrushColor = "Black"
  436. GraphicsWindow.DrawText(505, 70, Text.Append(Text.GetSubText( "00000000", 0, 8 - Text.GetLength( score ) ), score))
  437. EndSub
  438.  
  439.  
  440. Sub SetupTemplates
  441. ' each piece has 4 boxes.
  442. ' the index of each entry within a piece represents the box number (1-4)
  443. ' the value of each entry represents to box zero-based box coordinate within the piece: tens place is x, ones place y
  444.  
  445. '_X_
  446. '_X_
  447. '_XX
  448.  
  449. Array.SetValue("template1", 0, 10)
  450. Array.SetValue("template1", 1, 11)
  451. Array.SetValue("template1", 2, 12)
  452. Array.SetValue("template1", 3, 22)
  453. Array.SetValue("template1", "color", "Yellow")
  454. Array.SetValue("template1", "dim", 3)
  455. Array.SetValue("template1", "pviewx", -12)
  456. Array.SetValue("template1", "pviewy", 12)
  457.  
  458.  
  459. '_X_
  460. '_X_
  461. 'XX_
  462. Array.SetValue("template2", 0, 10)
  463. Array.SetValue("template2", 1, 11)
  464. Array.SetValue("template2", 2, 12)
  465. Array.SetValue("template2", 3, 02)
  466. Array.SetValue("template2", "color", "Magenta")
  467. Array.SetValue("template2", "dim", 3)
  468. Array.SetValue("template2", "pviewx", 12)
  469. Array.SetValue("template2", "pviewy", 12)
  470.  
  471.  
  472. '_X_
  473. 'XXX
  474. '_
  475. Array.SetValue("template3", 0, 10)
  476. Array.SetValue("template3", 1, 01)
  477. Array.SetValue("template3", 2, 11)
  478. Array.SetValue("template3", 3, 21)
  479. Array.SetValue("template3", "color", "Gray")
  480. Array.SetValue("template3", "dim", 3)
  481. Array.SetValue("template3", "pviewx", 0)
  482. Array.SetValue("template3", "pviewy", 25)
  483.  
  484.  
  485. 'XX_
  486. 'XX_
  487. '_
  488. Array.SetValue("template4", 0, 00)
  489. Array.SetValue("template4", 1, 10)
  490. Array.SetValue("template4", 2, 01)
  491. Array.SetValue("template4", 3, 11)
  492. Array.SetValue("template4", "color", "Cyan")
  493. Array.SetValue("template4", "dim", 2)
  494. Array.SetValue("template4", "pviewx", 12)
  495. Array.SetValue("template4", "pviewy", 25)
  496.  
  497.  
  498. 'XX_
  499. '_XX
  500. '_
  501. Array.SetValue("template5", 0, 00)
  502. Array.SetValue("template5", 1, 10)
  503. Array.SetValue("template5", 2, 11)
  504. Array.SetValue("template5", 3, 21)
  505. Array.SetValue("template5", "color", "Green")
  506. Array.SetValue("template5", "dim", 3)
  507. Array.SetValue("template5", "pviewx", 0)
  508. Array.SetValue("template5", "pviewy", 25)
  509.  
  510.  
  511. '_XX
  512. 'XX_
  513. '_
  514. Array.SetValue("template6", 0, 10)
  515. Array.SetValue("template6", 1, 20)
  516. Array.SetValue("template6", 2, 01)
  517. Array.SetValue("template6", 3, 11)
  518. Array.SetValue("template6", "color", "Blue")
  519. Array.SetValue("template6", "dim", 3)
  520. Array.SetValue("template6", "pviewx", 0)
  521. Array.SetValue("template6", "pviewy", 25)
  522.  
  523.  
  524. '_X
  525. '_X
  526. '_X
  527. '_X
  528. Array.SetValue("template7", 0, 10)
  529. Array.SetValue("template7", 1, 11)
  530. Array.SetValue("template7", 2, 12)
  531. Array.SetValue("template7", 3, 13)
  532. Array.SetValue("template7", "color", "Red")
  533. Array.SetValue("template7", "dim", 4)
  534. Array.SetValue("template7", "pviewx", 0)
  535. Array.SetValue("template7", "pviewy", 0)
  536. EndSub
Add Comment
Please, Sign In to add comment