[class] Shoot class

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Posts: 15
Joined: Sat Oct 21, 2017 11:56 am

[class] Shoot class

Post by shinprog » Mon Oct 23, 2017 9:03 am

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.

Step 1\ Add the class in your game

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

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

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

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

Make a bullet and set the target with position given in arguments.

Step 6\Display all

Code: Select all

function love.draw()
function love.update(dt)
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 !
the demo and the class
(4.82 KiB) Downloaded 44 times
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

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

Re: [class] Shoot class

Post by erasio » Tue Oct 24, 2017 6:31 pm

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 ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests