Page 1 of 1

a small tank game

Posted: Mon Mar 03, 2014 12:32 pm
by proudsea
Hi everyone,
This is the first time I post topic in the forums. I have been learning LOVE for almost a month, and it deeply attracts me.
The small program in the attachment is a tank game, the player could move around and click space to shoot the enemy.
It is also my first love game.... so please give me some advice about the learn road map, very thanks!!

Re: a small tank game

Posted: Mon Mar 03, 2014 7:53 pm
by davisdude
Error wrote:

Code: Select all

Error

game.lua:54: bad argument #1 to 'rad' (number expected, got nil)

Traceback
[C]: in function 'rad'
game.lua:54: in function 'draw'
main.lua:28: in function 'draw'
[C]: in function 'xpcall'

Re: a small tank game

Posted: Mon Mar 03, 2014 11:28 pm
by SneakySnake
math.random expects its arguments to be integers. If they are not, weird things could happen.
http://www.lua.org/manual/5.1/manual.html wrote: When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].
Here, you are trying to call math.random with non-integer arguments:

Code: Select all

ev.direction = game.direction[math.floor(math.random(1.01, 4.99))]
In this case, math.random seems to sometimes return a value >= 5. Don't ask me why,
game.direction[5] is not a valid direction. This ultimately results in the error davisdude posted above.

You also repeat the same piece of code multiple times. You should probably turn it into a function.

Here is a patch that fixes the error, and also turns the repeated code into a function: https://gist.github.com/crumblingstatue ... 41b91128f2
The diff is a bit messed up because the code uses mixed space/tab indentation.

EDIT: On an unrelated note, does the game window have to be so high? I use a resolution of 1024x768. I know that today, most people use resolutions higher than that, but still. It does not seem necessary for this game.

Re: a small tank game

Posted: Mon Mar 03, 2014 11:48 pm
by Ref
Your collision handling is messed up.
Enemies can't go through walls but you can???
Enemies colliding with enemies give weird results.
And of course, you need

Code: Select all

math.random(10,49)/10

Need a scoring system.
Good start - keep at it.

Re: a small tank game

Posted: Tue Mar 04, 2014 2:13 am
by proudsea
SneakySnake wrote:math.random expects its arguments to be integers. If they are not, weird things could happen.
http://www.lua.org/manual/5.1/manual.html wrote: When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].
Here, you are trying to call math.random with non-integer arguments:

Code: Select all

ev.direction = game.direction[math.floor(math.random(1.01, 4.99))]
In this case, math.random seems to sometimes return a value >= 5. Don't ask me why,
game.direction[5] is not a valid direction. This ultimately results in the error davisdude posted above.

You also repeat the same piece of code multiple times. You should probably turn it into a function.

Here is a patch that fixes the error, and also turns the repeated code into a function: https://gist.github.com/crumblingstatue ... 41b91128f2
The diff is a bit messed up because the code uses mixed space/tab indentation.

EDIT: On an unrelated note, does the game window have to be so high? I use a resolution of 1024x768. I know that today, most people use resolutions higher than that, but still. It does not seem necessary for this game.
Very thanks SneakySnake!
You really give me some good ideas about programming, such as exploring the api document, reduce the redundant code...
about the height of the window, I think the size of the player, enemy, as well as the brick should adjust their size to suit different kinds of size, right?

Re: a small tank game

Posted: Tue Mar 04, 2014 2:16 am
by proudsea
Ref wrote:Your collision handling is messed up.
Enemies can't go through walls but you can???
Enemies colliding with enemies give weird results.
And of course, you need

Code: Select all

math.random(10,49)/10

Need a scoring system.
Good start - keep at it.
Hi Ref, thanks for your kindly advices, I will complete the game and think about how to success the game.