Games
- New section on creating games
- First need to change the coding level on main screen choose ‘Skill level’ box and select ‘coder’
- New script, instead of ‘blank turtle’ use ‘blank’
- Add game library (add new, library, seach for game, click it)
- Game library give us a bunch of functions (similar to how turtle library gave us a bunch of functions)
Need to start the game
var board := ♻game→start
Can set background image by using the board’s
set background picture
functionboard→set background picture(✿Beautiful Background)
AppInventor has bunches of images in its data base you can search for them by searching for art and then search for a thing
Sprites
- To make 2D games, need to draw 2d images (called sprites)
Add a sprite with the
create picture
functionboard→create picture(✿ Picture of Something)
- It may be too big or too small
- To change the attributes of a sprite, need to have a reference to it it
- Can create a reference to it by storing it in a variable
Fortunately there is a ‘store in var’ button to make this easy
var sprite := board→create picture(✿ Picture of Something)
- Note though that it gives you a name, if you want a different name there is a rename button.
- Calling sprite functions is a lot like calling turtle functions
- Find a list of all of them in the same way
Now we have access to sprite functions, like
set width
andset height
var sprite→set width(100)
- Only need to set one the other will be set to maintain aspect ratio
Can also set the x y of the sprite
var sprite→set x(0)
Note the coordinate system
Animation
Can also set the x and y velocity of the sprite
var sprite→set speed x(100)
- Try setting one
- Try changing the sign
- Try setting both
- Try chaning the magnitude
- But the sprite just goes off the screen
- We can prevent it the same way we did with turtle
So we put
if sprite→x > board→width then sprite→set speed x(0) else do nothing end if
- But this doesn’t work, because all of the code in the
main
function is executed once before the animation starts What we need is a way to run code at every frame of animation
board→on every frame do this code is executed every frame of animation end
- But what if the sprites velocity is random?
Calculate the new speed from the old speed
var new speed x := sprite→speed x * -1 sprite→set speed x(new speed)
Quiz
- socrative.com
- Room name ‘INQ241’