Code Example 1 Love.Load

This is the most important part of the program. Everything that happens in the game needs the variables created upon this section of the code. For those not familiar with a love program, the function love.load() is only ever called once. This allows for the creation of every variable needed. Also a lot of computation can occur during this function so that the actual game runs faster. I personally added a lot of code to this section so that the actual game functions faster. I’m not sure if I needed to create all these variables here but it helps me keep track of every variable I am using. The largest and most important part o this code is the creation of the map the game occurs, the creation of each individual enemies and finally the creation of the towers. When a later version of the game is complete all that information will come in from another section of the program. So its a lot to take in but I think the code below gives a valuable insight to how this game works. Also I would like to apologize for the format of the code, all my indention disappeared.

 

function love.load()
dif = 1 — amount of mountains
size = 32
–whx = love.graphics.getHeight()
whx = 600
wwy = 800
–wwy = love.graphics.getWidth()
wx = whx/ 16
wy = wwy/ 16

–set camera
camera:setBounds(0, 0, wwy, whx)

— creating tile array
tile = {}
tile16 = {}
tile32 = {}
if size >= 16 and size <= 64 then
for i =0, 14 do
tile[i] = love.graphics.newImage(i ..”tile”..size..”.png”)
end
for i = 0, 14 do
tile16[i] = love.graphics.newImage(i..”tile16.png”)
end
for i = 0, 14 do
tile32[i] = love.graphics.newImage(i..”tile32.png”)
end
end

–standard map grid
map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}

— creating standard random map
for i = 1, (wx + 1) do
map[i] = {}
for l = 1, wy do
int = math.random(0, 10)
if i == 1 then
map[i][l] = 1
elseif wx <= i  then
map[i][l] = 4
elseif l == wy then
map[i][l] = 1
elseif l == 1 then
map[i][l] = 4
elseif int >= dif then
map[i][l] = 0
else
map[i][l] = 1
end

end
end
–end standard random map

–create image map
map1 = {}
for i = 1, (wx + 1) do
map1[i] = {}
for l = 1, wy do
map1[i][l] = map[i][l]
end
end
–end second array map

ilz = 0 –RV location

–begin speical map
— 5 is dirt
— 6 is light water
–second array special colors
for i = 1, (wx +1) do
for l = 1, wy do
int = math.random(0, 7)
— draw dirt
if i ~= 1 then
if map1[i][l] == 1 and l ~= wy then    –make dirt
if map1[i][l + 1] ~= 1 then
map1[i][l + 1] = 5
end

if map1[i][l – 1] ~= 1 and (l-1) > 3 then
map1[i][l – 1] = 5
end
end
if map1[i][l] == 1 and l  ~= wy then
if i <= (wx – 1) and map1[i + 1][l] ~= 1 then
map1[i + 1][l] = 5
end
if i > 2 and map1[i – 1][l] ~= 1 then
map1[i – 1][l] = 5
end
end
end
–draw light water
if map1[i][l] == 4 then
if l == 1 and i <= wx then
if map1[i][l + 1] == 1 then
map1[i][l + 1] = 6
map[i][l + 1] = 0
else
map1[i][l + 1] = 6
end
elseif i > wx  and l ~= 1 then
if map1[i – 1][l] == 1 then
map1[i – 1][l] = 1
else
map1[i – 1][l] = 6
end
end

if int >= 2 and l == 1 then
if map1[i][l + 2] == 1 then
map1[i][l + 2] = 6
map[i][l + 2] = 0
else
map1[i][l + 2] = 6
end
elseif int >= 3 and i >= wx then
if map1[i – 2][l] == 1 then
map1[i – 2][l] = 1
else
map1[i – 2][l] = 6
end
end
end
end
end

–end speical map

–player varibles and enemy
zxp = 0    –player x postion
zyp = 0    –player y postion

exp = 0 — enemy x postion
eyp = 0 — enemy y postion

— add the player and hole
map[13][14] = 2

map1[16][wy – 1] = 8 — test hole
map1[17][wy – 1] = 8 — test hole
map[16][wy – 1] = 0 — test hole
map[17][wy – 1] = 0 — test hol
ilz = 16

number = 0
–tower array thing
tArr = {}
towerInt = 6
toff = 0
for i = 1, towerInt do
tArr[i] = {}
— differnt aspects of a tower
— 1 tile image number
— 2 attack
— 3 range of attack
— 4 speed of attack
— x and y are 5 and 6
— 7,8 will be chaning x and y stuff
— 9 will be if moving
— 10 will be if attacking
— 11 and 12 will be spots on menu
— 13 will be individual counter for time
— 14 will be type of attack
— if 14 is 1 then all around attack
— if 14 is 2 then single attack
— 15 will be amount of attacks or type of attacks
— 16 will be the amount of resources required
— 17 will be the thing benth its placement
— 18 will be the health of destroy able
— 19, 20 will be there
for l = 1, 20 do
tArr[i][l] = 0
if l == 4 then
tArr[i][l] = 35
elseif l == 5 or l == 11 then
–standard is 880
tArr[i][l] = 820
elseif l == 6 or l == 12 then
tArr[i][l] = 60 + toff
elseif l == 9 then
tArr[i][l] = false
elseif l == 10 then
tArr[i][l] = false
elseif l == 16 then
tArr[i][l] = 40
elseif l == 17 then
–having issue with this placement
tArr[i][l] = 100
elseif l == 18 then
tArr[i][l] = -1
end
end
toff = toff + 40
end
tArr[1][1] = 7    –yellow tower
tArr[1][2] = 15    –attack power
tArr[1][3] = 1    –range of attack
tArr[1][14] = 1  –attack all around
–tArr[1][15] = 1  –how long it
–tArr[1][4] = 35

tArr[2][1] = 9    –purple tower
tArr[2][2] = 5    –attack power
tArr[2][3] = 3    –range of attack
tArr[2][4] = 25    –speed of attakc
tArr[2][14] = 2    –can only attack once
tArr[2][16] = 35 –R amout balnce it

tArr[3][1] = 12 — new barrier
tArr[3][2] = 0
tArr[3][3] = 0
tArr[3][16] = 100 –R amount
tArr[3][18] = 100 –health of the tower

tArr[4][1] = 10 –trap hole
tArr[4][2] = 100
tArr[4][3] = 0
tArr[4][14] = 1
tArr[4][16] = 75 — amount of resourses balnce
tArr[4][18] = 100 — health of tower goes down

tArr[5][1] = 11
tArr[5][2] = 100
tArr[5][3] = 10
tArr[5][14] = 1
tArr[5][15] = 1     –dies in one turn
tArr[5][16] = 250 –amount of resoures need mess with

tArr[6][1] = 14 –this will be destroy stuff
tArr[6][2] = 0
tArr[6][3] = 0
tArr[6][15] = 1
tArr[6][16] = 0

barAr = {} –this will be the array of barriers

–end tower array

inPAr = {}
for    z = 1, 35 do
inPAr[z] = false
end

WeCost = {
{500, 200, 0},
{250, 300, 0},
{50, 200, 0},
{10, 150, 0}
}

–player array of info
— add arrays for mutiple eneimes
player = {
100, –health of player
{“chain saw”, “baseball bat”, “stick”}, –names of wepons
{50, 20, 5}, –close range attack
{1, 1, 2}, — close range ranges
{“sniper rifle”,”shotgun”, “pistole”}, –names of long rane
{200, 50, 5}, — long range attack
{6, 3, 4},    –range of long attacks
3, –equip axe
3    –equip gun
}

–inital enemy creation
— cant go over 35 something else wrong
place = false
enNum = 35 — create how many enemies
for i = 1, enNum do
place = false
while place == false do
int = math.random(1, 35)
–int = 34
if inPAr[int] == false then
map[int + 1][3] = 3
inPAr[int] = true
place = true
end
end
end

en = {}
for i = 1, enNum  do
en[i] = {}
for l = 1, 11 do
en[i][l] = 0
if l == 3 then
en[i][l] = 100
elseif l == 5 then
en[i][l] = “r”
elseif l == 6 then
en[i][l] = math.random(25, 35) –mess with
elseif l ==7 then
–play with this
targ = math.random(0, 10)
if targ <= 8 then
en[i][l] = 8
else
en[i][l] = 2
end
elseif l == 9 then
en[i][l] = {}
for w =1, 2 do
en[i][l][w] = 0
end
elseif l == 10 then
en[i][l] = false
elseif l == 11 then
–en[i][l] = math.random(5, 10)–maybe too strong
if en[i][7] == 2 then
en[i][l] = math.random(4, 8)
else
en[i][l] = math.random(1, 5)
end

end
–one is x cord
–two is y cord
–three is health
— four is Not in right dir
— five is dir
— six is speed
— seven should be the target of the enemy
— 8 will be the timer for each
— 9 will be array cords of the goal
— 9 1 is the x distance
— 9 2 is the y distance
–10 will be if met goal bol
–11 will be how much their attack is
end
end

–enemy index varible
enInV = 1

for i = 1, #map do
for l = 1, #map[i] do
if map[i][l] == 2 then
zxp = l
zyp = i
end
if map[i][l] == 3 then
exp = l
eyp = i
— add enemys postions to array
en[enInV][1] = eyp
en[enInV][2] = exp
if enInV >= 35 then
enInV = 1
else
enInV = enInV + 1
end

end
end
end

— end enemy creation

— enable held keys
love.keyboard.setKeyRepeat(10, 200)

xs = 0    –map creation
ys = 0    –map creation
–es = 1    — enemy index

temp2 = 0 — storing temp numbs

Ndir = 0 — not direction enemy counter

counter = 0 — counter for color placements

time = 0 — enemy time
time2 = 0 — tower time

Resources = 2000
zoom = false
pause = true — start true
FirstPause = true
space = false
MousePressed = false    –if mouse is pressed
shortA = false    — if short attack is true
longA = false    — if long attack is true
mousEE = false — if the mouse is on an enemy
InMenu = false    — in the menu
InYellow = false     — in the tower
InPurple = false     — in the second tower
towerAttack = false    — same as bellow
towerA = 0    — if towers are attacking
tAmount = 0 –Keep track of the amount of towers
RvHealth = 1000 — rv health and stuff
RvAttack = false –if ens attacking lower rv health
FastF = false — fast forwarding enemys and towers
fastS = 35 — enemy speed and such
enCounter = 0
NewGame = false –if new game needed
EquipM = false –equip menu pause this
TowerM = false

text = 0
–spacePr = false

oldTower = 100

–love.graphics.newFont(20)

— u1, r1, l1, d1
m1 = 100
u1 = 100
r1 = 100
l1 = 100
d1 = 100
lowest12 = 100
small = false –need to keep
singleAt = false

–temp10 = 100

ofsetX = 0 — ofset for mouse x
ofsetY = 0 — ofset for mouse y

CMI = 100 — Current Mouse map index
CX = 100
CY = 100

Mx = 0
My = 0

–do first path finding
for w = 1, enNum do
PathFinding(w)
end
end