GDScript Code Golf

So I had a little bit of fun with space invaders. First I tried doing a simplified version under 1K chars. I squeezed it to just a few characters above 1000. It’s hard as space invaders have a lot of non-deterministic state.

Then I tried doing a proper version and see if it could be done under 1500. Here’s the result. It’s 1368. It has all of the original features except the mothership, and I guess that too could be packed up to 1500. The game creates all of the graphics from code. The enemies are animated and they explode :smiley:

Just paste onto a label and run:

extends Label;var e;var t;var x=20;var b=0 ;var c=-10;var g;var u=0;var l=3;var p;var r;var d;var q=0; var o; var w
func _draw(k=0,a=0,n=0):
	a=Rect2(b,c,1,6);q+=1;p+=d*(1-g*0.019)
	for i in 45:		
		if e[i]<3:
			k=Vector2i(p+i%9*16,r+i/9*16);e[i]+=0.05;draw_texture(t[q%18/9+max(0,e[i])],k)
			n=Rect2(k.x,k.y,8,6);for j in w:if n.intersects(j):w.erase(j)
			if k.y>145||!g:d=0
			if k.x&256:d=-d;r+=8;break
			if n.intersects(a)&&e[i]<0:c=-10;e[i]=2;g-=1;u+=10	
			if randf()<9e-4*(1-log(g*0.012)):o+=[[k.x,k.y]]
	draw_texture(t[4],Vector2(x,147));draw_rect(a,-1)
	for i in w:
		draw_rect(i,-1);if i.intersects(a):w.erase(i);c=-10
		for j in o:if i.intersects(Rect2(j[0],j[1],1,6)):w.erase(i);o.erase(j)
	for i in o:i[1]+=1;a=Rect2(i[0],i[1],1,6);draw_rect(a,-1);if a.intersects(Rect2(x,147,8,6)):d=0
	if!d:_ready()
func _ready(a=0x8142ff99ffff7e):
	o=[];t=[];d=1;for z in[a,a^165<<48,0xaa55aa55aa55aa,0x3c3c3c3c00,0xffffff3c18]:
		e=[];for i in 56:e+=[-1,-(z&1)];z>>=1
		t+=[ImageTexture.create_from_image(Image.create_from_data(8,7,0,1,e))]
	if g:l-=1;if!l:u=0;l=3
	scale=Vector2(4.3,4);texture_filter=1;p=10;r=p;g=45;e.resize(g);e.fill(-9e9)
	w=[];for j in 4:for i in 42:w+=[Rect2(j*65+20+i%7*4,120+i/7*4,4,4)]
func _process(d):
	x=clamp(x+Input.get_axis("ui_left","ui_right")*2,0,255)
	c-=4;queue_redraw();text="%d | %d"%[l,u];if Input.is_action_just_pressed("ui_accept")&&c<0:b=x+4;c=145

7 Likes

You are on a roll :+1::joy:

2 Likes

i think i play code golf even thought i didnt realize it…

1 Like

Schedule for this year’s GodotCon was just announced and there is one talk dedicated to Code Golf without code. Might be interesting to see how they approached this :slight_smile:

5 Likes