Showing posts with label 2D plane. Show all posts
Showing posts with label 2D plane. Show all posts

Monday, October 19, 2015

A Pong Game - programmed with Scratch

Different versions of the ever popular Pong game are available on Scratch. Here’s the link from ScratchEd: http://scratched.gse.harvard.edu/sites/default/files/scratch-lesson-7-the-pong-game.pdf. The following lesson plan using Scratch is one that I created for our Grade 5 students, taking inspiration from this sample code for Pong.
Given the code for an interactive Pong video game, the students edit the program to create multiple versions of the game with varying difficulty levels. The aim is to gain familiarity with the Scratch software platform and learn how to create games/ programs that involve user interaction. The students learn to modify the various facets of the code, and in the process learn about reactive programming, conditionals, loops, sprites, backgrounds and variables. They discuss how the variables in the given program can be used to model the different factors (forces, energy, orientation, type of materials, etc.) involved in the movements of a bouncing ball.

Platform

We use the S4A (Scratch for Arduino) platform for the Grade 5 students in our school. This project is intended to provide an introduction to this software platform before the kids start using it as the IDE for Arduino. S4A is based on Scratch from MIT and is quite similar to it. Note that in this particular assignment, we do not use the Arduino.
The sample code for Pong can be found in the Examples/Games folder in S4A.

Computer Science Concepts

Reactive programing
Forever loops
Conditionals

Common Core Standards

  • Variables
  • Angular measurements
  • Coordinate geometry for a 2D plane
  • Forces & interactions involved in a bouncing ball

The Lesson Plan

The aim is to learn about the basics of the Scratch/S4A platform in this assignment:

  • open, edit, save & close a project
  • sprites & backgrounds
  • the Scratch instruction set

The students read the given code and understand the various programming components that are involved in the design of the game. The factors that affect the movement of the pong ball are included as variables in the code. Students learn how modifying them can change the way the game is designed. The difficulty level of the game can be altered by modifying the variables. A discussion on how the variables in the program represent the different factors (forces, energy, orientation, type of materials, etc.) that govern the movement of the pong ball can round up the class.

This assignment also provides a first hand look at the use of conditionals and forever loops. Reactive programming, where the sprites react to various key presses or mouse movement is also learned.

Slides

I put together a set of slides for this lesson as I felt that it might be easier for the kids to tag along with the lecture, by programming on their computers. They can store the different versions of their program under different file names. 

The Pong Game PPT



Programming Assignment

Modify the code for the sprites to create different versions of the game, as suggested below.

Paddle Sprite:

The code for the Paddle Sprite in the sample code is designed to follow the mouse/cursor horizontally.

  1. Can you modify the code for the Paddle Sprite so that it follows the mouse pointer vertically on the screen?
  2. Modify the code for the Paddle Sprite so that it follows the mouse pointer everywhere on the screen.

Ball Sprite:

Try experimenting with different values for the variables. Note the changes you see each time.

  1. Use the “pen down” instruction to track the path of the ball.
  2. Would the game ever take off if the starting position has the ball touching the red zone on the background?
  3. Guess why the “forever loop” is used in the game.
  4. Guess why the “180 - direction” is used to set the direction of the ball when it hits the paddle.
  5. Guess what the “turn random angle” does. Experiment with it to increase the difficulty level of the game.
  6. Can you make the ball move faster at the start of the game by changing one of the variables?
  7. Can you make the ball move faster when it hits the paddle by changing one of the variables?
  8. Increase the difficulty level of the game by increasing the speed & randomness of the ball.

Challenge

  1. Introduce a variable to keep track of scores - you start with zero points & every time the paddle hits the ball, you get a point.
  2. Can you program the paddle to be moved on the screen using the arrow keys on the keyboard, instead of the cursor?

Summary

Summarize the lesson by going over what some of the variables in the game represent. Here are a few examples:

  1. When you change the speed of the ball, what does it imply about the loss/transfer of energy?
  2. If the ball moves faster after hitting the paddle, what kind of material could the paddle be made of? Is it one that absorbs much of the ball’s energy?
  3. The red background in the sample code makes the ball come to rest completely. Discuss the energy transfer of the ball in this case. What materials could possibly have made the pong ball come to rest on coming into contact with it?

Here’s a link with more info about the science of a pong ball, if you would like to go further with the discussions.

Monday, September 28, 2015

Draw Polygons & Compound Figures with a Given Area using Pro-Bot

Can you program the Pro-Bot to draw polygons & compound figures with a given area?

This, again, is an assignment that I designed for our Grade 3 students. It relates to the Common Core Math Standards: Geometric measurement: understand the concepts of areaFor this exercise, I highly recommend using graph paper, as it provides a helpful medium for the kids to work out the math problems. Provide at least one sheet per child to work out the problem and then additional sheets as required for the groups to draw the figures using Pro-Bot. Here is a link to a graph paper with 1 cm grid in PDF format; you can make copies for the students to draw on using the Pro-Bot.


Area of a Figure


The area of a figure is the number of squares required to cover it completely, and is specified in square units. Here's an article from math.com that gives a quick overview of the topic.

How do you calculate the area of a given figure? You add the number of squares needed to cover the entire figure. Say you are given a square with sides 3 cm each. You need 9 squares of sides 1 cm x 1 cm to cover it completely. The area of the square is 3 x 3 = 9 sq.cm. Similarly, a 5 cm x 6 cm rectangle has an area of 30 sq.cm.

Can we do the reverse too? Given the area, can we come up with the design for a figure with that area? 

Let's look at an example. Given an area of 9 sq.cm, how many polygons can we draw? We can draw multiple polygons, all with the exact same area of 9 sq.cm. In the figure below, you can see:
  • a square 3 cm x 3 cm,
  • couple of polygons with an area of 9 sq.cm.

Can you think of more polygons with an area of 9 sq.cm?

Let's now look at a scenario that shows the practical application of the concept of area. And then program the Pro-Bot to draw a few polygons with a given area.

Programming Assignment

You work for an architectural firm, and have been asked to design a single story house with a floor area of 100 square meters (roughly 1076 sq.ft.) You are to draw and present various designs for the floor plan.

  1. How many different ways can you draw the floor plan with an area of 100 sq.m.? Provide at least 2 to 3 different designs and make a rough drawing of the figures that you come up with.
  1. Classify the figures that you came up with into the various classes of polygons based on the number of sides.
  1. Program the Pro-Bot to draw them on graph paper. Use 1 sq.cm. to represent 1 sq.m. in your figures.
  1. Assume that the plot of land available for the construction, is a rectangle that is 15 m long and 8 m wide. Can you provide a design(s) to build a 100 sq.m. building in this plot? Program your Pro-Bot to draw the design. 
  2. You can program the Pro-Bot to draw other figures with the 100 sq.m. area. Or explore other figures with different areas.

Draw Polygons with a Given Perimeter using Pro-Bot

Can you program the Pro-Bot to draw polygons & compound figures with a given perimeter?

This is an assignment that I designed for our Grade 3 students. It relates to the Common Core Math Standards: Geometric measurement: recognize perimeterFor this exercise, I highly recommend using graph paper, as it provides a helpful medium for the kids to work out the math problems. Provide at least one sheet per child to work out the problem and then additional sheets as required for the groups to draw the figures using Pro-Bot. Here is a link to a graph paper with 1 cm grid in PDF format; you can make copies for the students to draw on using the Pro-Bot.


Perimeter of a Figure


A perimeter is a path that surrounds a two-dimensional shape. The term may be used for either the path or its length. It can be thought of as the length of the outline of a shape. (Wiki)

How do you calculate the perimeter of a given figure? You add the length of all the sides of that figure that form its outline. Say you are given a square with sides 3 cm each. The perimeter of the square is 3 + 3 + 3 + 3 = 12 cm. Similarly, a 5 cm x 6 cm rectangle. has a perimeter of 5 + 6 + 5 + 6 = 22 cm. 

Can we do the reverse too? Given the perimeter, can we come up with the design for a figure with that perimeter? 

Let's look at an example. Given a perimeter of 12 cm, how many polygons can we draw? We can draw multiple polygons, all with the exact same perimeter of 12 cm. In the figure below, you can see:
  • a square 3 cm x 3 cm,
  • a rectangle 5 cm x 1 cm,
  • a rectangle 4 cm x 2 cm,
  • a hexagon with sides 3 cm, 1 cm, 1 cm, 1 cm, 4 cm, 2 cm

Can you think of more polygons with a perimeter of 12 cm?

Let's now look at a scenario that shows the practical application of the concept of perimeters. And then program the Pro-Bot to draw a few polygons with a given perimeter.

Programming Assignment

Old McDonald lives on a farm and has lots of animals. He would like to build a new set of fences to keep his cows safe.

  1. If Old McDonald has 36 meters of fencing available, how many different ways can he build an enclosed area for his cows? Make a rough drawing of the figures that you come up with and then program the Pro-Bot to draw them on graph paper. Use 1 cm to represent 1 m in your figures.
  2. Classify the figures that you came up with into the various classes of polygons based on the number of sides.
  3. Write programs for Pro-Bot to draw at least 3 of the figures that you came up with.
  4. Suppose Old McDonald has only 35 meters of fencing available, but wants to build a square or rectangular fence using all of that fencing material. Would it be possible for him to build it? Why or why not? 
  5. You can program the Pro-Bot to draw other figures with the 36 cm perimeter. Or explore other figures with different perimeters.


Tuesday, April 21, 2015

A Drive through the Zoo: Learning about Angles using Pro-Bot

My child's class started learning about angles about a week ago. And the teacher requested me to design an exercise for Pro-Bot that will introduce the idea of angles. As the kids are very new to the concept, I wanted to keep this one fairly simple, while still using a mix of acute, obtuse and right angles.

A trip to the zoo is almost always part of our field trips every school year. The zoo in our city happens to have buses that drive along the various points of interest/ themed animal enclosures. I felt that the turns made by the bus in this familiar setting of the zoo, might be a good way to introduce angles.

In this exercise, a scaled down version of the bus route, provides the path for Pro-Bot to drive on. As the Bot drives along the path, it is required to make angular turns at each point. I have provided the angular measurements required in degrees and distances in centimeters. The bus travels in a loop, the directions are shown using arrows. Note that the map below is not drawn to scale.





Based on the above map, write a program for Pro-Bot to drive along the following paths, assuming that the Pro-Bot is facing in the forward direction:
  1. The path from the Entrance of the zoo to the African Savanah. How many turns did the Pro-Bot make? Were the angles acute, obtuse or right angles?
  2. To continue from the African Savanah (where you stopped before), to the Elephants enclosure, how many degrees did the Pro-Bot turn? 
  3. Program the path from the African Savanah to the Raptors. How many turns did the Pro-Bot make? Were the angles acute, obtuse or right angles?
  4. Continue the path from the Raptors to the Kids' Play Area. Did you use acute, obtuse or right angles for the turns?
Alternatively, the kids can be asked to program the entire bus route as a single program, marking the types of angles used along the way.


Friday, August 1, 2014

A Treasure Hunt with Pro-Bot

In this assignment, we shall use Pro-Bot to do a treasure hunt. We shall work with a graph and use XY coordinates to specify the points where the treasures are located. We shall also think of ways of optimizing our paths to get to the various locations.

The minimum distance that Pro-Bot can traverse is 1cm. Hence, the graph paper that you use should have grids of a minimum dimension of 1cm x 1cm. If not, you can always draw your own set of XY axes and put in coordinates 1 cm or more apart. You can then set each step of Pro-Bot to be the distance between successive points on your XY axes. 


Computer Science concepts involved:  Sequential programming, Optimization

Math concepts involved:  XY coordinates, Measuring distances on a graph, Finding shortest path between points, Cost comparison, Angles

Grade levels:  3, 4, 5

Hours required:  1 or more

Compare and Contrast Different Paths for Pro-Bot’s Treasure Hunt


Pro-Bot is going on a treasure hunt. It has been told the coordinates at which the treasures are kept. Can you write the programs for Pro-Bot to get to the treasure?

Let us start with Pro-Bot placed at location (0, 0).
  1. The first treasure is located at the coordinates (3, 4). Can you write a program for Pro-Bot to get to this point by traveling only along horizontal and vertical paths (i.e., turns only at 90 degrees)? There are no limits on the number of units of horizontal or vertical distance that Pro-Bot can travel continuously at a time. Remember that Pro-Bot starts its journey from location (0, 0).
  2. What is the total distance traveled by Pro-Bot in your above program? Is there a shorter path for Pro-Bot to reach the point (3, 4) from its starting point of (0, 0), if you did not have the condition that Pro-Bot can only travel along horizontal and vertical paths? 
  3. Can you write a program for Pro-Bot to travel along your new shorter path to get to the point (3, 4)? Assume that Pro-Bot is facing the X-axis at the start point.
  4. If gas costs $3.00 per gallon, and Pro-Bot uses a gallon of gas per step, how much did it cost for gas using your first program? How much did it cost by using your second program with the shorter path? How much money did you save?

     A second treasure for Pro-Bot:
  1. There is a second treasure located at the coordinates (6, 8). If Pro-Bot has to pick up the second treasure soon after the first one, what path would it take under the condition that it can only use horizontal and vertical paths (i.e., turns only at 90 degrees)? Can you modify your first program to allow Pro-Bot to pick up both treasures, with the starting point at (0, 0)? 
  2. What is the total distance traveled by Pro-Bot now in your above program? Is there a shorter path for Pro-Bot to reach both points ((3, 4) and (6, 8)) from its starting point of (0, 0), if you did not have the condition that Pro-Bot can only travel along horizontal and vertical paths?
  3. Can you write a program for Pro-Bot to travel along the shorter path to get to both the points (3, 4) and (6, 8)? Assume that Pro-Bot is facing the X-axis at the start point.
  4. If gas costs $3.00 per gallon, and Pro-Bot uses a gallon of gas per step, how much did it cost for gas in your first program where you traveled only along horizontal and vertical paths? How much money did it cost using your second program with the shorter path? How much money did you save?



A Different Spin on Pro-Bot’s Treasure Hunt


Let’s try a variation on our treasure hunt. This time, we have an extra condition:  
Pro-Bot can take only one step (or 1 unit) at a time in any direction. 

For example, to get to a point with coordinates (2, 1), Pro-Bot cannot travel 2 cm straight along the X axis; it can take 1 step horizontally along the X axis, and then move up 1 step and then turn and move 1 step horizontally to get to (2, 1). 
  1. Can you write a program for Pro-Bot to get to the treasure at location (3, 4) starting from (0, 0) with the condition that you can take only one step at a time in any direction. Do you travel more distance to get to the treasure under this condition?
  2. If gas costs $3 per gallon and Pro-Bot uses a gallon of gas per step, how much does it cost to get to the treasure?
  3. Can you write a program for Pro-Bot to get to the treasure at location (6, 8) starting from (0, 0) with the condition that you can take only one step at a time in any direction. 
  4. If gas costs $3 per gallon and Pro-Bot uses a gallon of gas per step, how much does it cost to get to the treasure?
  5. Can you write a program for Pro-Bot to get to the treasure at location (6, 8) starting from (3, 4) with the condition that you can take only one step at a time in any direction.