Question

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
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Question

Post by kikito »

You must transform your main.lua a bit; this is the simplest thing you can do:

Code: Select all

 -- main.lua
require('pacman')

function love.load()
  loadPacman()
end

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

function love.draw()
  drawPacman()
end

And, inside pacman.lua, you only define pacman stuff; just rename the love.load, love.update and love.draw functions there with loadPacman, updatePacman & drawPacman

Code: Select all

 -- pacman.lua
function loadPacman()
  ... -- same as before
end

function updatePacman(dt)
  ... -- same as before
end

function drawPacman()
  ... -- same as before
end
Once you have this, you can more or less easily extend it; you can have a ghosts.lua file with loadGhosts, updateGhosts and drawGhosts functions, and add them to main.lua, right after loadPacman, updatePacman and drawPacman.
When I write def I mean function.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post by Acnesium »

Jasoco wrote:Of course! Don't put multiple love.***() functions. You're going about it wrong. Instead, put the love.***() functions in main.lua and call the other individual update, draw, load functions with different names, from the originals.
Ah! now i get it, Thank you very much!
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post by Acnesium »

Hello,

I am in kinda trouble. I want to create a menu, map, enemies and I want to have pacman move to the right automatically when I press Right.

However this is kinda of a big deal to ask to you guys, so do you guys know some sort of site or tutorials where I could find this kind of stuff. I already looked through the wiki and searched around the forums (still doing) but I don't seem to be finding anything.

Thanks anyway!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question

Post by Robin »

You'll need to use the callback love.keypressed or the function love.keyboard.isDown for things like that.

Example usage:

Code: Select all

function love.keypressed(key)
    if key == 'right' then
        --move pacman right
    end
end

Code: Select all

function love.update(dt)
    if love.keyboard.isDown('right') then
        --move pacman right
    end
end
Help us help you: attach a .love.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post by Acnesium »

Robin wrote: SNIP
Yes, but I can't seem to figure out how I get the original pacman movement like this one: http://www.pacman.nl/pacman.html.

This is what I got at the moment:

Code: Select all

function playerLoad()
	playerUp = love.graphics.newImage("images/pacmanUp.png")
	playerDown = love.graphics.newImage("images/pacmanDown.png")
	playerRight = love.graphics.newImage("images/pacmanRight.png")
	playerLeft = love.graphics.newImage("images/pacmanLeft.png")
	
	player = {
	animUp = newAnimation(playerUp, 32, 32, 0.1, 2);
	animDown = newAnimation(playerDown, 32, 32, 0.1, 2);
	animRight = newAnimation(playerRight, 32, 32, 0.1, 2);
	animLeft = newAnimation(playerLeft, 32, 32, 0.1, 2);
	
	x = 256;
	y = 256;
	s = 100;
	}
	
	playerAnim = player.animUp
	dt = love.timer.getDelta()
	
end

function goUp(dt)
	if player.y >= 0 then
		player.y = player.y - (player.s * dt)
		playerAnim = player.animUp
		playerAnim:update(dt)
	end
end

function playerUpdate(dt)
	if love.keyboard.isDown("up") then
		goUp(dt)
	end
end

function playerDraw()
	playerAnim:draw(player.x, player.y)
end
Thanks.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question

Post by Robin »

The way you do that is you remember the direction pacman is going (that is, put it in a variable).

You change that variable only on a keypress (and when there is a collision).
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Question

Post by Jasoco »

Pac-Man is complicated, but easy to figure out. I has been completely documented in how it works in the Pac-Man Dossier.

I used that page to create my own clone of Pac-Man that works about 90% like the original complete with ghost AI. You can have this code to check out. Mind it's a few months old and I've learned new Lua tricks since then so it may not look optimized. In fact I haven't even looked at the code in months so...
Pac-Man.love
wokka wokka wokka wokka
(7.32 KiB) Downloaded 118 times
The main things I didn't implement are collisions to kill, levels, score and "corner cutting". (You'll read about it in the Dossier. It's very integral to mastering the game. If you can't get cutting in, you fail! FAIL!! MUAHAHAHAHA!!!!! Fail.) And because of that, I do not suggest you use this code at all. Rather learn SOME things from it. It's buggy and not accurate 100%. Only 90%. Also, this was before I learned how to use framebuffers to scale the graphics so sorry for the 1:1 scale of pixels. Man, CRT's were low resolution back then.

READ THE DOSSIER!

Edit: Looking back, it's about 10 months old. Man, where did this year go? I made this in December! I had recently broken up with my girlfriend and was feeling good so I was able to concentrate.
Acnesium
Prole
Posts: 8
Joined: Sun Oct 16, 2011 12:17 pm

Re: Question

Post by Acnesium »

Jasoco wrote:--SNIP--
Thats awesome! I will definitely use this. Thanks you!
User avatar
McClane
Prole
Posts: 2
Joined: Thu Nov 24, 2011 8:39 pm
Location: Brazil

Re: Question

Post by McClane »

I had a problem with the require command but people at #love helped me out :3
The problem was that the name of the "file.lua" can't be upper case.

Thanks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 1 guest