I created a program but all I had was the game itself. So I browse through @Reefwing’s tutorials and find a guide for creating the splash screen. I created it and after it’s done it goes to the easy button menu. What kind of kind would I use to tell codea to start the game once I press the easy button. Right now it’s starting in the background because I hear the sounds (no visuals). Sorry if this is a noob question, but I’m really stuck on it. I will post my code if anyone asks to see it in order to help me. Thanks guys! This is a really great community
Hi @veeeralp - the secret is to use a state machine, have a look at Tutorial 5. In the action method for your button (the one that gets called when the button is tapped), change your gameState variable to the run program state. Then in your draw() method check what state you are in and draw the appropriate screen. The MineSweeper game in Tutorial 6 uses this exact technique. You can download the code for the Main class at https://www.dropbox.com/sh/dtvpqnsvltsqq7c/PnhlMtRnUI/Main.lua
Let me know if you have any further questions.
Ok, I’ll read through it later because I’m busy today. I’ll let you know if I have any questions. Thanks
I took you whole minesweeper game and took out the code and classes that I didn’t need. I noticed that for the button action for the easy, medium, or hard it was function() easyButtonPressed(). Following what you said would I change it gameState == stateRun. So the final code would be. easyButton.action = gameState == stateRun. I’m not too sure
Hi @veeralp,
You dont need to change that bit, the only bit you need to change is the draw() method.
What happens in MineSweeper is when you tap the easy button for example, easyButtonPressed() will be called and this will set gameState = stateRun.
Remember draw() gets called automatically roughly 60 times per second. So in draw() you need a block that looks like:
if gameState == stateSplash then
splashScreen: draw()
elseif gameState == stateMenu then
drawMenu()
elseif gameState == stateRun then
drawYourGameScreen()
elseif gameState == stateWon then
handleWinState()
elseif gameState == stateLost then
handleLostState
end
The gameState variable determines what gets drawn each frame. Does that make sense?
I followed what you said and I put drawMyGame() if gameSTate == stateRun. But there is a problem. It doesn’t draw my game when I press the easy button. Should I post my code for more help?
Heres the main code
--# Main
-- MineSweeper
-- Reefwing Software (reefwing.com.au)
--
-- Version 1.1
--
-- A reprisal of the classic MineSweeper game, written entirely on the iPad.
-- Game icons were built using Spritely, the pixel editor included with Codea.
--
-- This demonstration game was built as part of a series of Tutorials on Codea
-- and programming with Lua for the iPad. These tutorials may be found at
-- www.codeatuts.blogspot.com.au
--
-- To make import of code into Codea easier, each class is available as a separate file
-- in our dropbox repository. This will also make it easier for reuse of classes in
-- your own projects.
function setup()
version = 2.0
saveProjectInfo("Description", "Bouncy by Veeral Patel.")
saveProjectInfo("Author", "Veeral Patel")
saveProjectInfo("Date", "18th July 2012")
saveProjectInfo("Version", version)
print("Bouncy v"..version.."\
")
-- Define the game colour palette
whiteColour = color(255, 255, 255)
blackColour = color(0, 0, 0)
redColour = color(243, 157, 33, 255)
blueColour = color(0, 188, 255, 255)
greenColour = color(45, 226, 23, 255)
-- Keep an eye on your FPS
FPS = 0
watch("FPS")
timeInterval = 0
frameCount = 0
-- keep track of your Game State, Tap State, and Game Difficulty
-- using Finite State Machines.
--
-- gameState is the overall state of the game.
--
-- tapState is used when the game is running. It toggles the tap function between
-- revealing a cell and flagging it as a possible mine. The button text of flagButton
-- indicates the current state.
--
-- gameDifficulty allows us to setup a new game with the correct parameters when the
-- newGameButton is tapped.
stateSplash = 0
stateMenu = 1
stateRun = 2
stateGameOver = 3
gameState = stateSplash
tapState = stateReveal
stateEasy = 7
stateMedium = 8
stateHard = 9
gameDifficulty = stateEasy
-- initialise the game variables and grid matrix
score = 0
-- create the 3 menu buttons, they wont be visible until you draw them.
-- Note that 50 pixels is the minimum height for the default font size
local mButtonSize = vec2(180, 50)
local mLocX = WIDTH/2 - mButtonSize.x/2
local mLocY = HEIGHT/2 + 20
easyButton = Button("Easy", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
easyButton.action = function() easyButtonPressed() end
mLocY = mLocY - 80
mediumButton = Button("Medium", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
mediumButton.action = function() mediumButtonPressed() end
mLocY = mLocY - 80
hardButton = Button("Hard", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
hardButton.action = function() hardButtonPressed() end
menuBorder = RoundBorder(10, 10, WIDTH - 10, HEIGHT - 10, 1, blueColour, blackColour)
-- create the run screen buttons
mLocX = WIDTH - mButtonSize.x/2 - 25
mLocY = HEIGHT - 195
mButtonSize = vec2(100, 50)
mLocY = 110 + mButtonSize.y*2
newGameButton = Button("New", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
newGameButton.action = function() newGameButtonPressed() end
mLocY = 110
menuButton = Button("Menu", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
menuButton.action = function() menuButtonPressed() end
-- create the splash screen
splashScreen = SplashScreen("Bouncy", 3)
-- Create the twinkling star background
end
-- Draw routines
function draw()
-- The main drawing function, called 60 times per second if possible.
-- This sets a black background color
background(blackColour)
-- Calculate and display FPS and track game time if game running.
frameCount = frameCount + 1
timeInterval = timeInterval + DeltaTime
if timeInterval > 1 then
if gameTimerOn then
-- update gameTime roughly every second.
gameTime = gameTime + 1
end
FPS = math.round(frameCount / timeInterval)
timeInterval = 0
frameCount = 0
end
-- Draw appropriate screen based on gameState
if gameState == stateSplash then
splashScreen: draw()
elseif gameState == stateMenu then
drawMenu()
elseif gameState == stateRun then
drawGameButtons()
elseif gameState == stateGameOver then
drawGameButtons()
text("Game Won!", WIDTH/2, 60)
end
end
function drawGameButtons()
-- These are the buttons visible on the game run screen
newGameButton:draw()
menuButton:draw()
end
function drawMenu()
-- Draw the Game Menu Buttons
menuBorder: draw()
font("Arial-BoldMT")
fill(whiteColour)
fontSize(20)
text("Select Game Difficulty", WIDTH/2, HEIGHT/2 + 150)
fill(blueColour)
fontSize(72)
text("Bouncy", WIDTH/2, HEIGHT/2 + 220)
easyButton:draw()
mediumButton:draw()
hardButton:draw()
end
function resetGrid()
end
-- Touch & Button Handling Functions
function newGameButtonPressed()
resetGrid()
gameTimerOn = false
if gameDifficulty == stateEasy then
easyButtonPressed()
elseif gameDifficulty == stateMedium then
mediumButtonPressed()
elseif gameDifficulty == stateHard then
hardButtonPressed()
end
end
function menuButtonPressed()
gameState = stateMenu
gameTimerOn = false
end
-- Menu Buttons
function resetGameParameters()
-- Regardless of the game difficulty selected these parameters
-- need to be reset when a new game is started.
gameTime = 0
-- Make sure that the Flag / Show cell button is in the default state.
-- Create the cell grid and seed with mines then run game
gameState = stateRun
end
function easyButtonPressed()
-- Initialise the parameters which determine difficulty
-- Namely: number of mines and the grid size
gameDifficulty = stateEasy
resetGameParameters()
end
function mediumButtonPressed()
-- Initialise the parameters which determine difficulty
-- Namely: number of mines and the grid size
gameDifficulty = stateMedium
resetGameParameters()
end
function hardButtonPressed()
-- Initialise the parameters which determine difficulty
-- Namely: number of mines and the grid size
gameDifficulty = stateHard
resetGameParameters()
end
function touched(touch)
-- It is important to explicitly state when to handle touches.
--
-- If you don't do this based on gameState then for example, when in the
-- game run state your menu button handling functions will still get called
-- even if the buttons are not being drawn.
if gameState == stateMenu then
easyButton:touched(touch)
mediumButton:touched(touch)
hardButton:touched(touch)
elseif gameState == stateRun then
menuButton:touched(touch)
newGameButton:touched(touch)
elseif gameState == stateGameOver then
menuButton:touched(touch)
newGameButton:touched(touch)
end
end
-- Utility Functions
function math.round(num)
-- math.round function courtesy of @Vega
return math.floor(num + 0.5)
end
In addition to this there is the, RoundBorder, SplashScreen, Button, and Fader class from the Minesweeper code
Hope anyone can help.
Please reply @Reefwing
@veeeralp - dude give me a chance, unfortunately giving out free advice doesn’t pay very well, so like most folks here I have a day job.
As to your code above what exactly is the problem? This code will just draw 2 buttons when in the run state. The drawMyGame() function is something you need to write. I have stripped out some more of the code you don’t need in an updated version below, streamlined the FPS code and commented where you need to stick you game drawing code.
function setup()
version = 2.1
saveProjectInfo("Description", "Bouncy by Veeral Patel.")
saveProjectInfo("Author", "Veeral Patel")
saveProjectInfo("Date", "18th July 2012")
saveProjectInfo("Version", version)
print("Bouncy v"..version.."\
")
-- Define the game colour palette
whiteColour = color(255, 255, 255)
blackColour = color(0, 0, 0)
redColour = color(243, 157, 33, 255)
blueColour = color(0, 188, 255, 255)
greenColour = color(45, 226, 23, 255)
-- Keep an eye on your FPS
FPS = 0
watch("FPS")
-- keep track of your Game State and Game Difficulty
-- using Finite State Machines.
--
-- gameState is the overall state of the game.
--
-- gameDifficulty allows us to setup a new game with the correct parameters when the
-- newGameButton is tapped.
stateSplash = 0
stateMenu = 1
stateRun = 2
stateGameOver = 3
gameState = stateSplash
stateEasy = 7
stateMedium = 8
stateHard = 9
gameDifficulty = stateEasy
-- initialise the game variables
score = 0
-- create the 3 menu buttons, they wont be visible until you draw them.
-- Note that 50 pixels is the minimum height for the default font size
local mButtonSize = vec2(180, 50)
local mLocX = WIDTH/2 - mButtonSize.x/2
local mLocY = HEIGHT/2 + 20
easyButton = Button("Easy", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
easyButton.action = function() easyButtonPressed() end
mLocY = mLocY - 80
mediumButton = Button("Medium", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
mediumButton.action = function() mediumButtonPressed() end
mLocY = mLocY - 80
hardButton = Button("Hard", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
hardButton.action = function() hardButtonPressed() end
menuBorder = RoundBorder(10, 10, WIDTH - 10, HEIGHT - 10, 1, blueColour, blackColour)
-- create the run screen buttons
mLocX = WIDTH - mButtonSize.x/2 - 25
mLocY = HEIGHT - 195
mButtonSize = vec2(100, 50)
mLocY = 110 + mButtonSize.y*2
newGameButton = Button("New", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
newGameButton.action = function() newGameButtonPressed() end
mLocY = 110
menuButton = Button("Menu", vec2(mLocX, mLocY), mButtonSize.x, mButtonSize.y)
menuButton.action = function() menuButtonPressed() end
-- create the splash screen
splashScreen = SplashScreen("Bouncy", 3)
end
-- Draw routines
function draw()
-- The main drawing function, called 60 times per second if possible.
-- This sets a black background color
background(blackColour)
-- Calculate and display FPS
FPS = math.round(1/DeltaTime)
-- Draw appropriate screen based on gameState
if gameState == stateSplash then
splashScreen: draw()
elseif gameState == stateMenu then
drawMenu()
elseif gameState == stateRun then
-- THIS IS WHERE YOU NEED TO PUT THE CODE WHICH
-- DRAWS YOUR GAME, OR STICK IT IN A FUNCTION OR
-- CLASS IF YOU PREFER AND CALL IT FROM HERE!!!!
drawGameButtons()
elseif gameState == stateGameOver then
drawGameButtons()
text("Game Won!", WIDTH/2, 60)
end
end
function drawGameButtons()
-- These are the buttons visible on the game run screen
newGameButton:draw()
menuButton:draw()
end
function drawMenu()
-- Draw the Game Menu Buttons
menuBorder: draw()
font("Arial-BoldMT")
fill(whiteColour)
fontSize(20)
text("Select Game Difficulty", WIDTH/2, HEIGHT/2 + 150)
fill(blueColour)
fontSize(72)
text("Bouncy", WIDTH/2, HEIGHT/2 + 220)
easyButton:draw()
mediumButton:draw()
hardButton:draw()
end
function resetGrid()
end
-- Touch & Button Handling Functions
function newGameButtonPressed()
if gameDifficulty == stateEasy then
easyButtonPressed()
elseif gameDifficulty == stateMedium then
mediumButtonPressed()
elseif gameDifficulty == stateHard then
hardButtonPressed()
end
end
function menuButtonPressed()
gameState = stateMenu
end
-- Menu Buttons
function easyButtonPressed()
-- Initialise the parameters which determine difficulty
gameDifficulty = stateEasy
gameState = stateRun
end
function mediumButtonPressed()
-- Initialise the parameters which determine difficulty
gameDifficulty = stateMedium
gameState = stateRun
end
function hardButtonPressed()
-- Initialise the parameters which determine difficulty
gameDifficulty = stateHard
gameState = stateRun
end
function touched(touch)
-- It is important to explicitly state when to handle touches.
--
-- If you don't do this based on gameState then for example, when in the
-- game run state your menu button handling functions will still get called
-- even if the buttons are not being drawn.
if gameState == stateMenu then
easyButton:touched(touch)
mediumButton:touched(touch)
hardButton:touched(touch)
elseif gameState == stateRun then
menuButton:touched(touch)
newGameButton:touched(touch)
elseif gameState == stateGameOver then
menuButton:touched(touch)
newGameButton:touched(touch)
end
end
-- Utility Functions
function math.round(num)
-- math.round function courtesy of @Vega
return math.floor(num + 0.5)
end
Sorry your probably in a different time zone. I used your suggestions and got it to work by putting in the class. Sorry I bothered you. I’m on summer vacation b/c I’m only 15.
@veeeralp - no bother, glad you got it working.