{"id":108,"date":"2012-05-03T20:26:01","date_gmt":"2012-05-03T20:26:01","guid":{"rendered":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/?p=108"},"modified":"2012-05-03T20:27:17","modified_gmt":"2012-05-03T20:27:17","slug":"code-example-1-love-load","status":"publish","type":"post","link":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/?p=108","title":{"rendered":"Code Example 1 Love.Load"},"content":{"rendered":"<p>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&#8217;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.<\/p>\n<p>&nbsp;<\/p>\n<p>function love.load()<br \/>\ndif = 1 &#8212; amount of mountains<br \/>\nsize = 32<br \/>\n&#8211;whx = love.graphics.getHeight()<br \/>\nwhx = 600<br \/>\nwwy = 800<br \/>\n&#8211;wwy = love.graphics.getWidth()<br \/>\nwx = whx\/ 16<br \/>\nwy = wwy\/ 16<\/p>\n<p>&#8211;set camera<br \/>\ncamera:setBounds(0, 0, wwy, whx)<\/p>\n<p>&#8212; creating tile array<br \/>\ntile = {}<br \/>\ntile16 = {}<br \/>\ntile32 = {}<br \/>\nif size &gt;= 16 and size &lt;= 64 then<br \/>\nfor i =0, 14 do<br \/>\ntile[i] = love.graphics.newImage(i ..&#8221;tile&#8221;..size..&#8221;.png&#8221;)<br \/>\nend<br \/>\nfor i = 0, 14 do<br \/>\ntile16[i] = love.graphics.newImage(i..&#8221;tile16.png&#8221;)<br \/>\nend<br \/>\nfor i = 0, 14 do<br \/>\ntile32[i] = love.graphics.newImage(i..&#8221;tile32.png&#8221;)<br \/>\nend<br \/>\nend<\/p>\n<p>&#8211;standard map grid<br \/>\nmap = {<br \/>\n{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },<br \/>\n{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },<br \/>\n{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },<br \/>\n{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },<br \/>\n{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },<br \/>\n{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }<br \/>\n}<\/p>\n<p>&#8212; creating standard random map<br \/>\nfor i = 1, (wx + 1) do<br \/>\nmap[i] = {}<br \/>\nfor l = 1, wy do<br \/>\nint = math.random(0, 10)<br \/>\nif i == 1 then<br \/>\nmap[i][l] = 1<br \/>\nelseif wx &lt;= i\u00a0 then<br \/>\nmap[i][l] = 4<br \/>\nelseif l == wy then<br \/>\nmap[i][l] = 1<br \/>\nelseif l == 1 then<br \/>\nmap[i][l] = 4<br \/>\nelseif int &gt;= dif then<br \/>\nmap[i][l] = 0<br \/>\nelse<br \/>\nmap[i][l] = 1<br \/>\nend<\/p>\n<p>end<br \/>\nend<br \/>\n&#8211;end standard random map<\/p>\n<p>&#8211;create image map<br \/>\nmap1 = {}<br \/>\nfor i = 1, (wx + 1) do<br \/>\nmap1[i] = {}<br \/>\nfor l = 1, wy do<br \/>\nmap1[i][l] = map[i][l]<br \/>\nend<br \/>\nend<br \/>\n&#8211;end second array map<\/p>\n<p>ilz = 0 &#8211;RV location<\/p>\n<p>&#8211;begin speical map<br \/>\n&#8212; 5 is dirt<br \/>\n&#8212; 6 is light water<br \/>\n&#8211;second array special colors<br \/>\nfor i = 1, (wx +1) do<br \/>\nfor l = 1, wy do<br \/>\nint = math.random(0, 7)<br \/>\n&#8212; draw dirt<br \/>\nif i ~= 1 then<br \/>\nif map1[i][l] == 1 and l ~= wy then\u00a0\u00a0 \u00a0&#8211;make dirt<br \/>\nif map1[i][l + 1] ~= 1 then<br \/>\nmap1[i][l + 1] = 5<br \/>\nend<\/p>\n<p>if map1[i][l &#8211; 1] ~= 1 and (l-1) &gt; 3 then<br \/>\nmap1[i][l &#8211; 1] = 5<br \/>\nend<br \/>\nend<br \/>\nif map1[i][l] == 1 and l\u00a0 ~= wy then<br \/>\nif i &lt;= (wx &#8211; 1) and map1[i + 1][l] ~= 1 then<br \/>\nmap1[i + 1][l] = 5<br \/>\nend<br \/>\nif i &gt; 2 and map1[i &#8211; 1][l] ~= 1 then<br \/>\nmap1[i &#8211; 1][l] = 5<br \/>\nend<br \/>\nend<br \/>\nend<br \/>\n&#8211;draw light water<br \/>\nif map1[i][l] == 4 then<br \/>\nif l == 1 and i &lt;= wx then<br \/>\nif map1[i][l + 1] == 1 then<br \/>\nmap1[i][l + 1] = 6<br \/>\nmap[i][l + 1] = 0<br \/>\nelse<br \/>\nmap1[i][l + 1] = 6<br \/>\nend<br \/>\nelseif i &gt; wx\u00a0 and l ~= 1 then<br \/>\nif map1[i &#8211; 1][l] == 1 then<br \/>\nmap1[i &#8211; 1][l] = 1<br \/>\nelse<br \/>\nmap1[i &#8211; 1][l] = 6<br \/>\nend<br \/>\nend<\/p>\n<p>if int &gt;= 2 and l == 1 then<br \/>\nif map1[i][l + 2] == 1 then<br \/>\nmap1[i][l + 2] = 6<br \/>\nmap[i][l + 2] = 0<br \/>\nelse<br \/>\nmap1[i][l + 2] = 6<br \/>\nend<br \/>\nelseif int &gt;= 3 and i &gt;= wx then<br \/>\nif map1[i &#8211; 2][l] == 1 then<br \/>\nmap1[i &#8211; 2][l] = 1<br \/>\nelse<br \/>\nmap1[i &#8211; 2][l] = 6<br \/>\nend<br \/>\nend<br \/>\nend<br \/>\nend<br \/>\nend<\/p>\n<p>&#8211;end speical map<\/p>\n<p>&#8211;player varibles and enemy<br \/>\nzxp = 0\u00a0\u00a0 \u00a0&#8211;player x postion<br \/>\nzyp = 0\u00a0\u00a0 \u00a0&#8211;player y postion<\/p>\n<p>exp = 0 &#8212; enemy x postion<br \/>\neyp = 0 &#8212; enemy y postion<\/p>\n<p>&#8212; add the player and hole<br \/>\nmap[13][14] = 2<\/p>\n<p>map1[16][wy &#8211; 1] = 8 &#8212; test hole<br \/>\nmap1[17][wy &#8211; 1] = 8 &#8212; test hole<br \/>\nmap[16][wy &#8211; 1] = 0 &#8212; test hole<br \/>\nmap[17][wy &#8211; 1] = 0 &#8212; test hol<br \/>\nilz = 16<\/p>\n<p>number = 0<br \/>\n&#8211;tower array thing<br \/>\ntArr = {}<br \/>\ntowerInt = 6<br \/>\ntoff = 0<br \/>\nfor i = 1, towerInt do<br \/>\ntArr[i] = {}<br \/>\n&#8212; differnt aspects of a tower<br \/>\n&#8212; 1 tile image number<br \/>\n&#8212; 2 attack<br \/>\n&#8212; 3 range of attack<br \/>\n&#8212; 4 speed of attack<br \/>\n&#8212; x and y are 5 and 6<br \/>\n&#8212; 7,8 will be chaning x and y stuff<br \/>\n&#8212; 9 will be if moving<br \/>\n&#8212; 10 will be if attacking<br \/>\n&#8212; 11 and 12 will be spots on menu<br \/>\n&#8212; 13 will be individual counter for time<br \/>\n&#8212; 14 will be type of attack<br \/>\n&#8212; if 14 is 1 then all around attack<br \/>\n&#8212; if 14 is 2 then single attack<br \/>\n&#8212; 15 will be amount of attacks or type of attacks<br \/>\n&#8212; 16 will be the amount of resources required<br \/>\n&#8212; 17 will be the thing benth its placement<br \/>\n&#8212; 18 will be the health of destroy able<br \/>\n&#8212; 19, 20 will be there<br \/>\nfor l = 1, 20 do<br \/>\ntArr[i][l] = 0<br \/>\nif l == 4 then<br \/>\ntArr[i][l] = 35<br \/>\nelseif l == 5 or l == 11 then<br \/>\n&#8211;standard is 880<br \/>\ntArr[i][l] = 820<br \/>\nelseif l == 6 or l == 12 then<br \/>\ntArr[i][l] = 60 + toff<br \/>\nelseif l == 9 then<br \/>\ntArr[i][l] = false<br \/>\nelseif l == 10 then<br \/>\ntArr[i][l] = false<br \/>\nelseif l == 16 then<br \/>\ntArr[i][l] = 40<br \/>\nelseif l == 17 then<br \/>\n&#8211;having issue with this placement<br \/>\ntArr[i][l] = 100<br \/>\nelseif l == 18 then<br \/>\ntArr[i][l] = -1<br \/>\nend<br \/>\nend<br \/>\ntoff = toff + 40<br \/>\nend<br \/>\ntArr[1][1] = 7\u00a0\u00a0 \u00a0&#8211;yellow tower<br \/>\ntArr[1][2] = 15\u00a0\u00a0 \u00a0&#8211;attack power<br \/>\ntArr[1][3] = 1\u00a0\u00a0 \u00a0&#8211;range of attack<br \/>\ntArr[1][14] = 1\u00a0 &#8211;attack all around<br \/>\n&#8211;tArr[1][15] = 1\u00a0 &#8211;how long it<br \/>\n&#8211;tArr[1][4] = 35<\/p>\n<p>tArr[2][1] = 9\u00a0\u00a0 \u00a0&#8211;purple tower<br \/>\ntArr[2][2] = 5\u00a0\u00a0 \u00a0&#8211;attack power<br \/>\ntArr[2][3] = 3\u00a0\u00a0 \u00a0&#8211;range of attack<br \/>\ntArr[2][4] = 25\u00a0\u00a0 \u00a0&#8211;speed of attakc<br \/>\ntArr[2][14] = 2\u00a0\u00a0 \u00a0&#8211;can only attack once<br \/>\ntArr[2][16] = 35 &#8211;R amout balnce it<\/p>\n<p>tArr[3][1] = 12 &#8212; new barrier<br \/>\ntArr[3][2] = 0<br \/>\ntArr[3][3] = 0<br \/>\ntArr[3][16] = 100 &#8211;R amount<br \/>\ntArr[3][18] = 100 &#8211;health of the tower<\/p>\n<p>tArr[4][1] = 10 &#8211;trap hole<br \/>\ntArr[4][2] = 100<br \/>\ntArr[4][3] = 0<br \/>\ntArr[4][14] = 1<br \/>\ntArr[4][16] = 75 &#8212; amount of resourses balnce<br \/>\ntArr[4][18] = 100 &#8212; health of tower goes down<\/p>\n<p>tArr[5][1] = 11<br \/>\ntArr[5][2] = 100<br \/>\ntArr[5][3] = 10<br \/>\ntArr[5][14] = 1<br \/>\ntArr[5][15] = 1 \u00a0\u00a0 \u00a0&#8211;dies in one turn<br \/>\ntArr[5][16] = 250 &#8211;amount of resoures need mess with<\/p>\n<p>tArr[6][1] = 14 &#8211;this will be destroy stuff<br \/>\ntArr[6][2] = 0<br \/>\ntArr[6][3] = 0<br \/>\ntArr[6][15] = 1<br \/>\ntArr[6][16] = 0<\/p>\n<p>barAr = {} &#8211;this will be the array of barriers<\/p>\n<p>&#8211;end tower array<\/p>\n<p>inPAr = {}<br \/>\nfor\u00a0\u00a0 \u00a0z = 1, 35 do<br \/>\ninPAr[z] = false<br \/>\nend<\/p>\n<p>WeCost = {<br \/>\n{500, 200, 0},<br \/>\n{250, 300, 0},<br \/>\n{50, 200, 0},<br \/>\n{10, 150, 0}<br \/>\n}<\/p>\n<p>&#8211;player array of info<br \/>\n&#8212; add arrays for mutiple eneimes<br \/>\nplayer = {<br \/>\n100, &#8211;health of player<br \/>\n{&#8220;chain saw&#8221;, &#8220;baseball bat&#8221;, &#8220;stick&#8221;}, &#8211;names of wepons<br \/>\n{50, 20, 5}, &#8211;close range attack<br \/>\n{1, 1, 2}, &#8212; close range ranges<br \/>\n{&#8220;sniper rifle&#8221;,&#8221;shotgun&#8221;, &#8220;pistole&#8221;}, &#8211;names of long rane<br \/>\n{200, 50, 5}, &#8212; long range attack<br \/>\n{6, 3, 4},\u00a0\u00a0 \u00a0&#8211;range of long attacks<br \/>\n3, &#8211;equip axe<br \/>\n3\u00a0\u00a0 \u00a0&#8211;equip gun<br \/>\n}<\/p>\n<p>&#8211;inital enemy creation<br \/>\n&#8212; cant go over 35 something else wrong<br \/>\nplace = false<br \/>\nenNum = 35 &#8212; create how many enemies<br \/>\nfor i = 1, enNum do<br \/>\nplace = false<br \/>\nwhile place == false do<br \/>\nint = math.random(1, 35)<br \/>\n&#8211;int = 34<br \/>\nif inPAr[int] == false then<br \/>\nmap[int + 1][3] = 3<br \/>\ninPAr[int] = true<br \/>\nplace = true<br \/>\nend<br \/>\nend<br \/>\nend<\/p>\n<p>en = {}<br \/>\nfor i = 1, enNum\u00a0 do<br \/>\nen[i] = {}<br \/>\nfor l = 1, 11 do<br \/>\nen[i][l] = 0<br \/>\nif l == 3 then<br \/>\nen[i][l] = 100<br \/>\nelseif l == 5 then<br \/>\nen[i][l] = &#8220;r&#8221;<br \/>\nelseif l == 6 then<br \/>\nen[i][l] = math.random(25, 35) &#8211;mess with<br \/>\nelseif l ==7 then<br \/>\n&#8211;play with this<br \/>\ntarg = math.random(0, 10)<br \/>\nif targ &lt;= 8 then<br \/>\nen[i][l] = 8<br \/>\nelse<br \/>\nen[i][l] = 2<br \/>\nend<br \/>\nelseif l == 9 then<br \/>\nen[i][l] = {}<br \/>\nfor w =1, 2 do<br \/>\nen[i][l][w] = 0<br \/>\nend<br \/>\nelseif l == 10 then<br \/>\nen[i][l] = false<br \/>\nelseif l == 11 then<br \/>\n&#8211;en[i][l] = math.random(5, 10)&#8211;maybe too strong<br \/>\nif en[i][7] == 2 then<br \/>\nen[i][l] = math.random(4, 8)<br \/>\nelse<br \/>\nen[i][l] = math.random(1, 5)<br \/>\nend<\/p>\n<p>end<br \/>\n&#8211;one is x cord<br \/>\n&#8211;two is y cord<br \/>\n&#8211;three is health<br \/>\n&#8212; four is Not in right dir<br \/>\n&#8212; five is dir<br \/>\n&#8212; six is speed<br \/>\n&#8212; seven should be the target of the enemy<br \/>\n&#8212; 8 will be the timer for each<br \/>\n&#8212; 9 will be array cords of the goal<br \/>\n&#8212; 9 1 is the x distance<br \/>\n&#8212; 9 2 is the y distance<br \/>\n&#8211;10 will be if met goal bol<br \/>\n&#8211;11 will be how much their attack is<br \/>\nend<br \/>\nend<\/p>\n<p>&#8211;enemy index varible<br \/>\nenInV = 1<\/p>\n<p>for i = 1, #map do<br \/>\nfor l = 1, #map[i] do<br \/>\nif map[i][l] == 2 then<br \/>\nzxp = l<br \/>\nzyp = i<br \/>\nend<br \/>\nif map[i][l] == 3 then<br \/>\nexp = l<br \/>\neyp = i<br \/>\n&#8212; add enemys postions to array<br \/>\nen[enInV][1] = eyp<br \/>\nen[enInV][2] = exp<br \/>\nif enInV &gt;= 35 then<br \/>\nenInV = 1<br \/>\nelse<br \/>\nenInV = enInV + 1<br \/>\nend<\/p>\n<p>end<br \/>\nend<br \/>\nend<\/p>\n<p>&#8212; end enemy creation<\/p>\n<p>&#8212; enable held keys<br \/>\nlove.keyboard.setKeyRepeat(10, 200)<\/p>\n<p>xs = 0\u00a0\u00a0 \u00a0&#8211;map creation<br \/>\nys = 0\u00a0\u00a0 \u00a0&#8211;map creation<br \/>\n&#8211;es = 1\u00a0\u00a0 \u00a0&#8212; enemy index<\/p>\n<p>temp2 = 0 &#8212; storing temp numbs<\/p>\n<p>Ndir = 0 &#8212; not direction enemy counter<\/p>\n<p>counter = 0 &#8212; counter for color placements<\/p>\n<p>time = 0 &#8212; enemy time<br \/>\ntime2 = 0 &#8212; tower time<\/p>\n<p>Resources = 2000<br \/>\nzoom = false<br \/>\npause = true &#8212; start true<br \/>\nFirstPause = true<br \/>\nspace = false<br \/>\nMousePressed = false\u00a0\u00a0 \u00a0&#8211;if mouse is pressed<br \/>\nshortA = false\u00a0\u00a0 \u00a0&#8212; if short attack is true<br \/>\nlongA = false\u00a0\u00a0 \u00a0&#8212; if long attack is true<br \/>\nmousEE = false &#8212; if the mouse is on an enemy<br \/>\nInMenu = false\u00a0\u00a0 \u00a0&#8212; in the menu<br \/>\nInYellow = false \u00a0\u00a0 \u00a0&#8212; in the tower<br \/>\nInPurple = false \u00a0\u00a0 \u00a0&#8212; in the second tower<br \/>\ntowerAttack = false\u00a0\u00a0 \u00a0&#8212; same as bellow<br \/>\ntowerA = 0\u00a0\u00a0 \u00a0&#8212; if towers are attacking<br \/>\ntAmount = 0 &#8211;Keep track of the amount of towers<br \/>\nRvHealth = 1000 &#8212; rv health and stuff<br \/>\nRvAttack = false &#8211;if ens attacking lower rv health<br \/>\nFastF = false &#8212; fast forwarding enemys and towers<br \/>\nfastS = 35 &#8212; enemy speed and such<br \/>\nenCounter = 0<br \/>\nNewGame = false &#8211;if new game needed<br \/>\nEquipM = false &#8211;equip menu pause this<br \/>\nTowerM = false<\/p>\n<p>text = 0<br \/>\n&#8211;spacePr = false<\/p>\n<p>oldTower = 100<\/p>\n<p>&#8211;love.graphics.newFont(20)<\/p>\n<p>&#8212; u1, r1, l1, d1<br \/>\nm1 = 100<br \/>\nu1 = 100<br \/>\nr1 = 100<br \/>\nl1 = 100<br \/>\nd1 = 100<br \/>\nlowest12 = 100<br \/>\nsmall = false &#8211;need to keep<br \/>\nsingleAt = false<\/p>\n<p>&#8211;temp10 = 100<\/p>\n<p>ofsetX = 0 &#8212; ofset for mouse x<br \/>\nofsetY = 0 &#8212; ofset for mouse y<\/p>\n<p>CMI = 100 &#8212; Current Mouse map index<br \/>\nCX = 100<br \/>\nCY = 100<\/p>\n<p>Mx = 0<br \/>\nMy = 0<\/p>\n<p>&#8211;do first path finding<br \/>\nfor w = 1, enNum do<br \/>\nPathFinding(w)<br \/>\nend<br \/>\nend<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/jordanguy.nmdprojects.net\/portfolio\/?p=108\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,9,3,18,16,17],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-final-project","category-just-talking","category-nmd-206","category-notes","category-research","category-tools"],"_links":{"self":[{"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":2,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":110,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions\/110"}],"wp:attachment":[{"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jordanguy.nmdprojects.net\/portfolio\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}