From 3259ffd3f970f362f1b3f3fb53fc41a244ed4363 Mon Sep 17 00:00:00 2001 From: Drew Dudash Date: Thu, 19 Apr 2012 19:28:27 -0500 Subject: [PATCH] Original Files --- README | 11 +++++ alive.bmp | Bin 0 -> 246 bytes dead.bmp | Bin 0 -> 246 bytes main.love | Bin 0 -> 1884 bytes main.lua | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 alive.bmp create mode 100644 dead.bmp create mode 100644 main.love create mode 100644 main.lua diff --git a/README b/README index e69de29..d2ac5b3 100644 --- a/README +++ b/README @@ -0,0 +1,11 @@ +Conway's Game of Life in Lua + LÖVE (Version "0.7.2 Game Slave" only, "Piggy" is not backwards compatable.) + +controls + space: pause/unpause + if paused + r: reload + q: quit + left click: make cell + right click: delete cell + + diff --git a/alive.bmp b/alive.bmp new file mode 100644 index 0000000000000000000000000000000000000000..5ef1971e87aadcaea437dcea77c0f1b8b78164b7 GIT binary patch literal 246 ecmZ?r{l)+RWa;eF1=AoPUT`P$X{Ld7Qx17md9fw+lt{{y5vWmbHSDtAq%QhG-6h z%o-~Y@%QejjQU#tre!e#BQFys>8t^TVBHtSw3~7UdE_-1NjAsf|NyFA`1?+Y7+x`6=Uy zoPU*P$mSn9WapebtE*hf8-B2=ej-e?0wGYwf*~-5*G6qjv=iqMX{QN|6r`=2?+l?OY zeCRa6Z(zEv#j-dURjny9uaUa2vb(SJLXQ$>*Ax9{MKKjZTkSVtiM8XINSU8)2Q*!q zX2T&yo0c=_#xsH!=VXsVal;yjv`8BNQz8w`Wa{HdUcyw4QiCPIL0)58$8U#Ol-kIM z9DKZ#NA5VA^Q@^ku1RoqqW~)nsW1JA-c4*CxI=JKPl65X1#e1ZJp#w{pDv+GWV?s# zvNh4Dv7ys7n=HtPOEB4D0qg1Lr^#>#R5y%;l~z@@_U0uc&Me>WqVeN!)ExiBT+p6v zbaiw*l(`3;;H;L@1Dgwa>%Zt;LD(IYZH9KYve7QXsjmuMNh0oG>GUb4H~(bITyXc` zCuYrgPNPsXC9vZ6>25;^l8;~K_jA-i)Q#E)yIaMrG1k~88XvAWKIh$l#zc0UbuY7t z_d}0WA_C59zfB~iKH$(Y+}ZN0nwRy29hK!HS{`1^3>kbr2!W@-O?@n#Rr6?t6p}?v zgvpF}k&2Z9QkRsr8EuEIk`LVVF7*6(sU`ku0<4lny(!&MKYtm*zUyv$z&h^8Y&S>~ zIyF#ZT0YNQuO086_@&CU&N_5LcI~I_Bz;NreYKwT3nwlV@{r8EJg=?h^UOD$SK*Z8 zJB4Nnxzq9@--&jC#Xj7c>7fMo^_c1GKhYy=?wAz6(&rg^AYOI&mp7larP}4n6s7G@ zlx0Iv7`sz*Hv(+=QlBz`N#CJdGQf9Ll1(lBZxRo5hhDEKFz)iFB!tFgELcMrZ3tqtnH%t`-|hqT{A zsJKyKLy(yLjkc*9Zn0gZ8HAW175tW|>Nsx3M!+l5U`%C^H1fE??C9h(m$OHg+BI6aH_5TTmV{_PSoWN@bn~ESRz#(o+G)%l?AKwf;Z2DJqN6}>m(Mb+f@xya;$KwI zbE9FCTY}ue1^Hk0(ZUCZMy&010U%$Ka!`jXw)dq12xi=Z+692W@BuDvxHt=y`5*o@ zVZ42C-v2~j72Jo0eH{SekpTWVq_|T65TNi?xaZHv`j+}T-~ausz!l{~-)!+M_IG~& Z!th+|Pxn|WFt-B$UhXaCYVm)%KLCxfQSkr( literal 0 HcmV?d00001 diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..cfd5e83 --- /dev/null +++ b/main.lua @@ -0,0 +1,134 @@ +--A Conway's Game of Life Implementation +--Drew Dudash + + +function love.load() --Intial Values + draw = {} --Sets the draw variable to a list + grid = {} --Sets the grid variable to a list + + game = "play" --can be "menu", "play", or "pause" + + alivecell_img = love.graphics.newImage("alive.bmp") --White Cell + deadcell_img = love.graphics.newImage("dead.bmp") --Black Cell + + fontWidth = 8 --The width of the font + fontHeight = 8 --The height of the font + + gridWidth = 90 --The length of a row of characters across the screen + gridHeight = 70 --The heigh of a column of characters across the screen + + do + local x + local y + for x = 1, gridWidth do --For each section of the first row + grid[x] = {} --Make a column down a list + for y = 1, gridHeight do --For each list + grid[x][y] = false --Make a dead cell character + end + end + end + + gridUp = grid + + function blankgrid() + local x + local y + local grid = {} + for x = 1, gridWidth do --For each section of the first row + grid[x] = {} --Make a column down a list + for y = 1, gridHeight do --For each list + grid[x][y] = false --Make a dead cell character + end + end + return grid + end + + function givetake(boolvalue) --Finds which cell the mouse is hovering over. + local mouse_x + local mouse_y + mouse_x = love.mouse.getX() + mouse_y = love.mouse.getY() + gridUp[((mouse_x - (mouse_x % fontWidth)) / fontWidth) + 1][((mouse_y - (mouse_y % fontHeight)) / fontHeight) + 1] = boolvalue + end +end + +function love.draw() --Draws the game + local i + local j + for i = 1,gridWidth do + for j = 1,gridHeight do + if grid[i][j] == false then + love.graphics.draw(deadcell_img,(i - 1) * fontWidth + 1,(j - 1) * fontHeight + 1) + else + love.graphics.draw(alivecell_img,(i - 1) * fontWidth + 1,(j - 1) * fontHeight + 1) + end + end + end + if game == "pause" then love.graphics.print("PAUSE",1,fontHeight * gridHeight) end +end + +function love.keypressed() --Handles keyboard input + if game == "pause" then --While the game is paused + if love.keyboard.isDown(" ") then --Unpauses life + game = "play" + end + if love.keyboard.isDown("r") then --Resets the board + grid = blankgrid() + end + if love.keyboard.isDown("q") then + love.event.push("q") + end + elseif game == "play" then --While the game is in play + if love.keyboard.isDown(" ") then --Pauses life + game = "pause" + end + end +end + +function love.mousepressed() --Handles mouse input + if game == "pause" then + if love.mouse.isDown("l") then --Left mouse breathes life into cells + givetake(true) + end + if love.mouse.isDown("r") then --Right mouse blots out cells + givetake(false) + end + end +end + +function love.update(dt) --Runs game logic + if game == "play" then --While game is in play + gridUp = blankgrid() --Resets the secondary grid to blank (A secondary grid exists so that all cells change simateanisaly. + local i + local j + --This takes care of updating squares based ont he adjacent ones, YEAH! + for i = 1,gridWidth do --Counts 'x' of the grid + for j = 1,gridHeight do --Count 'y' of the grid + local bordercount --Keeps count of adjacent 'true' squares + bordercount = 0 + local k + local v + for k = -1,1 do --Checks the grid point along with the ones to the left and right + if grid[i + k] ~= nil then --Stops if the first array is non-existant + for v = -1,1 do --Checks the grid points above and below the left right and center points + if grid[i + k][j + v] ~= nil then --Stops if the second array is non-existant + if k ~= 0 or v ~= 0 then --If the point is at grid[i][j] do nothing + if grid[i + k][j + v] == true then --If the point is true add to bordercount + bordercount = bordercount + 1 + end + end + end + end + end + end + --Conways Rules + if grid[i][j] == true and bordercount < 2 then gridUp[i][j] = false end --Any live cell with fewer than two live neighbours dies, as if caused by under-population. + if grid[i][j] == true and bordercount == 2 or bordercount == 3 then gridUp[i][j] = true end --Any live cell with two or three live neighbours lives on to the next generation. + if grid[i][j] == true and bordercount > 3 then gridUp[i][j] = false end --Any live cell with more than three live neighbours dies, as if by overcrowding. + if grid[i][j] == false and bordercount == 3 then gridUp[i][j] = true end --Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. + end + end + grid = gridUp --Sets the grid that is read for drawing to the new pattern + love.timer.sleep(80) --Delays the game so it runs slower + end +end \ No newline at end of file