Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program #4
- *
- * Authors:
- * Paul-Valentin Mini (pcamille@ucsc.edu)
- * Gavin Hughes (gthughes@ucsc.edu)
- * Date: Feb. 11, 2012
- *
- */
- float positionHUDX;
- float positionHUDY;
- float playerXspeed;
- float playerYspeed;
- float playerMoveX = 200;
- float playerMoveY = 300;
- boolean moveDirectionX;
- boolean moveDirectionY;
- float snitchXpos;
- float snitchYpos;
- float snitchXspeed = 5;
- float snitchYspeed = 4.4;
- int snitchXdirection = 1;
- int snitchYdirection = 1;
- PFont f;
- void setup() {
- size(400, 400);
- rectMode(CENTER);
- ellipseMode(CENTER);
- smooth();
- f = loadFont("Batang-48.vlw");
- textFont(f,32);
- }
- void draw() {
- keyShortcuts();
- speedDetection();
- movePlayer();
- moveSnitch();
- }
- void speedDetection() { // Detect the mouse position and calculate speed.
- if (mouseX < 200) {
- playerXspeed = (mouseX/20);
- moveDirectionX = false;
- }
- else if (mouseX > 200) {
- playerXspeed = (mouseX-200)/20;
- moveDirectionX = true;
- }
- if (mouseY < 200) {
- playerYspeed = (mouseY/20);
- moveDirectionY = false;
- }
- else if (mouseY > 200) {
- playerYspeed = (mouseY-200)/20;
- moveDirectionY = true;
- }
- }
- void moveSnitch() { // make the snitch move around
- snitchXpos = snitchXpos + ( snitchXspeed * snitchXdirection );
- snitchYpos = snitchYpos + ( snitchYspeed * snitchYdirection );
- if (snitchXpos > width-10 || snitchXpos < 0) {
- snitchXdirection *= -1;
- }
- if (snitchYpos > height-10 || snitchYpos < 0) {
- snitchYdirection *= -1;
- }
- drawSnitch(snitchXpos+5, snitchXpos+5);
- }
- void movePlayer() { // Apply the calculated speed & direction.
- if (moveDirectionX == false) {
- if (moveDirectionY == false) {
- playerMoveX = playerMoveX - playerXspeed;
- playerMoveY = playerMoveY - playerYspeed;
- }
- else if (moveDirectionY == true) {
- playerMoveX = playerMoveX - playerXspeed;
- playerMoveY = playerMoveY + playerYspeed;
- }
- }
- else if (moveDirectionX == true) {
- if (moveDirectionY == false) {
- playerMoveX = playerMoveX + playerXspeed;
- playerMoveY = playerMoveY - playerYspeed;
- }
- else if (moveDirectionY == true) {
- playerMoveX = playerMoveX + playerXspeed;
- playerMoveY = playerMoveY + playerYspeed;
- }
- }
- limitPlayerLoc();
- background(#DBDBDB);
- drawPlayer(playerMoveX, playerMoveY);
- drawPositionHUD();
- }
- void limitPlayerLoc() { // make sure the player doesnt leave the screen too much.
- if (playerMoveX < -15) {
- playerMoveX = -14;
- }
- else if (playerMoveX > 420) {
- playerMoveX = 419;
- }
- if (playerMoveY < -15) {
- playerMoveY = -14;
- }
- else if (playerMoveY > 420) {
- playerMoveY = 419;
- }
- }
- void drawPositionHUD() {
- if (playerMoveX < 0) {
- positionHUDX = 5;
- positionHUDY = playerMoveY;
- }
- else if (playerMoveX > 400) {
- positionHUDX = 395;
- positionHUDY = playerMoveY;
- }
- else if (playerMoveY < 0) {
- positionHUDY = 5;
- positionHUDX = playerMoveX;
- }
- else if (playerMoveY > 400) {
- positionHUDY = 395;
- positionHUDX = playerMoveX;
- }
- fill(0,255,0);
- rect(positionHUDX, positionHUDY, 10, 10);
- }
- void drawPlayer(float playerLocX, float playerLocY) { // draw the player
- stroke(5);
- line(playerLocX, playerLocY, playerLocX, playerLocY+50);
- noStroke();
- fill(#835100);
- rect(playerLocX, playerLocY+50, 10, 10);
- fill(255, 0, 0);
- ellipse(playerLocX, playerLocY+20, 15, 15);
- fill(#FF9393);
- stroke(1);
- ellipse(playerLocX, playerLocY+17, 10, 10);
- noStroke();
- }
- void drawSnitch(float snitchLocX, float snitchLocY) { //draw the snitch
- stroke(1);
- fill(#DADB28);
- ellipse(snitchLocX, snitchLocY, 10, 10);
- }
- void drawVictory() { //victory display
- fill(#E03939);
- rect(200, 200, 400, 400);
- fill(255, 0, 0);
- rect(200, 200, 200, 200);
- fill(#39E0D1);
- text("You", 170, 150);
- fill(#39E0D1);
- text("win", 170, 200);
- fill(#39E0D1);
- text("everything!", 115, 250);
- noLoop();
- }
- void drawDefeat() { //defeat display
- fill(#35226C);
- rect(200, 200, 400, 400);
- fill(0);
- rect(200, 200, 200, 200);
- fill(255);
- text("GAME", 150, 185);
- fill(255);
- text("OVER", 153, 235);
- noLoop();
- }
- void keyShortcuts() {
- if (keyPressed == true) {
- if ((key == 'w') || (key == 'W')) {
- drawVictory();
- }
- else if ((key == 'l') || (key == 'L')) {
- drawDefeat();
- }
- }
- }
Add Comment
Please, Sign In to add comment