Guest User

Untitled

a guest
Jun 22nd, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. /*
  2.  * Program #4
  3.  *
  4.  * Authors:
  5.  * Paul-Valentin Mini (pcamille@ucsc.edu)
  6.  * Gavin Hughes (gthughes@ucsc.edu)
  7.  * Date: Feb. 11, 2012
  8.  *
  9.  */
  10.  
  11. float positionHUDX;
  12. float positionHUDY;
  13. float playerXspeed;
  14. float playerYspeed;
  15. float playerMoveX = 200;
  16. float playerMoveY = 300;
  17. boolean moveDirectionX;
  18. boolean moveDirectionY;
  19.  
  20. float snitchXpos;
  21. float snitchYpos;
  22. float snitchXspeed = 5;
  23. float snitchYspeed = 4.4;
  24. int snitchXdirection = 1;
  25. int snitchYdirection = 1;
  26.  
  27. PFont f;  
  28.  
  29. void setup() {
  30.   size(400, 400);
  31.   rectMode(CENTER);
  32.   ellipseMode(CENTER);
  33.   smooth();
  34.   f = loadFont("Batang-48.vlw");
  35.   textFont(f,32);
  36. }
  37.  
  38. void draw() {
  39.   keyShortcuts();
  40.   speedDetection();
  41.   movePlayer();
  42.   moveSnitch();
  43. }
  44.  
  45. void speedDetection() {   // Detect the mouse position and calculate speed.
  46.   if (mouseX < 200) {
  47.     playerXspeed = (mouseX/20);
  48.     moveDirectionX = false;
  49.   }
  50.   else if (mouseX > 200) {
  51.     playerXspeed = (mouseX-200)/20;
  52.     moveDirectionX = true;
  53.   }
  54.   if (mouseY < 200) {
  55.     playerYspeed = (mouseY/20);
  56.     moveDirectionY = false;
  57.   }
  58.   else if (mouseY > 200) {
  59.     playerYspeed = (mouseY-200)/20;
  60.     moveDirectionY = true;
  61.   }
  62. }
  63.  
  64. void moveSnitch() {    // make the snitch move around
  65.   snitchXpos = snitchXpos + ( snitchXspeed * snitchXdirection );
  66.   snitchYpos = snitchYpos + ( snitchYspeed * snitchYdirection );
  67.   if (snitchXpos > width-10 || snitchXpos < 0) {
  68.     snitchXdirection *= -1;
  69.   }
  70.   if (snitchYpos > height-10 || snitchYpos < 0) {
  71.     snitchYdirection *= -1;
  72.   }
  73.   drawSnitch(snitchXpos+5, snitchXpos+5);
  74. }
  75.  
  76. void movePlayer() {                  // Apply the calculated speed & direction.
  77.   if (moveDirectionX == false) {
  78.     if (moveDirectionY == false) {
  79.       playerMoveX = playerMoveX - playerXspeed;
  80.       playerMoveY = playerMoveY - playerYspeed;
  81.     }
  82.     else if (moveDirectionY == true) {
  83.       playerMoveX = playerMoveX - playerXspeed;
  84.       playerMoveY = playerMoveY + playerYspeed;
  85.     }
  86.   }
  87.   else if (moveDirectionX == true) {
  88.     if (moveDirectionY == false) {
  89.       playerMoveX = playerMoveX + playerXspeed;
  90.       playerMoveY = playerMoveY - playerYspeed;
  91.     }
  92.     else if (moveDirectionY == true) {
  93.       playerMoveX = playerMoveX + playerXspeed;
  94.       playerMoveY = playerMoveY + playerYspeed;
  95.     }
  96.   }
  97.   limitPlayerLoc();
  98.   background(#DBDBDB);
  99.   drawPlayer(playerMoveX, playerMoveY);
  100.   drawPositionHUD();
  101. }
  102.  
  103. void limitPlayerLoc() {     // make sure the player doesnt leave the screen too much.
  104.   if (playerMoveX < -15) {
  105.     playerMoveX = -14;
  106.   }
  107.   else if (playerMoveX > 420) {
  108.     playerMoveX = 419;
  109.   }
  110.   if (playerMoveY < -15) {
  111.     playerMoveY = -14;
  112.   }
  113.   else if (playerMoveY > 420) {
  114.     playerMoveY = 419;  
  115.   }
  116. }
  117.  
  118. void drawPositionHUD() {
  119.   if (playerMoveX < 0) {
  120.     positionHUDX = 5;
  121.     positionHUDY = playerMoveY;
  122.   }
  123.   else if (playerMoveX > 400) {
  124.     positionHUDX = 395;
  125.     positionHUDY = playerMoveY;
  126.   }
  127.   else if (playerMoveY < 0) {
  128.     positionHUDY = 5;
  129.     positionHUDX = playerMoveX;
  130.   }
  131.   else if (playerMoveY > 400) {
  132.     positionHUDY = 395;
  133.     positionHUDX = playerMoveX;
  134.   }
  135.   fill(0,255,0);
  136.   rect(positionHUDX, positionHUDY, 10, 10);
  137. }
  138.  
  139. void drawPlayer(float playerLocX, float playerLocY) {   // draw the player
  140.   stroke(5);
  141.   line(playerLocX, playerLocY, playerLocX, playerLocY+50);
  142.   noStroke();
  143.   fill(#835100);
  144.   rect(playerLocX, playerLocY+50, 10, 10);
  145.   fill(255, 0, 0);
  146.   ellipse(playerLocX, playerLocY+20, 15, 15);
  147.   fill(#FF9393);
  148.   stroke(1);
  149.   ellipse(playerLocX, playerLocY+17, 10, 10);
  150.   noStroke();
  151. }
  152.  
  153. void drawSnitch(float snitchLocX, float snitchLocY) {  //draw the snitch
  154.   stroke(1);
  155.   fill(#DADB28);
  156.   ellipse(snitchLocX, snitchLocY, 10, 10);
  157. }
  158.  
  159. void drawVictory() {  //victory display
  160.   fill(#E03939);
  161.   rect(200, 200, 400, 400);
  162.   fill(255, 0, 0);
  163.   rect(200, 200, 200, 200);
  164.   fill(#39E0D1);
  165.   text("You", 170, 150);
  166.   fill(#39E0D1);
  167.   text("win", 170, 200);
  168.   fill(#39E0D1);
  169.   text("everything!", 115, 250);
  170.   noLoop();
  171. }
  172.  
  173. void drawDefeat() {  //defeat display
  174.   fill(#35226C);
  175.   rect(200, 200, 400, 400);
  176.   fill(0);
  177.   rect(200, 200, 200, 200);
  178.   fill(255);
  179.   text("GAME", 150, 185);
  180.   fill(255);
  181.   text("OVER", 153, 235);
  182.   noLoop();
  183. }
  184.  
  185. void keyShortcuts() {
  186.   if (keyPressed == true) {
  187.     if ((key == 'w') || (key == 'W')) {
  188.       drawVictory();
  189.     }
  190.     else if ((key == 'l') || (key == 'L')) {
  191.       drawDefeat();
  192.     }
  193.   }
  194. }
Add Comment
Please, Sign In to add comment