Unit 5 Seminar 1

Embed Size (px)

Citation preview

  • 7/25/2019 Unit 5 Seminar 1

    1/39

    Randomization and Variables

  • 7/25/2019 Unit 5 Seminar 1

    2/39

    Random Numbers

    When programminggames, we often wantto use random

    numbers

    Scratch providesbuilt-in functions for

    generating randomnumbers.

  • 7/25/2019 Unit 5 Seminar 1

    3/39

    Catch ames

    !ou need to use random numbers in catch games

    Scenario" # bunches of bananas are followingfrom the s$% and the user is to use the arrow $e%

    to move a mouse to catch them&n order to record scores, we need to use avariable ' we will use it in this program ande(plain later.

  • 7/25/2019 Unit 5 Seminar 1

    4/39

    )lgorithm

    *. +on$e% prompts user what to do. Set mon$e%s initial position

    . /et user move mon$e% with arrow $e%

    0. Set score to 1

    #. /oop # times2ut banana in a random position

    Show and let banana fall down

    &f banana meets mon$e%

    score 3 score 4*

    5ide banana

    6. ame 7ver

  • 7/25/2019 Unit 5 Seminar 1

    5/39

    bananaScript 8*9

    !ou should beable to write themon$e%Script%ourself. 5ereshow %ou write themain program of

    the bananaScript

  • 7/25/2019 Unit 5 Seminar 1

    6/39

    bananaScript 89

    Notice %ou onl% needto randomize (. &talwa%s falls from thes$%

    &t needs to fall straight

    down ' use the samenumber for (

  • 7/25/2019 Unit 5 Seminar 1

    7/39

    Write %our own game

    !ou will be as$ed to write %our owncatch game. Remember to add aprompt before the game starts andgame over when it ends.

    )t this point %ou are initializing the

    score in the bananaScript. &t will bemodified later after %ou learn moreabout the scope of variables

  • 7/25/2019 Unit 5 Seminar 1

    8/39

    5aunted Castle

    Can %ou write a program of a ghost is appearingand disappearing in different spots in a castle:

  • 7/25/2019 Unit 5 Seminar 1

    9/39

    +ove Sprite to Random 2osition

    2ut the randomizednumber in the appropriatebo(

    We use *01 and 11 andnot the whole stage toaccommodate the size of

    the spriteWrite the program andsave it as ;haunted< ' we

    need to modif% it later

  • 7/25/2019 Unit 5 Seminar 1

    10/39

    Score

    =he program can easil% be turned to agame b% letting the user clic$ on theghost.

    =o record score, we need to use variables' we have alread% done so before but letsloo$ at it closel% before continuing with thegame.

  • 7/25/2019 Unit 5 Seminar 1

    11/39

    Variables

    ) variable is a named storage location inthe computers memor%

    ) variable can onl% hold onevalue at atime. ) new value will replace the previous

    one

  • 7/25/2019 Unit 5 Seminar 1

    12/39

    Scope

    =he scopeof variable refers to the part of acomputer where a variable is binding

    /ocal variable" belongs to a specific spriteor method

    lobal variable" belongs to and can beused in the whole program

  • 7/25/2019 Unit 5 Seminar 1

    13/39

    Variable Name and &nitial Value

    Variable Names"+ust be uni>uewithin the method ' can be casesensitive depending on the language

    Should be meaningful

    &nitial Value"=he value that is first stored in the variable whenit is created. &nitializingmeans setting a value for

    a variable when it is first created.&n Scratch %ou must initialize as it $eeps thevalue of the variables of the previous run of theprogram

  • 7/25/2019 Unit 5 Seminar 1

    14/39

    Counting Numbers

    ?se data to create a localvariable to let the cat use theloop to count from * to #.

  • 7/25/2019 Unit 5 Seminar 1

    15/39

    5aunted ame

    Now modif% the;haunted< program b%adding a globalvariablefor score.

    !ou should be able tocount the score now

  • 7/25/2019 Unit 5 Seminar 1

    16/39

    =esting and @i(ing Augs

    =he program seemsto wor$ well 'however, if %ou hit the

    same ghost twice oreven thrice, the scorecontinues to add up.

    =he wa% to fi( it is tochec$ whether theghost has alread% been

    clic$ed each time itappears. &f it is the firsttime, add score,otherwise, no score

    )lso, the game never ends. !oushould add a global variable to set atime limit for the game to end

  • 7/25/2019 Unit 5 Seminar 1

    17/39

    )lgorithm for @irst 2erson Shootingame

    ;5aunted< is a first person shooting8ffs9 game. /ets doit properl% again starting from the algorithm"

    *.Set all variables to 1 and time/eft to *1.Set up ghost and gives instruction

    ./oop @orevertime/eft 3 time/eft -*ma$e ghost disappearset isClic$ed to 1

    ma$e ghost appear in a random place0.When ghost is clic$ed

    &f isClic$ed is 1)dd score and effectSet isClic$ed to *

    @or interactive games, %ou

    should alwa%s provideinstructions or tell userwhen or how to inputinformation ' this is calleda prompt

    #. &f time/eft 3 1Stop ever%thing

  • 7/25/2019 Unit 5 Seminar 1

    18/39

    bac$dropScript" lobal Variables

    Remember we useglobal variables forscore and timer becausethe% are for all sprites -

    in case %ou add othersprites 8e.g a witch9 thescore still wor$s

    Convention" We set andif possible deal withglobal variables in thebac$dropScript

  • 7/25/2019 Unit 5 Seminar 1

    19/39

    Script for the ghostSprite

    )dd comment"Calculate scores andadd effect

    +odif% and save;haunted< again.

  • 7/25/2019 Unit 5 Seminar 1

    20/39

    Codes for the +ethods

  • 7/25/2019 Unit 5 Seminar 1

    21/39

    )ccumulator

    =he score increases b% * each time, but%ou can also decide how much to increase

    )n accumulatoris a variable used tocalculate the total of some numbers

    &magine %ou have a new pigg% ban$. =hefirst time %ou put in B. =he second timeB*. =he third time B0. 5ow much is therein the pigg% ban$:

  • 7/25/2019 Unit 5 Seminar 1

    22/39

    )lgorithm

    *st time" =otal 3 =otal 4 8 1 4 3 9

    nd time" =otal 3 =otal 4 * 8 4 * 3 9

    rd time" =otal 3 =otal 4 0 8 4 0 3 9)s a result, %ou have B in %our ban$.

    =he algorithm is"

    Total = Total + Amount Each Time

  • 7/25/2019 Unit 5 Seminar 1

    23/39

    Cashier

    We will write a program to demonstratethis.

    =here are a few items with prices. =heuser is going to clic$ on the item and thetotal amount will be calculated.

  • 7/25/2019 Unit 5 Seminar 1

    24/39

    )dd the Sprites

    !ou need a cashier and some fruits. )ddprices to fruits and ma$e a chec$ out sprite

  • 7/25/2019 Unit 5 Seminar 1

    25/39

    )lgorithm

    *. +a$e three global variables ' current price, ta(and total price and initialize them to zero

    . Dach time an item is clic$ed"

    current price 3 item pricetotal 3 total 4 current price

    0. When chec$ out is clic$ed"

    ta( 3 total E 1.*total 3 total 4 ta(

    Cashier sa%s the total

  • 7/25/2019 Unit 5 Seminar 1

    26/39

    =he Code" &nitialization andCalculate one item 8*9

  • 7/25/2019 Unit 5 Seminar 1

    27/39

    =he Code" 7ther &tems 89

    )s all the other items calculate the samewa%, clic$ and drag the appleScript to the

    other fruits and change the current price tothe corresponding price

  • 7/25/2019 Unit 5 Seminar 1

    28/39

    =he Code" chec$7ut 89

    When the user clic$s the chec$7ut sprite, itneeds to send a message to the cashier so shewill do the calculation. !ou have alread%

    learned how to use it in ?nit 0 ' use broadcast.

  • 7/25/2019 Unit 5 Seminar 1

    29/39

    =he Code 809 " cashierScript

    Save theprogramas;cashier