0
Microsoft® Small Basic<br />Collision Detection<br />Estimated time to complete this lesson: 1 hour<br />
Collision Detection<br />In this lesson, you will learn about:<br />Using the concept of collision detection in games. <br />
What is Collision Detection?<br />Now let’s talk about collision detection in games.<br />In general, collision detection ...
 Checking whether a collision occurred between the identified objects or not.</li></li></ul><li>How to Detect Collision?<b...
 Hit the Target – The Game<br />You use the logic of collision detection to detect the collision between the turtle and th...
Hit the Target – How to Play<br />It’s time to play the game!<br />Steps to play the game:<br /><ul><li>Enter the angle to...
Enter the distance that the turtle needs to cover to hit the target.
Click the ‘Hit’ button to hit the target.
Click the ‘Reset’ button to reset the position of the target.</li></li></ul><li>Hit the Target – The Code<br />Now let’s u...
Upcoming SlideShare
Loading in...5
×

4.3 collision detection

915

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
915
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Code:&apos; Copyright (c) Microsoft Corporation. All rights reserved.GraphicsWindow.Hide()gw = 620gh = 450GraphicsWindow.BackgroundColor = &quot;LightBlue&quot;GraphicsWindow.CanResize = &quot;False&quot;GraphicsWindow.Width = gwGraphicsWindow.Height = ghGraphicsWindow.Top = ( Desktop.Height - gh ) / 2GraphicsWindow.Left = ( Desktop.Width - gw ) / 2GraphicsWindow.Title = &quot;Hit the Target&quot;GraphicsWindow.Show()Turtle.Hide()rx = 40ry = 20turtlex = 315turtley = 300width = 550height = 390ScoreboxX = 400Enterkey = &quot;Return&quot;target = 10life = 3score = 0GraphicsWindow.FontSize = 30GraphicsWindow.DrawText(100, 200, &quot;Press ENTER to start the game!&quot;)GraphicsWindow.KeyDown = OnpressSub Onpress enter = enter + 1If GraphicsWindow.LastKey = Enterkey And enter = 1 ThenGraphicsWindow.Clear()GraphicsWindow.FontSize = 12 GUI()MovingShape()ScoreShow()Controls.ButtonClicked = hitClickEndIfEndSubSub hitClick angle = Math.Round(Controls.GetTextBoxText(angletxt)) distance = Math.Round(Controls.GetTextBoxText(distancetxt)) clicked = Controls.GetButtonCaption(Controls.LastClickedButton) If clicked = &quot;Hit&quot; ThenGraphicsWindow.PenWidth = 0Turtle.Angle = angleTurtle.Move(distance)If Turtle.x &gt; x and Turtle.x &lt; x + 93 and Turtle.y &gt; y and Turtle.y &lt; y + 96 ThenShapes.HideShape(target) score = score + 10 scoreshow()Program.Delay(500)If target = score ThenGameEnd()EndIfControls.SetTextBoxText(angletxt, &quot;&quot;)Controls.SetTextBoxText(distancetxt, &quot;&quot;)MovingShape()ElseProgram.Delay(1000)If life &gt; 1 ThenGraphicsWindow.ShowMessage(&quot;Click Ok to continue the game&quot;,&quot;Reset&quot;) clicked = &quot;Reset&quot;Else GameEnd()EndIfEndIfEndIfIf clicked = &quot;Reset&quot; Then life = life - 1If score &gt;= 10 Then score = score - 5EndIfscoreshow()Controls.SetTextBoxText(angletxt, &quot;&quot;)Controls.SetTextBoxText(distancetxt, &quot;&quot;)MovingShape() If life &lt; 1 ThenGameEnd()EndIfEndIfEndSubSub GUI image = Program.Directory + &quot;cartoon.gif&quot; GraphicsWindow.DrawRectangle(rx, ry, width, height) target = Shapes.AddImage(image) GraphicsWindow.BrushColor = &quot;Black&quot;Shapes.Move(target, 100, 50) Turtle.Show()Turtle.X = turtlexTurtle.y = turtleyTurtle.Angle = 0GraphicsWindow.DrawText(rx + 5, height - 10, &quot;Enter Angle:&quot;)angletxt = Controls.AddTextBox(turtlex - 190, Turtley + 70)Controls.SetSize(angletxt, 40, 30) GraphicsWindow.DrawText(rx + 130, height - 10, &quot;Enter Distance:&quot;)distancetxt = Controls.AddTextBox(turtlex - 50, Turtley + 70)Controls.SetSize(distancetxt, 40, 30) firebutton = Controls.AddButton(&quot;Hit&quot;, turtlex + 20, Turtley + 70) Controls.SetSize(firebutton, 80, 30) EndsubSub MovingShapeShapes.ShowShape(target)Turtle.x = turtlexTurtle.Y = turtleyTurtle.Angle = 0 x = Math.GetRandomNumber(450) y = Math.GetRandomNumber(80)If x &lt;= 40 Then x = 40Shapes.Animate(target, x, y, 1000)ElseIf y &lt;= 20 Then y = 20Shapes.Animate(target, x, y, 1000)Else Shapes.Animate(target, x, y, 1000)EndIfEndSubSub GameEndGraphicsWindow.ShowMessage(&quot;Your score is:&quot; + score, &quot;Game Over&quot;) Program.End()EndSubSub ScoreShowGraphicsWindow.FontSize = 14GraphicsWindow.BrushColor = &quot;Gray&quot;GraphicsWindow.FillRectangle(ScoreboxX + 50, ScoreboxX - 65, 135, 70) GraphicsWindow.BrushColor = &quot;White&quot;GraphicsWindow.DrawText(ScoreboxX + 60, ScoreboxX - 55, &quot;Score :&quot;)GraphicsWindow.DrawText(ScoreboxX + 110, ScoreboxX - 55, score)GraphicsWindow.DrawText(ScoreboxX + 60, ScoreboxX - 20, &quot;Life :&quot;)GraphicsWindow.DrawText(ScoreboxX + 110, ScoreboxX - 20, life) EndSub
  • Code: &apos; Copyright (c) Microsoft Corporation. All rights reserved.GraphicsWindow.Hide()gw = 620gh = 450GraphicsWindow.CanResize = &quot;False&quot;GraphicsWindow.Width = gwGraphicsWindow.Height = ghGraphicsWindow.Top = (Desktop.Height - gh) / 2GraphicsWindow.Left = (Desktop.Width - gw) / 2GraphicsWindow.Title = &quot;Catch the Apples!&quot;GraphicsWindow.Show()applesLeft = 30gameScore = 0catcherImage = Program.Directory + &quot;catcher.jpg&quot;appleimage = Program.Directory + &quot;apple.jpg&quot;GraphicsWindow.MouseMove = OnMouseMoveCreateUI() While applesLeft &gt; 0DropApple()applesLeft = applesLeft - 1 Shapes.SetText(applesLeftTextBox, &quot;Apples Left: &quot; + applesLeft)EndWhileGraphicsWindow.ShowMessage(&quot;Your Score is: &quot; + gameScore, &quot;GameOver&quot;)Program.End()Sub CreateUIGraphicsWindow.BackgroundColor = &quot;LightBlue&quot; catcher = Shapes.AddImage(catcherImage) apple = Shapes.AddImage(appleimage) GraphicsWindow.BrushColor = &quot;Gray&quot;GraphicsWindow.FillRectangle(1, 1, gw, 40) GraphicsWindow.FontName = &quot;Verdana&quot;GraphicsWindow.FontSize = 18GraphicsWindow.BrushColor = &quot;White&quot;GraphicsWindow.DrawText(220, 5, &quot;Catch the Apples!&quot;) scoreTextBox = Shapes.AddText(&quot;Score: 0&quot;)Shapes.Move(scoreTextBox, 480, 5)applesLeftTextBox = Shapes.AddText(&quot;Apples Left: &quot; + applesLeft)Shapes.Move(applesLeftTextBox, 5, 5)GraphicsWindow.PenColor = &quot;Black&quot;GraphicsWindow.DrawLine(0, 50, 620, 50)Shapes.Move(catcher, 0, gh - 40) Mouse.HideCursor()EndSubSub OnMouseMovecatcherX = Math.Min(GraphicsWindow.MouseX, 570)Shapes.Move(catcher, catcherX, gh - 40)EndSubSub DropAppleappleX = Math.GetRandomNumber(600)appleY = 50While (appleY &lt; gh + 20)Shapes.Move(apple, appleX, appleY)Program.Delay(15)DetectCatch()appleY = appleY + 5EndWhileEndSubSub DetectCatchIf appleY &gt;= gh - 40 And appleX &lt; catcherX + 50 And appleX &gt;= catcherX ThengameScore = gameScore + 1Shapes.SetText(scoreTextBox, &quot;Score: &quot; + gameScore) appleY = gh + 20EndIfEndSub
  • Transcript of "4.3 collision detection"

    1. 1. Microsoft® Small Basic<br />Collision Detection<br />Estimated time to complete this lesson: 1 hour<br />
    2. 2. Collision Detection<br />In this lesson, you will learn about:<br />Using the concept of collision detection in games. <br />
    3. 3. What is Collision Detection?<br />Now let’s talk about collision detection in games.<br />In general, collision detection is about determining the intersection of two moving objects.<br />Collision detection in games takes place in the following steps:<br /><ul><li> Selecting two objects to be tested for collision.
    4. 4. Checking whether a collision occurred between the identified objects or not.</li></li></ul><li>How to Detect Collision?<br />Before we discuss how to detect collisions, you should consider what happens to two objects when they collide. <br />Collision detection is a basic aspect of two- and three-dimesional games. Algorithms help to detect the collision. Most games use posteriori collision detection—that is, they detect the collision after it has occurred.<br />One of the objects could destroy the other, one could move the other, or both of them might move from their previous positions.<br />
    5. 5. Hit the Target – The Game<br />You use the logic of collision detection to detect the collision between the turtle and the target. The game ends as soon as the two objects collide.<br />The game involves two objects: the turtle and the target. The player has to make the turtle hit the target by entering the correct angle and the distance.<br />Now that you have understood the concept of collision detection in games, let’s make a game that uses the logic of collision detection.<br />
    6. 6. Hit the Target – How to Play<br />It’s time to play the game!<br />Steps to play the game:<br /><ul><li>Enter the angle to set the direction that the turtle will move.
    7. 7. Enter the distance that the turtle needs to cover to hit the target.
    8. 8. Click the ‘Hit’ button to hit the target.
    9. 9. Click the ‘Reset’ button to reset the position of the target.</li></li></ul><li>Hit the Target – The Code<br />Now let’s understand the code for the game in detail…<br /><ul><li>Use the GraphicsWindow object to create the user interface.
    10. 10. Next, use the Controls object to add a click button and text box and set the size of control buttons.
    11. 11. Use the Shapes object to add an image of the shape, and to move and animate the shape and set its opacity level.
    12. 12. Use the Turtle object and you can set its angle and moving distance by using the Math object.</li></li></ul><li>Let’s Summarize…<br />Congratulations! Now you know how to:<br />Use the concept of collision detection in games. <br />
    13. 13. It’s Time to Apply Your Learning…<br />Using the concept of collision detection, create a game that involves two objects: a bucket and apples. The apples fall randomly from the top of the screen. The player tries to catch the apples in the bucket. The game runs for 30 seconds. Include a score board to display the total number of apples caught by the player in 30 seconds. <br />Your game should look somewhat like the following.<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×