Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. /*************************************************************************
  2. * Einföld beinagrind fyrir tölvuleiknum Pong.
  3. *
  4. *************************************************************************/
  5.  
  6. import java.awt.event.KeyEvent;
  7.  
  8. public class Pong {
  9. public static void main(String[] args) {
  10. int stig = 0; //stig
  11.  
  12. // set the scale of the coordinate system
  13. StdDraw.setXscale(-1.0, 1.0);
  14. StdDraw.setYscale(-1.0, 1.0);
  15.  
  16. // initial values of ball
  17. double rx = 0.480, ry = 0.860; // position
  18. double vx = 0.015, vy = 0.023; // velocity
  19. double radius = 0.05; // radius
  20.  
  21. // initial values of paddle
  22. double[] px = { -0.2, 0.2, 0.2, -0.2 };
  23. double[] py = { -0.85, -0.85, -.90, -0.90 };
  24. double dpx = 0.018; // inc. paddle movement
  25.  
  26. //initial value of box
  27. double bx = Math.random(), by = 0.51;
  28. for (int i = 0; i < 1; i--) {
  29. by = Math.random();
  30. if (by > 0.5) {
  31. break;
  32. }
  33. }
  34. // main animation loop
  35. while (true) {
  36.  
  37. // bounce ball off wall according to law of elastic collision
  38. if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
  39. if (ry + vy > 1.0 - radius) vy = -vy;
  40.  
  41.  
  42. if (py[0] > ry + vy) { //Athuga bolta á spaða
  43. if (px[0] < rx + vx && px[1] > rx + vx) {
  44. vy = -vy;
  45. stig++;
  46. }
  47. }
  48. if (by + radius > ry + vy && by - radius < ry + vy) { //Athuga bolta á kassa
  49. if (bx + radius > rx + vx && bx - radius < rx + vx) {
  50. bx = Math.random();
  51. for (int i = 0; i < 1; i--) {
  52. by = Math.random();
  53. if (by > 0.5) {
  54. break;
  55. }
  56. }
  57. stig += 2;
  58. }
  59. }
  60. StdDraw.text(0.7, 0.9, "Stig: " + stig); //Skrifa stig
  61.  
  62. if (ry + vy < -0.9) { //Athuga bolta á botn gluggans
  63. break;
  64. }
  65. // update ball position
  66. rx = rx + vx;
  67. ry = ry + vy;
  68. // check if paddle moved
  69. if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT) && px[0] - dpx >= -1.0)
  70. for (int i = 0; i < 4; i++) px[i] -= dpx;
  71. if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT) && px[1] + dpx <= 1.0)
  72. for (int i = 0; i < 4; i++) px[i] += dpx;
  73.  
  74. StdDraw.clear();
  75.  
  76. // draw ball on the screen
  77. StdDraw.setPenColor(StdDraw.RED);
  78. StdDraw.filledCircle(rx, ry, radius);
  79.  
  80. // Teikna kassa
  81. StdDraw.setPenColor(StdDraw.BOOK_BLUE);
  82. StdDraw.filledSquare(bx, by, 0.05);
  83.  
  84. // draw paddle on the screen
  85. StdDraw.setPenColor(StdDraw.BLUE);
  86. StdDraw.filledPolygon(px, py);
  87.  
  88. // display and pause for 20 ms
  89. StdDraw.show();
  90. StdDraw.pause(20);
  91. }
  92. StdDraw.clear();
  93. StdDraw.text(0.1, 0.1, "Þú tapaðir með " + stig + " stig");
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement