Game Development Reference
In-Depth Information
myPoint.y=240
print(myPoint.x, myPoint.y)
A point is a better way to store your coordinates than to have pairs of variables such as playerX ,
playerY ; monster1X , monster1Y ; monster2X , monster2Y ; and so on. By having a point table structure,
you can just have playerCoords , monsterCoords , and so on. This will also be useful when we look
into vectors, which are slightly more complicated than simple tables. The best part is that our two
functions inc and dec shall work without any changes. It is as simple as coding
myPoint.x=inc(myPoint.x,8)
myPoint.y=inc(myPoint.y,4)
=math.random(1,10)*1000
print("Well done")
print("There is always a second chance")
print("you have missed even the second chance")
end
What we have done here is use math.random , a function that helps generate a random number. We
request a whole number (integer) in the range of 1 to 10, and then we multiply that by 1,000 to get
a score in the range of 1,000 to 10,000. This score is then used to determine the message that will
be printed. For that conditional evaluation, we use if .. elseif .. end blocks to check for our
conditions.
Flipping a Coin
You just learned about the random number generator used in many games and languages. In this
exercise, we'll try to use the random number generator to emulate a coin flip. The good thing about
a coin flip is that it is either heads or tails. There are many ways of handling this, but we shall try the
simplest one: getting a random number that is either 1 or 2. So, if we get 1, we call it “heads,” and if
we get 2, we call it “tails.”
local theFlip=math.random(1,2)
if theFlip ==1 then
print("Got heads")
else
print("Got tails")
end
 
Search WWH ::




Custom Search