Small Basic中文站-快乐编程

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4963|回复: 0

SmallBasic图形编程大杂烩

[复制链接]
发表于 2010-6-16 10:36:58 | 显示全部楼层 |阅读模式
本帖最后由 akyao 于 2010-6-16 10:40 编辑

SmallBasic图形编程大杂烩
一起来研究研究
源代码:
  1. '***************************************************************************************
  2. 'XZG681
  3. 'Sample program to demonstate virtually all of the SmallBasic GraphicsWindow commands
  4. 'Includes use of Shapes, Images, Arrays, Mouse and Keyboard control
  5. 'Does all of the main features required to write games using basic examples
  6. 'You will need to look at the code to see what is supposed to happen and how it is done
  7. 'Play with it, change it, break it then fix it
  8. '***************************************************************************************
  9. '
  10. 'Create a graphics window
  11. '
  12. 'Keep it hidden till we want to show it
  13. GraphicsWindow.Hide()
  14. 'Give it a title
  15. GraphicsWindow.Title = "Graphics Window Example"
  16. 'Set its size and position (use variables gw and gh because they may be useful later)
  17. gw = 800
  18. gh = 600
  19. GraphicsWindow.Width = gw
  20. GraphicsWindow.Height = gh
  21. 'The top and left = 4 position the window neatly in the top left of the screen
  22. GraphicsWindow.Top = 4
  23. GraphicsWindow.Left = 4
  24. 'Set a background colour
  25. GraphicsWindow.BackgroundColor = "LightBlue"
  26. 'Set it so it cannot be resized
  27. GraphicsWindow.CanResize = "False"
  28. 'Show the window
  29. GraphicsWindow.Show()
  30. '
  31. 'Create a red ball of diameter 50 (radius 25) and place it in the middle of the window (leave it there for 5 seconds)
  32. '
  33. radius = 25
  34. GraphicsWindow.BrushColor = "Red"
  35. GraphicsWindow.PenColor = "Black"
  36. ' We set the position to be the (screen centre - radius), since the screen position is defined as the top left of the ball
  37. ball = Shapes.AddEllipse(2*radius,2*radius)
  38. Shapes.Move(ball,gw/2-radius,gh/2-radius)
  39. Program.Delay(5000) ' 5000 milliseconds = 5 seconds
  40. '
  41. 'Move the ball randomly by animation 10 times (once per second)
  42. '
  43. For i = 1 To 10
  44. x = Math.GetRandomNumber(gw)
  45. y = Math.GetRandomNumber(gh)
  46. Shapes.Animate(ball,x-radius,y-radius,1000)
  47. Program.Delay(1000) ' We have to delay (pause) to wait for the animation to finish
  48. EndFor
  49. '
  50. 'Move the ball to follow the mouse for 10 seconds
  51. '
  52. start = Clock.Second
  53. time = 0
  54. While (time < 10)
  55. xm = GraphicsWindow.MouseX
  56. ym = GraphicsWindow.MouseY
  57. Shapes.Move(ball,xm-radius,ym-radius)
  58. time = Clock.Second - start
  59. 'If we go over the minute then seconds go back to 0 so add 60 seconds
  60. If (time < 0) Then
  61. time = time+60
  62. EndIf
  63. 'Display the mouse coordinates - first overwrite the last output
  64. GraphicsWindow.PenColor = GraphicsWindow.BackgroundColor
  65. GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
  66. GraphicsWindow.FillRectangle(gw-120,10,120,30)
  67. GraphicsWindow.BrushColor = "Black"
  68. GraphicsWindow.FontSize = 20
  69. GraphicsWindow.DrawText(gw-120,10,"("+xm+","+ym+")")
  70. EndWhile
  71. '
  72. 'Use 10 balls and move the one selected by the mouse left down (delete with right down)
  73. 'Run until all balls are deleted
  74. 'The moved balls change colour
  75. '
  76. GraphicsWindow.Clear()
  77. 'Create an array of 10 balls and their random positions
  78. nball = 10
  79. For i = 1 To nball
  80. GraphicsWindow.BrushColor = "LightGreen"
  81. GraphicsWindow.PenColor = "Black"
  82. xi = radius + Math.GetRandomNumber(gw-2*radius)
  83. yi = radius + Math.GetRandomNumber(gh-2*radius)
  84. ball = Shapes.AddEllipse(2*radius,2*radius)
  85. Shapes.Move(ball,xi-radius,yi-radius)
  86. ballsi = ball
  87. EndFor
  88. start = Clock.Second
  89. iball = 0
  90. While (nball > 0)
  91. If (Mouse.IsLeftButtonDown = "True") Then
  92. xm = GraphicsWindow.MouseX
  93. ym = GraphicsWindow.MouseY
  94. 'If no ball selected, then check if we are over one
  95. If (iball = 0) Then
  96. For i = 1 To nball
  97. dist = Math.SquareRoot((xm-xi)*(xm-xi)+(ym-yi)*(ym-yi))
  98. If (dist <= radius) Then
  99. iball = i
  100. 'To change the colour we delete it and replace it with a new ball
  101. GraphicsWindow.BrushColor = "Pink"
  102. Shapes.Remove(ballsiball)
  103. ballsiball = Shapes.AddEllipse(2*radius,2*radius)
  104. 'We are finished and don't want to continue checking since we have already deleted a ball so end this loop
  105. Goto completed1
  106. EndIf
  107. EndFor
  108. EndIf
  109. completed1:
  110. 'Move selected ball
  111. If (iball > 0) Then
  112. ball = ballsiball
  113. xiball = xm
  114. yiball = ym
  115. Shapes.Move(ball,xiball-radius,yiball-radius)
  116. EndIf
  117. Else
  118. 'drop current ball
  119. iball = 0
  120. EndIf
  121. 'Delete a ball with right click
  122. If (Mouse.IsRightButtonDown = "True") Then
  123. xm = GraphicsWindow.MouseX
  124. ym = GraphicsWindow.MouseY
  125. For i = 1 To nball
  126. dist = Math.SquareRoot((xm-xi)*(xm-xi)+(ym-yi)*(ym-yi))
  127. If (dist <= radius) Then
  128. 'Remove the displayed object
  129. Shapes.Remove(ballsi)
  130. 'Now remove the array element i - we do this by overwriting it with the balls further up the array
  131. For j = i To nball-1
  132. ballsj = ballsj+1
  133. xj = xj+1
  134. yj = yj+1
  135. EndFor
  136. 'Delete the last ball (now moved up the array 1 place)
  137. ballsnball = ""
  138. xnball = ""
  139. ynball = ""
  140. 'Reduce the count of balls
  141. nball = nball-1
  142. 'We are finished and don't want to continue checking since we have already deleted a ball so end this loop
  143. Goto completed2
  144. EndIf
  145. EndFor
  146. EndIf
  147. completed2:
  148. EndWhile
  149. '
  150. 'Replace the ball with an image and move with arrow keys for 20 seconds
  151. '
  152. 'Delete the last ball and set a new one as a downloaded image - get its radius
  153. GraphicsWindow.Clear()
  154. image = ImageList.LoadImage("http://www.smallbasic.cn/template/sns/image/logo.jpg")
  155. ball = Shapes.AddImage(image)
  156. radius = ImageList.GetWidthOfImage(image)/2
  157. 'Set variables to say if keys are pressed or not
  158. keyLeft = 0
  159. keyRight = 0
  160. keyUp = 0
  161. keyDown = 0
  162. 'Start an event for keydown and keyup
  163. GraphicsWindow.KeyDown = OnKeyDown
  164. GraphicsWindow.KeyUp = OnKeyUp
  165. 'Use the event to set the keypress flags - these are only called when a key is pressed or released
  166. Sub OnKeyDown
  167. key = GraphicsWindow.LastKey
  168. If (key = "Left") Then
  169. keyLeft = 1
  170. ElseIf (key = "Right") Then
  171. keyRight = 1
  172. ElseIf (key = "Up") Then
  173. keyUp = 1
  174. ElseIf (key = "Down") Then
  175. keyDown = 1
  176. EndIF
  177. EndSub
  178. Sub OnKeyUp
  179. key = GraphicsWindow.LastKey
  180. If (key = "Left") Then
  181. keyLeft = 0
  182. ElseIf (key = "Right") Then
  183. keyRight = 0
  184. ElseIf (key = "Up") Then
  185. keyUp = 0
  186. ElseIf (key = "Down") Then
  187. keyDown = 0
  188. EndIF
  189. EndSub
  190. 'Start in window centre
  191. x = gw/2
  192. y = gh/2
  193. start = Clock.Second
  194. time = 0
  195. While (time < 20)
  196. 'Update position if a key is currently down
  197. If (keyLeft = 1) Then
  198. x = x-1
  199. EndIf
  200. If (keyRight = 1) Then
  201. x = x+1
  202. EndIf
  203. If (keyUp = 1) Then
  204. y = y-1 ' The pixes for the window increase downwards from the top
  205. EndIf
  206. If (keyDown = 1) Then
  207. y = y+1
  208. EndIf
  209. 'Check for ball leaving screen - reneter other side
  210. If (x < 0) Then
  211. x = gw
  212. EndIf
  213. If (x > gw) Then
  214. x = 0
  215. EndIf
  216. If (y < 0) Then
  217. y = gh
  218. EndIf
  219. If (y > gh) Then
  220. y = 0
  221. EndIf
  222. 'Move the ball to the new position
  223. Shapes.Move(ball,x-radius,y-radius)
  224. 'Put a small delay in - the computer is too quick - this controls the update rate (frames per second)
  225. 'This is not the true fps since it doesn't account for the time drawing etc
  226. fps = 500
  227. Program.Delay(1000/fps)
  228. time = Clock.Second - start
  229. 'If we go over the minute then add 60 seconds
  230. If (time < 0) Then
  231. time = time+60
  232. EndIf
  233. EndWhile
  234. '
  235. 'Now use the arrow keys to accelerate the ball and run until 50 wall hits (almost the same code)
  236. 'We can use the keyboard events unchanged
  237. 'Reverse spin the ball when we hit a boundary
  238. '
  239. 'Start in window centre
  240. x = gw/2
  241. y = gh/2
  242. 'Zero initial velocity
  243. u = 0
  244. v = 0
  245. start = Clock.Second
  246. spin = 0.0 'Ball spin rate (Positive is anticlockwise)
  247. angle = 0
  248. hits = 0
  249. While (hits < 50)
  250. 'Update position if a key is currently down
  251. If (keyLeft = 1) Then
  252. u = u-1
  253. EndIf
  254. If (keyRight = 1) Then
  255. u = u+1
  256. EndIf
  257. If (keyUp = 1) Then
  258. v = v-1 ' The pixes for the window increase downwards from the top
  259. EndIf
  260. If (keyDown = 1) Then
  261. v = v+1
  262. EndIf
  263. 'Update position - divide by 500 to stop the acceleration being too extreme
  264. 'we can do gravity, friction etc here if we want
  265. x = x+u/500
  266. y = y+v/500
  267. 'Check for ball leaving screen - bounce the ball this time - and spin it
  268. If (x < radius) Then
  269. u = -u
  270. spin = spin+v
  271. hits = hits+1
  272. EndIf
  273. If (x > gw-radius) Then
  274. u = -u
  275. spin = spin-v
  276. hits = hits+1
  277. EndIf
  278. If (y < radius) Then
  279. v = -v
  280. spin = spin-u
  281. hits = hits+1
  282. EndIf
  283. If (y > gh-radius) Then
  284. v = -v
  285. spin = spin+u
  286. hits = hits+1
  287. EndIf
  288. 'Move the ball to the new position
  289. Shapes.Move(ball,x-radius,y-radius)
  290. 'Rotate the ball with the current spin direction
  291. Shapes.Rotate(ball,angle)
  292. angle = angle+0.0005*spin
  293. 'Put a small delay in - the computer is too quick - this controls the update rate (frames per second)
  294. 'This is not the true fps since it doesn't account for the time drawing etc
  295. fps = 500
  296. Program.Delay(1000/fps)
  297. EndWhile
  298. '
  299. 'Create an array of 10 randomly sized and coloured rotating boxes, then delete them when clicked with the mouse
  300. '
  301. GraphicsWindow.Clear()
  302. 'Create arrays of boxes and their positions and sizes
  303. For i = 1 To 10
  304. xi = 20+Math.GetRandomNumber(gw-60) ' not too close the the screen edge
  305. yi = 20+Math.GetRandomNumber(gh-60)
  306. wi = 10+Math.GetRandomNumber(30)
  307. hi = 10+Math.GetRandomNumber(30)
  308. GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
  309. GraphicsWindow.PenColor = "Black"
  310. box = Shapes.AddRectangle(wi,hi)
  311. 'Note xi,yi are top left of box, not its centre
  312. Shapes.Move(box,xi,yi)
  313. boxesi = box
  314. displayi = 1 ' flag that box is displayed
  315. EndFor
  316. 'Start a mouse click event
  317. GraphicsWindow.MouseDown = OnMouseDown
  318. 'Write the mouse click event to get coordinates of mouse click and set a flag that the mouse was clicked
  319. clicked = 0
  320. Sub OnMouseDown
  321. xm = GraphicsWindow.MouseX
  322. ym = GraphicsWindow.MouseY
  323. clicked = 1
  324. EndSub
  325. 'Continue while boxes remain
  326. nboxes = Array.GetItemCount(boxes)
  327. angle = 0
  328. While (nboxes > 0)
  329. 'Mouse was clicked
  330. If (clicked = 1) Then
  331. 'Check each box
  332. For i = 1 To Array.GetItemCount(boxes)
  333. 'Only look for remaining displayed boxes
  334. If (displayi = 1) Then
  335. box = boxesi
  336. 'Since the boxes are rotating use the maximum size to check for click region
  337. 'This is not exactly correct for rotating boxes but often in games efficient is more important than absolute correctness
  338. dmax = Math.Max(wi,hi)
  339. If (xm >= xi And xm <= xi+dmax And ym >= yi And ym <= yi+dmax) Then
  340. 'Delete this box from display and reduce the box count by 1
  341. 'Note the original arrays are not deleted since we are keeping track of box status using displayi
  342. Shapes.Remove(box)
  343. nboxes = nboxes-1
  344. displayi = 0
  345. Sound.PlayClick()
  346. EndIf
  347. EndIf
  348. EndFor
  349. 'Reset clicked flag to off since we have done everything with it
  350. clicked = 0
  351. EndIf
  352. 'Rotate the boxes (even i anticlockwise, odd i clockwise - negative angle)
  353. For i = 1 To Array.GetItemCount(boxes)
  354. If (displayi = 1) Then
  355. box = boxesi
  356. Shapes.Rotate(box,angle*(1-2*Math.Remainder(i,2)))
  357. EndIf
  358. EndFor
  359. angle = angle+1
  360. Program.Delay(10) ' Small delay to keep the rotation smooth
  361. EndWhile
  362. '
  363. 'Finish up
  364. '
  365. image = ImageList.LoadImage("http://www.smallbasic.cn/template/sns/image/logo.jpg")
  366. GraphicsWindow.Clear()
  367. GraphicsWindow.FontSize = 100
  368. GraphicsWindow.FontBold = "True"
  369. GraphicsWindow.FontName = "Rockwell"
  370. GraphicsWindow.DrawResizedImage(image,0,0,gw,gh)
  371. R = 255
  372. G = 0
  373. B = 0
  374. dR = -1
  375. dG = 2
  376. dB = 3
  377. clicked = 0
  378. While (clicked = 0)
  379. 'Change the red, green, blue components of the colour at different rates to get changing colours
  380. If (R < 0 Or R > 255) Then
  381. dR = -dR
  382. R = R+dR
  383. EndIf
  384. If (G < 0 Or G > 255) Then
  385. dG = -dG
  386. G = G+dG
  387. EndIf
  388. If (B < 0 Or B > 255) Then
  389. dB = -dB
  390. B = B+dB
  391. EndIf
  392. colour = GraphicsWindow.GetColorFromRGB(R,G,B)
  393. GraphicsWindow.BrushColor = colour
  394. GraphicsWindow.DrawText(200,160,"All Done")
  395. GraphicsWindow.DrawText(100,340,"Click to Exit")
  396. GraphicsWindow.PenWidth = 10
  397. GraphicsWindow.PenColor = colour
  398. GraphicsWindow.DrawLine(50,gh-50,gw-50,gh-50)
  399. GraphicsWindow.DrawLine(gw-50,gh-50,gw-50,50)
  400. GraphicsWindow.DrawLine(gw-50,50,50,50)
  401. GraphicsWindow.DrawLine(50,50,50,gh-50)
  402. R = R+dR
  403. G = G+dG
  404. B = B+dB
  405. Program.Delay(20)
  406. EndWhile

  407. Program.End()
复制代码

回复

使用道具 举报

发表于 2018-11-20 10:06:25 | 显示全部楼层
代码有错误啊,运行不了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|Small Basic中文站-快乐编程 ( 闽ICP备09051788号 |

GMT+8, 2018-12-18 03:01 , Processed in 0.270224 second(s), 8 queries , File On.

Powered by Discuz! X3.3

快速回复 返回顶部 返回列表