diff --git a/Daydream.ttf b/Daydream.ttf new file mode 100644 index 0000000..35f12e4 Binary files /dev/null and b/Daydream.ttf differ diff --git a/bullet.lua b/bullet.lua new file mode 100644 index 0000000..a8119ec --- /dev/null +++ b/bullet.lua @@ -0,0 +1,71 @@ +Bullet = Object:extend() + +function Bullet:new(x, y, p, image) + self.image = love.graphics.newImage(image) + self.x = x + self.y = y + self.p = p + self.width = self.image:getWidth() + self.height = self.image:getHeight() +end + +function Bullet:update(dt) + --if bullet belongs to player 1, travel upwards + if self.p == 1 then + self.y = self.y - (500 * dt) + --if bullet belongs to player 2, travel downwards + elseif self.p == 2 then + self.y = self.y + (500 * dt) + end + + --check if the bullet has hit a player + if self.p == 1 then + if + self.x < UserPlayer2.x + UserPlayer2.width + and UserPlayer2.x < self.x + self.width + and self.y < UserPlayer2.y + UserPlayer2.height + and UserPlayer2.y < self.y + self.height + then + UserPlayer2.health = UserPlayer2.health - 1 + Bullets1 = {} + end + end + + --check if the bullet has hit a player + if self.p == 2 then + if + self.x < UserPlayer1.x + UserPlayer1.width + and UserPlayer1.x < self.x + self.width + and self.y < UserPlayer1.y + UserPlayer1.height + and UserPlayer1.y < self.y + self.height + then + UserPlayer1.health = UserPlayer1.health - 1 + Bullets2 = {} + end + end + + --check if the bullet has hit a wall + for i, v in ipairs(Walls) do + if self.x < v.x + v.w and v.x < self.x + self.width and self.y < v.y + v.h and v.y < self.y + self.height then + if self.p == 1 then + Bullets1 = {} + elseif self.p == 2 then + Bullets2 = {} + end + end + end +end + +function Bullet:clear() + Bullets1 = {} + Bullets2 = {} +end + +function Bullet:draw() + for _, v in ipairs(Bullets1) do + love.graphics.draw(self.image, self.x, self.y) + end + for _, v in ipairs(Bullets2) do + love.graphics.draw(self.image, self.x, self.y) + end +end diff --git a/bullet.png b/bullet.png new file mode 100644 index 0000000..64aa9c1 Binary files /dev/null and b/bullet.png differ diff --git a/classic.lua b/classic.lua new file mode 100644 index 0000000..cbd6f81 --- /dev/null +++ b/classic.lua @@ -0,0 +1,68 @@ +-- +-- classic +-- +-- Copyright (c) 2014, rxi +-- +-- This module is free software; you can redistribute it and/or modify it under +-- the terms of the MIT license. See LICENSE for details. +-- + + +local Object = {} +Object.__index = Object + + +function Object:new() +end + + +function Object:extend() + local cls = {} + for k, v in pairs(self) do + if k:find("__") == 1 then + cls[k] = v + end + end + cls.__index = cls + cls.super = self + setmetatable(cls, self) + return cls +end + + +function Object:implement(...) + for _, cls in pairs({...}) do + for k, v in pairs(cls) do + if self[k] == nil and type(v) == "function" then + self[k] = v + end + end + end +end + + +function Object:is(T) + local mt = getmetatable(self) + while mt do + if mt == T then + return true + end + mt = getmetatable(mt) + end + return false +end + + +function Object:__tostring() + return "Object" +end + + +function Object:__call(...) + local obj = setmetatable({}, self) + obj:new(...) + return obj +end + + +return Object diff --git a/conf.lua b/conf.lua new file mode 100644 index 0000000..7065f0c --- /dev/null +++ b/conf.lua @@ -0,0 +1,6 @@ +function love.conf(t) + t.window.width = 1280 + t.window.height = 720 + t.window.title = "Space Invaders" + t.window.icon = "green.png" +end diff --git a/drawGame.lua b/drawGame.lua new file mode 100644 index 0000000..f1d3d72 --- /dev/null +++ b/drawGame.lua @@ -0,0 +1,49 @@ +function DrawGameOver() + local s = "" + if HasP1Won then + s = "Player 1 Wins" + elseif HasP2Won then + s = "Player 2 Wins" + end + local sw, sh = GameFont:getWidth(s), GameFont:getHeight(s) + local centreX = love.graphics.getWidth() / 2 + local centreY = love.graphics.getHeight() / 2 + love.graphics.print(s, centreX - sw / 2, centreY - sh / 2) +end + +function DisplayHealth() + local height = love.graphics.getHeight() + local width = love.graphics.getWidth() + love.graphics.print("" .. UserPlayer1.health, width - 120, height - 120) + love.graphics.print("" .. UserPlayer2.health, 0, 0) +end + +function drawGame() + -- Debug Info + love.graphics.setColor(1, 1, 1) -- RGB values for white are (1, 1, 1) + love.graphics.setFont(DebugFont) + love.graphics.print("FPS: " .. love.timer.getFPS(), 1200, 10) + -- + + love.graphics.setFont(GameFont) + --love.graphics.print("" .. TimeToPlaceWall, 300, 500) + DisplayHealth() + DrawGameOver() + + -- Bullets Draw + for _, v in ipairs(Bullets1) do + v:draw() + end + for _, v in ipairs(Bullets2) do + v:draw() + end + + -- Players Draw + UserPlayer1:draw() + UserPlayer2:draw() + + -- Walls Draw + for _, v in ipairs(Walls) do + v:draw() + end +end diff --git a/enemy.lua b/enemy.lua new file mode 100644 index 0000000..03d1d43 --- /dev/null +++ b/enemy.lua @@ -0,0 +1,25 @@ +Enemy = Object:extend() + +function Enemy:new(x, y, image, speed) + self.image = love.graphics.newImage(image) + self.x = x + self.y = y + self.speed = speed + self.width = self.image:getWidth() + self.height = self.image:getHeight() +end + +function Enemy:update(dt) + --check if enemy has touched the wall and bouce back + if self.x < 0 then + self.x = 0 + self.speed = -self.speed + elseif self.x + self.width > love.graphics.getWidth() then + self.x = love.graphics.getWidth() - self.width + self.speed = -self.speed + end +end + +function Enemy:draw() + love.graphics.draw(self.image, self.x, self.y) +end diff --git a/extra.png b/extra.png new file mode 100644 index 0000000..cbbf823 Binary files /dev/null and b/extra.png differ diff --git a/green.png b/green.png new file mode 100644 index 0000000..6f2f4f2 Binary files /dev/null and b/green.png differ diff --git a/keyPressed.lua b/keyPressed.lua new file mode 100644 index 0000000..3b49ebb --- /dev/null +++ b/keyPressed.lua @@ -0,0 +1,13 @@ +function keyPressed(key) + if key == "space" then + local bullet = Bullet(UserPlayer1.x + UserPlayer1.width / 2, UserPlayer1.y, 1, "bullet.png", 500) + table.insert(Bullets1, bullet) + elseif key == "return" then + local bullet = Bullet(UserPlayer2.x + UserPlayer2.width / 2, UserPlayer2.y, 2, "bullet.png", 500) + table.insert(Bullets2, bullet) + elseif key == "escape" then + love.event.quit() + elseif key == "r" then + love.load() + end +end diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..43dd82c --- /dev/null +++ b/main.lua @@ -0,0 +1,47 @@ +function love.load() + -- Objects, classes + Object = require("classic") + require("player") + require("bullet") + require("walls") + + -- Lua files for the game + require("updateGame") + require("drawGame") + require("keyPressed") + + love.math.setRandomSeed(os.time()) + + GameFont = love.graphics.newFont("Daydream.ttf", 60) + DebugFont = love.graphics.newFont("Daydream.ttf", 12) + love.graphics.setFont(GameFont) + + ScrnHeight = love.graphics.getHeight() + ScrnWidth = love.graphics.getWidth() + + UserPlayer1 = Player(400, ScrnHeight - 30, 1, 3, "player1.png", 300) + UserPlayer2 = Player(200, 0, 2, 3, "player2.png", 300) + + Bullets1 = {} + Bullets2 = {} + + Walls = {} + + TIMER = 10 + + TimeToPlaceWall = TIMER + HasP1Won = false + HasP2Won = false +end + +function love.keypressed(key) + keyPressed(key) +end + +function love.update(dt) + updateGame(dt) +end + +function love.draw() + drawGame() +end diff --git a/player.lua b/player.lua new file mode 100644 index 0000000..b8f526d --- /dev/null +++ b/player.lua @@ -0,0 +1,39 @@ +Player = Object:extend() + +function Player:new(x, y, p, health, image, speed) + self.image = love.graphics.newImage(image) + self.x = x + self.y = y + self.p = p + self.health = health + self.speed = speed + self.width = self.image:getWidth() + self.height = self.image:getHeight() +end + +function Player:update(dt) + if self.p == 1 then + if love.keyboard.isDown("d") then + self.x = self.x + (self.speed * dt) + elseif love.keyboard.isDown("a") then + self.x = self.x - (self.speed * dt) + end + elseif self.p == 2 then + if love.keyboard.isDown("right") then + self.x = self.x + (self.speed * dt) + elseif love.keyboard.isDown("left") then + self.x = self.x - (self.speed * dt) + end + end + + --check if player is out of bounds + if self.x < 0 then + self.x = 0 + elseif self.x + self.width > love.graphics.getWidth() then + self.x = love.graphics.getWidth() - self.width + end +end + +function Player:draw() + love.graphics.draw(self.image, self.x, self.y) +end diff --git a/player1.png b/player1.png new file mode 100644 index 0000000..347069d Binary files /dev/null and b/player1.png differ diff --git a/player2.png b/player2.png new file mode 100644 index 0000000..bfaedfb Binary files /dev/null and b/player2.png differ diff --git a/red.png b/red.png new file mode 100644 index 0000000..04efa8a Binary files /dev/null and b/red.png differ diff --git a/updateGame.lua b/updateGame.lua new file mode 100644 index 0000000..094a22d --- /dev/null +++ b/updateGame.lua @@ -0,0 +1,53 @@ +function CheckHealth(p1, p2) + if p1.health == 0 then + HasP2Won = true + elseif p2.health == 0 then + HasP1Won = true + end +end + +function updateGame(dt) + TimeToPlaceWall = TimeToPlaceWall - dt + if TimeToPlaceWall <= 0 then + --place a wall at a random location + local ranx = love.math.random(100, 1000) + local rany = love.math.random(100, 600) + local ranw = love.math.random(100, 500) + local ranh = love.math.random(20, 80) + local lifetime = love.math.random(4, 10) + local wall = Wall(ranx, rany, ranw, ranh, lifetime) + table.insert(Walls, wall) + TimeToPlaceWall = TIMER --reset the timer + end + + -- Walls Updates + for i, v in ipairs(Walls) do + v:update(dt) + end + + -- Check the players health + CheckHealth(UserPlayer1, UserPlayer2) + + -- Bullets Updates + for i, v in ipairs(Bullets1) do + v:update(dt) + if v.y < 0 then --top of screen + table.remove(Bullets1, i) + elseif v.y > love.graphics.getHeight() then --bottom of screen + table.remove(Bullets1, i) + end + end + + for i, v in ipairs(Bullets2) do + v:update(dt) + if v.y < 0 then --top of screen + table.remove(Bullets2, i) + elseif v.y > love.graphics.getHeight() then --bottom of screen + table.remove(Bullets2, i) + end + end + + -- Player Updates + UserPlayer1:update(dt) + UserPlayer2:update(dt) +end diff --git a/walls.lua b/walls.lua new file mode 100644 index 0000000..68a68eb --- /dev/null +++ b/walls.lua @@ -0,0 +1,32 @@ +Wall = Object:extend() + +function Wall:new(x, y, w, h, lifetime) + self.x = x + self.y = y + self.w = w --width of wall + self.h = h --height of wall + self.lifetime = lifetime +end + +function lerp(a, b, t) + return a + (b - a) * t +end + +function Wall:update(dt) + --slowly decrease the lifetime of the wall + self.lifetime = self.lifetime - dt + --delete wall after lifetime is up + if self.lifetime <= 0 then + for i, v in ipairs(Walls) do + if v == self then --if the wall is the same as the wall we want to delete + table.remove(Walls, i) + end + end + end +end + +function Wall:draw() + local rectangleColor = { 1, 0, 0 } -- Starting color (red) + love.graphics.setColor(rectangleColor) --white + love.graphics.rectangle("fill", self.x, self.y, self.w, self.h) +end diff --git a/yellow.png b/yellow.png new file mode 100644 index 0000000..4055b3d Binary files /dev/null and b/yellow.png differ