X Tutup

liquidream.paste.lol / scoreboard-1k.lua · 5 months ago·

-- global vars
ss = userdata"[gfx]280877707700777077707070777070007770777077707070070000700070707070007000007070707070707007007770077077707770777000707770777070700700700000700070007070700070707000707770777077707770007077707770007077700070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000[/gfx]"
players = {"Player 1","Player 2"}
scores = {0,0}
p=false   -- state for mouse press/hold
tap=false -- will be set to true for only one cycle (to avoid code repeats)
ren=0     -- rename player # (0 if not in rename mode)
brk=1     -- tracks the player starting/breaking for current frame
_t=0      -- game timer state (can be paused)
stp=false -- pause state for game timer
gui = create_gui()

-- init -------------------
function _init()
	menuitem(1, "Pause/Resume Timer",
		function() stp=not stp end
	)
	menuitem(2, "Switch Starting Player",
		function() brk=3-brk end
	)
	menuitem(3, "Reset Game",
		function() scores = {0,0} _t=0 end
	)
	for i=1,2 do
	menuitem(3+i, "Set Player "..i.." Name",
		function() edit(i) end
	)
	end
	-- create text box gui control to use for any player renaming
	text_bar = gui:attach_text_editor{
		x=169,y=75,
		width=140,height=15,
		key_callback = {
			enter=function()
				players[ren]=text_bar:get_text()[1]
				ren=0
				text_bar:set_keyboard_focus(false)
			end
		}
	}
end

function _draw()
	-- update -------------------
	mx, my, mb = mouse()
	tap=false
	if (mb>0 and not p) p=true tap=true
	if (mb==0) p=false
	
	-- disable pause menu when renaming, to allow enter to submit
	window{pauseable=ren==0}
	
	-- handle all input (if not in player "rename" mode)
	if ren==0 then
		if (tap and mx<240 and my<=68) edit(1) return
		if (tap and mx>=240 and my<=68) edit(2) return
		if (btnp(11) or (tap and my>68 and my<80)) brk=3-brk return
		if (btnp(4) or (tap and mx<240 and my<200)) scores[1]+=1 brk=3-brk return
		if (scores[1]>0 and (btnp(0) or (tap and mx<240 and my>=200))) scores[1]-=1 return
		if (btnp(5) or (tap and mx>=240 and my<200)) scores[2]+=1 brk=3-brk return
		if (scores[2]>0 and (btnp(1) or (tap and mx>=240 and my>=200))) scores[2]-=1 return
		if (btnp(14)) scores={0,0} -- Q to "quit" (reset scores)
	end
	-- draw ---------------------
	cls(12)
	
	-- draw_ui --
	rectfill(240,0,480,270,8)
	
	?"\134",brk==1 and 110 or 359,72,7

	?"\^w\^t"..players[1],114-((#players[1]/2)*9),50,7
	?"\^w\^t"..players[2],363-((#players[2]/2)*9),50,7
	
	for playernum=1,2 do
		bs=0
		o=0
		s=scores[playernum]
		s%=100 
		if s>9 then
			bs=s\10
			s%=10
		else
			-- offset to center when < 10
			o=-44
		end
		-- use udata as sprite to draw pico-8 numbers depending on current scores
		if (bs>0) sspr(ss, bs*4,0, 3,7, 36+(playernum-1)*250,96, 72,168)
		sspr(ss, s*4,0, 3,7, 120+o+(playernum-1)*250,96, 72,168)
	end
	
	-- timer
	rrectfill(190,240,100,40,3,7)
	if (not stp and ren==0) _t+=1/60
	h=flr(_t/3600)
	m=flr(_t/60)%60
	s=flr(_t%60)
	h=h<10 and "0"..h or h
	m=m<10 and "0"..m or m
	s=s<10 and "0"..s or s
	?"\^w\^t"..h..":"..m..":"..s,201,248,5
	
	
	
	-- player "rename" mode
	if ren>0 then
		poke (0x550b, 0x3f)
		fillp(0b1010010110100101)
		rectfill(0,0,480,270,1)
		fillp()
		poke (0x550b, 0x00)
		gui:update_all()
		?"\^o1ffEnter Player "..ren.." Name:",169,60,7
		gui:draw_all()
	end
end

-- edit player #
function edit(p)
	ren=p
	text_bar:set_text(players[ren])
	text_bar:set_cursor(#players[ren]+1,1)
	text_bar:set_keyboard_focus(true)
end
X Tutup