User Tools

Site Tools


spcs:summer2014:d6

This is an old revision of the document!


Afternoon Teams: (Angel, Ellie), (Clair, Emily, Mei-Ling), (Anastasia, Alix), (Float, Jeni), (Ed, Andrew), (Raymond, Will), (Zuri, Jove), (Fang, Penny)

Topics

Buggy Sorting Code

// defining the array
 
int cheesesticks [] = {21, 33, 44, 22 ,55 ,66 ,77, 33, 22, 11, 44, 33, 22, 11, 55 ,66, 5, 2, 100, 20};
 
// counters
int i = -1;
int j = 0;
 
// storing the minimum 
int min;
 
 
// setup runs once
void setup () {
 
  size (800,800);
 
  for (int k=0; k<20; k++)
    println(cheesesticks[i]);
}
 
// runs foreverrrrr! (not really)
 
void draw () {
 
  background(0);
 
  // only runs after j reaches 20  
  if ((j == 20) && (i < 19)) {
 
     i++;
     j=i+1;
     min = cheesesticks[i];
  }
 
  // runs as long as j is less than 20
  if (j < 20) {
 
    if (cheesesticks[j] < min){
      min = cheesesticks[j];
      int temp = cheesesticks[i];
      cheesesticks[i] = cheesesticks[j];
      cheesesticks[j] = temp;
    }
    j++;
  }
 
 
  // Draws the state of the cheesesticks  
  fill (0,0,255);
 
  for (int l=0; l<20; l++)
  {
    rect(0, l*20, cheesesticks[l]*5, 10);
  }
 
  println("poop");
 
}

Ball Code

int x = 30;
int dx = 0;
 
int y = 20;
int dy = 0;
 
void setup() {
  size (500,500);
 
}
 
void draw() {
  //background(255,255,0);
  fill(random(256),random(256),random(256));
 
  ellipse(x,y,20,20);
 
  // d-pad = direction-pad
  if (keyPressed && key == 'd') {
    dx = 1;
  } 
  else {
    dx = 0;
  }
 
 
  if (keyPressed && key == 's') {
    dy = 1;
  }
  else {
    dy = 0;
  }
 
 
  x = dx + x;
  y = dy + y;
 
  /*
 
  // Collision Detection
 
  if (y > 480 || y < 0) {
    dy = dy * -1;
  }
 
  y = y + dy;
 
 
  if (x > 480 || x < 0) {
    dx = dx * -1;
  }
 
  x = x + dx;
  */
 
}

Assignments

Part 1: Blog and Github (Review)

Read Turing's Paper.

Part 2: Bouncing Balls

Part 3: Processing (Teams) - Around the world for Mousey

Mousey the Mouse wants to visit each of you next month. Download a world map, NOT the one on this page. Any kind of world map will do (as long as it's a valid image file format). Find out where everyone lives in the class and help Mousey plan the shortest possible trip. You will approximate the locations based off of the map you choose. Mousey has a jetpack and starts at Stanford.

Step 1: Background image

  1. Find an image of the world
  2. Google “background processing.js”
  3. Find out where everyone is from in our class
  4. Place an ellipse at every location
    1. You may want to create a class object to handle these “vertices” or locations.

Step 2: An object that has x and y

See this sample code for the Car class.

// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
 
void setup() {
  size(200,200);
  // Parameters go inside the parentheses when the object is constructed.
  myCar1 = new Car(color(255,0,0),0,100,2); 
  myCar2 = new Car(color(0,0,255),0,10,1);
}
 
void draw() {
  background(255);
  myCar1.drive();
  myCar1.display();
  myCar2.drive();
  myCar2.display();
}
 
// Even though there are multiple objects, we still only need one class. 
// No matter how many cookies we make, only one cookie cutter is needed.
class Car { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
 
  // The Constructor is defined with arguments.
  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }
 
  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,20,10);
  }
 
  void drive() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
}

Step 3: Adjacency Matrix

You may have realized that using a matrix (or 2-D array) makes it easy to represent the graph. Construct an Adjacency Matrix, where the weights of the edges are the distances between one point and every other point.

Step 4: Shortest Path

Using some algorithm… Maybe Breadth First Search, draw a path that on the screen that highlights what's going on in the code. See example of lines above.

Step 5: Travelling Sales Person

Find the shortest distance that Mousey can go INCLUDING his return home.

Step 6: Animation

Now make the program animate!


Part 4 - JS Platformer

Cookbook! You know what to do.

Additional Challenge: Make your own SPRITES! This part is fun :)

(Help Mousey get his cheese)

http://www.piskelapp.com/


Feedback!

/soe/sherol/.html/teaching/data/attic/spcs/summer2014/d6.1406049825.txt.gz · Last modified: 2014/07/22 10:23 by ffpaladin