Creative game programming lesson 2 keyboard controls the movement of birds

Time:2021-10-18

demonstration:

Video presentation

get ready:

1. Follow Dali’s log, download the editor or directly use the online editor

2. Download pictures

3. Start

3.1 load pictures and create canvas
function preload() {
img = loadImage(‘bird.png’);
}
function setup() {
createCanvas(400,400);// Create a 400 * 400 canvas
}
3.2 create background and draw bird pictures
var x=0,y=0;// Bird coordinates
function draw(){
background(0,255,255);// Generate a sky blue background
image(img, x, y);// Method of drawing picture image (picture name, picture X coordinate, picture y coordinate)
}
3.3 press the key to control the flight direction of the bird
function keyPressed(){
If (keycode = = = left_arrow) {/ / press the left key
x-=1; // Move left
}Else if (keycode = = = right_arrow) {/ / right click
x+=1; // Move right
}Else if (keycode = = = down_arrow) {/ / press the key
y+=1; // Move down
}Else if (keycode = = = up_arrow) {/ / press the up key
y-=1; // Move up
}
}
3.4 keep the bird moving and place the keypressed () method in the draw () method
keyPressed();

Welcome to my VX official account: Da Li journal, my website: bigbigli.com