Advertisement
lexiluvsya

DinoHat

Apr 4th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. PImage myDinoHat;
  2. PImage myBackground;
  3.  
  4. int dinox = 0;
  5. int ballSize;
  6.  
  7. // Paddle Variables
  8. float paddleX;
  9. int paddleSize = 70;
  10.  
  11. int x = 0;
  12. int y = 0;
  13.  
  14. int ySpeed = 5;
  15. int xSpeed = 5;
  16.  
  17.  
  18. void setup() {
  19. size(500,500);
  20.  
  21. myDinoHat = loadImage("DinoHat.png");
  22. myBackground = loadImage("MiddleEast.png");
  23.  
  24. background(255);
  25.  
  26. // Reset game
  27. newGame();
  28. }
  29.  
  30.  
  31. void draw ()
  32. {
  33.  
  34. x = x + xSpeed;
  35. y = y + ySpeed;
  36.  
  37. // Check if the ball hits any wall
  38. if (x > width) {
  39. xSpeed = -5;
  40. //soundWall.rewind();
  41. //soundWall.play();
  42. }
  43. if (x < 0) {
  44. xSpeed = 5;
  45. //soundWall.rewind();
  46. //soundWall.play();
  47. }
  48. if (y < 0) {
  49. ySpeed = 5;
  50. //soundWall.rewind();
  51. //soundWall.play();
  52. }
  53. if (y > (height - 35)) {
  54. // GAME OVER MAN
  55. // Ball hit bottom of the screen
  56. // Restart game
  57. newGame();
  58. //soundLose.rewind();
  59. //soundLose.play();
  60. }
  61.  
  62. // Check if ball hit paddle
  63. if (ballHitPaddle()) {
  64. ySpeed = -ySpeed;
  65.  
  66. // This line resets the ball position just above the paddle
  67. y = (height - 50) - (ballSize / 2);
  68. //soundPaddle.rewind();
  69. //soundPaddle.play();
  70.  
  71. }
  72. //dinox = mouseX; // this just has the picture of the dinosaur become the mouse, and the new one at the bottom just has the speed of the mouse slow down
  73. dinox += (mouseX - dinox)/ 20; //the higher the number the slower it is, the lower the number the faster it is
  74. image(myBackground, 0, 0, 900, 800); // (x, y, width, height)
  75.  
  76. image(myDinoHat, dinox, height - 50);
  77.  
  78.  
  79. //Drawing the ball
  80. noStroke();
  81. ballSize = 18;
  82. //background(255, 153, 204);
  83. ellipse(x, y, ballSize, ballSize);
  84.  
  85. }
  86.  
  87. // This functions checks if a ball has hit the paddle
  88. boolean ballHitPaddle() {
  89. // We set up a bunch of helper variables
  90. int ballLeft = x - (ballSize / 2);
  91. int ballRight = x + (ballSize / 2);
  92. int ballTop = y - (ballSize / 2);
  93. int ballBottom = y + (ballSize / 2);
  94.  
  95. int padLeft = int(paddleX);
  96. int padRight = int(paddleX) + paddleSize;
  97. int padTop = height - 50;
  98.  
  99. // Eliminating all cases where a collision would be impossible
  100. if (ballBottom < padTop) {
  101. return false;
  102. }
  103. if (ballRight < padLeft) {
  104. return false;
  105. }
  106. if (ballLeft > padRight) {
  107. return false;
  108. }
  109.  
  110. // We checked against all negative cases
  111. // A collision has probably occured
  112. return true;
  113. }
  114.  
  115.  
  116.  
  117. //Restarts Game
  118. void newGame() {
  119. x = int(random(50, width - 50));
  120. y = 50;
  121. xSpeed = 5;
  122. ySpeed = 5;
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement