init push
This commit is contained in:
parent
30654aa7fc
commit
6c1b73d970
BIN
Daydream.ttf
Normal file
BIN
Daydream.ttf
Normal file
Binary file not shown.
71
bullet.lua
Normal file
71
bullet.lua
Normal file
@ -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
|
||||||
BIN
bullet.png
Normal file
BIN
bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 379 B |
68
classic.lua
Normal file
68
classic.lua
Normal file
@ -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
|
||||||
6
conf.lua
Normal file
6
conf.lua
Normal file
@ -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
|
||||||
49
drawGame.lua
Normal file
49
drawGame.lua
Normal file
@ -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
|
||||||
25
enemy.lua
Normal file
25
enemy.lua
Normal file
@ -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
|
||||||
13
keyPressed.lua
Normal file
13
keyPressed.lua
Normal file
@ -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
|
||||||
47
main.lua
Normal file
47
main.lua
Normal file
@ -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
|
||||||
39
player.lua
Normal file
39
player.lua
Normal file
@ -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
|
||||||
BIN
player1.png
Normal file
BIN
player1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 190 B |
BIN
player2.png
Normal file
BIN
player2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
53
updateGame.lua
Normal file
53
updateGame.lua
Normal file
@ -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
|
||||||
32
walls.lua
Normal file
32
walls.lua
Normal file
@ -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
|
||||||
BIN
yellow.png
Normal file
BIN
yellow.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 220 B |
Loading…
x
Reference in New Issue
Block a user