a small tank game

Show off your games, demos and other (playable) creations.
Post Reply
proudsea
Prole
Posts: 3
Joined: Mon Mar 03, 2014 12:24 pm

a small tank game

Post 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!!
Attachments
tank.love
a tank game
(41.09 KiB) Downloaded 231 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: a small tank game

Post 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'
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: a small tank game

Post 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.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: a small tank game

Post 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.
proudsea
Prole
Posts: 3
Joined: Mon Mar 03, 2014 12:24 pm

Re: a small tank game

Post 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?
proudsea
Prole
Posts: 3
Joined: Mon Mar 03, 2014 12:24 pm

Re: a small tank game

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests