Difference between revisions of "Love - Lemmings Movement"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Lemmings Class & List of Lemmings)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
This tutorial will create the movement for an individual Lemming, it will fall to the ground and then move in one direction until it hits something it can't climb and then switch direction.
 
This tutorial will create the movement for an individual Lemming, it will fall to the ground and then move in one direction until it hits something it can't climb and then switch direction.
 +
 +
I have extended this by creating different subclasses of lemming, ones to block, explode, tunnel, bridge and so on.
  
 
I used this image for the map:
 
I used this image for the map:
Line 12: Line 14:
 
[[File:Lemming.png]]
 
[[File:Lemming.png]]
  
I have extended this by creating different subclasses of lemming, ones to block, explode, tunnel, bridge and so on.
 
 
==Variables Required==
 
==Variables Required==
 
<syntaxhighlight lang=lua>
 
<syntaxhighlight lang=lua>
Line 18: Line 19:
 
local height = 800
 
local height = 800
  
local lemtime = 2
+
local lemtime = 2 -- time between lemmings
 
local elapsed = lemtime
 
local elapsed = lemtime
local maxlem = 10
+
local maxlem = 10 -- number of lemmings to create
local lemcount = 0
+
local lemcount = 0 -- used to count the number of lemmings created
  
local startx = 100
+
local startx = 100 -- drop position for new lemmings
 
local starty = 100
 
local starty = 100
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 29: Line 30:
 
==Lemmings Class & List of Lemmings==
 
==Lemmings Class & List of Lemmings==
 
<syntaxhighlight lang=lua>
 
<syntaxhighlight lang=lua>
Lems = {}
+
Lems = {} -- a table/list for the lemmings
  
function Lem(x,y)
+
function Lem(x,y) -- a class for a lemming
 
local self = {
 
local self = {
direction = 1,
+
direction = 1, -- 1 is right -1 is left
climbheight = 4,
+
climbheight = 4, -- the height from one pixel to another the lemming can climb
 
width = 10,
 
width = 10,
 
height = 20,
 
height = 20,
xpos = x,
+
xpos = x, -- current position
ypos=y
+
ypos = y
 
}
 
}
 
 
Line 44: Line 45:
 
bl = self.checkground(self.xpos,self.ypos + self.height)
 
bl = self.checkground(self.xpos,self.ypos + self.height)
 
br = self.checkground(self.xpos + self.width,self.ypos + self.height)
 
br = self.checkground(self.xpos + self.width,self.ypos + self.height)
if not bl and not br then
+
if not bl and not br then -- currently in the air
 
self.ypos=self.ypos+1
 
self.ypos=self.ypos+1
else
+
else -- currently on the ground
 
local testheight = 0
 
local testheight = 0
 
local found = false
 
local found = false
while not found and testheight <= self.climbheight do
+
while not found and testheight <= self.climbheight do -- this tests upto four pixels for ground
if self.direction == 1 then
+
if self.direction == 1 then -- if moving right check the pixel in front
 
frontx = self.xpos + self.width
 
frontx = self.xpos + self.width
 
fronty =  self.ypos + (self.height) - testheight
 
fronty =  self.ypos + (self.height) - testheight
else
+
else -- if moving left check the pixel behind
 
frontx = self.xpos - 1
 
frontx = self.xpos - 1
 
fronty =  self.ypos + (self.height) - testheight
 
fronty =  self.ypos + (self.height) - testheight
Line 66: Line 67:
 
end
 
end
 
 
if not found then
+
if not found then -- the obstacle can't be climbed
self.direction = self.direction * -1
+
self.direction = self.direction * -1 -- so change direction
 
end
 
end
 
end
 
end
 
end
 
end
 
 
function self.checkground(x,y)
+
function self.checkground(x,y) -- method used to check a pixel of the level
if mapdata:getPixel(x,y) ~= bgcolor then
+
if mapdata:getPixel(x,y) ~= bgcolor then -- bgcolor is set in the love.load method
 
return true
 
return true
 
else
 
else

Latest revision as of 11:07, 11 July 2019

This tutorial will create the movement for an individual Lemming, it will fall to the ground and then move in one direction until it hits something it can't climb and then switch direction.

I have extended this by creating different subclasses of lemming, ones to block, explode, tunnel, bridge and so on.

I used this image for the map:

Level.png

I also made this map for my extended version:

Levelnew.png

I used this image for a lemming: Lemming.png

Variables Required

local width = 800
local height = 800

local lemtime = 2 -- time between lemmings
local elapsed = lemtime
local maxlem = 10 -- number of lemmings to create
local lemcount = 0 -- used to count the number of lemmings created

local startx = 100 -- drop position for new lemmings
local starty = 100

Lemmings Class & List of Lemmings

Lems = {} -- a table/list for the lemmings

function Lem(x,y) -- a class for a lemming
	local self = {
		direction = 1, -- 1 is right -1 is left
		climbheight = 4, -- the height from one pixel to another the lemming can climb
		width = 10,
		height = 20,
		xpos = x, -- current position
		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 -- currently in the air
			self.ypos=self.ypos+1
		else -- currently on the ground
			local testheight = 0
			local found = false
			while not found and testheight <= self.climbheight do -- this tests upto four pixels for ground
				if self.direction == 1 then -- if moving right check the pixel in front
					frontx = self.xpos + self.width
					fronty =  self.ypos + (self.height) - testheight
				else -- if moving left check the pixel behind
					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 -- the obstacle can't be climbed
				self.direction = self.direction * -1 -- so change direction
			end
		end
	end
	
	function self.checkground(x,y) -- method used to check a pixel of the level
		if mapdata:getPixel(x,y) ~= bgcolor then -- bgcolor is set in the love.load method
			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