Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: pianoman on July 13, 2011, 12:04:11 pm

Title: What's wrong with my code???
Post by: pianoman on July 13, 2011, 12:04:11 pm
Hi, everyone!
I've been working on a Pacman game for the nspire in Lua, but I've encountered a major problem, and I have no idea what's wrong. :banghead:
The problem: I made a function wallcheck() to check if Pacman ran into a wall, which stops him from moving. It works fine when the script is first created, but as soon as I use an arrow, it completely ignores the wallcheck() function an proceeds to run through walls.
I've tried just about everything, to no avail. :mad:
Please help!
Here is the code for my game thus far.
Code: (Pacman Lua) [Select]
function on.enterKey()
maze={{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}}
leveldata={{4,4,1,50,50,50,6}}
mazewidth=leveldata[lev][1]
mazeheight=leveldata[lev][2]
move=leveldata[lev][3]
blockwidth=leveldata[lev][4]
pacx=leveldata[lev][5]
pacy=leveldata[lev][6]
gm=leveldata[lev][7]
dir=1
mouth=1
m=1
timer.start(.01)
end

function on.tabKey()
lev=1
on.enterKey()
end

function dirx()
if dir==1 then
return move
elseif dir==2 or dir==4 then
return 0
else
return 0-move
end
end

function diry()
if dir==4 then
return move
elseif dir==1 or dir==3 then
return 0
else
return 0-move
end
end

function wallcheck()
if dir==1 then
if maze[lev][gm+1]==0 then
return false
else
return true
end
elseif dir==2 then
if maze[lev][gm-mazewidth]==0 then
return false
else
return true
end
elseif dir==3 then
if maze[lev][gm-1]==0 then
return false
else
return true
end
elseif dir==4 then
if maze[lev][gm+mazewidth]==0 then
return false
else
return true
end
end
end

function gmchange()
if dir==1 then
gmchan=1
elseif dir==2 then
gmchan=0-mazewidth
elseif dir==3 then
gmchan=0-1
elseif dir==4 then
gmchan=mazewidth
end
end

on.tabKey()

function on.paint(gc)
for a=1, mazeheight do
for b=1, mazewidth do
local n=maze[lev][mazewidth*(a-1)+b]
if n==0 then
gc:fillRect(blockwidth*(b-1),blockwidth*(a-1),blockwidth,blockwidth)
end
end
end
gc:fillArc(pacx,pacy,blockwidth,blockwidth,45,315+45*mouth)
end

function on.timer()
if m~=blockwidth then
pacx=pacx+dirx()
pacy=pacy+diry()
m=m+1
elseif m==blockwidth then
gmchange()
gm=gm+gmchan
if wdir~=0 then
dir=wdir
wdir=0
end
if wallcheck() then
pacx=pacx+dirx()
pacy=pacy+diry()
m=1
end
end
mouth=0-mouth
platform.window:invalidate()
end

function on.arrowKey(key)
if key=="left" then
if m~=blockwidth then
wdir=3
else
dir=3
end
elseif key=="right" then
if m~=blockwidth then
wdir=1
else
dir=1
end
elseif key=="up" then
if m~=blockwidth then
wdir=2
else
dir=2
end
elseif key=="down" then
if m~=blockwidth then
wdir=4
else
dir=4
end
end
platform.window:invalidate()
end
EDIT: Removed things that do not immediately pertain to the problem.
Title: Re: What's wrong with my code???
Post by: ExtendeD on July 13, 2011, 12:14:14 pm
First try to locate the problem by removing the code which doesn't contribue to it.
Use a set of calls to print() to identify precisely what's wrong with the program flow. Tell us what you expected to be printed, and what is actually printed.
You will probably get more help with this.
Title: Re: What's wrong with my code???
Post by: pianoman on July 13, 2011, 12:19:38 pm
First try to locate the problem by removing the code which doesn't contribue to it.
Use a set of calls to print() to identify precisely what's wrong with the program flow. Tell us what you expected to be printed, and what is actually printed.
You will probably get more help with this.
Print? This is for the nspire...
I'll try removing the unnecessary stuff, but at this point, there isn't much of that.
Title: Re: What's wrong with my code???
Post by: ExtendeD on July 13, 2011, 12:21:07 pm
print() outputs strings to the console on nspire_emu.
Title: Re: What's wrong with my code???
Post by: pianoman on July 13, 2011, 12:23:31 pm
print() outputs strings to the console on nspire_emu.
That's a problem... I can't get nspire_emu to work on my computer w/ OS 3.0.1.
It just gives me the screen it gave the bricked calcs.
Title: Re: What's wrong with my code???
Post by: ExtendeD on July 13, 2011, 01:04:09 pm
Did you follow this tutorial (http://hackspire.unsads.com/wiki/index.php/Emulators#nspire_emu)?
Title: Re: What's wrong with my code???
Post by: pianoman on July 13, 2011, 01:23:45 pm
Yup, I even talked to Goplat, followed his instructions, didn't work.
Title: Re: What's wrong with my code???
Post by: Jim Bauwens on July 13, 2011, 04:12:08 pm
First of all your code is over complicated, you are making it much more hard than you should.
I would suggest to try to make it much more simple. I know this might be hard, but it really should happen, and will help you when you are in a further stage. If you need any other help, just ask :)
Title: Re: What's wrong with my code???
Post by: Munchor on July 13, 2011, 04:13:45 pm
PianoMan, if the PacMan is for the Contest, I'm not sure if you can share the whole code O.O And you just did...
Title: Re: What's wrong with my code???
Post by: Hayleia on July 14, 2011, 03:00:29 am
I don't think it is for the contest because Pianoman here (http://ourl.ca/12018/227368;topicseen#new) asks for levels. But we should wait for his answer.
Title: Re: What's wrong with my code???
Post by: pianoman on July 15, 2011, 12:42:16 pm
Sorry for the delay in the reply.
Jim, thanks for the advice, I'm working on doing that. :)
No, this isn't for the contest. I'm working on an RPG for the contest, although I may have to switch to a puzzle game... I'm running out of plot ideas. :P