Making use of what you have learnt in the previous exercises, try to complete the following challenges.
-
Set a new background using a picture (use board->set background picture).
-
Change the appearance of the ball and the two bats using graphics (use board -> create picture).
-
Make the ball spin based on the x speed of the ball.
-
Add sound effects for when the ball hits the bats and perhaps for when a goal is scored.
-
Adjust the numeric values being randomly generated in the fireball action, to increase or decrease the speed that the ball moves at initially.
-
Adjust the AI bat speed to increase or decrease how difficult it is to beat the AI controlled bat.
-
Publish the script.
End of session 2
Session 3 – Creating games with multiple sprites of the same type When finished, students will be able to: -
Add obstacles
-
Resize picture sprites
-
Create sprite sets
-
Add and remove sprites from a sprite set
-
Delete sprites
-
Use FOR and FOR EACH loops
-
Check for collisions between multiple sprites and handle collision response
When finished, students should have a basic understanding of: -
Collections (arrays/lists)
-
Loops
Collections
Most programming languages have some way of creating a collection of variables, usually in the form of an Array or Linked list. In TouchDevelop they are simply called collections and you can have collections of a number of different variable types, such as a collection of numbers, a collection of strings and even a collection of sprites. The one that we will make use of most in games, is the collection of sprites, which is referred to in TouchDevelop as a Sprite Set.
Collections allow you to create multiple instances of the same variable type, linked to a single variable name. Each instance is sometimes referred to as an element. You can add or remove elements from the collection. You reference each separate element in the collection by using an index number. For instance we could create a sprite set called aliens, by entering the line of code shown below.![](50331_html_m26889061.png)
We could then create a local sprite variable and then add it into the sprite set, as shown opposite. The first 3 lines just create a sprite and set its position and speed. The last line adds that specific sprite as an element of the sprite set aliens.
If this is the first sprite you have added into the collection, you could reference it as shown below. In this example, we are setting the height, for the same sprite that we declared above.![](50331_html_1ddd37ce.png)
The reason it says at(0) is because 0 is the first element in the collection, the second would be 1 and so on.
You can also remove elements from collections; the example below show the two different methods. The first line would look through the collection for an element matching sprite and remove it. The second line would remove the sprite at position 2 in the sprite set, position 2 being the third element as the index starts at 0.![](50331_html_m278a6197.png)
FOR Loops
We have already touched on loops a little, by using a gameloop in our previous games. However an important part of any programming language is the ability to create a loop to repeat a certain bit of code multiple times. In a lot of cases we want to repeat some code, a specific amount of times, which is where the FOR Loop comes in. In TouchDevelop, FOR loops repeat a certain amount of times, counting up a local number variable from 0 until a limit is reached (which you can set). In the example below the counter variable “i” starts at 0 on the first loop, then on the second loop it goes to 1 and so on until it hits 5 and at that point since “i” is no longer less than 5 it jumps out of the loop. So when it hits 5 it jumps out before executing the loop, so inside the loop “i” will never equal 5, however the loop is repeated a total of 5 times. Inside the loop “i” will be 0 on the first loop and 4 on the final loop.
This example adds 5 sprite elements to the aliens sprite set, each one with a slightly different x position which I am setting it to 100+(i*50). So the first element will be at 100, the second element at 150, the third at 200 and so on.
FOR EACH Loops![](50331_html_22f07ae9.png)
Another type of loop is the FOR EACH loop, which is used specifically with collections. This loop does not repeat a set amount of times; instead it repeats the exact amount of times needed to match the number of elements in the collection that has been specified. Each element in the collection is temporarily stored into a local variable (e in the example opposite), which can be used to access each individual element in the collection. In the example opposite, every element in the aliens sprite set is assigned a new width of 80. Notice the “where true” statement, you can change this to only select elements from the collection that match a certain criteria, leaving it as it is means all elements will be selected.
WHILE Loops
WHILE loops repeat a section of code until a condition becomes false, so there is not a set amount of times that it will repeat, it just depends on when the condition is broken. In the example below the loop repeats as long as ballsprite has a “speed x” of less than or equal to 50. Inside the WHILE loop a random number of between 0 and 199 is being assigned into the “speed x”, so the loop will repeat until it generates a random number of greater than 50.
Dostları ilə paylaş: |