Advertisement
Izzard

"Ball Flinger" source

Jan 1st, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. function setup()
  2. randomize()
  3. end
  4.  
  5. function randomize()
  6.     gravity = math.random(1)
  7.     holeX = math.random(0,WIDTH)
  8.     ballX = math.random(0,WIDTH)
  9.     ballVertVelo = math.random(-50,10)
  10.     ballSize = math.random(25,300)
  11.     holeHoriVelo = math.random(-20,20)
  12.     ballHoriVelo = math.random(-20,20)
  13.     ballY = math.random(ballSize,HEIGHT)
  14. end
  15.  
  16. function draw()
  17. -- clear screen to blue
  18. background(188, 189, 223, 255)
  19.  
  20. -- draw the ground
  21. fill(56, 133, 29, 255) -- green
  22. noStroke()
  23. rect(0,0, WIDTH, 50)
  24.  
  25.  
  26.  
  27. -- draw the hole
  28. fill(0)
  29. rect(holeX - ballSize/2,5,ballSize * 1.5,35)
  30.  
  31.  
  32. -- draw the ball shadow
  33. noStroke()
  34. fill(0, 0, 0, 122)
  35. ellipse (ballX, 25, ballSize * 0.8 + ballY/10, math.min(ballY/20,50))
  36.  
  37.  
  38. -- draw the ball
  39. fill(255, 0, 0, 255) -- ball colour (red)
  40. stroke(153, 26, 33, 255) -- ball outline colour (red)
  41. strokeWidth(3)    -- outline thickness
  42. ellipse (ballX, ballY, ballSize)
  43. -- draw ball highlight
  44. stroke(223, 173, 171, 255)
  45. fill(255, 255, 255, 255)
  46. ellipse (ballX + 0.15 * ballSize, ballY + 0.2 * ballSize, ballSize * 0.4)
  47.  
  48.  
  49.  
  50. -- bounce ball off walls
  51. if ballX < 0 or ballX >= WIDTH then
  52.     ballHoriVelo = -ballHoriVelo
  53. end
  54.  
  55. -- bounce hole off walls
  56. if holeX < ballSize/2 then
  57.     holeHoriVelo = -holeHoriVelo
  58.     holeX = ballSize/2
  59. elseif holeX > WIDTH - ballSize then
  60.     holeHoriVelo = -holeHoriVelo
  61.     holeX = WIDTH - ballSize
  62. end
  63.  
  64.  
  65.    
  66. -- bounce ball off the ground (but not the hole)
  67. if ballY < 25 + ballSize/2 and (ballX >= holeX + ballSize or ballX <= holeX - ballSize) then
  68.     sound(DATA, "ZgBAPgAfOF4fKi1AjVEFPgAAAABoZUk+CQBqfyM/R0ASQEBF")
  69.     ballVertVelo = ballVertVelo - 2 * ballVertVelo
  70. else
  71.     ballVertVelo = ballVertVelo - gravity
  72. end
  73.  
  74. -- fall through hole
  75. if ballY > 0 then
  76. ballX = ballX + ballHoriVelo
  77. ballY = ballY + ballVertVelo
  78. holeX = holeX + holeHoriVelo
  79. else
  80.     sound(SOUND_PICKUP, 24328)
  81.     randomize()
  82. end
  83.  
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement