Differences
This shows you the differences between two versions of the page.
| — |
code:simplesidescrolling [2011/07/26 22:35] (current) gillian created |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <code java> | ||
| + | //store the x and y coordinates for "platforms" in an array | ||
| + | int num_platforms = 50; | ||
| + | int[] platform_x = new int[num_platforms]; | ||
| + | int[] platform_y = new int[num_platforms]; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | size(400, 200); | ||
| + | //create platform coordinates -- the x coordinate should go up by 100s | ||
| + | //the y coordinate should be a random number between 50 and 70 | ||
| + | for (int i = 0; i < num_platforms; i++) | ||
| + | { | ||
| + | platform_x[i] = i*100; | ||
| + | platform_y[i] = int(random(50, 70)); | ||
| + | } | ||
| + | smooth(); | ||
| + | noStroke(); | ||
| + | } | ||
| + | |||
| + | //the position of the "character" | ||
| + | int char_pos_x = 40; | ||
| + | |||
| + | void draw() | ||
| + | { | ||
| + | //draw the background | ||
| + | background(51); | ||
| + | |||
| + | //technically, what we're doing here is moving everything | ||
| + | //in the scene by the character's position + width of screen/2 | ||
| + | //this keeps the character in the middle of the screen, and moves | ||
| + | //everything to the left by the amount the player has moved forward | ||
| + | //the "translate" method means that we don't need to modify the | ||
| + | //position of everything in the scene, we can just tell Processing | ||
| + | //to treat everything like it's been moved by the amount specified | ||
| + | //first parameter is amount to move in x, second parameter is y | ||
| + | translate(-char_pos_x + width/2, 0); | ||
| + | fill(25, 195, 245); | ||
| + | //draw the platforms just like we normally would | ||
| + | for (int i = 0; i < num_platforms; i++) | ||
| + | { | ||
| + | rect(platform_x[i], platform_y[i], 80, 200); | ||
| + | } | ||
| + | |||
| + | //and draw the player just like we normally would | ||
| + | fill(206, 170, 52); | ||
| + | ellipse(char_pos_x, 50, 20, 20); | ||
| + | } | ||
| + | |||
| + | void keyPressed() | ||
| + | { | ||
| + | //if the d key is pressed, move the player to the right | ||
| + | //otherwise, move the player to the left | ||
| + | if (key == 'd') | ||
| + | { | ||
| + | char_pos_x = char_pos_x + 2; | ||
| + | } | ||
| + | else if (key == 'a') | ||
| + | { | ||
| + | char_pos_x = char_pos_x - 2; | ||
| + | } | ||
| + | } | ||
| + | </code> | ||