4
Introduction to Programming Class 6 Paul Brebner

Introduction to programming - class 6

Embed Size (px)

Citation preview

Page 1: Introduction to programming - class 6

Introduction to ProgrammingClass 6

Paul Brebner

Page 2: Introduction to programming - class 6

Exercise

• Create a cannon trajectory game– Place a cannon at lower left of screen, with a target on lower right.– Set initial cannon angle (0-90 degrees, 0 is horizontal, 90 is vertical), and

velocity, and cannon ball location (x and y, same as cannon).– Calculate velocity of the cannon ball, vx and vy, initially:

• float vx = velocity * cos(radians(angle));• float vy = velocity * sin(radians(angle));

– Each frame, change x and y position (and draw ball) and vy as follows:• x += vx; // add vx to get next cannon ball x position• y -= vy; // subtract vy to get next cannon ball y position• vy -= g; // g is gravity acting down, try 1

– Remember that in processing x increases from 0 to width, and y from 0 to height (starting at top left, so increases as ball goes down!)

– Use frameRate(1) (or slower) to make debugging easier– How will you change the angle and velocity? How will you fire? How will you

check if the ball hits the target? What happens when it hits?– Can you put another cannon on the right and make the game 2 player?– Can you work out how load images for the cannon, ball, target, explosion etc?– Next page is a simple online example

Page 3: Introduction to programming - class 6

Try this examplehttp://phet.colorado.edu/sims/projectile-motion/projectile-motion_en.html

Page 4: Introduction to programming - class 6

Example program structure, add detail to TODOs// global variables for cannon and cannon ball position and velocity go here ...TODOboolean cannonFired = false; // true after cannon fired until ball lands

void setup() // set size, background (sky colour), cannon position, and frame rate{TODO

}

void draw() // everything done once per frame{

// draw cannonTODOif (cannonFired){

// move and draw the ball until it hits the ground again, then set cannonFired to falseTODO

}else{// change angle and velocity and firing of cannon, set cannonFired to true and cannon ball x and y to cannon

locationTODO

}}