OK,
So being new to lua and Corona and put together this game from various tutorials. I wanted to release this for Halloween. But ran into some issues. So here is the code. The main card game is from the mobile tuts tutorial on a memory game, and the blood splatter effects are from the Fruit Samurai example. Took me about a week, which is pretty good considering how new I am. The best way to learn is just to take examples and add to them and see what works.
Also, please comment if you find a better way to do things, this may be the ugliest coding you’ve ever seen.
[lua]module(…, package.seeall)
require (“physics”)
require(“ui”)
require (“director”)
– Zombie Match
function new()
local localGroup = display.newGroup()
physics.start()
– physics.setDrawMode ( “hybrid” ) – Uncomment in order to show in hybrid mode
physics.setGravity( 0, 9.8 * 2)
–count to see if game is done
local isDone = 0
–Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;
–Hide status bar
display.setStatusBar(display.HiddenStatusBar);
–Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0
–Declare variable to track button select
local secondSelect = 0
local checkForMatch = false
–Declare button, buttonCover, and buttonImages table
local button = {}
local buttonCover = {}
local buttonImages = {1,1, 2,2, 3,3, 4,4, 5,5, 6,6}
–Declare and prime a last button selected variable
local lastButton = display.newImage(“1.png”);
lastButton.myName = 1;
localGroup:insert(lastButton)
–Notify player if match is found or not
local matchText = display.newText("", 0, 0, native.systemFont, 26)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(0, 0, 0)
matchText.x = _W/2
matchText.y = _H/2
–Set starting point for button grid
x = -20
–insert background
local bg = display.newImage(“bg.png”)
localGroup:insert(bg)
– Audio for exploded zombies
local blood = audio.loadSound(“blood.wav”)
–audio loading
local cardFlip= audio.loadSound(“zombieBite.wav”)
local zombieMoan = audio.loadSound(“zombieMoan.wav”) --endgame
– Gush filter should not interact with other stuff
local gushProp = {density = 1.0, friction = 0.3, bounce = 0.2, filter = {categoryBits = 4, maskBits = 8} }
– Splash properties
local splashFadeTime = 2500
local splashFadeDelayTime = 5000
local splashInitAlpha = .5
local splashSlideDistance = 50 – The amoutn of of distance the splash slides down the background
– Contains all the available splash images
local splashImgs = {}
– Gush properties
local minGushRadius = 10
local maxGushRadius = 25
local numOfGushParticles = 15
local gushFadeTime = 500
local gushFadeDelay = 500
local minGushVelocityX = -350
local maxGushVelocityX = 350
local minGushVelocityY = -350
local maxGushVelocityY = 350
– Groups for holding the fruit and splash objects
local splashGroup
local fruitGroup
splashGroup = display.newGroup()
fruitGroup = display.newGroup()
– Initialize splash images
table.insert(splashImgs, “splash1.png”)
table.insert(splashImgs, “splash2.png”)
table.insert(splashImgs, “splash3.png”)
localGroup:insert(splashGroup)
localGroup:insert(fruitGroup)
– The game function
–Set up game function
function game(object, event)
if(event.phase == “began”) then
if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true
audio.play(cardFlip)
lastButton:removeEventListener( “touch”, lastButton )
elseif(checkForMatch == true) then
if(secondSelect == 0) then
–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
audio.play(cardFlip)
–If buttons do not match, flip buttons over
if(lastButton.myName ~= object.myName) then
lastButton:addEventListener( “touch”, lastButton )
–matchText.text = “Match Not Found!”;
timer.performWithDelay(200, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;
end, 1)
–If buttons DO match, remove button
elseif(lastButton.myName == object.myName) then
lastButton:addEventListener( “touch”, lastButton )
–matchText.text = “Match Found!”;
isDone = isDone+1
timer.performWithDelay(200, function()
matchText.text = “”;
checkForMatch = false;
secondSelect = 0;
createSplash(lastButton)
audio.play(blood)
createGush(lastButton)
createSplash(object)
createGush(object)
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();
end, 1)
–if done then display congrats
if(isDone == 6) then
timer.performWithDelay(1000,function()
matchText.text = “You’ve killed them all!”
localGroup:insert(matchText)
end,1)
end
end
end
end
end
end
–Place buttons on screen
for count = 1,3 do
x = x + 90
y = 20
for insideCount = 1,4 do
y = y + 90
–Assign each image a random location on grid
temp = math.random(1,#buttonImages)
–print(temp);
button[count] = display.newImage(buttonImages[temp] … “.png”);
–Position the button
button[count].x = x;
button[count].y = y;
–Give each a button a name
button[count].myName = buttonImages[temp]
localGroup:insert(button[count])
button[count].number = totalButtons
–Remove button from buttonImages table
table.remove(buttonImages, temp)
–Set a cover to hide the button image
buttonCover[totalButtons] = display.newImage(“blankCard.png”);
localGroup:insert(buttonCover[totalButtons])
buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;
totalButtons = totalButtons + 1
–Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( “touch”, button[count] )
end
end
– blood splatter effects
–example from fruit samurai
function getRandomSplash()
return display.newImage(splashImgs[math.random(1, #splashImgs)])
end
– Creates a gushing effect that makes it look like juice is flying out of the fruit
function createGush(fruit)
local i
for i = 0, numOfGushParticles do
local gush = display.newCircle( fruit.x, fruit.y, math.random(minGushRadius, maxGushRadius) )
gush:setFillColor(255, 0, 0, 255)
gushProp.radius = gush.width / 2
physics.addBody(gush, “dynamic”, gushProp)
local xVelocity = math.random(minGushVelocityX, maxGushVelocityX)
local yVelocity = math.random(minGushVelocityY, maxGushVelocityY)
gush:setLinearVelocity(xVelocity, yVelocity)
transition.to(gush, {time = gushFadeTime, delay = gushFadeDelay, width = 0, height = 0, alpha = 0, onComplete = function(event) gush:removeSelf() end})
end
end
function createSplash(fruit)
local splash = getRandomSplash()
splash.x = fruit.x
splash.y = fruit.y
splash.rotation = math.random(-90,90)
splash.alpha = splashInitAlpha
splashGroup:insert(splash)
transition.to(splash, {time = splashFadeTime, alpha = 0, y = splash.y + splashSlideDistance, delay = splashFadeDelayTime, onComplete = function(event) splash:removeSelf() end})
end
– title image
local title = display.newImage(“titleSmall2.png”)
title.x = _W/2
title.y = 40
localGroup:insert(title)
–backbutton
local backButton = display.newImage(“back.png”)
backButton.x = 260
backButton.y = 460
localGroup:insert(backButton)
–back button function
local function pressBack(event)
if event.phase == “ended” then
director:changeScene (“menu”,“fade”)
end
end
backButton:addEventListener (“touch”, pressBack)
return localGroup;
end[/lua] [import]uid: 85045 topic_id: 15980 reply_id: 60614[/import]