Unit 4: Day 12
Tasks
51.1 Gravity Example - Including Introduction to Functions with RETURN Values
- See code below
- The Gravity Example is an advanced example that does not directly apply to the assignments in this unit.
- Before beginning, remind students of the end of course major project (AKA the CSP Create Task) as this might give them a possible project idea if they are interested in creating game-style project.
- It also introduces the concept of a RETURN statement by creating ‘collision event’ functions for each platform. Once we figure out the complex code for our collision IF statements, we can forget about it and just use the function call platform1() to simply.
- Discuss the following AP Knowledge statements about functions (aka procedures)
- AAP3.A.1- A procedure is a named group of programming instructions that may have parameters and return values.
- AAP-3.B.1 One common type of abstraction is procedural abstraction, which provides a name for a process and allows a procedure to be used only knowing what it does, not how it does it.
- AAP-3.B.2 Procedural abstraction allows a solution to a large problem to be based on the solutions of smaller subproblems. This is accomplished by creating procedures to solve each of the subproblems.
- AAP-3.B.3 The subdivision of a computer program into separate subprograms is called modularity.
- AAP-3.B.4 A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program code reuse, which helps manage complexity.
- AAP-3.B.5 Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
- AAP-3.B.6 Using procedural abstraction helps improve code readability.
- AAP-3.B.7Using procedural abstraction in a program allows programmers to change the internals of the procedure (to make it faster, more efficient, use less storage, etc.) without needing to notify usersof the change as long as what the procedure does is preserved.
51.2 Assignment P5 2.5 - Yes/No, Rock or Tic
- Instructions for this assignment can be found in the folder: 1.4 - Year 1: Programming Part 2
- This assignment has 3 differentiated levels for students of different skills.
- Encourage your advanced students to attempt the Tic-Tac-Toe version
51.3 Practice Questions Using AP Classroom
- If you have access to AP Classroom, take a few moments to go over the Topic Questions for the various programming concepts.
Gravity Example
// Level 1
let rm1_squarex = 50.0;
let rm1_squarey = 0;
let rm1_speedx = 0;
let rm1_speedy = -3.0;
//Level 2
function preload(){
}//end preloading of images and fonts
function setup() {
let sketch = createCanvas(600, 600);
sketch.parent("mycanvas");
background(100,92,80);
}//end setup
function draw() {
background(100,92,80);
rect(rm1_squarex,rm1_squarey,50,50);
rm1_squarex += rm1_speedx; // positive speed goes right
rm1_squarey += rm1_speedy; // positive speed goes down, negative speed for a jump
fill(255);
rect(25, 550, 250, 20); //platform 1
rect(400, 520, 300, 20); //platform 1
if( platform1() ){ //functions used for AP CSP example
rm1_speedy=0;
} else if ( platform2() ){
rm1_speedy=0;
} else {
rm1_speedy += 0.05; //gravity
}
}//end draw
function keyPressed() {
if(key==='w'){
rm1_squarey-=5; //this ensures it is up off the platform
rm1_speedy=-5;
} else if(key==='d'){
rm1_speedx = 3;
} else if(key==='a'){
rm1_speedx = -3;
}
}//end keyPressed
function keyReleased() {
if(key==='d'){
rm1_speedx = 0;
} else if (key==='a'){
rm1_speedx = 0;
}
}//end keyPressed
function platform1(){
//This is what is known as a collision event. If there is a collision with the platform, it returns true.
//The IF statement compares the 4 sides of the square with the location of the platforms. Note that the platforms are 250 wide and 20 tall.
if(rm1_squarex+50>25 && rm1_squarex<275 && rm1_squarey+50>550 && rm1_squarey<570){
return true;
} else {
return false;
}
}//end platform1
function platform2(){
if(rm1_squarex+50>400 && rm1_squarex<700 && rm1_squarey+50>520 && rm1_squarey<540){
return true;
} else {
return false;
}
}//end platform2
Continue to Unit 4: Day 13 »