Tag Archives: lua

Lua – Guess My Number

I was messing around with Lua tonight, and as a basic exercise threw together a Guess My Number game. Unfortunately, WordPress.com doesn’t have syntax highlighting for Lua. C’est la vie!

I also learned that the first number you grab from math.random() may not be properly randomized after seeding. Running my code in the interpreter generated 36 five times in a row as the answer, which clued me in to this :) Calling math.random() once after seeding seems to fix it, though.

-- Guess My Number
-- Author: Zachary Hoefler

--INITIALIZATION
-- Generate a random number
math.randomseed( os.time() )
math.random(); -- make sure first number is actually randomized (some implementations
               -- require this, but it doesn't break ones that don't)
local THE_NUMBER = math.random(1,100) -- generates a number from 1 to 100, inclusive

-- Welcome the user
print("Guess My Number, by Zach Hoefler")

-- MAIN GAME LOOP
local guess = nil -- explicitly declared to make it local
while (guess ~= THE_NUMBER) do
   -- prompt for a guess; use io.write() so there's no newline after the prompt
   io.write("Please enter a guess from 1-100: ");
   guess = io.read("*number")

   -- Compare to the correct number
   if guess > THE_NUMBER then
      print("You guessed too high!")
   elseif guess < THE_NUMBER then
      print("You guessed too low!")
   end
end
-- Number was guessed correctly
assert(guess == THE_NUMBER)

-- Congratulate the user
print("Congratulations, you guessed correctly!")

Strictly speaking, I could’ve used repeat-until instead of while, but the while loop felt a bit more readable.

Personal Goals Update

My first post on this blog was about my current goals, and thus far I’ve stuck to them. Not only that, though, but I have a clearer view of how I want to go about things!

1. Learn Lua

I’ve been following along with Programming in Lua, a free book available on the official Lua website. My goal is to get through most of it by the end of the week. I’ve been considering doing a small project to get some practice with the language; I’ll probably make a hangman game. Hangman would allow me to work with a lot of the language’s features, and it’s simple enough where I’d mostly get caught up on language-related errors (which is what I want) rather than messing up algorithms.

Regarding the book, I’m actually pretty impressed by it. I’m strongly considering grabbing a print copy once I get a chance.

2. Get a taste of DirectX

Though I haven’t started this one yet–I’m waiting until after I’ve gotten a grasp of Lua–I’ve got a better idea of what I’d like to do:

  1. Learn Lua
  2. Jump into DirectX using C++ with tutorials
  3. Create a 2D arcade game using C++, DirectX, and Lua scripting.

By doing all of this, not only would I (hopefully!) learn DirectX pretty well, I’d also walk away with a cool game and a solid portfolio piece! Most likely, the arcade game would be based on something simple (Asteroids? Space Invaders?), but jazzed up with particle effects and the like. I’ll probably work toward this a lot over our holiday break from school, and hopefully I’ll be finishing it up before winter courses are done.

Longer term, I’d also like to try creating either a fighting game or strategy game. Also on the list is learning 3D with DirectX.

3. Stay up to date on Games Industry news

I seem to have found the perfect solution with Google Reader. This goal is something I have tried to accomplish a few times, but I just couldn’t get checking news sites into my daily routine; however, being able to get the latest updates of various news sites, comics, etc. all in one location has led me to checking Google Reader regularly. I’m currently subscribed to Gamasutra, The Escapist, GameCareerGuide, and a number of other sites. I really wish I’d discovered RSS readers earlier!

4. Play/analyze new games regularly

I’d like to incorporate this into my daily routine at some point, but for now I’m only pushing towards getting this done every other day. Finding and analyzing games is simple enough–I have a bunch of unplayed games, friends to borrow games from, sites like Kongregate, etc.–but doing write-ups is a bit harder.

Although I’d planned to post a write-up for every game on this blog (and I’ve posted one already), I’m not sure how feasible that will be. I definitely plan to do write-ups now and then, but with the amount of time it takes to do a good write-up (rather than a jumbled list of thoughts from my notes), I don’t think I’ll be doing one for every single game I try. I’ll probably be experimenting a bit with different formats to see if writing up something a little less formal is doable.


 

Finally, I highly recommend the site Remember the Milk, a free to-do list manager. I’ve been using it for the past couple weeks, and not only has it been awesome for keeping track of everything I need to get done, I’ve also been significantly more productive! Between daily reminders and being able to set deadlines that I’ll actually be able to keep track of, it’s been a godsend.

  • Learn Lua – I have heard nothing but good things about Lua, and it seems to come up frequently with games. When one also considers that the first edition of Programming in Lua is legally available for free online, teaching myself the language is definitely something I plan to do in the near future!
  • Get a taste of DirectX – Having worked in XNA, and after getting a brief introduction to OpenGL, introducing myself to DirectX seems like the next logical step. Although I don’t plan on attempting to master it in the immediate future, I would like to learn the basics and reach the point where it does not intimidate me, at the least :)
  • Stay up to date on Games Industry news – I’ve been making a point to read Gamasutra and The Escapist daily to stay on top what’s going on in the Games Industry
  • Play (and analyze) a new game at least every other day – In an effort to both improve my knowledge of what games are out there and to improve my ability to analyze games, I’m going to push myself to play and analyze a new game at least every other day. If I can spare even 15-20 minutes every day or two to trying out a new game, I should definitely be able to pull this one off. I also plan on posting my analysis of the games on this blog!

Learning Lua

Sticking to my plans, I’ve begun teaching myself Lua. Although it’s a bit odd to wrap my head around after just diving head-first into C++, it’s pretty cool!

One lesson I’ve learned (or, rather, had reaffirmed) is how important writing clean code is. Given Lua doesn’t require semicolons to separate statements and doesn’t use braces, you can get away with some really sloppy, hard-to-read stuff. The following are all equivalent:

-- Easy to read factorial function
function fact (n)
   if n == 0 then
      return 1
   else
      return n * fact(n-1)
   end
end
-- Hard to read, but simple enough where you could probably figure it out
function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end
-- Oh boy.
function
fact
(n) if n ==
0 then return
1 else
return n *
fact(n
-1) end

end

Having only really worked with C#, C++, Java, and ActionScript, it’s going to take some effort getting used to the syntax of Lua; however, I’m up for the challenge!

Current Goals

With fall quarter classes coming to an end, I’ve had time to unwind and start thinking about my current short-term goals:

  • Learn Lua – I have heard nothing but good things about Lua, and it seems to come up frequently with games. When one also considers that the first edition of Programming in Lua is legally available for free online, teaching myself the language is definitely something I plan to do in the near future!
  • Get a taste of DirectX – Having worked in XNA, and after getting a brief introduction to OpenGL, introducing myself to DirectX seems like the next logical step. Although I don’t plan on attempting to master it in the immediate future, I would like to learn the basics and reach the point where it does not intimidate me, at the least :)
  • Stay up to date on Games Industry news – I’ve been making a point to read Gamasutra and The Escapist daily to stay on top what’s going on in the Games Industry
  • Play (and analyze) a new game at least every other day – In an effort to both improve my knowledge of what games are out there and to improve my ability to analyze games, I’m going to push myself to play and analyze a new game at least every other day. If I can spare even 15-20 minutes every day or two to trying out a new game, I should definitely be able to pull this one off. I also plan on posting my analysis of the games on this blog!