Script - Hud Pong

From FreeSpace Wiki
Revision as of 19:28, 18 April 2010 by TopAce (talk | contribs) (Notes: spelling)
Jump to: navigation, search

Creates a special hud gauge that allows you to play pong. Use the mouse to manipulate the paddles.

Table Entry

#Global Hooks

$GameInit:

[

--HudPong (tm)
w = gr.getScreenWidth() 
h = gr.getScreenHeight()

poneh = 0
ptwoh = 0
gameh = 300		--you can change this to what
gamew = 600		--ever resolution you want.
gamex = w / 2	--set what coords to draw
gamey = h / 2	--the game at here (game center).
ballx = gamex
bally = gamey
ballxvel = 1	--or change theese if you
ballyvel = 1	--want a faster ball speed
balla = 255
deathmsg = "Dont Loose Your Balls!"

drawpongarea = function(x, y, sx, sy)	--pong box here
	gr.setColor(0,200,0,255)		--pong green
	gr.drawLine(x-sx, y+sy, x+sx, y+sy)
	gr.drawLine(x-sx, y-sy, x+sx, y-sy)
	gr.setColor(0,100,0,20)
	gr.drawRectangle(x-sx, y+sy, x+sx, y-sy)	--now featuring a transparent back
end

drawpongpaddle = function(x, y)
	gr.setColor(0,0,200,255)
	gr.drawRectangle(x+5, y+15, x-5, y-15, true)
end

drawpongball = function(x, y, a)
	gr.setColor(200,0,0,a)
	gr.drawCircle(10,x,y)
end

--im nuts

]

$HUD:

[

--HudPong

drawpongarea(gamex,gamey,gamew/2,gameh/2)	--draw playing area every frame

if w >= 1024 then		--compensate for resolution
	maxis1 = ((ms.getY() /  768) * (gameh * 0.9))	--paddels, what a mess, my math sucks
	maxis2 = ((ms.getX() / 1024) * (gameh * 0.9))
else
	maxis1 = ((ms.getY() / 480) * (gameh * 0.9))	--paddels, what a mess, my math sucks, now in lowres!
	maxis2 = ((ms.getX() / 640) * (gameh * 0.9))
end


poneh = (maxis1 + (gamey - (gameh / 2))) + ((gameh - (gameh * 0.9)) / 2)
ptwoh = (maxis2 + (gamey - (gameh / 2))) + ((gameh - (gameh * 0.9)) / 2)
drawpongpaddle(gamex + (gamew/2) - 5, poneh)
drawpongpaddle(gamex - (gamew/2) + 5, ptwoh)

ballx = ballx + ballxvel	--move and draw the ball
bally = bally + ballyvel
drawpongball(ballx, bally, balla)

if bally < (gamey - (gameh / 2)) + 5 or bally > (gamey + (gameh / 2)) - 5 then
	ballyvel = ballyvel * -1	--bounce off borders
end

if ballx < (gamex - (gamew / 2)) or ballx > (gamex + (gamew / 2)) then	--tell the player when he gets castrated and reset the game
	gr.setColor(200,200,200,255)
	gr.drawString(deathmsg, gamex - (gr.getStringWidth(deathmsg) / 2) , gamey)
	balla = balla - 8
	if balla < 1 then
		ballx = gamex
		bally = gamey
		balla = 255
	end
end

if (ballx < (gamex - (gamew / 2)) + 15 and bally > ptwoh - 15 and bally < ptwoh + 15) or (ballx > (gamex + (gamew / 2)) - 15 and bally > poneh - 15 and bally < poneh + 15) then
	ballxvel = ballxvel * -1	--bounce off paddles
end 

--nuts indeed

]

#End

Notes

This gives an example of how to handle multiple resolutions. It's also a good introduction to using the mouse in a hud script.