Advertisement
Guest User

Catch the ball with some kind of penis.

a guest
Dec 24th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1.  
  2. function generateNewPosition()
  3.     return math.random(640), math.random(480)
  4. end
  5.  
  6. function generateNewColor()
  7.     return math.random(255), math.random(255), math.random(255)
  8. end
  9.  
  10. function inCircle(x,y, center_x, center_y, radius)
  11.     local dx, dy = x - center_x, y - center_y
  12.     if dx*dx + dy*dy < (radius * radius) then
  13.         return true
  14.     end
  15.     return false
  16. end
  17.  
  18. function createPlayer(px,py,pkey,pspeed, pimage)
  19.     player = {
  20.         direction = 0,
  21.         x = px, y = py,
  22.         moving = false,
  23.         key = pkey,
  24.         points = 0,
  25.         speed = pspeed,
  26.         image = love.graphics.newImage(pimage)
  27.     }
  28.    
  29.     return player
  30. end
  31.  
  32.  
  33. function reset(noPlayers)
  34.     numPlayers = noPlayers
  35.     playerImages = {
  36.         "redPlayer.png",
  37.         "bluePlayer.png",
  38.         "yellowPlayer.png",
  39.         "greenPlayer.png"
  40.     }
  41.     keys = { 'a','l','c','n' }
  42.  
  43.     for i = 1,numPlayers do
  44.         players[i] = createPlayer(
  45.             math.random(640),math.random(480),
  46.             keys[i], 250,
  47.             playerImages[i])
  48.     end
  49.    
  50.     ball = {
  51.         x = 200,
  52.         y = 400,
  53.         r = 200, g = 20, b = 20
  54.     }
  55. end
  56.  
  57. function love.load()   
  58.     players = {}
  59.     ball = {}
  60.     ballImage = love.graphics.newImage("ball.png")
  61.     numPlayers = 2
  62.     stdRot = 3.14/2
  63.     reset(numPlayers)
  64. end
  65.  
  66. function love.update(dt)
  67.    
  68.     for i = 1,numPlayers do
  69.         p = players[i]
  70.         if p.moving then
  71.             p.x = p.x + (math.cos(p.direction) * p.speed * dt)
  72.             p.y = p.y + (math.sin(p.direction) * p.speed * dt)
  73.            
  74.             for j = 1, numPlayers do
  75.                 if not (i == j) then
  76.                     p2 = players[j]
  77.                     if inCircle(p2.x, p2.y,
  78.                                          p.x, p.y, 64) then
  79.                         p.x = p.x - (math.cos(p.direction) * p.speed * dt)
  80.                         p.y = p.y - (math.sin(p.direction) * p.speed * dt)
  81.                     end
  82.                 end
  83.             end
  84.         else
  85.             -- rotate
  86.             p.direction = p.direction + 6.24*dt
  87.            
  88.             if (p.direction > 6.24) then
  89.                 p.direction = p.direction - 6.24
  90.             end
  91.         end
  92.        
  93.         if (inCircle(p.x, p.y, ball.x, ball.y, 40)) then
  94.             p.points = p.points + 1
  95.             ball.x, ball.y = generateNewPosition()
  96.             ball.r, ball.g, ball.b = generateNewColor()
  97.         end
  98.     end
  99. end
  100.  
  101. function love.draw()
  102.    
  103.     -- draw bg
  104.     love.graphics.setBackgroundColor(240,240,240)
  105.    
  106.     -- draw elements
  107.     love.graphics.setColor(255,255,255);
  108.    
  109.     for i = 1,numPlayers do
  110.         p = players[i]
  111.         love.graphics.setColor(255,255,255);
  112.         love.graphics.draw(p.image,
  113.             p.x,p.y,p.direction + stdRot,
  114.             1.0, 1.0, 32, 32)
  115.            
  116.         love.graphics.setColor(0,0,0);
  117.         love.graphics.print("Player " .. i .. " (" .. p.key .. ") points: " .. p.points .. " x,y: " .. p.x .. "," .. p.y , 10, 10 + i*20)
  118.     end
  119.     love.graphics.print("Ball: x " .. ball.x .. " y " .. ball.y, 10, 30 + numPlayers*20)
  120.    
  121.     love.graphics.setColor(ball.r, ball.g, ball.b)
  122.     love.graphics.draw(ballImage, ball.x, ball.y)
  123. end
  124.  
  125. function love.keypressed(key, unicode)
  126.     for i = 1, numPlayers do
  127.         if key == players[i].key then
  128.             players[i].moving = true
  129.         end
  130.     end
  131.    
  132.     if key == 'r' then
  133.         reset(numPlayers)
  134.     end
  135.    
  136.     if key == '1' then
  137.         reset(1)
  138.     elseif key == '2' then
  139.         reset(2)
  140.     elseif key == '3' then
  141.         reset(3)
  142.     elseif key == '4' then
  143.         reset(4)
  144.     end
  145. end
  146.  
  147. function love.keyreleased(key, unicode)
  148.     for i = 1, numPlayers do
  149.         if key == players[i].key then
  150.             players[i].moving = false
  151.         end
  152.     end
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement