commit 417bbd42b6b08f0706cf045b4f2bb092a5ab46ce Author: DarkElfMagic Date: Tue Apr 18 20:52:15 2023 -0500 FIRST PROJECT IN PONG diff --git a/ai.lua b/ai.lua new file mode 100644 index 0000000..9e9a863 --- /dev/null +++ b/ai.lua @@ -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 diff --git a/ball.lua b/ball.lua new file mode 100644 index 0000000..f4e8d53 --- /dev/null +++ b/ball.lua @@ -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 diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..9fe8e49 --- /dev/null +++ b/conf.lua @@ -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 diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..c3d6d46 --- /dev/null +++ b/main.lua @@ -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 diff --git a/player.lua b/player.lua new file mode 100644 index 0000000..ff15572 --- /dev/null +++ b/player.lua @@ -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