Advertising (This ad goes away for registered users. You can Login or Register)

[LUA] Need help with code plz

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Joel16
Posts: 914
Joined: Wed Oct 12, 2011 8:47 pm

Re: [LUA] Need help with code plz

Post by Joel16 »

dragonxtamer596 wrote:I've been busy and not working on my game lately but I came back and I forgot how to make a not statement work.
My goal is to make the character go in only one direction at a time. It worked to stop diagonal in the direction pushed but if a player does try example left and down he will go up right. What is going on here?
This is what I tried \/

Code: Select all

     if controls.left() ~= (controls.up() or controls.down())  then
       Player.x = Player.x - 1.25
     end
     if controls.right() ~= (controls.up() or controls.down()) then
       Player.x = Player.x + 1.25
     end
     if controls.up() ~= (controls.left() or controls.right()) then
       Player.y = Player.y - 1.25
     end
     if controls.down() ~= (controls.left() or controls.right()) then
       Player.y = Player.y + 1.25
     end
Can you tell me more in detail about your 'goal'. I don't think I understand what you're trying to say
Advertising
"Forever in darkness, a guardian devil."
pancakes4ever
Posts: 56
Joined: Sat Jul 20, 2013 12:42 am

Re: [LUA] Need help with code plz

Post by pancakes4ever »

dragonxtamer596 wrote:I've been busy and not working on my game lately but I came back and I forgot how to make a not statement work.
My goal is to make the character go in only one direction at a time. It worked to stop diagonal in the direction pushed but if a player does try example left and down he will go up right. What is going on here?
This is what I tried \/

Code: Select all

     if controls.left() ~= (controls.up() or controls.down())  then
       Player.x = Player.x - 1.25
     end
     if controls.right() ~= (controls.up() or controls.down()) then
       Player.x = Player.x + 1.25
     end
     if controls.up() ~= (controls.left() or controls.right()) then
       Player.y = Player.y - 1.25
     end
     if controls.down() ~= (controls.left() or controls.right()) then
       Player.y = Player.y + 1.25
     end
Not too familiar with lua but this looks like its partly a logic error. I would say if "controls.up()" does what i think it does, player.y should equal itself + 1.25. Also wouldn't it make more logical sense to state for example:

if controls.left() and (controls.up() or controls.down()) then
Player.x = Player.x - 1.25
end
Advertising
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

Ty guys for the help but I fixed the issue.

Code: Select all

 if controls.left() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() then
       Player.x = Player.x - 1.25
       direction = 1
     end
   
 if controls.right() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() then
       Player.x = Player.x + 1.25 
       direction = 2
     end

 if controls.up() and controls.right() then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 3

   elseif controls.up() and controls.left() then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 3

   elseif controls.up() then
       Player.y = Player.y - 1.25
       direction = 3
     end

 if controls.down() and controls.right() then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 4

   elseif controls.down() and controls.left() then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 4

   elseif controls.down() then
       Player.y = Player.y + 1.25
       direction = 4
     end
I'm having an issue with gif images now.

Code: Select all

playerl=image.load("Images/leftidle.gif")
playerr=image.load("Images/rightidle.gif")
playeru=image.load("Images/upidle.gif")
playerd=image.load("Images/downidle.gif")
playerwl=image.load("Images/left.gif")
playerwr=image.load("Images/right.gif")
playerwu=image.load("Images/up.gif")
playerwd=image.load("Images/down.gif")
Some code

Code: Select all

 if controls.left() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() then
       Player.x = Player.x - 1.25
       direction = 1
     end
   
 if controls.right() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() then
       Player.x = Player.x + 1.25 
       direction = 2
     end

 if controls.up() and controls.right() then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 3

   elseif controls.up() and controls.left() then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 3

   elseif controls.up() then
       Player.y = Player.y - 1.25
       direction = 3
     end

 if controls.down() and controls.right() then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 4

   elseif controls.down() and controls.left() then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 4

   elseif controls.down() then
       Player.y = Player.y + 1.25
       direction = 4
     end

some code

while true do

some code

Code: Select all

if direction == 1 then 
 if not controls.left() then
   playerl.blit(Player.x,Player.y,playerl)
 elseif controls.left() then
   playerwl.blit(Player.x,Player.y,playerwl)
end 
end 

if direction == 2 then 
 if not controls.right() then
   playerr.blit(Player.x,Player.y,playerr)
 elseif controls.right() then
   playerwr.blit(Player.x,Player.y,playerwr)
end
end 

if direction == 3 then 
 if not controls.up() then
   playeru.blit(Player.x,Player.y,playeru)
 elseif controls.up() then
   playerwu.blit(Player.x,Player.y,playerwu)
end
end 

if direction == 4 then 
 if not controls.down() then
   playerd.blit(Player.x,Player.y,playerd)
 elseif controls.down() then
   playerwd.blit(Player.x,Player.y,playerwd)
end
end 
Whole thing if you need \/

[spoiler]

Code: Select all

green = color.new(0,128,0)
white = color.new(255,255,255)
brown = color.new(156,90,60)
black = color.new(0,0,0)
darkg = color.new(70,70,70)
lightg = color.new(180,180,180)
red = color.new(255,0,0)
blue = color.new(0,0,255)

if not direction then
direction = 1
end

if not text1 then
text1 = 0
end
if not dagger1 then
dagger1 = 1
end
if not axe1 then
axe1 = 0
end
if not broadsword1 then
broadsword1 = 0
end
if not elucidator1 then
elucidator1 = 0
end
if not morningstar1 then
morningstar1 = 0
end
if not spear1 then
spear1 = 0
end
if not scimitar1 then
scimitar1 = 0
end
if not kopesh1 then
kopesh1 = 0
end
if not cloth1 then
cloth1 = 1
end
if not leather1 then
leather1 = 0
end
if not mail1 then
mail1 = 0
end
if not black1 then
black1 = 0
end
if not steel1 then
steel1 = 0
end
if not holy1 then
holy1 = 0
end
if not weapon then
weapon = "dagger"
end
if not armor then
armor = "cloth"
end

if not Level then
Level = 1
end
if not Experience then
Experience = 0
end
if not attributes then
attributes = 0
end
if not nextlevel then
nextlevel = 8
end
if not mhealth then
mhealth = 0
end
if not mmana then
mmana = 0
end
if not damage1 then
damage1 = 0
end
if not defence1 then
defence1 = 0
end
if not spells1 then
spells1 = 0
end

if not myCash then
myCash = 0
end
if not tarea then
tarea = 1
end
if not area then
area = 1
end

if not hpotioncost then
hpotioncost = 50
end
if not mpotioncost then
mpotioncost = 20
end
if not hpotionsell then
hpotionsell = 25
end
if not mpotionsell then
mpotionsell = 10
end

if not axebuy then
axebuy = 1500
end
if not broadswordbuy then
broadswordbuy = 3000
end
if not morningstarbuy then
morningstarbuy = 2250
end
if not spearbuy then
spearbuy = 1250
end
if not scimitarbuy then
scimitarbuy = 2500
end
if not kopeshbuy then
kopeshbuy = 500
end
if not leatherbuy then
leatherbuy = 1000
end
if not mailbuy then
mailbuy = 1500
end
if not blackbuy then
blackbuy = 1800
end
if not steelbuy then
steelbuy = 2550
end
if not holybuy then
holybuy = 6000
end

if not daggersell then
daggersell = 25
end
if not axesell then
axesell = 375
end
if not broadswordsell then
broadswordsell = 750
end
if not morningstarsell then
morningstarsell = 550
end
if not spearsell then
spearsell = 300
end
if not scimitarsell then
scimitarsell = 625
end
if not kopeshsell then
kopeshsell = 125
end
if not clothsell then
clothsell = 125
end
if not leathersell then
leathersell = 250
end
if not mailsell then
mailsell = 375
end
if not blacksell then
blacksell = 450
end
if not steelsell then
steelsell = 625
end
if not holysell then
holysell = 1500
end

if not hpotions then
hpotions = 0
end
if not mpotions then
mpotions = 0
end

if not healthbar then
healthbar = 10
end
if not manabar then
manabar = 10
end

if not hattributes then
hattributes = 0
end
if not mattributes then
mattributes = 0
end
if not health then
health = 100 + mhealth
end
if not mana then
mana = 100 + mmana
end

if not quest then
quest = 0
end

mxhealth = 100 + mhealth

mxmana = 100 + mmana

if not spells2 then
spells2 = 0
end

playerl=image.load("Images/leftidle.gif")
playerr=image.load("Images/rightidle.gif")
playeru=image.load("Images/upidle.gif")
playerd=image.load("Images/downidle.gif")
playerwl=image.load("Images/left.gif")
playerwr=image.load("Images/right.gif")
playerwu=image.load("Images/up.gif")
playerwd=image.load("Images/down.gif")
 
background = image.create(480,272,green)

block1 = image.create(60,18,brown)

block2 = image.create(480,1,black)

block3 = image.create(1,272,black)

block4 = image.create(133,64,darkg)

block6 = image.create(60,64,darkg)

block5 = image.create(27,7,lightg)

block7 = image.create(27,7,lightg)

block8 = image.create(27,7,lightg)

block9 = image.create(27,7,lightg)

block10 = image.create(27,7,lightg)

Player = { x = 220 , y = 116}

playerHeight = 15
playerWidth = 15

function checkcontrols()
controls.read()
  
if controls.press("r") then 
screenpic("ms0:/PSP/PHOTO")
end

   

 if controls.left() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 1

   elseif controls.left() then
       Player.x = Player.x - 1.25
       direction = 1
     end
   
 if controls.right() and controls.down()  then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() and controls.up()  then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 2

   elseif controls.right() then
       Player.x = Player.x + 1.25 
       direction = 2
     end

 if controls.up() and controls.right() then
       Player.y = Player.y - 1.25
       Player.x = Player.x - 1.25
       direction = 3

   elseif controls.up() and controls.left() then
       Player.y = Player.y - 1.25
       Player.x = Player.x + 1.25
       direction = 3

   elseif controls.up() then
       Player.y = Player.y - 1.25
       direction = 3
     end

 if controls.down() and controls.right() then
       Player.y = Player.y + 1.25
       Player.x = Player.x - 1.25
       direction = 4

   elseif controls.down() and controls.left() then
       Player.y = Player.y + 1.25
       Player.x = Player.x + 1.25
       direction = 4

   elseif controls.down() then
       Player.y = Player.y + 1.25
       direction = 4
     end

   if controls.press("start") then
       dofile("ingamemenu.lua")
     end
   if controls.press("select") then
       dofile("attributes.lua")
     end
end

Block1 = {}
Block1[1] = { x = 210, y = 0, height = block1:height(), width = block1:width() }

Block2 = {}
Block2[1] = { x = 0, y = 0, height = block2:height(), width = block2:width() }
Block2[2] = { x = 0, y = 271, height = block2:height(), width = block2:width() }

Block3 = {}
Block3[1] = { x = 0, y = 0, height = block3:height(), width = block3:width() }
Block3[2] = { x = 479, y = 0, height = block3:height(), width = block3:width() }

Block4 = {}
Block4[1] = { x = 45, y = 15, height = block4:height(), width = block4:width() }
Block4[2] = { x = 300, y = 15, height = block4:height(), width = block4:width() }
Block4[3] = { x = 45, y = 195, height = block4:height(), width = block4:width() }

Block6 = {}
Block6[1] = { x = 290, y = 195, height = block6:height(), width = block6:width() }
Block6[2] = { x = 370, y = 195, height = block6:height(), width = block6:width() }

Block5 = {}
Block5[1] = { x = 90, y = 79, height = block5:height(), width = block5:width() }

Block7 = {}
Block7[1] = { x = 355, y = 79, height = block7:height(), width = block7:width() }

Block8 = {}
Block8[1] = { x = 90, y = 188, height = block8:height(), width = block8:width() }

Block9 = {}
Block9[1] = { x = 307, y = 188, height = block9:height(), width = block9:width() }

Block10 = {}
Block10[1] = { x = 387, y = 188, height = block10:height(), width = block10:width() }


function collisionCheck1(block1)
   if (Player.x + playerWidth > block1.x) and (Player.x < block1.x + block1.width) and (Player.y + playerHeight > block1.y) and (Player.y < block1.y + block1.height) and (controls.press("cross")) then
  area = area + 1 
  dofile("enviroment.lua")
  end
end


function collisionCheck2(block2)
   if (Player.x + playerWidth > block2.x) and (Player.x < block2.x + block2.width) and (Player.y + playerHeight > block2.y) and (Player.y < block2.y + block2.height) then
    Player.x = oldx
    Player.y = oldy
  end
end


function collisionCheck3(block3)
   if (Player.x + playerWidth > block3.x) and (Player.x < block3.x + block3.width) and (Player.y + playerHeight > block3.y) and (Player.y < block3.y + block3.height) then
    Player.x = oldx
    Player.y = oldy
  end
end


function collisionCheck4(block4)
   if (Player.x + playerWidth > block4.x) and (Player.x < block4.x + block4.width) and (Player.y + playerHeight > block4.y) and (Player.y < block4.y + block4.height) then
    Player.x = oldx
    Player.y = oldy
  end
end

function collisionCheck6(block6)
   if (Player.x + playerWidth > block6.x) and (Player.x < block6.x + block6.width) and (Player.y + playerHeight > block6.y) and (Player.y < block6.y + block6.height) then
    Player.x = oldx
    Player.y = oldy
  end
end

function collisionCheck5(block5)
   if (Player.x + playerWidth > block5.x) and (Player.x < block5.x + block5.width) and (Player.y + playerHeight > block5.y) and (Player.y < block5.y + block5.height) and (controls.press("cross")) then
     dofile("shop.lua")
  end
end

function collisionCheck7(block7)
   if (Player.x + playerWidth > block7.x) and (Player.x < block7.x + block7.width) and (Player.y + playerHeight > block7.y) and (Player.y < block7.y + block7.height) and (controls.press("cross")) then
     dofile("shop1.lua")
  end
end

function collisionCheck8(block8)
   if (Player.x + playerWidth > block8.x) and (Player.x < block8.x + block8.width) and (Player.y + playerHeight > block8.y) and (Player.y < block8.y + block8.height) and (controls.press("cross")) then
     dofile("shop2.lua")
  end
end

function collisionCheck9(block5)
   if (Player.x + playerWidth > block5.x) and (Player.x < block5.x + block5.width) and (Player.y + playerHeight > block5.y) and (Player.y < block5.y + block5.height) then
   screen.print(5,260,"Item Shop",black)  
  end
end

function collisionCheck10(block7)
   if (Player.x + playerWidth > block7.x) and (Player.x < block7.x + block7.width) and (Player.y + playerHeight > block7.y) and (Player.y < block7.y + block7.height) then
   screen.print(5,260,"Weapon Shop",black)     
  end
end

function collisionCheck11(block8)
   if (Player.x + playerWidth > block8.x) and (Player.x < block8.x + block8.width) and (Player.y + playerHeight > block8.y) and (Player.y < block8.y + block8.height) then
   screen.print(5,260,"Armor Shop",black)     
  end
end

function collisionCheck12(block9)
   if (Player.x + playerWidth > block9.x) and (Player.x < block9.x + block9.width) and (Player.y + playerHeight > block9.y) and (Player.y < block9.y + block9.height) and (controls.press("cross")) then
     dofile("house.lua")     
  end
end

function collisionCheck13(block10)
   if (Player.x + playerWidth > block10.x) and (Player.x < block10.x + block10.width) and (Player.y + playerHeight > block10.y) and (Player.y < block10.y + block10.height) then
  screen.print(5,260,name.."'s home",black)
     if (controls.press("cross")) then 
     dofile("house1.lua")     
  end
end
end

while true do
 
   oldx = Player.x
   oldy = Player.y
   screen.clear()

   checkcontrols()

   collisionCheck1(Block1[1])

   collisionCheck2(Block2[1])
   collisionCheck2(Block2[2])

   collisionCheck3(Block3[1])
   collisionCheck3(Block3[2])

   collisionCheck4(Block4[1])
   collisionCheck4(Block4[2])
   collisionCheck4(Block4[3])

   collisionCheck5(Block5[1])

   collisionCheck6(Block6[1])
   collisionCheck6(Block6[2])

   collisionCheck7(Block7[1])

   collisionCheck8(Block8[1])

   collisionCheck12(Block9[1])

   collisionCheck13(Block10[1])

   background:blit(0,0)

   for a = 1,1 do
     block1:blit(Block1[a].x,Block1[a].y)
   end

   for a = 1,2 do
     block2:blit(Block2[a].x,Block2[a].y)
   end

   for a = 1,2 do
     block3:blit(Block3[a].x,Block3[a].y)
   end

   for a = 1,3 do
     block4:blit(Block4[a].x,Block4[a].y)
   end

   for a = 1,1 do
     block5:blit(Block5[a].x,Block5[a].y)
   end

   for a = 1,2 do
     block6:blit(Block6[a].x,Block6[a].y)
   end

   for a = 1,1 do
     block7:blit(Block7[a].x,Block7[a].y)
   end
   
   for a = 1,1 do
     block8:blit(Block8[a].x,Block8[a].y)
   end

   for a = 1,1 do
     block9:blit(Block9[a].x,Block9[a].y)
   end

   for a = 1,1 do
     block10:blit(Block10[a].x,Block10[a].y)
   end

   draw.fillrect(10,10,health/4,healthbar,red)

   screen.print(10,30,health,white)

   draw.fillrect(370,10,mana/4,manabar,blue)

   screen.print(370,30,mana,white)

   screen.print(Player.x-8,Player.y-10,name,.6,black,color.new(0,0,0,0))
  
if direction == 1 then 
 if not controls.left() then
   playerl.blit(Player.x,Player.y,playerl)
 elseif controls.left() then
   playerwl.blit(Player.x,Player.y,playerwl)
end 
end 

if direction == 2 then 
 if not controls.right() then
   playerr.blit(Player.x,Player.y,playerr)
 elseif controls.right() then
   playerwr.blit(Player.x,Player.y,playerwr)
end
end 

if direction == 3 then 
 if not controls.up() then
   playeru.blit(Player.x,Player.y,playeru)
 elseif controls.up() then
   playerwu.blit(Player.x,Player.y,playerwu)
end
end 

if direction == 4 then 
 if not controls.down() then
   playerd.blit(Player.x,Player.y,playerd)
 elseif controls.down() then
   playerwd.blit(Player.x,Player.y,playerwd)
end
end 

   collisionCheck9(Block5[1])

   collisionCheck10(Block7[1])

   collisionCheck11(Block8[1])

   screen.waitvblankstart()
   screen.flip()

   end
[/spoiler]
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

This is the issue I'm having they are staying in one place and not moving.Example of what i mean first is what I'm getting second is what I want.
Attachments
1-1.png
1-1.png (276 Bytes) Viewed 2549 times
left.gif
left.gif (1.16 KiB) Viewed 2549 times
Locked

Return to “Programming and Security”