FIRST PROJECT IN PONG

This commit is contained in:
DarkElfMagic 2023-04-18 20:52:15 -05:00
commit 417bbd42b6
5 changed files with 200 additions and 0 deletions

47
ai.lua Normal file
View File

@ -0,0 +1,47 @@
AI = {}
function AI:load()
self.width = 20
self.height = 100
self.x = love.graphics.getWidth() - self.width - 50
self.y = love.graphics.getHeight() / 2
self.yVel = 0
self.speed = 500
self.timer = 0
self.rate = 0.5
end
function AI:update(dt)
self:move(dt)
self.timer = self.timer + dt
if self.timer > self.rate then
self.timer = 0
self:acquireTarget()
end
end
function AI:move(dt)
self.y = self.y + self.yVel * dt
end
function AI:acquireTarget()
if Ball.y + Ball.height < self.y then
self.yVel = -self.speed
elseif Ball.y > self.y + self.height then
self.yVel = self.speed
else
self.yVel = 0
end
end
function AI:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

74
ball.lua Normal file
View File

@ -0,0 +1,74 @@
Ball = {}
function Ball:load()
self.x = love.graphics.getWidth() / 2
self.y = love.graphics.getHeight() / 2
self.width = 20
self.height = 20
self.speed = 200
self.xVel = -self.speed
self.yVel = 0
end
function Ball:update(dt)
self:move(dt)
self:collide()
end
function Ball:collide()
if checkCollision(self, Player) then
self.xVel = self.speed
local middleBall = self.y + self.height / 2
local middlePlayer = Player.y + Player.height / 2
local collisionPosition = middleBall - middlePlayer
self.yVel = collisionPosition * 5
end
if checkCollision(self, AI) then
self.xVel = -self.speed
local middleBall = self.y + self.height / 2
local middleAI = AI.y + AI.height / 2
local collisionPosition = middleBall - middleAI
self.yVel = collisionPosition * 5
end
if self.y < 0 then
self.y = 0
self.yVel = -self.yVel
elseif self.y + self.height > love.graphics.getHeight() then
self.y = love.graphics.getHeight() - self.height
self.yVel = -self.yVel
end
if self.x < 0 then
self.x = love.graphics.getWidth() / 2 - self.width / 2
self.y = love.graphics.getHeight() / 2 - self.height / 2
self.yVel = 0
self.xVel = self.speed
end
if self.x + self.width > love.graphics.getWidth() then
self.x = love.graphics.getWidth() / 2 - self.width / 2
self.y = love.graphics.getHeight() / 2 - self.height / 2
self.yVel = 0
self.xVel = -self.speed
end
end
function Ball:move(dt)
self.x = self.x + self.xVel * dt
self.y = self.y + self.yVel * dt
end
function Ball:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

8
conf.lua Normal file
View File

@ -0,0 +1,8 @@
function love.conf(t)
t.title = "Pong" -- The title of the window the game is in (string)
t.version = "11.3" -- The LÖVE version this game was made for (string)
t.console = true -- Attach a console (boolean, Windows only)
t.window.width = 1280 -- The window width (number)
t.window.height = 720 -- The window height (number)
end

32
main.lua Normal file
View File

@ -0,0 +1,32 @@
require("player")
require("ball")
require("ai")
function love.load()
Player:load()
Ball:load()
AI:load()
end
function love.update(dt)
Player:update(dt)
Ball:update(dt)
AI:update(dt)
end
function love.draw()
Player:draw()
Ball:draw()
AI:draw()
end
function checkCollision(a, b)
if a.x + a.width > b.x and a.x < b.x + b.width and a.y + a.height > b.y and a.y < b.y + b.height then
return true
else
return false
end
end

39
player.lua Normal file
View File

@ -0,0 +1,39 @@
Player = {}
function Player:load()
self.x = 50
self.y = love.graphics.getHeight() / 2
self.width = 20
self.height = 100
self.speed = 500
end
function Player:update(dt)
self:move(dt)
self:checkBoundaries()
end
function Player:move(dt)
if love.keyboard.isDown("w") then
self.y = self.y - self.speed * dt
elseif love.keyboard.isDown("s") then
self.y = self.y + self.speed * dt
end
end
function Player:checkBoundaries()
if self.y < 0 then
self.y = 0
elseif self.y + self.height > love.graphics.getHeight() then
self.y = love.graphics.getHeight() - self.height
end
end
function Player:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end