Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateNewPosition()
- return math.random(640), math.random(480)
- end
- function generateNewColor()
- return math.random(255), math.random(255), math.random(255)
- end
- function inCircle(x,y, center_x, center_y, radius)
- local dx, dy = x - center_x, y - center_y
- if dx*dx + dy*dy < (radius * radius) then
- return true
- end
- return false
- end
- function createPlayer(px,py,pkey,pspeed, pimage)
- player = {
- direction = 0,
- x = px, y = py,
- moving = false,
- key = pkey,
- points = 0,
- speed = pspeed,
- image = love.graphics.newImage(pimage)
- }
- return player
- end
- function reset(noPlayers)
- numPlayers = noPlayers
- playerImages = {
- "redPlayer.png",
- "bluePlayer.png",
- "yellowPlayer.png",
- "greenPlayer.png"
- }
- keys = { 'a','l','c','n' }
- for i = 1,numPlayers do
- players[i] = createPlayer(
- math.random(640),math.random(480),
- keys[i], 250,
- playerImages[i])
- end
- ball = {
- x = 200,
- y = 400,
- r = 200, g = 20, b = 20
- }
- end
- function love.load()
- players = {}
- ball = {}
- ballImage = love.graphics.newImage("ball.png")
- numPlayers = 2
- stdRot = 3.14/2
- reset(numPlayers)
- end
- function love.update(dt)
- for i = 1,numPlayers do
- p = players[i]
- if p.moving then
- p.x = p.x + (math.cos(p.direction) * p.speed * dt)
- p.y = p.y + (math.sin(p.direction) * p.speed * dt)
- for j = 1, numPlayers do
- if not (i == j) then
- p2 = players[j]
- if inCircle(p2.x, p2.y,
- p.x, p.y, 64) then
- p.x = p.x - (math.cos(p.direction) * p.speed * dt)
- p.y = p.y - (math.sin(p.direction) * p.speed * dt)
- end
- end
- end
- else
- -- rotate
- p.direction = p.direction + 6.24*dt
- if (p.direction > 6.24) then
- p.direction = p.direction - 6.24
- end
- end
- if (inCircle(p.x, p.y, ball.x, ball.y, 40)) then
- p.points = p.points + 1
- ball.x, ball.y = generateNewPosition()
- ball.r, ball.g, ball.b = generateNewColor()
- end
- end
- end
- function love.draw()
- -- draw bg
- love.graphics.setBackgroundColor(240,240,240)
- -- draw elements
- love.graphics.setColor(255,255,255);
- for i = 1,numPlayers do
- p = players[i]
- love.graphics.setColor(255,255,255);
- love.graphics.draw(p.image,
- p.x,p.y,p.direction + stdRot,
- 1.0, 1.0, 32, 32)
- love.graphics.setColor(0,0,0);
- love.graphics.print("Player " .. i .. " (" .. p.key .. ") points: " .. p.points .. " x,y: " .. p.x .. "," .. p.y , 10, 10 + i*20)
- end
- love.graphics.print("Ball: x " .. ball.x .. " y " .. ball.y, 10, 30 + numPlayers*20)
- love.graphics.setColor(ball.r, ball.g, ball.b)
- love.graphics.draw(ballImage, ball.x, ball.y)
- end
- function love.keypressed(key, unicode)
- for i = 1, numPlayers do
- if key == players[i].key then
- players[i].moving = true
- end
- end
- if key == 'r' then
- reset(numPlayers)
- end
- if key == '1' then
- reset(1)
- elseif key == '2' then
- reset(2)
- elseif key == '3' then
- reset(3)
- elseif key == '4' then
- reset(4)
- end
- end
- function love.keyreleased(key, unicode)
- for i = 1, numPlayers do
- if key == players[i].key then
- players[i].moving = false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement