Breakout clone

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
danglethorne
Posts: 2
Joined: Wed Sep 07, 2005 2:27 am

Breakout clone

Post by danglethorne »

Here is some code i quickly whipped up for breakout. All it has is a ball and paddle so far - no bricks to destroy. Ball angle currently doesn't change according to where it hits on the paddle, so you can't control where the ball goes. Press square to launch the ball, and control the paddle with analog stick. Just copy the code to index.lua

Code: Select all

--[[

Breakout clone, Copyright &#40;c&#41; 2005 Nigel Brine <[email protected]> 

&#93;&#93;

System.usbDiskModeActivate&#40;&#41;

red = Color.new&#40;255, 0, 0&#41;;
black = Color.new&#40;0, 0, 0&#41;;
white = Color.new&#40;255, 255, 255&#41;;
breakout = Image.createEmpty&#40;480,272&#41;
breakout&#58;clear&#40;black&#41;

-- define globals
frameskip = 0
score = 0
high = 0
paddle = &#123;&#125;
ball = &#123;&#125;

function updatePaddle&#40;&#41;

	-- clear paddle
	breakout&#58;fillRect&#40;paddle.x, paddle.y, paddle.w, paddle.h, black&#41; 
	-- update position
	if &#40;paddle.x + paddle.dx*paddle.vx < 1&#41; then
		paddle.x = 1
	elseif &#40;paddle.x + paddle.dx*paddle.vx + paddle.w > 480&#41; then  
		paddle.x = 480-paddle.w
	else
		paddle.x = paddle.x + paddle.dx*paddle.vx
	end
	-- redraw paddle
	breakout&#58;fillRect&#40;paddle.x, paddle.y, paddle.w, paddle.h, red&#41;
end

function updateBall&#40;&#41;

	-- clear ball
	breakout&#58;fillRect&#40;ball.x, ball.y, ball.w, ball.h, black&#41;
	-- update position and angle
	if &#40;ball.x + ball.v*math.sin&#40;ball.ang&#41; < 1&#41; then
		ball.x = 1
		ball.y = ball.y - ball.v*math.cos&#40;ball.ang&#41;
		ball.ang = 2*math.pi-ball.ang
	elseif &#40;ball.x + ball.v*math.sin&#40;ball.ang&#41; > 480-ball.w&#41; then
		ball.x = 480-ball.w
		ball.y = ball.y - ball.v*math.cos&#40;ball.ang&#41;
		ball.ang = 2*math.pi-ball.ang
	elseif &#40;ball.y - ball.v*math.cos&#40;ball.ang&#41; < 1&#41; then
		ball.y = 1
		ball.x = ball.x + ball.v*math.sin&#40;ball.ang&#41;
		ball.ang = math.pi-ball.ang
	elseif &#40;ball.y - ball.v*math.cos&#40;ball.ang&#41; >= paddle.y-ball.h&#41; and &#40;ball.y - ball.v*math.cos&#40;ball.ang&#41; <= paddle.y-ball.h-ball.v*math.cos&#40;ball.ang&#41;&#41; and &#40;ball.x + ball.v*math.sin&#40;ball.ang&#41; >= paddle.x&#41; and &#40;ball.x + ball.v*math.sin&#40;ball.ang&#41; <= paddle.x + paddle.w&#41; then
		ball.ang = math.pi-ball.ang
		ball.x = ball.x + ball.v*math.sin&#40;ball.ang&#41;
		ball.y = ball.y - ball.v*math.cos&#40;ball.ang&#41;
		--ball.y = paddle.y
		--ball.ang = math.pi-ball.ang		
	elseif &#40;ball.y - ball.v*math.cos&#40;ball.ang&#41; > 272-ball.h&#41; then
		--ball.y = 272-ball.h
		--ball.x = ball.x + ball.v*math.sin&#40;ball.ang&#41;
		--ball.ang = math.pi-ball.ang
		ball.isin = 0 -- ball is out of play
	else
		ball.x = ball.x + ball.v*math.sin&#40;ball.ang&#41;
		ball.y = ball.y - ball.v*math.cos&#40;ball.ang&#41;
	end
	-- redraw ball
	if ball.isin == 1 then
		breakout&#58;fillRect&#40;ball.x, ball.y, ball.w, ball.h, white&#41;
	end
end

function newGame&#40;&#41;

	score = 0
	paddle.x = 30  -- position of paddle
	paddle.y = 250
	paddle.w = 60  -- dimensions of paddle
	paddle.h = 10
	paddle.dx = 0  -- direction of paddle, -1 = left, +1 = right
	paddle.vx = 20 -- velocity of paddle
	ball.x = 200
	ball.y = 100
	ball.w = 5
	ball.h = 5
	ball.v = 5
	ball.ang = math.pi/4 -- trajectory of ball
	ball.isin = 0 -- is ball in play?

end

function keyboardControl&#40;&#41;
	screen.waitVblankStart&#40;&#41;
	pad = Controls.read&#40;&#41;
	anax = pad&#58;analogX&#40;&#41;
	if math.abs&#40;anax&#41; > 32 then
		if anax > 0 then 
			paddle.dx = 1
		else 
			paddle.dx = -1
		end
		paddle.vx = math.exp&#40;0.028*math.abs&#40;anax&#41;&#41;;
	elseif pad&#58;left&#40;&#41; then
		paddle.dx = -1
		paddle.vx = 5
	elseif pad&#58;right&#40;&#41; then
		paddle.dx = 1
		paddle.vx = 5
	else
		paddle.dx = 0
		paddle.vx = 0
	end
	if pad&#58;square&#40;&#41; and ball.isin == 0 then
		ball.isin = 1
		ball.x = 200
		ball.y = 100
	end
end

-- init
newGame&#40;&#41;

-- game loop
while not Controls.read&#40;&#41;&#58;start&#40;&#41; do
	for i=0,frameskip do
		keyboardControl&#40;&#41;
	end
	updatePaddle&#40;&#41;
	updateBall&#40;&#41;
	screen&#58;blit&#40;0, 0, breakout&#41;
	screen.waitVblankStart&#40;&#41;
	screen.flip&#40;&#41;
end
cgruber
Posts: 36
Joined: Tue Sep 06, 2005 6:38 am

Post by cgruber »

Your math looks more complex than it really needs to be. All those trig functions can be replaced with a bit of simple if then logic.
cracker
Posts: 3
Joined: Sat Sep 10, 2005 6:22 am

Post by cracker »

Why use if-thens? Use a trig table instead.
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

cracker wrote:Why use if-thens? Use a trig table instead.
whats a trig table?
cracker
Posts: 3
Joined: Sat Sep 10, 2005 6:22 am

Post by cracker »

An array with floating values of pre-computed trigonometric functions that is either produced with an external program and included with the source or created dynamically at runtime. It lets you run programs with intense floating point calculations a lot faster since you don't have to do the floating point math to get the value.. you can just load it from the array.
cgruber
Posts: 36
Joined: Tue Sep 06, 2005 6:38 am

Post by cgruber »

That's fine as well.
danglethorne
Posts: 2
Joined: Wed Sep 07, 2005 2:27 am

Post by danglethorne »

Not sure how you could calculate the balls position without using trigonometry, I would be interested to see your if/then alternative. The code is a lot bigger than it could be cause it was a very rushed job. Also I'm not sure why u would use a trig table when lua already provides math.sin and math.cos, and overall processing required is minimal.

The real game I'm interested in developing is a psp version of xblast, which is a multiplayer bomberman type game, tell me if u are interested working on it.
cracker
Posts: 3
Joined: Sat Sep 10, 2005 6:22 am

Post by cracker »

The method you use is fine because I'm sure the PSP is more than capable to run your script fast. I was just replying to cgruber since a bunch of if-thens would make the code way more verbose than it needs to be, is slower, etc. A trig table is a better alternative if you want to avoid runtime floating point calculations -- which as I mentioned before you don't really need to do since the PSP is pretty powerful.
Post Reply