games. learning objectives o writing games! o understanding the built-in 2d game engine o events in...
TRANSCRIPT
![Page 1: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/1.jpg)
games
![Page 2: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/2.jpg)
learning objectiveso writing games!
o understanding the built-in 2D game engine
o events in gameso using art (pictures, sounds)
![Page 3: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/3.jpg)
language recapo local variables: store and reuse a value
var x := 5x->post to wall
o if statement: make decisions!if x = 5 then ...else ...
o for loop: repeat code multiple timesfor 0 <= i < n do ...
![Page 4: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/4.jpg)
global variableso a global variable holds a value in memory and “on
disk”;o a global variable can be read or written too;o A global variable is available everywhere
• definition
data->x := 0◳x := 0
• reading
◳x->post to wall
• update with new value
◳x := 5 ◳ is the shorthand
for ‘data’
![Page 5: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/5.jpg)
fruit ninja liteo one sprite shoots up from the
bottom of the screeno user needs to tap the sprite
before sprite falls back on the bottom
o when tapped, the sprite shoots up again from the bottom
o 1 point per tap successfulo game over if 3 missed sprites
![Page 6: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/6.jpg)
boardo 2D sprite-based game engineo physics: sprites have speed, gravity,
frictiono game loop: timer event to ‘step’ the worldo touch events on sprites
![Page 7: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/7.jpg)
coordinates and unitso origin (0, 0) is top left
cornero landscape or portraito y coordinates
positive going down
x
y
(0,0)
![Page 8: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/8.jpg)
1. basic game template
var board: Board
action main
◳board := media->create landscape board(800,480)
◳board->post to wall
x: 800
y: 480 pixel
(0,0)display on screen
creates a 2D game engine
global variable
![Page 9: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/9.jpg)
1.5 setting up physicsvar board: Board
action main
◳board := media->create landscape board(800,480)
◳board->post to wall
◳board->set gravity(0, 700)
event gameloop
◳board->evolve
◳board->update on wall
x: 800
y: 480 pixel
(0,0)
gravity falling down
applies physics every 50 ms
![Page 10: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/10.jpg)
2. creating a spriteo create an ellipse in the boardo store the sprite in a local variableo promote the sprite variable to data
action main
...
◳fruit:= ◳board->create ellipse(100, 100)
width heightmeaningful name!
![Page 11: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/11.jpg)
3. sprite shooting from the bottom
o position the sprite just above bottom of the boardo set speed such that the sprite goes up the screen
action shoot
◳fruit->set pos(400, 500)
◳fruit->set speed(0, -800)
action main ... code->shoot
x: 800
y: 480 pixel
(0,0)
(400, 500)
![Page 12: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/12.jpg)
tap sprite evento tap sprite is an event raised when
the user taps a sprite. Attaches to a global variable of kind Sprite.
event tap sprite: fruit(
sprite : Sprite, x : Number, y : Number)
name of the global variable
(x,y) is the coordinate of the tap
![Page 13: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/13.jpg)
4. tap and shoot
o When sprite is tapped, shoot the sprite from the bottom again
event tap sprite: fruit(...)
code->shoot
![Page 14: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/14.jpg)
5. score
o on tap, increase the score by 1
action main
...◳score := 0
event tap sprite: sprite(...)
...
◳score := ◳score + 1
![Page 15: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/15.jpg)
6. when the sprite falls through the bottom…
o the sprite shoots back
action gameloop
...if ◳fruit->y > 480 and ◳fruit->speed y > 0 then code->shoot
beyond the bottom
going down
![Page 16: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/16.jpg)
7. when the sprite falls through the bottom for the 3rd time…
o the user has 3 chances and them game overaction main
...◳life := 3
action gameloop
...if ◳fruit->y > 800 and ◳fruit->speed y > 0then
◳life := ◳life – 1
if ◳life < 1 then ◳score->post to wall
time->stop
one life lost
no more life, stop the game
![Page 17: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/17.jpg)
customizations
![Page 18: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/18.jpg)
art - pictureso using pictures in your games
![Page 19: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/19.jpg)
backgroundo set screen background color or
image
wall->set background(color->blue)
or…wall->set backround picture(art->background)
![Page 20: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/20.jpg)
picture spriteo use create picture instead of create ellipse
o resize the picture (if needed)
action main◳fruit := ◳board->create ellipse(100,100)◳fruit := ◳board->create picture(art->fruit)
var pic := art->picture->clonepic->resize(100,-1)◳fruit := ◳board->create picture(pic)
this is the art we just added
![Page 21: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/21.jpg)
art - soundso same as picture for sounds
• still under development in web version
o When sprite is tapped, play the splash sound
event tap sprite: sprite(...)
code->shootart->splash->play
![Page 22: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/22.jpg)
randomness fruit trajectories
the sprite could start from any place at
the bottom
The sprite could have a little angle to get a bell-
shape trajectory
make sure the fruit stays in the screen!
![Page 23: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/23.jpg)
randomness fruit trajectories
o math->random(5) returns a number between 0 and 5 not included, i.e. 0,1,2,3 or 4.
action shoot
var x := 120 + math->random(540)◳fruit->set pos(x, 500)
var speedx := -50 + math->random(100)
◳fruit->set speed(speedx, -800)
tweak these values!
![Page 24: Games. learning objectives o writing games! o understanding the built-in 2D game engine o events in games o using art (pictures, sounds)](https://reader030.vdocuments.site/reader030/viewer/2022032804/56649e535503460f94b49dcb/html5/thumbnails/24.jpg)
leaderboardso use bazaar->post leaderboard score(score) to publish a score online
o use bazaar->post leaderboard to wall to display the leaderboard
o leaderboards only work on published scripts