超过lua上帝的语言

上帝的语言(god)它是基于lua和RPP新一代编程语言


为什么需要它?

1.好多人不喜欢lua语法,god的语法更像C

2.god支持元编程、闭包、协程

3.凡是lua支持的特性god也支持,lua不支持的宏god也支持,因此god比lua强大

4.god能够使用lua库,甚至同意god和lua混合编程

5.god是最快的脚本语言之中的一个,性能与C接近。由于god基于luaJIT

6.god的编译器仅仅有92行代码。这是世界上最简单的编译器,即使是菜鸟也能看懂并改动

7.god全然开源。92行代码想不开源都不行


官网:https://github.com/roundsheep/god




god写的控制台贪食蛇:


ffi=require('ffi')

ffi.cdef[
	#pragma pack(1) 
	typedef struct 
	{
		int dwSize;
		int bVisible;
	}CONSOLE_CURSOR_INFO;
	int SetConsoleCursorInfo(int hConsoleOutput,
		CONSOLE_CURSOR_INFO* lpConsoleCursorInfo);
	int system(const char* s);
	typedef struct 
	{
		unsigned short x;
		unsigned short y;
	}COORD;
	int SetConsoleCursorPosition(int hConsoleOutput,
		COORD dwCursorPosition);
	int GetStdHandle(int nStdHandle);
	int printf(const char* fmt,...);
	typedef unsigned short ushort;
	int SetConsoleTextAttribute(int h,ushort w);
	short GetAsyncKeyState(int vkey);
	int GetTickCount();
]

clear=function(v)
{
	var i
	for(i=1,table.maxn(v))
	{
		v[i]=null
	}
}

exist=function(v,a)
{
	var i
	for(i=1,table.maxn(v))
	{
		if(v[i]==a)
		{
			return true
		}
	}
	return false
}

push=function(v,a)
{
	v[table.maxn(v)+1]=a
}

push_front=function(v,a)
{
	var count=#v
	var i
	for(i=count,1,-1)
	{
		v[i+1]=v[i]
	}
	v[1]=a
}

food=function()
{
	g_food=math.modf(math.random()*10000)%200+1
	if(exist(g_arr,g_food))
	{
		food()
	}
}

update=function()
{
	var i
	for(i=1,200)
	{
		gotoxy(i%10*2,math.modf(i/10))
		if(exist(g_arr,i))
		{
			out('■')
		}
		elif(i==g_food)
		{
			out('★')
		}
		else
		{
			out('  ')
		}
	}
}

gotoxy=function(x,y)
{
	var pos=ffi.new('COORD',[])
	pos.x=x
	pos.y=y
	ffi.C.SetConsoleCursorPosition(g_std_out,pos)
}

out=function(s)
{
	ffi.C.printf(s)
}

init=function()
{
	math.randomseed(os.time())
	g_std_out=ffi.C.GetStdHandle(-11)
	g_next=10
	clear(g_arr)
	push(g_arr,105)
	food()
	var cur_info=ffi.new('CONSOLE_CURSOR_INFO',[])
	cur_info.dwSize=1
	cur_info.bVisible=0
	ffi.C.SetConsoleCursorInfo(g_std_out,cur_info)
	ffi.C.system('mode con cols=20 lines=22')
	ffi.C.SetConsoleTextAttribute(g_std_out,0x0a)
	gotoxy(0,20)
	out(' ******************')
}

key=function()
{
	var temp
	if(ffi.C.GetAsyncKeyState(0x26)!=0)
	{
		temp=-10
	}
	elif(ffi.C.GetAsyncKeyState(0x28)!=0)
	{
		temp=10
	}
	elif(ffi.C.GetAsyncKeyState(0x25)!=0)
	{
		temp=-1
	}
	elif(ffi.C.GetAsyncKeyState(0x27)!=0)
	{
		temp=1
	}
	else
	{
		return
	}
	if(table.maxn(g_arr)<2||g_arr[2]!=g_arr[1]+temp)
	{
		g_next=temp
	}
}

check=function()
{
	var temp=g_arr[1]+g_next
	if(temp<1||temp>200||math.abs(temp%10-g_arr[1]%10)>1||exist(g_arr,temp))
	{
		return false
	}
	return true
}

g_arr=[]
init()
var start=ffi.C.GetTickCount()
while(true)
{
	key()
	if(ffi.C.GetTickCount()-start<100)
	{
		goto continue
	}
	start=ffi.C.GetTickCount()
	if(!check())
	{
		break
	}
	push_front(g_arr,g_arr[1]+g_next)
	if(g_food!=g_arr[1])
	{
		g_arr[#g_arr]=null
	}
	else
	{
		food()
	}
	update()
	::continue::
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值