I thought programming would have been really hard, but this wasn’t

Session 2 – Creating your first game

Yüklə 197,55 Kb.
ölçüsü197,55 Kb.
1   2   3   4   5   6   7   8   9   10

Session 2 – Creating your first game

When finished, students will be able to:

  • Use random numbers

  • Use IF conditions and logical operators

  • Add actions and events

  • Add comments

  • Add and subtract numeric variables

  • Create and display text sprites

  • Add time limits

  • Add sound effects

  • Clone and publish scripts

  • Create high score tables

  • Add and use libraries

  • Check for collisions between sprites

When finished, students should have a basic understanding of:

  • Conditions

  • Logical operators

  • Arithmetic operators

  • Functions (Actions and Events)

  • Parameter passing

Cloning and publishing scripts

TouchDevelop allows you to re-use existing scripts, by simply creating a copy of an existing script. This script may be one you created or it may be one created by another TouchDevelop user. This feature is called “clone script”, which is very useful if you are making a game which shares a lot of similarities with an existing game script and it also allows you to try different things in a script without worrying about messing up the original.

When you publish a script (Unless you publish it as hidden) it allows other TouchDevelop users around the world to run your script and make changes to the code, by installing it into their account. This is something that you can take advantage of and learn from other users by installing their scripts into your account and having a look at the way they have done things. Publishing a script in TouchDevelop is not the same as commercially publishing your application to the Windows store.

Random numbers

Random numbers are used extensively in almost all computer games, as without some element of randomness in a game they quickly become very predictable and boring. They can be used to set the speed or direction that an enemy is moving in or used to randomise choices made by the artificial intelligence (AI) elements in a game such as which enemy should appear and how skilled the enemy character should be and so on.


Programmers need to think in a logical manner and a massive part of that are the decision points in code, known as conditions. All high level programming languages have some form of IF THEN ELSE statement, where you can make choices based on logical conditions, a pseudo-code example would be: IF LIVES EQUALS ZERO THEN GAME IS OVER

In TouchDevelop the code for this would look as shown opposite. The ELSE condition occurs whenever the condition set in the IF part is false, so whenever lives is not equal to zero the ELSE condition runs.

In the example opposite the line which calls the game over action is indented, that is the way you know in TouchDevelop if code is part of the IF condition or not. If you add a line inside an IF condition or indeed a LOOP (which we will look at later on) you can choose whether to make it part of it or not part of it. Once you add a line you will notice a button with an arrow inside it called “move left” (as shown opposite). If you select “move left” then the line will no longer be part of the condition.

In the 2 examples below there is a subtle difference in the 2nd example where the last line is not indented. In the example on the left, if lives equals zero it runs the game over action, if lives is not equal to zero it adds 5 to score and then adds another 1 to score. In the example on the right the difference is that 1 is added to score regardless of what the result of the IF statement is.

Sometimes you need multiple conditions, for instance the game should end if lives reaches zero or the time limit runs out. This is where logical operators come into play (AND, OR and NOT). In the example below, if the gametime is less than or equal to 0 OR lives is equal to zero then the game over action is called. You can use the AND operator to check if two conditions are true, for instance in a 2 player game you might check if player 1 lives equals zero and player 2 lives equals zero before ending the game. I’ve included a list of all the logical operators on the next page.

Logical operators in TouchDevelop
















As you will probably have noticed in the previous examples where we added numbers to the score variable, TouchDevelop also has a number of arithmetic operators.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   10

Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2019
rəhbərliyinə müraciət

    Ana səhifə