Page 1 of 1

Game Like Pacman

Posted: Tue Nov 08, 2011 4:28 am
by en_ef
Hy all.
I want to make a game like a pacman. I can't make collision with wall, my hero always stop if collision with wall and can't moving again. But my hero work finely if collision with coin. Any idea?

Re: Game Like Pacman

Posted: Tue Nov 08, 2011 5:47 am
by Taehl
My first guess would be that you're penetrating the wall - you're moving inside it (probably so little you can't see it). And since you're now inside it, your collision detection algorithm is reporting a collision every frame, so you can't move along or back out of it. To fix that, you need to make sure your Pacman is "pushed back out" of the wall when you collide with it.

Re: Game Like Pacman

Posted: Tue Nov 08, 2011 9:05 am
by en_ef
can you give me an example?
can i do it if i don't use "Physics" function?
I just use "graphics" function for make this game.

thanks.

Re: Game Like Pacman

Posted: Tue Nov 08, 2011 12:06 pm
by coffee
Somewhat off-topic. Just for fun and "inspiration" :)
http://www.royal-paw.com/games/netpack/

Re: Game Like Pacman

Posted: Tue Nov 08, 2011 4:24 pm
by Taehl
You have to post your .love before I can tell you how to fix it.

Re: Game Like Pacman

Posted: Tue Nov 08, 2011 8:07 pm
by Jasoco
So many people are trying to make Pac-Man. A game that looks like Pac-Man is easy to make. A game that functions exactly like Pac-Man however, is not as easy.

I'll leave this here for your future use...
http://home.comcast.net/~jpittman2/pacm ... ssier.html

Re: Game Like Pacman

Posted: Wed Nov 09, 2011 3:00 am
by en_ef
this is my .love
JeblehMan.love
(11.46 KiB) Downloaded 373 times
thanks for all.

Re: Game Like Pacman

Posted: Wed Nov 09, 2011 6:49 am
by Taehl
Alright, reviewing your code:

- First off, note that Pacman didn't use free movement, rather, it had a tile-based movement system. A free movement system like yours is fine, though. My primary warning would be to make sure it's easy to move the jebleh through "doorway" formations (and not get stuck on the blok corners)
- Part of your collision oddity is due to you using circular collision (checking only the distance between objects) on a grid-based world. The other part is due to the distance you move the jebleh when you touched a wall. The distance you should move back it is half of the size of jebleh plus half of the size of a blok.
- Here's revised collision code which works much more like what you're looking for. Simply replace what you have with this. It's a little bit odd still, but that's due to things like how you draw your graphics off-center. It's fixable with some number tweaking.

Code: Select all

if love.keyboard.isDown('up') then
	fungsijebleh.Posisi.y = fungsijebleh.Posisi.y - jeblehspeed * dt
	if fungsijebleh.Posisi.y < 25 then
		fungsijebleh.Posisi.y = 25
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.y = b.y + as + bs
		end
	end
end
if love.keyboard.isDown('down') then
	fungsijebleh.Posisi.y = fungsijebleh.Posisi.y + jeblehspeed * dt
	if fungsijebleh.Posisi.y > height - 35 then
		fungsijebleh.Posisi.y = height - 35 
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.y = b.y - as - bs
		end
	end
end	
if love.keyboard.isDown('left') then
	fungsijebleh.Posisi.x = fungsijebleh.Posisi.x - jeblehspeed * dt
	if fungsijebleh.Posisi.x < 25 then
		fungsijebleh.Posisi.x = 25
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.x = b.x + as + bs
		end
	end
end
if love.keyboard.isDown('right') then
	fungsijebleh.Posisi.x = fungsijebleh.Posisi.x + jeblehspeed * dt
	if fungsijebleh.Posisi.x > width - 35 then
		fungsijebleh.Posisi.x = width - 35
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.x = b.x - as - bs
		end
	end
end
PS) Indonesian? Your English is pretty good. Not perfect, but quite understandable.

Re: Game Like Pacman

Posted: Wed Nov 09, 2011 7:47 am
by en_ef
Taehl wrote:Alright, reviewing your code:

- First off, note that Pacman didn't use free movement, rather, it had a tile-based movement system. A free movement system like yours is fine, though. My primary warning would be to make sure it's easy to move the jebleh through "doorway" formations (and not get stuck on the blok corners)
- Part of your collision oddity is due to you using circular collision (checking only the distance between objects) on a grid-based world. The other part is due to the distance you move the jebleh when you touched a wall. The distance you should move back it is half of the size of jebleh plus half of the size of a blok.
- Here's revised collision code which works much more like what you're looking for. Simply replace what you have with this. It's a little bit odd still, but that's due to things like how you draw your graphics off-center. It's fixable with some number tweaking.

Code: Select all

if love.keyboard.isDown('up') then
	fungsijebleh.Posisi.y = fungsijebleh.Posisi.y - jeblehspeed * dt
	if fungsijebleh.Posisi.y < 25 then
		fungsijebleh.Posisi.y = 25
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.y = b.y + as + bs
		end
	end
end
if love.keyboard.isDown('down') then
	fungsijebleh.Posisi.y = fungsijebleh.Posisi.y + jeblehspeed * dt
	if fungsijebleh.Posisi.y > height - 35 then
		fungsijebleh.Posisi.y = height - 35 
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.y = b.y - as - bs
		end
	end
end	
if love.keyboard.isDown('left') then
	fungsijebleh.Posisi.x = fungsijebleh.Posisi.x - jeblehspeed * dt
	if fungsijebleh.Posisi.x < 25 then
		fungsijebleh.Posisi.x = 25
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.x = b.x + as + bs
		end
	end
end
if love.keyboard.isDown('right') then
	fungsijebleh.Posisi.x = fungsijebleh.Posisi.x + jeblehspeed * dt
	if fungsijebleh.Posisi.x > width - 35 then
		fungsijebleh.Posisi.x = width - 35
	end
	for z,i in pairs(fungsiblok) do
		local a,b,as,bs = fungsijebleh.Posisi, i.Posisi, jeblehsize/2, bloksize/2
		if a.x-as < b.x+bs and a.x+as > b.x-bs and a.y-as < b.y+bs and a.y+as > b.y-bs then
			fungsijebleh.Posisi.x = b.x - as - bs
		end
	end
end
PS) Indonesian? Your English is pretty good. Not perfect, but quite understandable.

Hey it's work. thanks Taehl.

Nice to have shared with you.

It's first time i used Lua and Love Emulator, and maybe i love it. LOL

Oh yeah i'm Indonesian. I'm so sorry about my English. :)