I wrote an article about Vertical Scrolling Game. That was one of challenges of the month - May 2014. At that time, I made a vertical scrolling game named Turtle Dodger. Today, I'd like to introduce the new version of this program (QZN342-4).
Main
The main part of this program calls four subroutines. Window title is changed for every versions, so the setting is placed out of Init(). A variable debug is set as "True" if we need to show debugging information.
1.' Turtle Dodger 0.6b2.' Copyright (c) 2014 Nonki Takahashi. The MIT License.3.'4.' History:5.' 0.6b 2014-07-30 Changed image and sorted subroutines. (QZN342-4)6.' 0.5b 2014-04-17 Changed to detect collision. (QZN342-3)7.' 0.4a 2014-04-17 Added opening. (QZN342-2)8.' 0.3a 2014-04-02 Avoided to hold while Turtle moving. (QZN342-1)9.' 0.2a 2014-04-02 Changed for Silverlight. (QZN342-0)10.' 0.1a 2014-04-02 Created. (QZN342)11.'12.title = "Turtle Dodger 0.6b"13.GraphicsWindow.Title = title14.debug = "False"15.Init()16.Opening()17.Game()18.Closing()Adding Obstacles
This time, I sorted subroutines in alphabetical order. Because, when a program becomes longer, it becomes little harder to find subroutines. So, I will introduce this subroutine which starts with A at the first. AddObject() adds one obstacle by calling Shapes.AddRectangle(). And calls Math.GetRandomNumber() three times for changing the color, position and angle of the obstacle.
19.Sub AddObject20. iMax = obj["iMax"] + 121. obj["iMax"] = iMax22. GraphicsWindow.PenWidth = 123. type = Math.GetRandomNumber(3)24. obj[iMax]["type"] = type25. GraphicsWindow.BrushColor = color[type]26. sz = size[type]27. obj[iMax]["obj"] = Shapes.AddRectangle(sz, sz)28. x = Math.GetRandomNumber(gw - 20) + 1029. y = -2030. obj[iMax]["x"] = x31. obj[iMax]["y"] = y32. Shapes.Move(obj[iMax]["obj"], x, y)33. Shapes.Rotate(obj[iMax]["obj"], Math.GetRandomNumber(360))34.EndSubProcedure for Closing
This subroutine rotates the Turtle three times and displays "GAME OVER". This position of the text is calculated to be centered horizontally. The detail about centering text is described here.
35.Sub Closing36. Timer.Pause()37. Turtle.Turn(720)38. GraphicsWindow.BrushColor = "White"39. GraphicsWindow.FontName = "Trebuchet MS"40. GraphicsWindow.FontSize = 4041. x = (gw - 217) / 242. y = 10043. GraphicsWindow.DrawText(x, y, "GAME OVER")44. Program.Delay(3000)45.EndSubEssential Part of Game
Subroutine Game() is the essential part of this game. I decided that the main character of this game is Turtle. May be many games uses Shapes objects. But I thought Turtle is easier to operate in the program code.
In vertical scrolling game, main character and obstacles must be moved simultaneously. A Mouse or keyboard event handler can move main character. And a timer event handler can move obstacles. That will be an event driven program. But since this program uses Turtle for main character, I gave up to move Turtle in a event handler because of an known issue in Small Basic v1.0.
While loop continues unless there happens collision between Turtle and an obstacle.
46.Sub Game47. Turtle.Speed = 748. Turtle.PenUp()49. x = gw / 250. y = gh - 4051. GraphicsWindow.BrushColor = "White"52. GraphicsWindow.FontSize = 1853. score = Shapes.AddText("0")54. Shapes.Move(score, 20, 20)55. If debug Then56. GraphicsWindow.BrushColor = "White"57. GraphicsWindow.FontSize = 1258. pos = Shapes.AddText("(" + x + "," + y + ")")59. GraphicsWindow.PenWidth = 160. cross1 = Shapes.AddLine(0, -8, 0, 8)61. cross2 = Shapes.AddLine(-8, 0, 8, 0)62. Shapes.Move(cross1, x, y)63. Shapes.Move(cross2, x, y)64. Shapes.Move(pos, gw - 100, 20)65. EndIf66. Turtle.MoveTo(x, y)67. Turtle.Angle = 068. Not = "False=True;True=False;"69. moving = "False"70. scrolling = "False"71. Ready()72. GraphicsWindow.KeyDown = OnKeyDown73. tick = "False"74. Timer.Interval = 1000 / 2475. Timer.Tick = OnTick76. lastems = Clock.ElapsedMilliseconds77. obj["iMin"] = 178. While Not[cd]79. If moving Then80. If key = "Left" Then81. Turtle.TurnLeft()82. Turtle.Move(30)83. Turtle.TurnRight()84. ElseIf key = "Right" Then85. Turtle.TurnRight()86. Turtle.Move(30)87. Turtle.TurnLeft()88. EndIf89. moving = "False"90. Else91. Program.Delay(100)92. EndIf93. EndWhile94.EndSubInitialization
This subroutine initializes window size and variables.
95.Sub Init96. gw = 59897. gh = 42898. GraphicsWindow.BackgroundColor = "DodgerBlue"99. GraphicsWindow.Width = gw100. GraphicsWindow.Height = gh101. color = "1=Orange;2=Cyan;3=Lime;"102. size = "1=20;2=16;3=12;"103. passed = 0104. cd = "False" ' collision detected105.EndSubKey Input Event Handler
I decided to use right and left arrow keys to move Turtle. Because I thought using mouse to move Turtle might be a little difficult for players. In this event handler, a flag moving is set to "True" and the last key is memorized. That's it. Then, moving Turtle will be done in the main loop of Game().
But, while the flag moving is "True", next key input will be rejected.
106.Sub OnKeyDown107. If Not[moving] Then108. moving = "True"109. key = GraphicsWindow.LastKey110. EndIf111.EndSubTimer Event Handler
I uses Timer for dropping obstacles. This subroutine scrolls obstacles 24 times per second (every 41 ms). And adds a new obstacle every 500 ms.
112.Sub OnTick113. If Not[scrolling] Then114. scrolling = "True"115. ems = Clock.ElapsedMilliseconds116. If ems - lastems > 500 Then117. AddObject()118. lastems = ems119. EndIf120. ScrollObject()121. scrolling = "False"122. EndIf123. If debug Then124. x = Math.Floor(Turtle.X)125. y = Math.Floor(Turtle.Y)126. Shapes.SetText(pos, "(" + x + "," + y + ")")127. Shapes.Move(cross1, x, y)128. Shapes.Move(cross2, x, y)129. EndIf130.EndSubOpening
This is not essential function of the game, but opening screen makes it more game like. This subroutine displays the title and a big image of a turtle. Image files used in published Small Basic program is be better to locate on the internet. In the past, I used to use my private rental server to locate images. But someday, this kind of site will be closed. After that, the images can't be accessed from the game programs. So, this time, I tried to locate this turtle image on the TechNet Gallery. I hope this image in the TechNet Gallery remains longer than my site.
One more comment. Using GraphicsWindow.DrawImage() might be simpler because this turtle doesn't move.
131.Sub Opening132. url = "http://gallery.technet.microsoft.com/Turtle-PNG-Bitmap-for-582b449c/file/116666/1/Turtle.png"133. bigTurtle = Shapes.AddImage(url)134. Shapes.Move(bigTurtle, 180, 140)135. GraphicsWindow.BrushColor = "White"136. GraphicsWindow.FontName = "Trebuchet MS"137. GraphicsWindow.FontSize = 50138. x = (gw - 443) / 2139. y = 40140. GraphicsWindow.DrawText(x, y, title)141. Program.Delay(3000)142. GraphicsWindow.Clear()143.EndSubProcedure Just before Game Start
If the game start just after opening, players may be hurried. So I added this subroutine to display "Ready?" for about 20 seconds.
144.Sub Ready145. GraphicsWindow.FontSize = 40146. rdy = Shapes.AddText("Ready?")147. x = (gw - 130) / 2148. y = 100149. Shapes.Move(rdy, x, y)150. For opacity = 100 To 0 Step -10151. Shapes.SetOpacity(rdy, opacity)152. Program.Delay(200)153. EndFor154. Shapes.Remove(rdy)155.EndSubScrolling Obstacles
This subroutine scrolls all obstacles 5 pixels (dots) downward. This subroutine is called from the timer event handler. That is no problem. Because Shapes operations work well in event handlers.
Obstacles that come below the bottom of the window are deleted here. This makes the program faster.
And in this subroutine, collision between the Turtle and an obstacle is detected by checking every distance between them.
156.Sub ScrollObject157. iMin = obj["iMin"]158. iMax = obj["iMax"]159. For i = iMin To iMax160. x = obj[i]["x"]161. y = obj[i]["y"] + 5162. tx = Math.Floor(Turtle.X)163. ty = Math.Floor(Turtle.Y)164. d = Math.SquareRoot(Math.Power(tx - x, 2) + Math.Power(ty - y, 2))165. If d < (size[obj[i]["type"]] + 16) / 2 Then166. cd = "True" ' collision detected167. Goto break168. EndIf169. If y > gh Then170. passed = passed + 1171. Shapes.SetText(score, passed)172. Shapes.Remove(obj[i]["obj"])173. obj[i] = ""174. obj["iMin"] = i + 1175. Else176. Shapes.Move(obj[i]["obj"], x, y)177. obj[i]["x"] = x178. obj[i]["y"] = y179. EndIf180. EndFor181. break:182.EndSubThis program size is 182 lines. That is not so big. I recommend you to challenge this kind of vertical scrolling game if it is your first graphical game.