Submit Your Own Code

PSP Lua CodeBase : Pong

Description:
 This is just a small Pong game which will hopefully help some people.

  -- EVILMANA.COM PSP LUA CODEBASE
-- www.evilmana.com/tutorials/codebase
-- Pong
-- SUBMITTED BY: Blake1

-- Colors
white = Color.new(255, 255, 255);
red = Color.new(255,0,0)

-- paddle image
paddleimg = Image.createEmpty(10, 60)
paddleimg:clear(white)

-- ball image
ballimg = Image.createEmpty(5,5 )
ballimg:clear(white)

-- score
score1 = 0
score2 = 0

---Direction
direction = "none" -- X direction
direction2 = "none" -- Y direction
Bstartdir = math.random(1,2) -- Ball start direction

-- paddles
paddle = { } -- paddle array
paddle[1] = { x = 5, y = 100, img = paddleimg, 
width = 10, height = 60 } -- paddle 1
paddle[2] = { x = 465, y = 100, img = paddleimg, 
width = 10, height = 60 } -- paddle 2

-- ball
ball = { } -- ball array
ball[1] = { x = 240, y = 130, img = ballimg, 
width = 10, height = 10} -- the ball

-- screen
screenwidth = 480 - paddleimg:width() -- the screen height
screenheight = 272 - paddleimg:height() -- the screen height

-- speed
speed = 2 -- the speed

-- Collision Detection Function
function collision(x1, y1, w1, h1, x2, y2, w2, h2)
if (y2 >= y1 and y1 + h1 >= y2) or (y2 + h2 >= y1 
and y1 + h1 >= y2 + h2) or (y1 >=
y2 and y2 + h2 >= y1) or (y1 + h1 >= y2 and y2 + h2 >= y1 + h1) 
then
if x2 >= x1 and x1 + w1 >= x2 then
return true
elseif x2 + w2 >= x1 and x1 + w1 >= x2 + w2 then
return true
elseif x1 >= x2 and x2 + w2 >= x1 then
return true
elseif x1 + w1 >= x2 and x2 + w2 >= x1 + w1 then
return true
end
end

return false
end

function movePlayer1( )
if pad:up() and paddle[1].y > 0 then
paddle[1].y = paddle[1].y - speed
end
if pad:down() and paddle[1].y < screenheight then
paddle[1].y = paddle[1].y + speed
end
end

function movePlayer2( )

if pad:triangle() and paddle[2].y > 0 then
paddle[2].y = paddle[2].y - speed
end

if pad:cross() and paddle[2].y < screenheight then
paddle[2].y = paddle[2].y + speed
end
end

while true do
screen:clear()
-- store player's position at beginning of each loop
oldx = ball[1].x
oldy = ball[1].y
screen:clear()

screen:print(220, 15, ""..score1, white)
screen:print(255, 15, ""..score2, red)

screen:blit(paddle[1].x, paddle[1].y, paddleimg )
screen:blit(paddle[2].x, paddle[2].y, paddleimg )

screen:blit(ball[1].x, ball[1].y, ball[1].img )
pad = Controls.read()

movePlayer1( )
movePlayer2( )

---Direction
if direction == "left" then
ball[1].x = ball[1].x - speed
end

if direction == "right" then
ball[1].x = ball[1].x + speed
end

if direction2 == "up" then
ball[1].y = ball[1].y - speed
end

if direction2 == "down" then
ball[1].y = ball[1].y + speed
end

---ball start direction
if Bstartdir == 1 then
direction = "left"
end

if Bstartdir == 2 then
direction = "right"
end

-- Collision Detection for paddle1
Bstartdir = 0
if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) then
direction = "right"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) and pad:up() then
Bstartdir = 0
direction = "right"
direction2 = "up"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[1].x,
paddle[1].y, paddle[1].width, paddle[1].height) and pad:down() then
Bstartdir = 0
direction = "right"
direction2 = "down"
end

--- Collision Detection for paddle2
if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) then
Bstartdir = 0
direction = "left"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) and pad:triangle()
then
Bstartdir = 0
direction = "left"
direction2 = "up"
end

if collision(ball[1].x, ball[1].y, ball[1].width,ball[1].height, paddle[2].x,
paddle[2].y, paddle[2].width, paddle[2].height) and pad:cross() then
Bstartdir = 0
direction = "left"
direction2 = "down"
end

-- Edges
if ball[1].x >= 478 then
score1 = score1 + 1
direction = "left"
end

if ball[1].x <= 2 then
score2 = score2 + 1
direction = "right"
end

if ball[1].y <= 0 then
direction2 = "down"
end

if ball[1].y >= 262 then
direction2 = "up"
end

screen.waitVblankStart()
screen.flip()
end


Back to CodeBase
 

Please welcome jramada, our newest member.

Who's Online: 5 Guests, 1 User
yaustar

Total Members: 555
Total Posts: 13059
Total Topics: 1480
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: PGELua Help - need term for a special rotation fix (mathematical issue) by yaustar
Re: Lua Loader? by Buddy4point0
Re: Lua Loader? by osgeld
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl
Lua Loader? by acer5050


Copyright © 2006-2007 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie