SlideShare a Scribd company logo
3
Most read
Microsoft® Small BasicCollision DetectionEstimated time to complete this lesson: 1 hour
Collision DetectionIn this lesson, you will learn about:Using the concept of collision detection in games.
What is Collision Detection?Now let’s talk about collision detection in games.In general, collision detection is about determining the intersection of two moving objects.Collision detection in games takes place in the following steps: Selecting two objects to be tested for collision.
 Checking whether a collision occurred between the identified objects or not.How to Detect Collision?Before we discuss how to detect collisions, you should consider what happens to two objects when they collide. 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.One of the objects could destroy the other, one could move the other, or both of them might move from their previous positions.
 Hit the Target – The GameYou 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.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.Now that you have understood the concept of collision detection in games, let’s make a game that uses the logic of collision detection.
Hit the Target – How to PlayIt’s time to play the game!Steps to play the game:Enter the angle to set the direction that the turtle will move.
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.Hit the Target – The CodeNow let’s understand the code for the game in detail…Use the GraphicsWindow object to create the user interface.

More Related Content

PPT
Introduction to Scratch Programming
PPTX
4.4 advanced games
PPTX
4.2 responding to events
PPTX
4.1 playing with shapes
PPT
Scratch for Data Logging and Graphing
PPT
An Intro to Scratch Programming for Parents
PPT
Scratch Lesson 2 – Paint Editor
PPTX
Computer Coding with Scratch: Lesson 2_primaryschoollessons
Introduction to Scratch Programming
4.4 advanced games
4.2 responding to events
4.1 playing with shapes
Scratch for Data Logging and Graphing
An Intro to Scratch Programming for Parents
Scratch Lesson 2 – Paint Editor
Computer Coding with Scratch: Lesson 2_primaryschoollessons

What's hot (20)

PPTX
Programming in scratch
PDF
Scratch Animation
PPTX
3 Kodu
PDF
Coding Basics with Scratch
PPTX
Scratch programming introduction to game creation
PPTX
Scratch an Editing App
PPTX
Scratch Lesson 5
PPT
Kodu
PPTX
2.3 exploring shapes
PPTX
Scratch Lesson 6
PPT
Scratch Lesson 1 – Basics
PPTX
Scratch Programming
PDF
Lasso tool
PPTX
Information technology
PPTX
Information technology
PPTX
Arcade Game Design with Sploder - Library Program
PPTX
6. production reflection(2)
PPTX
Lesson three presentation
DOCX
Houchens jeffrey secret helper gdd
PPTX
Presentation q4
Programming in scratch
Scratch Animation
3 Kodu
Coding Basics with Scratch
Scratch programming introduction to game creation
Scratch an Editing App
Scratch Lesson 5
Kodu
2.3 exploring shapes
Scratch Lesson 6
Scratch Lesson 1 – Basics
Scratch Programming
Lasso tool
Information technology
Information technology
Arcade Game Design with Sploder - Library Program
6. production reflection(2)
Lesson three presentation
Houchens jeffrey secret helper gdd
Presentation q4
Ad

More from allenbailey (17)

PPTX
5.1 sharing code
PPTX
3.6 debugging aids
PPTX
3.5 the controls object
PPTX
3.4 events and interactivity
PPTX
3.3 the math object
PPTX
3.2 stacks and arrays
PPTX
3.1 file input and output
PPTX
2.6 flickr, image list, and network objects
PPTX
2.5 clock, desktop, and dictionary objects
PPTX
2.4 sound, program, and text objects
PPTX
2.2 turtle graphics
PPTX
2.1 graphics window
PPTX
1.5 branching and subroutines
PPTX
1.4 conditions and loops
PPTX
1.3 variables
PPTX
1.2 statements, properties, and operations
PPTX
1.1 introduction to small basic
5.1 sharing code
3.6 debugging aids
3.5 the controls object
3.4 events and interactivity
3.3 the math object
3.2 stacks and arrays
3.1 file input and output
2.6 flickr, image list, and network objects
2.5 clock, desktop, and dictionary objects
2.4 sound, program, and text objects
2.2 turtle graphics
2.1 graphics window
1.5 branching and subroutines
1.4 conditions and loops
1.3 variables
1.2 statements, properties, and operations
1.1 introduction to small basic
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Review of recent advances in non-invasive hemoglobin estimation
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf

4.3 collision detection

  • 1. Microsoft® Small BasicCollision DetectionEstimated time to complete this lesson: 1 hour
  • 2. Collision DetectionIn this lesson, you will learn about:Using the concept of collision detection in games.
  • 3. What is Collision Detection?Now let’s talk about collision detection in games.In general, collision detection is about determining the intersection of two moving objects.Collision detection in games takes place in the following steps: Selecting two objects to be tested for collision.
  • 4. Checking whether a collision occurred between the identified objects or not.How to Detect Collision?Before we discuss how to detect collisions, you should consider what happens to two objects when they collide. 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.One of the objects could destroy the other, one could move the other, or both of them might move from their previous positions.
  • 5. Hit the Target – The GameYou 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.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.Now that you have understood the concept of collision detection in games, let’s make a game that uses the logic of collision detection.
  • 6. Hit the Target – How to PlayIt’s time to play the game!Steps to play the game:Enter the angle to set the direction that the turtle will move.
  • 7. Enter the distance that the turtle needs to cover to hit the target.
  • 8. Click the ‘Hit’ button to hit the target.
  • 9. Click the ‘Reset’ button to reset the position of the target.Hit the Target – The CodeNow let’s understand the code for the game in detail…Use the GraphicsWindow object to create the user interface.
  • 10. Next, use the Controls object to add a click button and text box and set the size of control buttons.
  • 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. Use the Turtle object and you can set its angle and moving distance by using the Math object.Let’s Summarize…Congratulations! Now you know how to:Use the concept of collision detection in games.
  • 13. It’s Time to Apply Your Learning…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. Your game should look somewhat like the following.

Editor's Notes

  • #8: Code:' Copyright (c) Microsoft Corporation. All rights reserved.GraphicsWindow.Hide()gw = 620gh = 450GraphicsWindow.BackgroundColor = "LightBlue"GraphicsWindow.CanResize = "False"GraphicsWindow.Width = gwGraphicsWindow.Height = ghGraphicsWindow.Top = ( Desktop.Height - gh ) / 2GraphicsWindow.Left = ( Desktop.Width - gw ) / 2GraphicsWindow.Title = "Hit the Target"GraphicsWindow.Show()Turtle.Hide()rx = 40ry = 20turtlex = 315turtley = 300width = 550height = 390ScoreboxX = 400Enterkey = "Return"target = 10life = 3score = 0GraphicsWindow.FontSize = 30GraphicsWindow.DrawText(100, 200, "Press ENTER to start the game!")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 = "Hit" ThenGraphicsWindow.PenWidth = 0Turtle.Angle = angleTurtle.Move(distance)If Turtle.x > x and Turtle.x < x + 93 and Turtle.y > y and Turtle.y < y + 96 ThenShapes.HideShape(target) score = score + 10 scoreshow()Program.Delay(500)If target = score ThenGameEnd()EndIfControls.SetTextBoxText(angletxt, "")Controls.SetTextBoxText(distancetxt, "")MovingShape()ElseProgram.Delay(1000)If life > 1 ThenGraphicsWindow.ShowMessage("Click Ok to continue the game","Reset") clicked = "Reset"Else GameEnd()EndIfEndIfEndIfIf clicked = "Reset" Then life = life - 1If score >= 10 Then score = score - 5EndIfscoreshow()Controls.SetTextBoxText(angletxt, "")Controls.SetTextBoxText(distancetxt, "")MovingShape() If life < 1 ThenGameEnd()EndIfEndIfEndSubSub GUI image = Program.Directory + "cartoon.gif" GraphicsWindow.DrawRectangle(rx, ry, width, height) target = Shapes.AddImage(image) GraphicsWindow.BrushColor = "Black"Shapes.Move(target, 100, 50) Turtle.Show()Turtle.X = turtlexTurtle.y = turtleyTurtle.Angle = 0GraphicsWindow.DrawText(rx + 5, height - 10, "Enter Angle:")angletxt = Controls.AddTextBox(turtlex - 190, Turtley + 70)Controls.SetSize(angletxt, 40, 30) GraphicsWindow.DrawText(rx + 130, height - 10, "Enter Distance:")distancetxt = Controls.AddTextBox(turtlex - 50, Turtley + 70)Controls.SetSize(distancetxt, 40, 30) firebutton = Controls.AddButton("Hit", 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 <= 40 Then x = 40Shapes.Animate(target, x, y, 1000)ElseIf y <= 20 Then y = 20Shapes.Animate(target, x, y, 1000)Else Shapes.Animate(target, x, y, 1000)EndIfEndSubSub GameEndGraphicsWindow.ShowMessage("Your score is:" + score, "Game Over") Program.End()EndSubSub ScoreShowGraphicsWindow.FontSize = 14GraphicsWindow.BrushColor = "Gray"GraphicsWindow.FillRectangle(ScoreboxX + 50, ScoreboxX - 65, 135, 70) GraphicsWindow.BrushColor = "White"GraphicsWindow.DrawText(ScoreboxX + 60, ScoreboxX - 55, "Score :")GraphicsWindow.DrawText(ScoreboxX + 110, ScoreboxX - 55, score)GraphicsWindow.DrawText(ScoreboxX + 60, ScoreboxX - 20, "Life :")GraphicsWindow.DrawText(ScoreboxX + 110, ScoreboxX - 20, life) EndSub
  • #10: Code: ' Copyright (c) Microsoft Corporation. All rights reserved.GraphicsWindow.Hide()gw = 620gh = 450GraphicsWindow.CanResize = "False"GraphicsWindow.Width = gwGraphicsWindow.Height = ghGraphicsWindow.Top = (Desktop.Height - gh) / 2GraphicsWindow.Left = (Desktop.Width - gw) / 2GraphicsWindow.Title = "Catch the Apples!"GraphicsWindow.Show()applesLeft = 30gameScore = 0catcherImage = Program.Directory + "catcher.jpg"appleimage = Program.Directory + "apple.jpg"GraphicsWindow.MouseMove = OnMouseMoveCreateUI() While applesLeft > 0DropApple()applesLeft = applesLeft - 1 Shapes.SetText(applesLeftTextBox, "Apples Left: " + applesLeft)EndWhileGraphicsWindow.ShowMessage("Your Score is: " + gameScore, "GameOver")Program.End()Sub CreateUIGraphicsWindow.BackgroundColor = "LightBlue" catcher = Shapes.AddImage(catcherImage) apple = Shapes.AddImage(appleimage) GraphicsWindow.BrushColor = "Gray"GraphicsWindow.FillRectangle(1, 1, gw, 40) GraphicsWindow.FontName = "Verdana"GraphicsWindow.FontSize = 18GraphicsWindow.BrushColor = "White"GraphicsWindow.DrawText(220, 5, "Catch the Apples!") scoreTextBox = Shapes.AddText("Score: 0")Shapes.Move(scoreTextBox, 480, 5)applesLeftTextBox = Shapes.AddText("Apples Left: " + applesLeft)Shapes.Move(applesLeftTextBox, 5, 5)GraphicsWindow.PenColor = "Black"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 < gh + 20)Shapes.Move(apple, appleX, appleY)Program.Delay(15)DetectCatch()appleY = appleY + 5EndWhileEndSubSub DetectCatchIf appleY >= gh - 40 And appleX < catcherX + 50 And appleX >= catcherX ThengameScore = gameScore + 1Shapes.SetText(scoreTextBox, "Score: " + gameScore) appleY = gh + 20EndIfEndSub