Enemies, obstacle's, power ups, life's and health bar (help)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Enemies, obstacle's, power ups, life's and health bar (help)

Post by ShadowPenguins »

Hey so i'm making a plat-former and haven't seen anything really on how to make spikes that kill the player and enemies that kill the player without having a game state. Also the other stuff i don't know how i'd go about it i think i have to edit the players table but im not sure after that point and how you'd restart and have it count

So how would I go about it. It's really puzzling :shock:

I am using bump for collisions

This one is the messed up one when i tried to add spikes i don't know what went wrong
insomnia.love
(3.84 MiB) Downloaded 179 times
Last edited by ShadowPenguins on Tue Jan 16, 2018 4:21 pm, edited 5 times in total.
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
Ostego160
Prole
Posts: 14
Joined: Tue Oct 17, 2017 7:18 pm

Re: Enemies and obstacle's

Post by Ostego160 »

Hello!

Are you using a collision detection library, and if so which one? That would help me give you some pseudo code. But here is the concept:

Store the enemies in a table, iterate through the table for player/enemy collision. Upon collision apply resolution (dx,dy) and reduce the health of the player (playerHealth = playerHealth - enemyDamage). The same would be done for world objects like obstacles. If you would like to write the collision yourself, investigate AABB collisions.

This is a super simplified explanation.

If you could provide a .love or a code sample I would LÖVE to help more.
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Enemies and obstacle's

Post by ShadowPenguins »

Ok so just before I post my code I know there will be someone who will be like oh you could've converted it to a .love yeah i know but eh its easier to post the code in my opinion.

Code: Select all

local bump = require "bump"
world = bump.newWorld()
font = love.graphics.setNewFont("Quicksand-Light.ttf")



enemy = {x = 0,
	y = 0,
	width = 32,
	height = 64,
	jump = -350,
	gravity = 500,
	runSpeed = 400,
	xVelocity = 0,
	yVelocity = 0,
	terminalVelocity = 900
	}

player = {
	x = 0,
	y = 0,
	width = 32,
	height = 64,
	jump = -350,
	gravity = 500,
	runSpeed = 400,
	xVelocity = 0,
	yVelocity = 0,
	terminalVelocity = 900
}

function player.setPosition(x, y)
	player.x, player.y = x, 1100
end

function player.update(dt)
	player.move(dt)
	player.applyGravity(dt)
	player.collied(dt)
end

function player.move(dt)
if love.keyboard.isDown("e") then
love.event.quit()
end
if love.keyboard.isDown("r") then  
love.event.quit("restart")
	 end
 if love.keyboard.isDown("q") then  
love.window.setMode( 1800, 900)
end
if love.keyboard.isDown("d") then
	player.xVelocity = player.runSpeed
elseif love.keyboard.isDown("a") then
	player.xVelocity = -player.runSpeed
else
	player.xVelocity = 0
	end
	
	if love.keyboard.isDown("w") then
	
		if player.yVelocity == 0 then
			player.yVelocity = player.jump
			end
			 
	end
end

if player.yVelocity ~= 0 then
		player.y = player.y + player.yVelocity 
		player.yVelocity = player.yVelocity - player.gravity 
	end

function player.applyGravity(dt)
	if player.yVelocity < player.terminalVelocity then
		player.yVelocity = player.yVelocity + player.gravity * dt
	else
		player.yVelocity = player.terminalVelocity
	end
end

function player.collied(dt)
	local futureX = player.x + player.xVelocity * dt
	local futureY = player.y + player.yVelocity * dt
	local nextX, nextY, cols, len = world:move(player, futureX, futureY )
	
	for i = 1, len do
	local col = cols[i]
	if col.normal.y == -1 or col.normal.y == 1 then
		player.yVelocity = 0 
		end
	end
	
	player.x = nextX
	player.y = nextY
end

function player.draw()
	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
	
end

local level = require "level_1"

function loadObjects(level)
local objects = level.layers[1].objects
for i = 1, #objects do
local obj = objects[i]
world:add(obj, obj.x, obj.y, obj.width, obj.height)
	end
end 

function drawObjects(level)
local objects = level.layers[1].objects
love.graphics.setColor(0,20,200)
for i = 1, #objects do
local obj = objects[i]
love.graphics.rectangle("line", obj.x, obj.y, obj.width, obj.height, love.graphics.setColor(0, 20, 200))
    love.graphics.setColor(255, 0, 0, 255)
    love.graphics.print("A is for left, D is for right, W is for jump.", 10, 1230, 200, 2, 2, love.graphics.setColor(255, 0, 0))
	love.graphics.print("E is to quit, Q is to change screen size.", 10, 1150, 200, 2, 2, love.graphics.setColor(255, 0, 0))
	love.graphics.print("R is for reset.", 10, 1050, 200, 2, 2, love.graphics.setColor(255, 0, 0))
	 love.graphics.print("Hello Welcome to my game, I hope you enjoy.", 10, 1250, 0, 2, 2, love.graphics.setColor(0, 255, 0))
	 love.graphics.print("The Objective of the game is get to the end.", 1150, 1040, 0, 2, 2, love.graphics.setColor(150, 150, 150))
	  love.graphics.print("You can climb upside down if you hold Jump.", 1690, 840, 0, 1, 1, love.graphics.setColor(0, 150, 255))
	  love.graphics.print("Careful if the cube monser hits you then you'll have to restart.", 3584.00, 1790.00, 0, 2, 2, love.graphics.setColor(0, 150, 255))
	end
end
--Main

function love.load()
player.img = love.graphics.newImage('purple.png')
	player.setPosition(love.graphics.getWidth()/2, 0)
	world:add(player, player.x, player.y, player.width, player.height)
	loadObjects(level)
	sound = love.audio.newSource("bensound-scifi.mp3")
	love.audio.play(sound)
print(love.system.getPowerInfo())
	
end

function love.update(dt)
	player.update(dt)
end

function love.draw()
	scale = 0.8
  dx = player.x - (love.graphics.getWidth() / 2) / scale
  dy = player.y - (love.graphics.getHeight() / 2) / scale

  love.graphics.scale(scale)
  love.graphics.translate(-dx, -dy)
	player.draw()
	love.graphics.draw(player.img, player.x, player.y, 0, 1, 1, 20, 20, love.graphics.setColor(255,255,255))
	drawObjects(level)
	
  end
im using "bump" for my collisions and tiled for my map and here are some stuff if you don't have bump and wanna play. Don't know how i'd go about doing the obstacles's and enemies
level_1.lua
(5.61 KiB) Downloaded 160 times
bump.lua
(20.96 KiB) Downloaded 145 times
Purple.png
Purple.png (985 Bytes) Viewed 8184 times
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by MadByte »

Killing an actor
There are many ways to "kill" an actor in a game. One would be to set the x and y position outside the visible screen area and reuse the table (and reset x,y) if the actor is alive again. Another and imho better solution would be to add an instance of an actor to a separate "world" table, iterate through all objects and if an actor has been killed, remove the instance from the table. Something like:

Code: Select all

function world.add(object)
  world.objects[#world.objects+1] = object
  return object
 end
  
 
function world.update(dt)
  for i=#world.objects, 1, -1 do
    local object = world.objects[i]
    if object.remove then table.remove(world.objects, i)
    else object.update(dt) end
  end
end
Another advantage would be that you don't have to update every single actor alone. Just update and draw all via world.update and world.draw.

Adding enemies
You could use the world approach I explained above to handle multiply enemies at once. just make sure that you add new instances to the world instead of the same one. Adding instances can also be done in different ways. If you're new to lua, the easiest one would be to do something like this:

Code: Select all

function newEnemy(x, y)
  local enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.remove = false
	
  function enemy:update(dt)
    -- movement, collision stuff, etc..
  end

  function enemy:draw()
    -- draw the enemy
  end
	
  return enemy
end

local myEnemy = world.add(newEnemy(100, 200))
An advantage of this approach is that it's quite easy to understand and apply. But it's not the cleanest and by far not the most efficient one.

Obstacles / Power-ups
Mostly the same stuff done for the enemy can be used on those as well. Those things are just objects the player can collid with, which have different collision resolutions (power-up: collid, remove, add buffs to the player ; obstacles(spikes): collid, kill player).

Life and health
Don't know how to have problems with these. Life isn't more then a variable decreasing if the player get "removed" / killed or added by power-ups. Nothing more like (when killed):

Code: Select all

if player.lives > 0 then player.lives = player.lives-1
else player.isDeadForEverEndTheDamnGameHere = true end  
Same for health.. just a variable decreasing, and if it gets below 0, kill the player, remove a life and re-spawn the player after some time.
I'm sure you get that done on your own.

btw. I think it's kinda rude to expect people to download every single dependency of your project and assemble them on their own.. Just upload a *.love and you'll receive lot more responses and maybe some changed/fixed code. Make sure it executes properly before uploading it.
Good luck.
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by ShadowPenguins »

MadByte wrote: Sat Jan 13, 2018 5:59 pm Killing an actor
There are many ways to "kill" an actor in a game. One would be to set the x and y position outside the visible screen area and reuse the table (and reset x,y) if the actor is alive again. Another and imho better solution would be to add an instance of an actor to a separate "world" table, iterate through all objects and if an actor has been killed, remove the instance from the table. Something like:

Code: Select all

function world.add(object)
  world.objects[#world.objects+1] = object
  return object
 end
  
 
function world.update(dt)
  for i=#world.objects, 1, -1 do
    local object = world.objects[i]
    if object.remove then table.remove(world.objects, i)
    else object.update(dt) end
  end
end
Another advantage would be that you don't have to update every single actor alone. Just update and draw all via world.update and world.draw.

Adding enemies
You could use the world approach I explained above to handle multiply enemies at once. just make sure that you add new instances to the world instead of the same one. Adding instances can also be done in different ways. If you're new to lua, the easiest one would be to do something like this:

Code: Select all

function newEnemy(x, y)
  local enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.remove = false
	
  function enemy:update(dt)
    -- movement, collision stuff, etc..
  end

  function enemy:draw()
    -- draw the enemy
  end
	
  return enemy
end

local myEnemy = world.add(newEnemy(100, 200))
An advantage of this approach is that it's quite easy to understand and apply. But it's not the cleanest and by far not the most efficient one.

Obstacles / Power-ups
Mostly the same stuff done for the enemy can be used on those as well. Those things are just objects the player can collid with, which have different collision resolutions (power-up: collid, remove, add buffs to the player ; obstacles(spikes): collid, kill player).

Life and health
Don't know how to have problems with these. Life isn't more then a variable decreasing if the player get "removed" / killed or added by power-ups. Nothing more like (when killed):

Code: Select all

if player.lives > 0 then player.lives = player.lives-1
else player.isDeadForEverEndTheDamnGameHere = true end  
Same for health.. just a variable decreasing, and if it gets below 0, kill the player, remove a life and re-spawn the player after some time.
I'm sure you get that done on your own.

btw. I think it's kinda rude to expect people to download every single dependency of your project and assemble them on their own.. Just upload a *.love and you'll receive lot more responses and maybe some changed/fixed code. Make sure it executes properly before uploading it.
Good luck.
Yeah but im very new and don't really know how to do a .love I will when i learn how to. Thanks for the help ill try it out. haha i have trouble with life/health because i keep getting errors
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by MadByte »

ShadowPenguins wrote: Sat Jan 13, 2018 6:59 pm Yeah but im very new and don't really know how to do a .love I will when i learn how to.
It's not that difficult.
The wiki in general is a great resource to learn all kinds of stuff about LÖVE.
ShadowPenguins wrote: haha i have trouble with life/health because i keep getting errors
:P
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by ShadowPenguins »

MadByte wrote: Sat Jan 13, 2018 7:05 pm
ShadowPenguins wrote: Sat Jan 13, 2018 6:59 pm Yeah but im very new and don't really know how to do a .love I will when i learn how to.
It's not that difficult.
The wiki in general is a great resource to learn all kinds of stuff about LÖVE.
ShadowPenguins wrote: haha i have trouble with life/health because i keep getting errors
:P
soo every time I do it, It just comes as an error would it be ok to just give a .zip :)
p.s. im doing everything right it just says make sure main.love is at the top it is at the top I swear
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by MadByte »

ShadowPenguins wrote: Sat Jan 13, 2018 7:49 pm soo every time I do it, It just comes as an error would it be ok to just give a .zip :)
p.s. im doing everything right it just says make sure main.love is at the top it is at the top I swear
I suppose you mean "main.lua" ? All you have to do is selecting the files and directories in your project folder (not the folder itself!) and zip them. When done, just rename the .zip to .love. If you're on Windows, you may need to un-hide file extensions.
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by ShadowPenguins »

MadByte wrote: Sat Jan 13, 2018 8:06 pm
ShadowPenguins wrote: Sat Jan 13, 2018 7:49 pm soo every time I do it, It just comes as an error would it be ok to just give a .zip :)
p.s. im doing everything right it just says make sure main.love is at the top it is at the top I swear
I suppose you mean "main.lua" ? All you have to do is selecting the files and directories in your project folder (not the folder itself!) and zip them. When done, just rename the .zip to .love. If you're on Windows, you may need to un-hide file extensions.
I got it working i just learned its very case sensitive :) . I added it up at the top. thank you for showing me how to make the love file
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Enemies, obstacle's, power ups, life's and health bar (help)

Post by ShadowPenguins »

when i tried to add spikes it all got messed up and i don't know what's wrong.
insomnia.love
(3.84 MiB) Downloaded 285 times
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 42 guests