Code: Select all
--Collaborative Coding Horror Experiment
function love.load()
ScreenWidth = love.graphics.getWidth()
ScreenHeight = love.graphics.getHeight()
player = Player(100,100)
Enemies = {}
for _ = 1, love.math.random( 5, 10 ) do Enemies[#Enemies + 1] = NewEnemy( love.math.random( 50, ScreenWidth - 50 ), love.math.random( 50, ScreenHeight - 50 ) ) end
end
function love.update(dt)
player:update(dt)
for _, Enemy in pairs( Enemies ) do Enemy:Update( dt ) end
end
function love.draw()
local rumble = 10
local c = healthcolor(player.health/player.health_max)
love.graphics.setColor(c[1],c[2],c[3])
local offset = {
x=math.random(0,rumble),
y=math.random(0,rumble)
}
local size = {
w=love.window.getWidth()-offset.x-math.random(0,rumble),
h=love.window.getHeight()-offset.y-math.random(0,rumble)
}
love.graphics.rectangle("fill",offset.x,offset.y,size.w,size.h)
player.draw()
for _, Enemy in pairs( Enemies ) do Enemy:Draw( dt ) end
end
function love.keypressed(key)
end
function love.keyreleased(key)
end
function love.mousepressed(x,y,button)
if button == "l" then
player.x, player.y = player.setPlayerPosition(x, y)
end
end
function love.mousereleased(x,y,button)
end
function Player(x,y)
local self = {x=x,y=y}
self.health = 100
self.health_max = 100
self.health_dt = 0
self.health_dt_t = 0.1
self.radius = 50
self.speed = 256
self.direction = 1
self.update = function(self,dt)
self.health_dt = self.health_dt + dt
if self.health_dt > self.health_dt_t then
self.health_dt = self.health_dt - self.health_dt_t
self:upkeep()
end
self:spin(dt)
self:CheckCircularCollisions( Enemies )
if love.keyboard.isDown( 'w' ) then self.y = self.y - self.speed * dt end
if love.keyboard.isDown( 'a' ) then self.x, self.direction = self.x - self.speed * dt, -1 end
if love.keyboard.isDown( 's' ) then self.y = self.y + self.speed * dt end
if love.keyboard.isDown( 'd' ) then self.x, self.direction = self.x + self.speed * dt, 1 end
if not love.keyboard.isDown( 'a' ) and not love.keyboard.isDown( 'd' ) then self.direction = 0 end
if love.keyboard.isDown( 'up' ) then self.radius = ( self.radius + 1 ) < 75 and self.radius + 1 or 75 end
if love.keyboard.isDown( 'down' ) then self.radius = ( self.radius - 1 ) > 20 and self.radius - 1 or 20 end
end
self.upkeep = function(self)
self.health = self.health - 1
end
function self.draw()
love.graphics.push()
love.graphics.translate(self.x, self.y)
love.graphics.rotate(self.rotation)
love.graphics.translate(-self.x, -self.y)
love.graphics.setColor(30,80,120)
love.graphics.circle("fill",self.x,self.y,self.radius,self.radius)
love.graphics.setColor(255,255,255)
love.graphics.circle("fill",self.x-self.radius / 2,self.y - ( self.radius / ( 10 / 3 ) ),self.radius / 10,self.radius)
love.graphics.circle("fill",self.x+self.radius / 2,self.y - ( self.radius / ( 10 / 3 ) ),self.radius / 10,self.radius)
love.graphics.arc("fill",self.x,self.y+(self.radius/5),self.radius/2.5,-0,math.pi)
love.graphics.pop()
love.graphics.setColor( 255, 255, 255 )
love.graphics.print( self.health )
end
self.rotation = 0
function self.spin(self, dt)
self.rotation = self.rotation + ( math.pi * 4 * dt * self.direction )
end
function self.setPlayerPosition(x,y)
self.x, self.y = x, y
return self.x, self.y
end
function self.CheckCircularCollisions( Self, CollisionTable )
for _, Enemy in pairs( CollisionTable ) do
local Length = GetDistance( Self.x, Self.y, Enemy.x, Enemy.y )
if Length < self.radius + Enemy.Radius then
Self.health = Self.health - Enemy.Damage
Enemy.Health = 0
end
end
end
return self
end
function GetDistance( x1, y1, x2, y2 )
return math.sqrt( ( x1 - x2 ) ^ 2 + ( y1 - y2 ) ^ 2 )
end
function healthcolor(i)
-- Clamp
if i > 1 then i = 1 end
if i < 0 then i = 0 end
-- 0 -> 0.5: 255
-- 0.5 -> 1: 255 .. 0
local r = 255*2 - 255*2*i
if r > 255 then r = 255 end
-- 0 -> 0.5: 0 .. 255
-- 0.5 -> 1: 255
local g = 255*2*i
if g > 255 then g = 255 end
-- 0 -> 1: 0
local b = 0
return {r,g,b}
end
function NewEnemy( x, y )
local Self = { x = x, y = y, Rotation = 0, Radius = love.math.random( 20, 50 ), Dir = 1, Index = #Enemies + 1 }
Self.VelocityX = Self.Radius / 10
Self.Damage = Self.Radius / 2
Self.Health = Self.Radius * 5
Self.Rotation = 0
function Self.Spin( Self, dt )
Self.Rotation = Self.Rotation + math.pi * ( Self.Radius / 20 ) * dt * Self.Dir
end
function Self.Update( Self, dt )
if Self.Health <= 0 then Enemies[Self.Index] = nil return end
Self.x = Self.x + Self.VelocityX
if Self.x - Self.Radius < 0 or Self.x + Self.Radius > ScreenWidth then Self.VelocityX, Self.Dir = Self.VelocityX * -1, Self.Dir * -1 end
Self.Eyes = {
{ x = Self.x - ( Self.Radius / 2 ), y = Self.y - ( Self.Radius / 2 ), Radius = Self.Radius / 10 },
{ x = Self.x + ( Self.Radius / 2 ), y = Self.y - ( Self.Radius / 2 ), Radius = Self.Radius / 10 },
}
Self:Spin( dt )
end
function Self.Draw( Self )
love.graphics.push()
love.graphics.translate( Self.x, Self.y )
love.graphics.rotate( Self.Rotation )
love.graphics.translate( -Self.x, -Self.y )
-- Body
love.graphics.setColor( 0, 0, 0 )
love.graphics.circle( 'fill', Self.x, Self.y, Self.Radius )
-- Eyes
love.graphics.setColor( 255, 255, 255 )
love.graphics.circle( 'fill', Self.Eyes[1].x, Self.Eyes[1].y, Self.Eyes[1].Radius )
love.graphics.circle( 'fill', Self.Eyes[2].x, Self.Eyes[2].y, Self.Eyes[2].Radius )
-- Mouth
love.graphics.setColor( 255, 0, 0 )
love.graphics.setLineWidth( Self.Radius / 16 )
love.graphics.line( Self.Eyes[1].x - ( Self.Radius / 3 ), Self.Eyes[1].y - ( Self.Radius / 3 ),
Self.Eyes[1].x + Self.Radius / 2, Self.Eyes[1].y )
love.graphics.line( Self.Eyes[2].x + ( Self.Radius / 3 ), Self.Eyes[2].y - ( Self.Radius / 3 ),
Self.Eyes[2].x - Self.Radius / 2, Self.Eyes[2].y )
love.graphics.arc( 'fill', Self.x, Self.y + ( Self.Radius / 4 ), Self.Radius / 1.75, -0, math.pi )
-- Teeth
love.graphics.setColor( 255, 255, 255 )
local MouthWidth = ( Self.Radius / 1.75 ) * 2
local MouthStartX = Self.x - MouthWidth / 2
local MouthY = Self.y + ( Self.Radius / 4 )
local NumTeeth = Self.Radius
local TeethSeperation = MouthWidth / NumTeeth
local TeethHeight = MouthWidth / 6
for Index = 0, NumTeeth - 1 do
local ToothX = MouthStartX + ( TeethSeperation * Index )
love.graphics.polygon( 'fill', ToothX, MouthY, ToothX + ( TeethSeperation / 2 ), MouthY + TeethHeight, ToothX + TeethSeperation, MouthY )
end
love.graphics.pop()
end
return Self
end
Added keyboard movement: use wasd to move, up to increase size, down to decrease size. You can still click if you want.