Author Topic: Lua Q&A  (Read 94208 times)

0 Members and 1 Guest are viewing this topic.

Offline LDStudios

  • Coder Of Tomorrow
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 388
  • Rating: +41/-1
    • View Profile
    • LD Studios
Re: Lua Q&A
« Reply #195 on: February 15, 2014, 11:18:32 am »
Yes I'm guessing that trigonometry is essential if I want to learn to program games. I learned basic trig in Geometry, how can I apply that to programming? It's something I've been wondering for a very long time, as i want to program my first game in lua, on the nspire.
EDIT:
Not actually ON the nspire, just for it.

Trig isnt really essential, unless you are going to work with rotating around a fixed point, or I guess if you are making some sort of 3D environment like DJ Omnimaga said



Offline njaddison

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 176
  • Rating: +24/-27
    • View Profile
Re: Lua Q&A
« Reply #196 on: February 15, 2014, 09:24:12 pm »
OK, so I'm programming my first game in Nspire Lua, and I have a lot of questions, but first let me deal with problems I've had so far.
The game is a simple pong clone. Here is the code so far:
Code: [Select]
function on.paint(gc)
    local screen = platform.window
    local h=screen:height()
    local w=screen:width()
    gc:setFont("sansserif", "b", 24)
    gc:setColorRGB(255,193,37)
    local sw = gc:getStringWidth("Pong")
    local sh = gc:getStringHeight("Pong")
    gc:drawString("Pong",w/2-sw/2,h/16+sh/16)
    gc:setFont("sansserif", "r", 12)
    gc:setColorRGB(255,255,255)
    local s2w = gc:getStringWidth("Simple Pong clone by njaddison")
    local s2h = gc:getStringHeight("Simple Pong clone by njaddison")
    gc:drawString("Simple Pong clone by njaddison",w/2-s2w/2,h/2+s2h/2)
end

function on.enterKey()
    screen:invalidate()
end
So far I've finished the title screen (subject to change) and that's pretty much it. I'm trying to make it so once you press the escape key after the game starts (game starts by pressing enter key), it will restart the code, or at least recall the title screen function. I think this is possible with
Code: [Select]
while true do loop, but I kept running into errors, and I don't even know if I can recall a function that is in a loop with a function that is outside of one.
Also, how would I go about making the title screen background black? I want it to look a bit like the original pong, and also, you can't see the "Simple pong clone by njaddison" if the screen is white because the text is white.

EDIT:
Also, an animation of pong in the background of the title screen would be nice, like with the lua clone of Cubefield.
« Last Edit: February 15, 2014, 09:26:27 pm by njaddison »
<a href="http://www.nerdtests.com/ft_nt2.php">
<img src="http://www.nerdtests.com/images/badge/nt2/5f42ec78e054645d.png" alt="NerdTests.com says I'm a Highly Dorky Nerd God.  Click here to take the Nerd Test, get geeky images and jokes, and talk to others on the nerd forum!">
</a>


Offline LDStudios

  • Coder Of Tomorrow
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 388
  • Rating: +41/-1
    • View Profile
    • LD Studios
Re: Lua Q&A
« Reply #197 on: February 15, 2014, 09:44:07 pm »
Well, I'm not really an expert, but here are some things:
You can use on.activate() so that every time u open it up it returns to the title screen. In on.activate, i would call a function to reset it to the title screen, and you can call the same function in your on.escapeKey() function.

To make the screen black:

gc:setColorRGB(0,0,0)
gc:fillRect(0,0,w,h)



Offline LD_Chimpman

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +6/-0
    • View Profile
Mirroring Images?
« Reply #198 on: March 20, 2014, 10:43:47 pm »
Hi, I wanted to know how to go about mirroring an image locally in lua. I have a bunch of images for character animations and I need copies of them mirrored horizontally but instead of inserting them in as seperate mirrored images I'd like to make mirrored copies of them within my code.

The first thing I tried was using image:copy() to copy them and make the width negative, however that gave an error since lua requires the width value to be >0.

Next I studied the way the image strings are made and figured out how I could potentially switch certain parts of an image's string to mirror it. However, I tried to make a function that would manipulate parts of an image's string and got errors, because the foreward slash that is used in the strings doesn't function like a normal string value, and it overally screws up everything you try to do with it (I tried using "\\" too, but then it didn't think that "\" in the image string = "\\").

So before I go crazy searching further for a really complex way to manipulate image strings, is there a simpler way to mirror images in lua?
Please help!  ???
PROJECTS:
Slime Volleyball: 10% Complete
nFinity: 55% Complete
nCoder: 100% Complete
Jumping Squares 2: 20% Complete
Cloning Dolly: 100% Complete

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #199 on: March 26, 2014, 06:30:57 pm »
I'm not so sure about mirroring images within the Lua code itself, but if you have the original image (.gif, .png etc) you could mirror it and convert it to image code.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline LD_Chimpman

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +6/-0
    • View Profile
Re: Lua Q&A
« Reply #200 on: March 26, 2014, 09:26:58 pm »

I'm not so sure about mirroring images within the Lua code itself, but if you have the original image (.gif, .png etc) you could mirror it and convert it to image code.

I'm hoping that I won't have to do that. I have quite a few images because they are for animations, and duplicating every one of those images to have a mirrored version of it would almost double the current file size. I know it has to be possible to do it locally since nspaint can modify and output image code, I'm just not quite sure how.
PROJECTS:
Slime Volleyball: 10% Complete
nFinity: 55% Complete
nCoder: 100% Complete
Jumping Squares 2: 20% Complete
Cloning Dolly: 100% Complete

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #201 on: March 27, 2014, 06:08:20 pm »
Take a look at http://wiki.inspired-lua.org/TI.Image. At the moment I don't have time to write a routine, but if you spend some time reading how the format works you should be able to do it. :)


Edit:
Whatever:
Code: [Select]

function mirror_img(imgstr, width , height)
    local out = imgstr:sub(1, 20)
    local p = 21
    for y=1, height do
        local row = ""
        for x=1, width do
        row =  imgstr:sub(p, p +1) .. row
        p = p + 2
    end
    out = out .. row
  end


  return out
end


img_hi_src = "\32\0\0\0\32\0\0\0\0\0\0\0\64\0\0\0\16\0\1\000alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalal\194\253alalalalal\194\253alalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalal\194\253alalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalal\194\253alalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalalal\194\253\194\253\194\253\194\253\194\253\194\253\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalal\194\253\194\253\194\253\194\253\194\253\194\253alal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalal\194\253\194\253\194\253alal\194\253\194\253\194\253\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253a\144alalalalal\194\253alalalalalal\194\253alalalalalalalalalalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalalalal\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253\194\253\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal"


img_hi = image.new(img_hi_src)
img_hi_reverted=image.new(mirror_img(img_hi_src, 32, 32))


function on.paint(gc)
    gc:drawImage(img_hi, 10, 10)
    gc:drawImage(img_hi_reverted, 10, 100)
end
« Last Edit: March 27, 2014, 06:25:11 pm by Jim Bauwens »

Offline LD_Chimpman

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +6/-0
    • View Profile
Re: Lua Q&A
« Reply #202 on: March 27, 2014, 07:02:21 pm »
Thanks so much!

I actually had spent some time studying the lua image string format by loading in a 2x2 pattern as well as a mirrored copy of the image and I found what parts of it
I needed to move around, my problem was the implementation of it. I was trying to split the image string into table values, separated by each "\". Then I was going
to move around the table values and concatenate the table for the final mirrored image. However since forward slashes act as some kind of weird special character
in lua, it overall just didn't work. I guess I was over complicating it.


Thanks again, now I can continue to make progress on my project!


For your help, here's a teaser of my project which so far is turning out really awesome if I do say so myself!  ;D
PROJECTS:
Slime Volleyball: 10% Complete
nFinity: 55% Complete
nCoder: 100% Complete
Jumping Squares 2: 20% Complete
Cloning Dolly: 100% Complete

Offline AnToX98

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 107
  • Rating: +10/-0
    • View Profile
Re: Lua Q&A
« Reply #203 on: March 29, 2014, 02:37:05 am »
Guys please Help me !


I can't find how to modify a segment shape position :(


It is actually possible ?


To get the segment coordinates : mysegment:a()/mysegment:b(), but I can't modify them :(



My Lua projects :

Offline AnToX98

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 107
  • Rating: +10/-0
    • View Profile
Re: Lua Q&A
« Reply #204 on: March 30, 2014, 08:57:28 am »
Please could someone help me :( ?



My Lua projects :

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #205 on: March 30, 2014, 05:06:02 pm »
Please could someone help me :( ?

Try calling the setPos function of the body you attached the segment to.

Offline AnToX98

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 107
  • Rating: +10/-0
    • View Profile
Re: Lua Q&A
« Reply #206 on: March 31, 2014, 02:26:19 am »
Here's my code :


Code: [Select]
------------------------------------------------
----------- LUA FALLDOWN, BY ANTOX98 -----------
------------------------------------------------


platform.apilevel = "2.0"


require "physics"


----------------------
----- BALL CLASS -----
----------------------


Ball = class()
Seg = class()


function Ball:init(x, y, w, mass)   
    self.width = w
    self.body = physics.Body(mass, physics.misc.momentForCircle(mass, 0, 10, ZERO))
    self.body:setPos(physics.Vect(x, y))
    self.body:setMass(mass)
    self.shape = physics.CircleShape(self.body, w, ZERO)
    self.shape:setRestitution(0.6)
    self.shape:setFriction(0.6)
end


function Seg:init(x1, y1, x2, y2)
    local a, b = physics.Vect(x1, y1), physics.Vect(x2, y2)
       
    local mass = physics.misc.INFINITY()
    self.coor = {x1,y1,x2,y2}
    self.body = physics.Body(mass, physics.misc.momentForSegment(mass, a, b))
    --self.body:setPos(physics.Vect(x1, y1))
    self.body:setMass(mass)
    self.shape = physics.SegmentShape(self.body, a, b, 10)
    self.shape:setRestitution(0.6)
    self.shape:setFriction(0.6)
end


function Ball:paint(gc)
    local p = self.body:pos()
    local x, y = p:x(), p:y()
    local r = self.width / 2
   
    gc:setColorRGB(255,0,0)
    gc:fillArc(x+r, y+r , self.width, self.width, 0, 360)
end


function Seg:paint(gc)
   
    local a = self.shape:a()
    local b = self.shape:b()
   
    gc:setColorRGB(0,0,0)
    gc:drawLine(a:x(), a:y(), b:x(), b:y())
end


function initGame()
    w = 318
    h = 212
   
    ZERO = physics.Vect(0,0)
    LARGE = physics.misc.INFINITY()
   
    space = physics.Space()
    space:setGravity(physics.Vect(0,9.)
   
    sol = Seg(0,200,w,200)
    --space:addBody(sol.body)
    space:addShape(sol.shape)
   
    ball = Ball(w/2-10, 30, 20, 1000)
   
    space:addBody(ball.body)
    space:addShape(ball.shape)
   
    sol.body:setPos(physics.Vect(0,-30))
   
    timer.start(0.01)
end
initGame()


function on.timer()
    space:step(0.1)
    platform.window:invalidate()
end


function on.paint(gc)
    ball:paint(gc) 
    sol:paint(gc)
end

sol.body:setPos(physics.Vect(0,-30))
[/size]
[/size][size=78%]Strangely, the body moves but not the shape....[/size][/code]



My Lua projects :