http://pastebin.com/knUNhTs4
Edit: I should add that this isn't for CC. Its a LUA project, and this is the best place to look.
function main()
--Do your stuff
end
function restart()
main()
end
restart()
Your main function should call your other functions being used in the game, but so your not calling a undefined variable make sure to defined a function before calling it. Like I did in my example.function on.arrowLeft = arrowleft
function.on.arrowRight = arrowright
function.on.spacebar = space
Function definitions done wrong.Well said, but you don't have to list what you said like there, there's an option right under Size to make a list of things, likeOkay…what the heck?
1. You need to end all if statements
2. Use the CC wiki to learn the CC APIs
3. You call functions with functionName()
4. You should create functions and not use so many if statements
5. Learn the CC APIs
6. This:Function definitions done wrong.function on.arrowLeft = arrowleft function.on.arrowRight = arrowright function.on.spacebar = space
7. Learn the CC APIs
8. Relearn/review parts of Lua
I know, but I've always preferred to do without it. Besides, it takes up more space (which makes it look nicer to some, but to me it looks…meh).-snip-
Well said, but you don't have to list what you said like there, there's an option right under Size to make a list of things, likeAnd it looks nicer.
- Thing 1
- Thing 2
- Thing 3
Additionally to this great summary by Smiley43210, read the PIL… If you want to become a programmer one day you will have to get used to reading developer documentation for languages, I do it almost every day… Sometimes its annoying, sometimes the documentation is bad, but to know the language it has to be done, and I will say that the PIL is one of the best that I have read.Okay…what the heck?
1. You need to end all if statements
2. Use the CC wiki to learn the CC APIs
3. You call functions with functionName()
4. You should create functions and not use so many if statements
5. Learn the CC APIs
6. This:Function definitions done wrong.function on.arrowLeft = arrowleft function.on.arrowRight = arrowright function.on.spacebar = space
7. Learn the CC APIs
8. Relearn/review parts of Lua
local variable = io.read("*n")
-- Instead of?
local variable = io.read("*number")
^
I read the "PIL" and can't you dolocal variable = io.read("*n") -- Instead of? local variable = io.read("*number")
lua> io.read("*n")
io:5: Unsupported format
lua> io.read("*number")
io:5: Unsupported format
^
I was talking about in regular Lua.
Lol guys im ten!
newGame==1
function main()
function game()
--Game Code here
end
game()
global newGame==0
print("New game? y/n")
io.read()
if io.read==("y") then newGame==1 elseif io.read==("n") then newGame==0 elseif not io.read==("y") and not read ("n") then print("Please enter y or n") end
end
function startGame()
if newGame==1 then main() elseif newGame==0 then end
end
startGame()
Nope, nope, nope, nope, nope!This code hurts me, even looking in a small segment. I'm 12 and I know how to do this. Functions are a wonderful thing. As are loops besides for the "if" loop. Either way, I might as well help you. First, put all of your game inside ONE function. At the end of the function, add a yes/no prompt and a variable containing a boolean value. Said boolean should equal 0 if they typed "n" and 1 if it was "y" then, after the end of the function, call it. Then set up an if statement that will run the function or exit the program.
Example:newGame==1 function main() function game() --Game Code here end game() global newGame==0 print("New game? y/n") io.read() if io.read==("y") then newGame==1 elseif io.read==("n") then newGame==0 elseif not io.read==("y") and not read ("n") then print("Please enter y or n") end end function startGame() if newGame==1 then main() elseif newGame==0 then end end startGame()
EDIT: The "read" should be "io.read". Fixed.
--# while loop
while <condition> do
--# code to loop
end
--# repeat loop
repeat
--# code to loop
until <condition>
--# incremental for loop
for i = <start>, <end>, <optional increment> do
--# code to loop
end
--# generic for loop
for <key>, <value> in <iterable structure> do
--# code to loop
end
newGame==1 --# this will be an error, you should have a single `=` there
function main()
--# why an anonymous function?
function game()
end
--# to then call it here, why not just do game code here?!
game()
--# there is no global keyword in Lua, I have the feeling you've come from Python? also again, wrong `=`
global newGame==0
print("New game? y/n")
--# you should use `read`, `io.read` uses `read` so just remove the need to perform that extra function call
--# you're not storing the value in a variable? o.O
io.read()
--# now you're checking a function pointer against a string
if io.read==("y") then
--# wrong `=`
newGame==1
--# again, checking function pointer... what language does this even work in?
elseif io.read==("n") then
--# wrong `=`
newGame==0
--# checking a function pointer, and then waiting for input from a user again.... actually the "n" would cause an error
elseif not io.read==("y") and not read ("n") then
print("Please enter y or n")
end
end
function startGame()
if newGame==1 then
main()
--# no need for this
elseif newGame==0 then
end
end
startGame()
local running = true
function game()
--# game code here
end
function main()
game()
print("New game? y/n")
local input = read():lower()
while input ~= "y" or input == "n" do
print("Please enter y or n")
input = read():lower()
end
running = input == "y"
end
function startGame()
while running do
main()
end
end
startGame()
local quitRequested = false
local function update()
--# increment any variables and such
end
local function render()
--# render any changes that have been made
end
local function processInput()
os.startTimer(0.1) --# allows the game to move on and not get stuck here.
local e = os.pullEventRaw()
--# process the event that was received
if e[1] == "key" and e[2] == keys.q then
quitRequested = true
end
end
local function main()
render()
repeat
processInput()
update()
render()
until quitRequested
end
main()