## [class] Shoot class

shinprog
Prole
Posts: 15
Joined: Sat Oct 21, 2017 11:56 am
Contact:

### [class] Shoot class

Hello world !

I work on a fangame and one part of the game is a shooting game.
I searched a lot for use guns and bullet and I found this : http://love2d.org/wiki/Tutorial:Fire_Toward_Mouse

So i make this class for my game and I post it here for other who want to use this.

It's open-source (under the MIT license) and you can include it in your game,of course !Just put the shoot.lua in your project folder and use a "require 'shoot'"

In attachments,the .love file with a demo.

How to use-it ?

It is very simple.

Code: Select all

require 'shoot'
Step 2\ Initialize the class.In the first and the second argument,set the position of the gun and in the third argument,the speed.
The default speed is 150,you can increment the value by 100 for more speed.
In the demo,the Xpos and Y pos is the center of the screen,and the speed is 250.

Code: Select all

war = shoot.init(xpos,ypos,speed)
Step 3\Change the sprite of the bullet

Code: Select all

war.changeSprite(image)
By default,the sprite of the bullet is a circle.You can change the bullet with an image.
Call this function with the path of the image.
If you call without arguments of the image can't be loaded,the bullet is by default a circle.

Step 3.1\Change the color

Code: Select all

war.changeColor(r,g,b)
If the bullet isn't an image,you can change the color of the circle with this function

Step 4\Change the position

Code: Select all

war.changePos(x,y)
The player isn't motionless ! If he change position,call this function with position of the player in argument.

Step 5\Make it fire

Code: Select all

war.fire(targetX,targetY)
Make a bullet and set the target with position given in arguments.

Step 6\Display all

Code: Select all

function love.draw()
war.draw()
end
function love.update(dt)
war.update(dt)
end
Call draw function for display bullets and update (don't miss the dt argument) for update position of the bullet.

You can read the code of the demo if you want more !
I hobe that you'll like it.
I you found a bug,reply in this post !
Attachments
test_direction.love
the demo and the class
I'm a french hight school student who starts programming
I'm not very comfortable with english but I'll do my best.

CO-funder of the Kagerou Project Fr Community.
http://kagescan.esy.es http://fb.me/KagerouProjectFr

erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

### Re: [class] Shoot class

Hey there and welcome to the forums!

It's awesome that you are trying to create libraries for everyone to use!

However. I have a few notes.

First of all. Libraries are meant to abstract complex things into simple, reusable and flexible code.
Especially things covered by tutorials with just a few lines of code (let's say less than 200) are probably not terribly helpful for game creation.
Even more so if they are designed around a singular usecase (as in this case. Move sprites towards mouse).

Which leads me to my second point. A projectile library without collision checking is just animating sprites to move somewhere. Not necessarily bad but a very very narrow usecase.

And as last nitpick. The standard for sharing libraries has truly become repositories. You can create repositories for free on GitHub!

It is also good etiquette to add a testing framework, which automatically makes sure the code runs as expected. Ideally integrating it into your repository so every commit you make and every pull request is also automatically checked for problems.

This might be overwhelming at first. And yeah. It is. But using version control (e.g. git and github) is absolutely essential for software development. And relying on the code being functional beyond it running well in a single usecase is obviously also really nice to have when considering to use a library. Busted is a rather widely used testing framework.

The last thing I want to do is discourage you from creating stuff!

But maybe dial down the library threads a bit?

I feel like this subforum should probably host mainly more flexible code. And not necessarily all the functionality that you created while learning.

There's nothing wrong with learning! But not every piece of code is useful for a larger amount of people

### Who is online

Users browsing this forum: yancouto and 4 guests