| View previous topic :: View next topic |
| Author |
Message |
danglethorne
Joined: 07 Sep 2005 Posts: 2
|
Posted: Wed Sep 07, 2005 2:36 am Post subject: Breakout clone |
|
|
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: | --[[
Breakout clone, Copyright (c) 2005 Nigel Brine <wilhelm.klink@gmail.com>
]]
System.usbDiskModeActivate()
red = Color.new(255, 0, 0);
black = Color.new(0, 0, 0);
white = Color.new(255, 255, 255);
breakout = Image.createEmpty(480,272)
breakout:clear(black)
-- define globals
frameskip = 0
score = 0
high = 0
paddle = {}
ball = {}
function updatePaddle()
-- clear paddle
breakout:fillRect(paddle.x, paddle.y, paddle.w, paddle.h, black)
-- update position
if (paddle.x + paddle.dx*paddle.vx < 1) then
paddle.x = 1
elseif (paddle.x + paddle.dx*paddle.vx + paddle.w > 480) then
paddle.x = 480-paddle.w
else
paddle.x = paddle.x + paddle.dx*paddle.vx
end
-- redraw paddle
breakout:fillRect(paddle.x, paddle.y, paddle.w, paddle.h, red)
end
function updateBall()
-- clear ball
breakout:fillRect(ball.x, ball.y, ball.w, ball.h, black)
-- update position and angle
if (ball.x + ball.v*math.sin(ball.ang) < 1) then
ball.x = 1
ball.y = ball.y - ball.v*math.cos(ball.ang)
ball.ang = 2*math.pi-ball.ang
elseif (ball.x + ball.v*math.sin(ball.ang) > 480-ball.w) then
ball.x = 480-ball.w
ball.y = ball.y - ball.v*math.cos(ball.ang)
ball.ang = 2*math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) < 1) then
ball.y = 1
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.ang = math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) >= paddle.y-ball.h) and (ball.y - ball.v*math.cos(ball.ang) <= paddle.y-ball.h-ball.v*math.cos(ball.ang)) and (ball.x + ball.v*math.sin(ball.ang) >= paddle.x) and (ball.x + ball.v*math.sin(ball.ang) <= paddle.x + paddle.w) then
ball.ang = math.pi-ball.ang
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.y = ball.y - ball.v*math.cos(ball.ang)
--ball.y = paddle.y
--ball.ang = math.pi-ball.ang
elseif (ball.y - ball.v*math.cos(ball.ang) > 272-ball.h) then
--ball.y = 272-ball.h
--ball.x = ball.x + ball.v*math.sin(ball.ang)
--ball.ang = math.pi-ball.ang
ball.isin = 0 -- ball is out of play
else
ball.x = ball.x + ball.v*math.sin(ball.ang)
ball.y = ball.y - ball.v*math.cos(ball.ang)
end
-- redraw ball
if ball.isin == 1 then
breakout:fillRect(ball.x, ball.y, ball.w, ball.h, white)
end
end
function newGame()
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()
screen.waitVblankStart()
pad = Controls.read()
anax = pad:analogX()
if math.abs(anax) > 32 then
if anax > 0 then
paddle.dx = 1
else
paddle.dx = -1
end
paddle.vx = math.exp(0.028*math.abs(anax));
elseif pad:left() then
paddle.dx = -1
paddle.vx = 5
elseif pad:right() then
paddle.dx = 1
paddle.vx = 5
else
paddle.dx = 0
paddle.vx = 0
end
if pad:square() and ball.isin == 0 then
ball.isin = 1
ball.x = 200
ball.y = 100
end
end
-- init
newGame()
-- game loop
while not Controls.read():start() do
for i=0,frameskip do
keyboardControl()
end
updatePaddle()
updateBall()
screen:blit(0, 0, breakout)
screen.waitVblankStart()
screen.flip()
end
|
|
|
| Back to top |
|
 |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Thu Sep 08, 2005 10:56 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
cracker
Joined: 10 Sep 2005 Posts: 3
|
Posted: Sat Sep 10, 2005 6:24 am Post subject: |
|
|
| Why use if-thens? Use a trig table instead. |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Sat Sep 10, 2005 6:28 am Post subject: |
|
|
| cracker wrote: | | Why use if-thens? Use a trig table instead. |
whats a trig table? |
|
| Back to top |
|
 |
cracker
Joined: 10 Sep 2005 Posts: 3
|
Posted: Sat Sep 10, 2005 7:37 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
cgruber
Joined: 06 Sep 2005 Posts: 36
|
Posted: Sat Sep 10, 2005 8:10 am Post subject: |
|
|
| That's fine as well. |
|
| Back to top |
|
 |
danglethorne
Joined: 07 Sep 2005 Posts: 2
|
Posted: Mon Sep 12, 2005 2:53 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
cracker
Joined: 10 Sep 2005 Posts: 3
|
Posted: Wed Sep 14, 2005 7:19 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
|