Advertisement
Guest User

Untitled

a guest
May 28th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. # Global constants
  2. HEIGHT = 600
  3. WIDTH = 800
  4. RADIUS = 30
  5. DIAMETER = 2*RADIUS
  6. PADDLE_HEIGHT = 120
  7. PADDLE_WIDTH = 10
  8. ACCELARATION = 1.05
  9.  
  10. # new_game() -> starts the game and contains animate method which is called continuously until stopped.
  11. def new_game
  12. # set playing boolean as true
  13. @playing = true
  14. # set velocity in x and y direction
  15. vel_x, vel_y = [8, 10]
  16. # show ball and set position in center of frame.
  17. @ball.show
  18. @ball.top, @ball.left = HEIGHT / 2, WIDTH / 2
  19.  
  20. # animate method is kind of a timer and runs on 30 frames per second.
  21. @animate = animate 30 do
  22. # start moving ball
  23. @ball.left += vel_x
  24. @ball.top += vel_y
  25.  
  26. # reflect ball if it hits on top or bottom.
  27. vel_y = -vel_y if @ball.top + DIAMETER >= HEIGHT or @ball.top <= 0
  28.  
  29. # move computer's paddle
  30. @comp.top =
  31. @ball.top + RADIUS - PADDLE_HEIGHT / 2 if @comp.top >= 0 or @comp.top <= HEIGHT - PADDLE_HEIGHT
  32. 0 if @comp.top < 0
  33. HEIGHT - PADDLE_HEIGHT if @comp.top > HEIGHT - PADDLE_HEIGHT
  34.  
  35. # check if ball hits side walls
  36. if @ball.left + DIAMETER >= WIDTH - PADDLE_WIDTH or @ball.left <= PADDLE_WIDTH
  37. # check if ball hits any of the paddle
  38. unless (@ball.left <= PADDLE_WIDTH and (@you.top..@you.top+PADDLE_HEIGHT).include? @ball.top + RADIUS) or (@ball.left + DIAMETER >= WIDTH - PADDLE_WIDTH and (@comp.top..@comp.top+PADDLE_HEIGHT).include? @ball.top + RADIUS)
  39. # stop the game if not and announce a winner
  40. stop "User" if @ball.left + DIAMETER >= WIDTH - PADDLE_WIDTH
  41. stop "Computer" if @ball.left <= PADDLE_WIDTH
  42. end
  43. # reflect ball if it hits a paddle and increase 5% speed.
  44. vel_x = -vel_x * ACCELARATION
  45. end
  46. end
  47. # move user's paddle according to mouse movement.
  48. motion { |x, y| @you.top = y - PADDLE_HEIGHT / 2 }
  49. end
  50.  
  51. # stop() -> called whenever ball is missed by any of the player
  52. def stop winner
  53. # set playing boolean to false
  54. @playing = false
  55. # stop animation, hide ball, display messages
  56. @animate.stop
  57. @ball.hide
  58. @banner = banner "Game Over.", align: "center"
  59. alert "#{winner} won."
  60. @subtitle = stack { subtitle "Press Space Bar or 'n' to start new game.", align: "center" }
  61. end
  62.  
  63. Shoes.app title: "Pong", height: HEIGHT, width: WIDTH, resizable: false do
  64. # spawn a ball, two paddles and display messages.
  65. stack { banner "Pong", align: "center" }
  66. @ball = oval top: HEIGHT / 2, left: WIDTH / 2, radius: RADIUS, fill: "#FD1"
  67. @you, @comp = [0, WIDTH - PADDLE_WIDTH].map {|x| rect top: HEIGHT / 2,
  68. left: x,
  69. height: PADDLE_HEIGHT,
  70. width: PADDLE_WIDTH,
  71. curve: 2
  72. }
  73. @banner = stack { banner "New game", align: "center" }
  74. @subtitle = stack { subtitle "Press Space Bar or 'n' to start.", align: "center" }
  75. # start the game by calling new_game when space bar or 'n' key is pressed.
  76. keypress do |key|
  77. if (key == " " or key == "n") and (not @playing)
  78. @banner.remove
  79. @subtitle.remove
  80. new_game
  81. end
  82. end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement