In Challenge of the Month - August 2013, there were following two challenges.
Game Challenge
Write a DuckShoot game.
Interface Challenge
Write a fancy Game Opening screen for DuckShoot game - see above.
At that time, I tried only opening. Today, I will introduce new DuckShoot game as Program ID TLR995-2.
This program has 425 lines. But there are 166 lines that automatically generated by Shapes editor. So actual coded lines are 259.
Main
I added shooting part. But only one duck comes. Because I thought the code becomes complex and worse readable if ducks increases.
1.' DuckShoot 0.31b2.' Copyright (c) 2013-2014 Nonki Takahashi. The MIT License.3.'4.' History:5.' 0.31b 2014-08-01 Sorted subroutines. (TLR995-2)6.' 0.3b 2014-07-26 Supported in remote. (TLR995-1)7.' 0.2b 2014-07-13 Created a core of shooting. (TLR995-0)8.' 0.1a 2013-08-03 Created as DuckShoot opening. (TLR995)9.' 0.0 2013-08-03 14:38:49 Shapes generated by Shapes 1.5b.10.'11.GraphicsWindow.Title = "DuckShoot 0.31b"12.SB_Workaround()13.Opening()14.GameInit()15.GameLoop()16.Ending()Ending
This subroutine calculates score from a number of shoot and a number of hit.
17.Sub Ending18. Shapes.Remove(stair)19. If hit[i] Then20. point = 121. Else22. point = 023. EndIf24. score = point * 110 - shoot * 1025. GraphicsWindow.BrushColor = "White"26. GraphicsWindow.FontSize = 4027. GraphicsWindow.DrawText(170, 180, "SCORE " + score)28. Program.Delay(500)29. GraphicsWindow.DrawText(170, 230, "SHOOT " + shoot)30. Program.Delay(500)31. GraphicsWindow.DrawText(170, 280, "HIT " + point)32. Program.Delay(2000)33. GraphicsWindow.FontSize = 5034. GraphicsWindow.DrawText(170, 80, "GAME OVER")35.EndSubInitialization
At this point there is one duck. But for the future, arrays are used instead of single variables for the duck. I decided to use mouse for the operation of the gun, so events for mouse click and move are initialized here.
36.Sub GameInit37. ' Game start38. Shapes.ShowShape(duck[1])39. mouseDown = "False"40. GraphicsWindow.MouseDown = OnMouseDown41. GraphicsWindow.MouseMove = OnMouseMove42. Shapes.Animate(duck[1], gw, 150, 3000)43. Program.Delay(3000)44. i = 145. hit[i] = "False"46. a[i] = 9047. x[i] = -dw48. yDuck = 15049. y[i] = yDuck50. Shapes.Move(duck[i], x[i], y[i])51. GraphicsWindow.PenWidth = 052. GraphicsWindow.BrushColor = bgColor53. yRS = yStair - (yDuck + dh / 2)54. shoot = 055.EndSubMain Loop
Last week obstacles are moved in the timer event handler. This time, a duck is moved 4 dots rightward in main loop.
When a player shoots the gun, another duck is drawn under the moving Shapes duck to check hitting. If the target is an ellipse or a rectangle, the hit checking can be calculated from the co-ordinate. But the duck shape is a little difficult for the calculation. So, I used GraphicsWindow.GetPixel() and check the color is the background color or not.
There is a problem on a browser at GetPixel. A browser hangs up and returns no response at GraphicsWindow.GetPixel() after GraphicsWindow.DrawImage(). Program imported into IDE doesn't have this problem. To avoid this problem, I changed this program not to use DrawImage() but to use FillRectangle(), FillTriangle() and FillEllipse() with shape data created by Shapes editor. This workaround works well because the duck drawing was made with Shapes editor.
When the gun hits, the duck image falls down by using Shapes.Zoom(). We can give zoom levels from 0.1 to 20 for this operation. So, be careful not to give smaller value than 0.1. In following subroutine, the zoom level is reduced little by little until 0.1.
I found there is another problem with Shapes.Zoom() in imported program. When the zoom level was smaller than 0.7, the program stopped. At that time, the image of the duck had transparent background. To avoid this issue, I changed the image to have opaque background.
56.Sub GameLoop57. While x[i] < gw58. Program.Delay(50)59. If mouseDown Then60. Sound.PlayClick()61. shoot = shoot + 162. If silverlight Then63. shX = x[i]64. shY = y[i]65. iMin = 166. iMax = 1067. Shapes_Draw()68. Else69. GraphicsWindow.DrawImage(img, x[i], y[i])70. EndIf71. color = GraphicsWindow.GetPixel(dx, dy)72. GraphicsWindow.PenWidth = 073. GraphicsWindow.BrushColor = bgColor74. GraphicsWindow.FillRectangle(x[i], y[i], dw, dh)75. If color <> bgColor Then76. hit[i] = "True"77. EndIf78. mouseDown = "False"79. EndIf80. If hit[i] Then81. If 0 < a[i] Then82. a[i] = a[i] - 583. cos = Math.Round(Math.Sin(Math.GetRadians(a[i])) * 100) / 10084. Shapes.Zoom(duck[i], 1, Math.Max(cos, 0.1))85. deltaY = yRS - yRS * cos86. y[i] = yDuck + deltaY87. EndIf88. EndIf89. x[i] = x[i] + 490. Shapes.Move(duck[i], x[i], y[i])91. EndWhile92.EndSubMouse Event Handler (on Click)
This subroutine sets a flag mouseDown and saves the co-ordinate of the mouse. This co-ordinate is used to check that the gun hits the duck in GameLoop().
93.Sub OnMouseDown94. mouseDown = "True"95. dx = GraphicsWindow.MouseX96. dy = GraphicsWindow.MouseY97.EndSubMouse Event Handler (on Move)
This subroutine moves the sighter depending on the mouse moving. The mouse pointer (the arrow mark) is hided when the mouse is in the window, and is showed when the mouse is out of the window.
98.Sub OnMouseMove99. mx = GraphicsWindow.MouseX100. my = GraphicsWindow.MouseY101. If 0 <= mx And mx < gw And 0 <= my And my < gh Then102. Mouse.HideCursor()103. Shapes.Move(sighter, mx - 40, my - 40)104. Else105. Mouse.ShowCursor()106. EndIf107.EndSubOpening
This subroutine shows the game title and images of a duck and a sighter. Originally these images were combination of shapes created with Shapes editor. But to make the motion of the images smoother, I converted these shapes data to .png files. The detail about how to convert shapes drawings created by Shapes editor to .png files is described here.
For following two reasons, I used both the image and shapes of the duck. One is for it's eye blink. The other is to avoid the GetPixel issue in browser described above (Main Loop).
108.Sub Opening109. bgColor = "#8B0000" ' DarkRed110. stColor = "#990000" ' for stair111. GraphicsWindow.BackgroundColor = bgColor112. gw = 598113. gh = 428114. GraphicsWindow.Width = gw115. GraphicsWindow.Height = gh116. GraphicsWindow.PenWidth = 0117. GraphicsWindow.BrushColor = bgColor118. GraphicsWindow.FillRectangle(0, 0, gw, gh)119. ' add duck image120. path = "http://gallery.technet.microsoft.com/site/view/file/119954/1/Duck2.png"121. img = ImageList.LoadImage(path)122. If silverlight Then123. dw = 246 + 1124. dh = 192 + 2125. Else126. dw = ImageList.GetWidthOfImage(img)127. dh = ImageList.GetHeightOfImage(img)128. EndIf129. duck[1] = Shapes.AddImage(img)130. Shapes.Move(duck[1], 194, 150)131. Shapes.HideShape(duck[1])132. ' add stair133. GraphicsWindow.BrushColor = stColor134. GraphicsWindow.PenWidth = 0135. stair = Shapes.AddRectangle(gw, gh - yStair)136. yStair = Math.Round(gh * 2 / 3)137. Shapes.Move(stair, 0, yStair)138. Shapes.HideShape(stair)139. ' initialize shapes140. GraphicsWindow.FontName = "Trebuchet MS"141. GraphicsWindow.FontSize = 50142. GraphicsWindow.BrushColor = "White"143. title = Shapes.AddText("DuckShoot")144. Shapes.Move(title, 170, 60)145. Shapes_Init()146. ' add shapes147. scale = 1148. angle = 0149. iMin = 1150. iMax = 10151. Shapes_Add()152. ' add sighter image153. path = "http://gallery.technet.microsoft.com/site/view/file/119955/1/Sighter.png"154. sighter = Shapes.AddImage(path)155. Shapes.Move(sighter, 250, 200)156. ' Blink start157. wait = "True"158. ems = Clock.ElapsedMilliseconds159. While wait160. Program.Delay(1000)161. x = 250 + (Math.GetRandomNumber(50) - 25)162. y = 200 + (Math.GetRandomNumber(50) - 25)163. Shapes.Move(sighter, x, y)164. Program.Delay(100)165. Shapes.HideShape(shape[4]["obj"])166. Program.Delay(100)167. Shapes.ShowShape(shape[4]["obj"])168. If 5000 < Clock.ElapsedMilliseconds - ems Then169. wait = "False"170. EndIf171. EndWhile172. Shapes.ShowShape(stair)173. iMin = 1174. iMax = 10175. Shapes_Remove()176. Shapes.Remove(title)177.EndSubDrawing Shapes
I wrote this subroutine for checking whether the gun shoot hit the duck or not. This subroutine is used only for this purpose so far. But for the future, I wrote this to be as general-purpose as possible. And it has still restrictions as follows.
- not support to draw border
- not support rotation for rectangles and ellipses
This routine is called only when the program is run in browser.
178.Sub Shapes_Draw179. ' Shapes | draw shapes180. ' param iMin, iMax - shape indices to add181. ' param shape - array of shapes182. ' param scale - 1 if same scale183. ' TODO to draw border line for rectangle, triangle and ellipse184. ' TODO to rotate rectangle and ellipse (text?)185. Stack.PushValue("local", x)186. Stack.PushValue("local", y)187. Stack.PushValue("local", i)188. s = scale189. For i = iMin To iMax190. If shape[i]["pw"] > 0 Then191. GraphicsWindow.PenColor = shape[i]["pc"]192. EndIf193. If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) Then194. GraphicsWindow.BrushColor = shape[i]["bc"]195. EndIf196. x = shX + shape[i]["x"] * s197. y = shY + shape[i]["y"] * s198. If shape[i]["func"] = "rect" Then199. GraphicsWindow.FillRectangle(x, y, shape[i]["width"]* s, shape[i]["height"] * s)200. ElseIf shape[i]["func"] = "ell" Then201. GraphicsWindow.FillEllipse(x, y, shape[i]["width"]* s, shape[i]["height"] * s)202. ElseIf shape[i]["func"] = "tri" Then203. x[1] = shX + shape[i]["x"] * s + shape[i]["x1"] * s204. y[1] = shY + shape[i]["y"] * s + shape[i]["y1"] * s205. x[2] = shX + shape[i]["x"] * s + shape[i]["x2"] * s206. y[2] = shY + shape[i]["y"] * s + shape[i]["y2"] * s207. x[3] = shX + shape[i]["x"] * s + shape[i]["x3"] * s208. y[3] = shY + shape[i]["y"] * s + shape[i]["y3"] * s209. angle = shape[i]["angle"]210. If angle <> 0 Then211. n = 3212. ox = (x[2] + x[3]) / 2213. oy = (y[1] + y[2]) / 2214. Shapes_RotatePolyline()215. EndIf216. GraphicsWindow.FillTriangle(x[1], y[1], x[2], y[2], x[3], y[3])217. ElseIf shape[i]["func"] = "line" Then218. x[1] = shX + shape[i]["x"] * s + shape[i]["x1"] * s219. y[1] = shY + shape[i]["y"] * s + shape[i]["y1"] * s220. x[2] = shX + shape[i]["x"] * s + shape[i]["x2"] * s221. y[2] = shY + shape[i]["y"] * s + shape[i]["y2"] * s222. If angle <> 0 Then223. n = 3224. ox = (x[2] + x[3]) / 2225. oy = (y[1] + y[2]) / 2226. Shapes_RotatePolyline()227. EndIf228. GraphicsWindow.DrawLine(x[1], y[1], x[2], y[2])229. ElseIf shape[i]["func"] = "text" Then230. If silverlight Then231. fs = Math.Floor(shape[i]["fs"] * 0.9)232. Else233. fs = shape[i]["fs"]234. EndIf235. GraphicsWindow.FontSize = fs * s236. GraphicsWindow.FontName = shape[i]["fn"]237. GraphicsWindow.DrawText(x, y, shape[i]["text"])238. EndIf239. EndFor240. i = Stack.PopValue("local")241. y = Stack.PopValue("local")242. x = Stack.PopValue("local")243.EndSubPolyline Rotation
This subroutine is written for rotating vertices of a triangle. But this subroutine supports more vertices as a polyline or a polygon for general-purpose.
244.Sub Shapes_RotatePolyline245. ' Shapes | rotate polyline246. ' param n - number of points247. ' param x, y - array of x and y co-ordinates248. ' param ox, oy, - center of rotation249. ' param angle - angle of rotation250. Stack.PushValue("local", i)251. _a = Math.GetRadians(angle)252. For i = 1 To n253. xi = (x[i] - ox) * Math.Cos(_a) + (y[i] - oy) * Math.Sin(_a)254. yi = - (x[i] - ox) * Math.Sin(_a) + (y[i] - oy) * Math.Cos(_a)255. x[i] = xi + ox256. y[i] = yi + oy257. EndFor258. i = Stack.PopValue("local")259.EndSubAuto Generated Code
And this program also have following subroutines. These subroutines are generated by Shapes editor 1.5b. So I skip to show these codes. But I listed up brief description for these. There are other subroutines generated by Shapes. But I deleted because they are not called in this program.
- SB_Workaround - determine which workarounds needed or not for running on browser
- Shapes_Add - adds Shapes objects along with the array shape
- Shapes_CalcWidthAndHeight - calculates whole width and height for data in the array shape
- Shapes_Init - sets duck shapes data into an array shape
- Shapes_Move - moves shapes added in Shapes_Add
- Shapes_Remove - removes shapes added in Shapes_Add
As duck shooting game, increasing ducks will make this game more fun. Would you like to challenge?