Difference between revisions of "Tutorial:Fire Toward Mouse"

m (Added: Other Languages)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{#set:LOVE version=0.7.2}}
+
This [[:Category:Tutorials|tutorial]] describes how to make a bullet fire toward the mouse when the user clicks. It is assumed that you know the basics of LOVE and Lua. All code takes place within main.lua.
{{#set:Description=Mouse, trig, fire}}
 
 
 
This tutorial describes how to make a bullet fire toward the mouse when the user clicks. It is assumed that you know the basics of LOVE and Lua. All code takes place within main.lua.
 
  
 
== Initialization ==
 
== Initialization ==
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
love.graphics.setBackgroundColor(54, 172, 248)
+
love.graphics.setBackgroundColor(0.21, 0.67, 0.97)
 
 
 
bulletSpeed = 250
 
bulletSpeed = 250
Line 23: Line 20:
 
<source lang="lua">
 
<source lang="lua">
 
function love.draw()
 
function love.draw()
love.graphics.setColor(255, 255, 255)
+
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle("fill",  player["x"], player["y"], player["width"], player["height"])
+
love.graphics.rectangle("fill",  player.x, player.y, player.width, player.height)
 
 
love.graphics.setColor(128, 128, 128)
+
love.graphics.setColor(0.5, 0.5, 0.5)
 
for i,v in ipairs(bullets) do
 
for i,v in ipairs(bullets) do
love.graphics.circle("fill", v["x"], v["y"], 3)
+
love.graphics.circle("fill", v.x, v.y, 3)
 
end
 
end
 
end
 
end
Line 41: Line 38:
 
function love.update(dt)
 
function love.update(dt)
 
for i,v in ipairs(bullets) do
 
for i,v in ipairs(bullets) do
v["x"] = v["x"] + (v["dx"] * dt)
+
v.x = v.x + (v.dx * dt)
v["y"] = v["y"] + (v["dy"] * dt)
+
v.y = v.y + (v.dy * dt)
 
end
 
end
 
end
 
end
Line 52: Line 49:
 
<source lang="lua">
 
<source lang="lua">
 
function love.mousepressed(x, y, button)
 
function love.mousepressed(x, y, button)
if button == "l" then
+
if button == 1 then
local startX = player["x"] + player["width"] / 2
+
local startX = player.x + player.width / 2
local startY = player["y"] + player["height"] / 2
+
local startY = player.y + player.height / 2
 
local mouseX = x
 
local mouseX = x
 
local mouseY = y
 
local mouseY = y
Line 68: Line 65:
 
</source>
 
</source>
  
The if statement at the beginning of this makes it so that we only fire a bullet if the user left clicks. The ''startX'' and ''startY'' variables define the origin of the bullet. We want the bullet to start at the center of the player so we use ''player["x"] + player["width"] / 2'' to get that.
+
The if statement at the beginning of this makes it so that we only fire a bullet if the user left clicks. The ''startX'' and ''startY'' variables define the origin of the bullet. We want the bullet to start at the center of the player so we use ''player.x + player.width / 2'' to get that.
  
 
To get angle we use ''math.atan2'' and pass it the difference in y and the difference in x. This function is very useful for finding the angle between any two objects.
 
To get angle we use ''math.atan2'' and pass it the difference in y and the difference in x. This function is very useful for finding the angle between any two objects.
Line 81: Line 78:
 
--[[User:Somethingmoreunique|Somethingmoreunique]] 03:22, 25 December 2011 (GMT)
 
--[[User:Somethingmoreunique|Somethingmoreunique]] 03:22, 25 December 2011 (GMT)
  
[[Category:Games]]
+
[[Category:Tutorials]]
 +
{{#set:LOVE Version=11.2}}
 +
{{#set:Description=Firing bullets toward the mouse}}
 +
 
 +
== Other Languages ==
 +
{{i18n|Tutorial:Fire Toward Mouse}}

Latest revision as of 13:38, 25 November 2019

This tutorial describes how to make a bullet fire toward the mouse when the user clicks. It is assumed that you know the basics of LOVE and Lua. All code takes place within main.lua.

Initialization

function love.load()
	love.graphics.setBackgroundColor(0.21, 0.67, 0.97)
	
	bulletSpeed = 250
	
	bullets = {}
	player = {x=250, y=250, width=15, height=15}
end

The first line just sets the background color as a nice blue. bulletSpeed is our variable that defines how fast a bullet will travel in pixels per second. The table bullets will hold a list of all our bullets and the table player holds all the info about our player.

Each bullet will be its own table inside the bullets table. It will contain the properties x, y, dx, and dy. The dx and dy variables define how much the bullet should move in pixels per second on the x and y axis.

Drawing Everything

function love.draw()
	love.graphics.setColor(1, 1, 1)
	love.graphics.rectangle("fill",  player.x, player.y, player.width, player.height)
	
	love.graphics.setColor(0.5, 0.5, 0.5)
	for i,v in ipairs(bullets) do
		love.graphics.circle("fill", v.x, v.y, 3)
	end
end

The first thing we do in the draw function is to set the color as white. After that, we draw a rectangle (which will represent the player).

The next segment sets the color as gray for the bullet. The for statement will go through each value in our table of bullets. i is the index of the current bullet and v is its value. For each bullet we draw it as a circle on the screen

Determining the Bullet's New Position

function love.update(dt)
	for i,v in ipairs(bullets) do
		v.x = v.x + (v.dx * dt)
		v.y = v.y + (v.dy * dt)
	end
end

Once again we go through the list of bullets. For each one we update its x and y position. We do this by taking the current position and adding the change multiplied by the delta time.

Firing the Bullet

function love.mousepressed(x, y, button)
	if button == 1 then
		local startX = player.x + player.width / 2
		local startY = player.y + player.height / 2
		local mouseX = x
		local mouseY = y
		
		local angle = math.atan2((mouseY - startY), (mouseX - startX))
		
		local bulletDx = bulletSpeed * math.cos(angle)
		local bulletDy = bulletSpeed * math.sin(angle)
		
		table.insert(bullets, {x = startX, y = startY, dx = bulletDx, dy = bulletDy})
	end
end

The if statement at the beginning of this makes it so that we only fire a bullet if the user left clicks. The startX and startY variables define the origin of the bullet. We want the bullet to start at the center of the player so we use player.x + player.width / 2 to get that.

To get angle we use math.atan2 and pass it the difference in y and the difference in x. This function is very useful for finding the angle between any two objects.

We find bulletDx and bulletDy by using some more trig. These varibales will define how fast the bullet will move in the x and y plane.

The final line here inserts our new bullet into the list of bullets

Conclusion

When all put together you get the start of a top-down shooter game. Try out making the player movable and making the bullets disappear when they go off screen.

--Somethingmoreunique 03:22, 25 December 2011 (GMT)


Other Languages