Author Topic: What's wrong with my code???  (Read 4347 times)

0 Members and 1 Guest are viewing this topic.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
What's wrong with my code???
« 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.
« Last Edit: July 13, 2011, 12:29:11 pm by pianoman »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: What's wrong with my code???
« Reply #1 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.
Ndless.me with the finest TI-Nspire programs

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: What's wrong with my code???
« Reply #2 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.
« Last Edit: July 13, 2011, 12:20:46 pm by pianoman »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: What's wrong with my code???
« Reply #3 on: July 13, 2011, 12:21:07 pm »
print() outputs strings to the console on nspire_emu.
« Last Edit: July 13, 2011, 12:21:20 pm by ExtendeD »
Ndless.me with the finest TI-Nspire programs

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: What's wrong with my code???
« Reply #4 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.

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: What's wrong with my code???
« Reply #5 on: July 13, 2011, 01:04:09 pm »
Did you follow this tutorial?
Ndless.me with the finest TI-Nspire programs

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: What's wrong with my code???
« Reply #6 on: July 13, 2011, 01:23:45 pm »
Yup, I even talked to Goplat, followed his instructions, didn't work.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: What's wrong with my code???
« Reply #7 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 :)
« Last Edit: July 13, 2011, 04:12:33 pm by jimbauwens »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: What's wrong with my code???
« Reply #8 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...

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: What's wrong with my code???
« Reply #9 on: July 14, 2011, 03:00:29 am »
I don't think it is for the contest because Pianoman here asks for levels. But we should wait for his answer.
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: What's wrong with my code???
« Reply #10 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
« Last Edit: July 15, 2011, 12:42:23 pm by pianoman »