Commit 7c13bf1e by Philipp Hansch

Added win/lose conditions and a menu. close #1

parent ef1fccbe
local Ball = {x, y, size}
Ball.__index = Ball
local speed = 15
function Ball.create()
local ball = {}
setmetatable(ball, Ball)
ball.size = 12
ball.speed = 200
Ball:resetBall()
math.randomseed(os.time())
return ball
end
......@@ -17,9 +15,9 @@ function Ball:draw()
love.graphics.rectangle("fill", self.x, self.y, self.size, self.size)
end
function Ball:update(paddle, level)
self.x = self.x + self.xdir * Game.dt * speed
self.y = self.y + self.ydir * Game.dt * speed
function Ball:update(paddle, game, dt)
self.x = self.x + self.xdir * dt * self.speed
self.y = self.y + self.ydir * dt * self.speed
-- ball collides with top, left or right
if(self.y <= 0) then
......@@ -32,9 +30,10 @@ function Ball:update(paddle, level)
self.xdir = -1
end
-- if y > height -> round over
-- if self.y > winheight then substract life and reset ball
if(self.y >= love.graphics.getHeight()) then
-- one life less
game.lifes = game.lifes - 1
self:resetBall()
end
-- check if ball collides with paddle
......@@ -49,27 +48,25 @@ function Ball:update(paddle, level)
end
end
-- check if ball collides with brick
for i,rows in ipairs(Bricks) do
for j, brick in ipairs(rows) do
if(self:checkCollision(brick) and brick.visible) then
-- check if ball collides with brick
if(self:checkCollision(brick) and brick.visible) then
-- Detect the side that was hit and reflect the ball appropriately
if(self.y >= (brick.y + brick.height)) then
self.ydir = 1 -- from below
print("from below")
elseif((self.y+self.size) <= brick.y+5) then
self.ydir = -1 -- from above
print("from above")
elseif((self.x + self.size) <= brick.x + 5) then
self.xdir = -1 -- from left (something not perfect here)
print("from left" .. self.x .. " " .. brick.x)
elseif(self.x > brick.x) then
self.xdir = 1 -- from right
print("from right")
end
brick.visible = false
game.bricksHit = game.bricksHit + 1
end
end
end
......@@ -81,10 +78,12 @@ function Ball:resetBall()
self.y = love.graphics.getHeight() / 2
--set random direction for x
math.randomseed(os.time())
self.xdir = math.random() >= 0.5 and 1 or -1
self.ydir = 1 --move down always
end
-- Ball <-> Paddle collision check
function Ball:checkCollision(paddle)
-- Simple bounding box collision check
-- source: https://love2d.org/wiki/BoundingBox.lua
......@@ -97,6 +96,7 @@ function Ball:checkCollision(paddle)
self.y < paddle_y2 and ball_y2 > paddle.y
end
-- Ball <-> Brick collision check
function Ball:checkCollision(brick)
local ball_x2 = self.x + self.size
local ball_y2 = self.y + self.size
......
-- Brick data structure
local Brick = { x, y, width, height, {r, g, b, a} }
Brick.__index = Brick
......@@ -15,9 +15,5 @@ function Brick.create(x, y, width, height, r, g, b)
return brick
end
function Brick:update()
end
return Brick
-- game stats
Game = {}
Game.width = love.graphics.getWidth()
Game.height = love.graphics.getHeight()
Game.dt = love.timer.getDelta()
local Game = {}
Game.__index = Game
function Game.create()
local game = {}
setmetatable(game, Game)
game.width = love.graphics.getWidth()
game.height = love.graphics.getHeight()
game.dt = love.timer.getDelta()
game.score = 0
game.lifes = 3
game.running = false
game.bricksHit = 0
return game
end
function Game:update()
self.dt = love.timer.getDelta()
if self.lifes == 0 then
self.running = false
end
if self.bricksHit == 100 then
self.running = false
end
end
function Game:draw()
-- draw lifes
love.graphics.print("Lifes:"..self.lifes, 10, 10, 0, 1, 1)
end
return Game
\ No newline at end of file
......@@ -27,7 +27,7 @@ end
function Level:setup()
for i=1,10 do
Bricks[i] = {} -- create a new row
Bricks[i] = {} -- create a new row
for j=1,10 do
x = 10 + i * 45
y = 10 + j * 20
......
require 'Game'
local Menu = {}
Menu.__index = Menu
function Menu.create()
local menu = {}
menu.width = love.graphics.getWidth()
menu.height = love.graphics.getHeight()
setmetatable(menu, Menu)
return menu
end
function Menu:draw()
love.graphics.print("Press 'Enter' to start the game", self.width/2-100, self.height/2-30)
love.graphics.print("Press 'Esc' to exit the game", self.width/2-100, self.height/2)
end
return Menu
\ No newline at end of file
local Game = require 'Game'
local Paddle = { x, y, width }
local Paddle = {}
Paddle.__index = Paddle
local height
local speed = 20
function Paddle.create()
function Paddle.create(game)
local paddle = {}
setmetatable(paddle, Paddle)
paddle.x = Game.width / 2
paddle.y = Game.height - 30
paddle.x = game.width / 2
paddle.y = game.height - 30
paddle.width = 100
paddle.height = 20
paddle.speed = 300
return paddle
end
......@@ -21,8 +17,8 @@ function Paddle:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
function Paddle:update(dir)
local moveX = self.x + dir * Game.dt * speed
function Paddle:update(dir, game)
local moveX = self.x + dir * game.dt * self.speed
--bounds checking
if(moveX < love.graphics.getWidth()-self.width-5 and moveX > 5) then
......
io.stdout:setvbuf("no")
local Level = require 'Level'
local Paddle = require 'Paddle'
local Ball = require 'Ball'
local Game = require 'Game'
local Menu = require 'Menu'
local paddle = Paddle.create()
local ball = Ball.create()
local level = Level.create()
function love.load()
level:setup()
local game = {}
local paddle = {}
local ball = {}
local level = {}
local menu = Menu.create(game)
function love.draw()
function love.draw()
paddle:draw()
ball:draw()
if game.running then
paddle:draw()
ball:draw()
level:draw()
game:draw()
else
-- show menu screen
menu:draw()
end
end
function love.update()
function love.update(dt)
if game.running then
ball:update(paddle, game, dt)
game:update()
if love.keyboard.isDown("a") then
paddle:update(-1) -- move paddle left
end
if love.keyboard.isDown("d") then
if love.keyboard.isDown("a") then
paddle:update(-1, game) -- move paddle left
end
if love.keyboard.isDown("d") then
paddle:update(1, game) -- move paddle right
end
else
-- handle menu screen input
if love.keyboard.isDown("return") then
startGame()
end
if love.keyboard.isDown("escape") then
love.event.quit()
end
end
end
function startGame()
game = Game.create()
level = Level.create()
game.running = true
level:setup()
paddle = Paddle.create(game)
ball = Ball.create()
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment