Log In  
Follow
Amac
Follow

Cart #gijuwagami-0 | 2020-11-23 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#84620 2020-11-23 06:49 ( Edited 2020-11-23 06:50)

Cart #nahutotike-0 | 2020-11-20 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

P#84512 2020-11-20 02:29 ( Edited 2020-11-20 02:29)

Cart #jahogefeni-0 | 2020-07-18 | Code ▽ | Embed ▽ | No License

This cartridge is made by Alex McCulloch

P#79532 2020-07-18 00:36

Cart #jipahefofu-0 | 2020-07-18 | Code ▽ | Embed ▽ | No License

This cart is made by Alex McCulloch.

P#79529 2020-07-18 00:15 ( Edited 2020-07-18 00:17)

Switching from Another programming to Lua

Objective

If you are wanting to program in Pico-8 but are familiar in another programming language except for LUA. This reference will help you get started.

Further Updates


I plan on extending this post to include multiple languages. I know over 13 programming languages. Please like and suggest what language you would like to see next. I also plan on covering more of each
language in detail later as well.

Suggestions


Please provide input if I make any mistakes such as the code does not compile or if you have a better tested solution.

Direct Comparison of Programming Languages.

Class Declaration

Notes:


To simply keep the instance variable "a" private for the table foo do not pass back the
address back from the create_foo function. Without the address an external user can not change the
value of "pa" (private "a").

Lua Code

--Lua

function create_foo(a)
 --make the "a" a private global variable 
 local self = {pa = a or 0}

 --considered a private method at the moment
 local _print = function(b)
  print(self.pa)
 end

 --changes print from a private function to a public method 
 return {print=_print}
end

foo = create_foo(5)
foo.print()

--[[
--prints nill
print(foo.pa)
--]]

output => 5

Equivalent Python 3 Code

class Foo:
  def __init__(self, a=0):
    self.__a = a

  def print(self):
    print(self.__a)

f = Foo(5)
f.print()

Equivalent C++ Code

/*
rewrite includes without spaces before you compile.
*/

#include < stdio.h >
#include < iostream >

using namespace std;

class Foo{
    private:
        int a;

    public:
        Foo(int a){ this->a = a; }
        void print();
};

void Foo::print(){
    cout << this->a << endl;
}

int main()
{
    Foo f(5);
    f.print();    
    return 0;
}

Equivalent Java Code

//Java

public class Main
{
    public static void main(String[] args) {
        Foo f = new Foo(5);
        f.print();
    }
}

class Foo{
    private int a;
    public Foo(int a){
        this.a = a;
    }
    public void print(){
        System.out.println(this.a);
    }

}

Equivalent C# Code

//C#
using System;

public class Program
{

    public static void Main()
    {
        Foo f = new Foo(5);
        f.print();      
    }
}

public class Foo
{
    int a = 0;

 public Foo(int a)
 {
     this.a = a;
 }

    public void print()
    {
     System.Console.Write(this.a);
    }
}

output => 5

Inheritance

Notes:


To take advantage of Inheritance In Lua keep in mind that Tables in Lua are Mutable by default.
Simply just add the FOO table to the Bar Table and return it to inherit all the methods or functions from
FOO.

Lua Code

--lua

function create_foo(a)
 local self = {pa = a or 0}

 local _print = function(b)
  print(self.pa)
 end

 return {print=_print}
end

function create_bar(o,b)

 local self = {pb = b or 0}

--BAR inherits values from FOO
 local temp = o

--add additional functions over inherited functions
    temp.get_b = function()
     return self.pb
    end

    temp.set_b = function(c)
     self.pb = c
    end

 return temp
end

bar = create_bar(create_foo(5),10)
bar.print()
print(bar.get_b())

--[[
--prints nil
print(bar.pb)
--]]

output => 5
10

Equivalent Python 3 Code

class Foo:
  def __init__(self, a=0):
    self.__a = a

  def print(self):
    print(self.__a)

class Bar(Foo):
  def __init__(self, a=0, b=0):
    super().__init__(a)
    self.b = b

bar = Bar(5,10)
bar.print()
print(bar.b)

Equivalent C++ Code

/*
rewrite includes without spaces before you compile.
*/

#include < stdio.h >
#include < iostream >

using namespace std;

class Foo{
    private:
        int a;

    public:
        Foo(int a){ this->a = a; }
        void print();
};

void Foo::print(){
    cout << this->a << endl;
}

class Bar : public Foo {
 private:
    int b;

 public:
    Bar(int a, int b) : Foo(a) { this->b = b; }
    int get_b() {return b;}
    void set_b(int value) {this->b = value;}
};

int main()
{
    Bar b(5,10);
    b.print();    
    cout << b.get_b() << endl;
    return 0;
}

Equivalent Java Code

//Java

public class Main
{
    public static void main(String[] args) {
        Bar b = new Bar(5,10);
        b.print();
        System.out.println(Integer.toString(b.get_b()));
    }
}

class Foo{
    private int a;
    public Foo(int a){
        this.a = a;
    }
    public void print(){
        System.out.println(this.a);
    }
}

class Bar extends Foo{
  private int b;
  public Bar(int a, int b){
      super(a);
      this.b = b;
  }
  public void set_b(int value){
      this.b = value;
  }
  public int get_b(){
      return this.b;
  }
}

Equivalent C# Code

//c#
using System;

public class Program
{

    public static void Main()
    {
        Bar b = new Bar(5,10);
        b.print();
        System.Console.Write(b.B);      
    }
}

public class Foo
{
    int a = 0;

 public Foo(int a)
 {
     this.a = a;
 }

    public void print()
    {
     System.Console.WriteLine(this.a);
    }
}

public class Bar : Foo
{
 int b;
 public Bar(int a, int b) : base(a) 
 {
  this.b = b;
 }

 public int B
 {
  get{return b;}
  set{b = value;}
 }
}

output => 5
10

P#70971 2020-03-31 06:32 ( Edited 2020-04-01 03:03)

Cart #flappy_zombie-0 | 2019-12-13 | Code ▽ | Embed ▽ | No License
3

Made this game for fun to learn Lua Programming Language.

My first game in Pico-8. My first game in Lua. One day challenge fun!

Made this in about 4 and half hours of my time. Although I am a seasonal programmer and also a Game Development Instructor in which my job requires me to make new Video Games on a daily basis. On average I have to make 3 mini-games a week. During summer up to 5 a week. It took me 10+ years to get to this point so please do not feel discouraged I used to suck at programming at one time.

I am used to huge languages like C++ and also C#/Java.
Analysis of Lua is that its a descent small language that can be learned entirely by a seasonal professional like me between 30-60 minutes. Its pretty amazing how awesome this language is how awesome Pico-8 is. I am impressed. C# takes about 5 years to master. C++ almost 9 years. This was not an easy task compared to Lua. It took me longer to learn Scratch (drag and drop programming is not quite there yet).

P#70952 2019-12-13 02:01 ( Edited 2019-12-13 02:05)

Follow Lexaloffle:        
Generated 2020-11-30 20:34 | 0.119s | 4194k | Q:81