Difference between revisions of "Love - Lemmings Movement"

From AQA Computer Science
Jump to: navigation, search
(Created page with "<syntaxhighlight lang=lua> local width = 800 local height = 800 local lemtime = 2 local elapsed = lemtime local maxlem=10 local lemcount = 0 local startx = 100 local starty...")
 
(Lemmings Class & List of Lemmings)
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==Variables Required==
 
<syntaxhighlight lang=lua>
 
<syntaxhighlight lang=lua>
 
local width = 800
 
local width = 800
Line 5: Line 6:
 
local lemtime = 2
 
local lemtime = 2
 
local elapsed = lemtime
 
local elapsed = lemtime
local maxlem=10
+
local maxlem = 10
 
local lemcount = 0
 
local lemcount = 0
  
 
local startx = 100
 
local startx = 100
 
local starty = 100
 
local starty = 100
 +
</syntaxhighlight>
  
 +
==Lemmings Class & List of Lemmings==
 +
<syntaxhighlight lang=lua>
 
Lems = {}
 
Lems = {}
  
Line 52: Line 56:
 
end
 
end
 
end
 
end
 
 
end
 
end
 
 
Line 65: Line 68:
 
return self
 
return self
 
end
 
end
 +
</syntaxhighlight>
  
 
+
==Method to Create Lemming==
 +
<syntaxhighlight lang=lua>
 
function createlem()
 
function createlem()
 
if lemcount<maxlem then
 
if lemcount<maxlem then
Line 73: Line 78:
 
end
 
end
 
end
 
end
 +
</syntaxhighlight>
  
 +
==love.load method==
 +
<syntaxhighlight lang=lua>
 
function love.load()
 
function love.load()
 
love.window.setMode(width, height)
 
love.window.setMode(width, height)
Line 81: Line 89:
 
lem = love.graphics.newImage("lemming.png")
 
lem = love.graphics.newImage("lemming.png")
 
end
 
end
 +
</syntaxhighlight>
  
 +
==love.update method==
 +
<syntaxhighlight lang=lua>
 
function love.update(dt)
 
function love.update(dt)
 
elapsed = elapsed - dt
 
elapsed = elapsed - dt
Line 94: Line 105:
 
end
 
end
 
end
 
end
   
+
  </syntaxhighlight>
 +
 
 +
==love.draw method==
 +
<syntaxhighlight lang=lua>
 
function love.draw()
 
function love.draw()
 
love.graphics.draw(map, 0, 0)
 
love.graphics.draw(map, 0, 0)

Revision as of 14:30, 9 July 2019

Variables Required

local width = 800
local height = 800

local lemtime = 2
local elapsed = lemtime
local maxlem = 10
local lemcount = 0

local startx = 100
local starty = 100

Lemmings Class & List of Lemmings

Lems = {}

function Lem(x,y)
	local self = {
		direction = 1,
		climbheight = 4,
		width = 10,
		height = 20,
		xpos = x,
		ypos=y
	}
	
	function self.update(dt)
		bl = self.checkground(self.xpos,self.ypos + self.height)
		br = self.checkground(self.xpos + self.width,self.ypos + self.height)
		if not bl and not br then
			self.ypos=self.ypos+1
		else
			local testheight = 0
			local found = false
			while not found and testheight <= self.climbheight do
				if self.direction == 1 then
					frontx = self.xpos + self.width
					fronty =  self.ypos + (self.height) - testheight
				else
					frontx = self.xpos - 1
					fronty =  self.ypos + (self.height) - testheight
				end	
				if not self.checkground(frontx, fronty) then
					self.xpos = self.xpos+self.direction
					self.ypos = self.ypos - testheight
					found=true
				end

				testheight = testheight + 1
			end
				
			if not found then
				self.direction = self.direction * -1
			end
		end
	end
	
	function self.checkground(x,y)
		if mapdata:getPixel(x,y) ~= bgcolor then
			return true
		else
			return false
		end	
	end
		
	return self	
end

Method to Create Lemming

function createlem()
	if lemcount<maxlem then
		Lems[lemcount] = Lem(startx,starty)
		lemcount = lemcount + 1
	end
end

love.load method

function love.load()
	love.window.setMode(width, height)
	mapdata = love.image.newImageData("level.png")
	map = love.graphics.newImage(mapdata)
	bgcolor = mapdata:getPixel(startx,starty)
	lem = love.graphics.newImage("lemming.png")
end

love.update method

function love.update(dt)
	elapsed = elapsed - dt
	
	if elapsed <= 0 then
		createlem()
		elapsed=lemtime
	end

	for _,test in pairs(Lems )do
		test.update(dt)
	end
end

love.draw method

function love.draw()
	love.graphics.draw(map, 0, 0)

	for _,test in pairs(Lems )do
		love.graphics.draw(lem,test.xpos, test.ypos)
	end
end