'
'	Tiny Flappy
'


'
'	Background
'
background #012
select screen "back"

?cheat = 0

color #8481
for 1 to 100 as ?r
	color (rgba 0, 50, 100, 2)
	?s = random 1 to 10
	?x = random -100 to 100
	?y = random -100 to 100
	trace frame -100, ?y - ?s to 100, ?y + ?s
	trace fill
next

'
select screen "front"
scale screen 0, 0 to 1000, 600


'
goto "restart"
load hiscore

do
	
	?offset + 1
	
	clear screen
	goto "helper"
	
	
	'	playing ? display score
	if ?play = 1
		?scored + (?score - ?scored) / 5
		create text (format number (round ?scored)) as "score"
		paint "score" to "ui"
		move "score" to 0, 85, 90
	end
	
	
	if ?play = 0
		' hiscores
		goto "intro"
	else
		' move player
		?vy - .01
		if ?vy > 0
			?vy * .9
		else
			?vy * 1.05
		end
		
		?py + ?vy * 2
	end
	
	
	' render smiley
	color #fd6
	trace circle 20 to ?px, ?py
	trace fill
	color 99
	trace circle 3 to ?px - 10, ?py + 4
	trace fill
	trace circle 3 to ?px + 10, ?py + 4
	trace fill
	if ?vy > 0
		trace frame ?px - 6, ?py - 6 to ?px + 6, ?py - 10
	else
		trace frame ?px - 14, ?py - 6 to ?px + 14, ?py - 8
	end
	trace fill
	
	
	
	' manage walls
	for 1 to ?dim as ?e
		
		?x = [elem, ?e, x]
		?y = [elem, ?e, y]
		?h = [elem, ?e, h]
		
		[elem, ?e, x] - ?speed
		
		if ?over = 1
			' gameover: do not manage walls
		else
			
			if [elem, ?e, a] = 1
				
				' beyond wall ? disable wall + gain score
				if ?x < ?px - ?size
					[elem, ?e, a] = 0
					?wall + 1' stats
					
					' exponential gain
					if ?play = 1
						?score + ?gain
						?gain * ?gain_ratio
						?gain_ratio * .985
					end
				else if ?cheat = 1
					?py = 300
					?vy = 0
				else
					if ?px > ?x and ?px < (?x + ?size)
						if ?py < (?y - ?h)
							' top wall
							goto "game over"
						else if ?py > (?y + ?h)
							' bottom wall
							goto "game over"
						end
					end
				end
			end
			
		end
		
		
		' time to add a new wall ?
		if ?x < -?size and ?delay < 0
			
			[elem, ?e, x] = 1000' right
			[elem, ?e, y] = random 200 to 400
			[elem, ?e, h] = ?space
			[elem, ?e, a] = 1' active
			
			' reset delay for the next one
			?delay = ?delay_max
			
			if ?play = 1
				?delay_max * .985' faster !
				?space - 2' closer !
				?size + 1.5' larger !
			end
		end
		
		' render wall
		if [elem, ?e, a] = 1
			color 1 + ?e
		else
			[elem, ?e, h] + 3
			color 51
		end
		trace frame ?x, ?y + ?h to ?x + ?size, 600
		trace fill
		trace frame ?x, 0 to ?x + ?size, ?y - ?h
		trace fill
		
		
	next
	?delay - 1
	
	'
	update
	
	
	'
	if ?over = 0
		' exit screen
		if ?py < 0 or ?py > 600
			goto "game over"
		end
		
		' jump
		if (press "space") = 1
			?vy = 5
			if ?play = 0
				goto "start game"
			end
		end
	else
		?px - 5
		
		if (press "enter") = 1
			goto "restart"
		end
	end
	
	
	if (is "label") = 1
		' game over anim
		?labely + 3
		turn "label" to 0, ?labely, 0
		move "label" to 0, 0, 20 + 20 * (sin ?labely / 2)
	end
	
	
loop

'

label "restart"
	
	for 1 to ?dim as ?e
		[elem, ?e, a] = 0
	next
	
	delete "label"
	
	color 3
	create text "JUMPY" as "label"
	paint "label" to "ui"
	color 2
	outline "label"
	move "label" to 0, 0, 30
	
	
	?offset = 0
	?dim = 40' max walls
	?wall = 1
	
	?px = 200' start pos
	?py = 300
	?vy = 0' y movement
	
	?play = 0
	?over = 0
	
	?size = 20
	
	
	?delay = 0
	?delay_max = 120
	?speed = 3
	
	
	trace size 2
	?score = 0
	?gain = 25
	?gain_ratio = 2.2
	
	?scored = 0
	?space = 150
return

'

label "start game"
	delete "label"
	?play = 1
return

'

label "game over"
	if ?over = 0
		?over = 1
		color 2
		?labely = 0
		create text "GAME OVER" as "label"
		move "label" to 0, 0, 30
		update
		save hiscore ?score
	end
return

'

label "helper"
	
	color 1
	trace frame 0, 0 to 1000, 600
	font size 4
	
	if ?play = 0
		?a = mod (frame), 100
		color (rgba 100, 100, 100, ?a)
		trace text "Hi " + (username) center 500, 125
		trace text "Press SPACE to jump and start..." center 500, 100
	else if ?over = 1
		trace text "Press ENTER to try again..." center 500, 100
	end
	
return


'

label "intro"
	' hiscores...
	
	color 1
	font size 3
	for 1 to 10 as ?r
		
		if (hiscore ?r) > 0
			if (hiscore ?r username) = (username)
				color 3
			else
				color 1
			end
			
			?y = 550 - 20 * ?r
			trace text (hiscore ?r username) align 450, ?y
			trace text (format number (hiscore ?r)) to 550, ?y
		end
	next
	
return
x