My first game attempt, need some help.

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

Moderators: Shine, Insert_witty_name

Post Reply
JHemperly
Posts: 2
Joined: Fri Aug 26, 2005 12:39 am
Location: U.S.A.

My first game attempt, need some help.

Post by JHemperly »

Having some problems with a very basic program :P. This is my first nosedive into the world of game programming, so don't laugh at this pathetic attempt. I'm trying to understand the concept of how space shooters work, and i think i have a fairly simple concept "game". Right now my controllable space ship is a smiley face, and the background is a cloudy sky. I have it set so that the smiley can fire one round at a time, and can't fire again until the first bullet disappears. Also, i have a picture of a housefly that stays still on the right side. What i want to do is have collision detection so that when the bullet hits the fly, it says "Hit" on the top of my screen.
My problems are this:

1. I can only fire one bullet at a time, i'd like to be able to fire multiple bullets.
2. When i think i have the logic right for collision detection, it thinks that the bullet hits no matter where it is on screen.
3. Because it thinks it is constantly hitting, i suffer a framerate hit until the bullet reaches the right side of the screen.

I'd really like to get into coding games, as figuring this out just by trial and error, and following the tutorials has been the most fun i've had in a long time. Somebody please tell me where my logic errors are and how to correct them. Thanks!

-J. Hemperly

Here's My Source
System.usbDiskModeActivate()
background = Image.load("/images/background.png")
smiley = Image.load("/images/smiley.png")
bullet = Image.load("/images/bullet.png")
fly = Image.load("/images/fly.png")
X = 0
Y = 0
BX = 0
BY = 0
BH = 0

function checkhit()
for BY=100,140 do
BH = BH+1
end
end

function shoot()
for BX=X+40,480 do
screen:clear()
screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)

pad = Controls.read()
if pad:start() then
break
end
if pad:right() then
X=X+2
end
if pad:left() then
X=X-2
end
if pad:up() then
Y=Y-2
end
if pad:down() then
Y=Y+2
end
if BH=1 then
screen:print (0, 0, "Hit")
screen.waitVblankStart()
bullethit=0
end
screen:blit(X, Y, smiley)
screen:blit(BX, BY, bullet)
screen:blit(400, 100, fly)
screen.waitVblankStart()
screen.flip()
BX = BX+7
end
end

while true do
if X>440 then X=440
else X = X
end
if X<0 then X=0
else X = X
end
if Y>232 then Y=232
else Y = Y
end
if Y<0 then Y=0
else Y = Y
end
screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
screen:blit(X, Y, smiley)
screen:blit(400, 100, fly)
screen.waitVblankStart()
screen.flip()
pad = Controls.read()
if pad:start() then
break
end
if pad:right() then
X=X+2
end
if pad:left() then
X=X-2
end
if pad:up() then
Y=Y-2
end
if pad:down() then
Y=Y+2
end
if pad:cross() then do
BX=X+40
BY=Y+18
shoot()
checkhit()
end
end
end
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post by rinco »

Collision detection can be done in a variety of ways.

One technique is to check the distance between two points... if dx = (bx - x1) and dy = (by - y1) then distance = square root of ( dx * dx + dy * dy ). This assumes x1,y1 is the centre of the fly.

A different technique is to check if the bullet is within the bounds of the fly, ie: if (bx < x2) and (bx > x1) and (by < y2) and (by > y1). This assumes x1,y1 is the top left corner of the fly and x2,y2 is the bottom right corner.

Oh and lose the else Y = Y and X = X... you don't need to remind Lua.
JHemperly
Posts: 2
Joined: Fri Aug 26, 2005 12:39 am
Location: U.S.A.

Post by JHemperly »

Okay, i fixed the collision detection, but it gave me another problem, which doesn't seem to make any sense.

I run it and get the following error:
error: SCRIPT.LUA:17: attempt to compare number with nil
Press start to restart

I don't see where it's getting nil from. I defined all of the variables i used. Am i missing something, or is my NyQuil playing tricks on me?

Just to clarify, the reason i define x2 and y2 as equations using x1 and y1 is that i would eventually like the fly to move, and would need to eventually change all four variables.
Here's the updated code:

System.usbDiskModeActivate()
background = Image.load("/images/background.png")
smiley = Image.load("/images/smiley.png")
bullet = Image.load("/images/bullet.png")
fly = Image.load("/images/fly.png")
X = 0
Y = 0
BX = 0
BY = 0
BH = false
x1 = 400
y1 = 100
x2 = x1 + 50
y2 = y1 + 40

function checkhit()
if (bx>x1) and (bx<x2) and (by>y1) and (bx<y2) then BH = true
while BH do
screen:print (0, 0, "Hit")
BH = false
end
end
end

function shoot()
for BX=X+40,480 do
screen:clear()
screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)

pad = Controls.read()
if pad:start() then
break
end
if pad:right() then
X=X+2
end
if pad:left() then
X=X-2
end
if pad:up() then
Y=Y-2
end
if pad:down() then
Y=Y+2
end
screen:blit(X, Y, smiley)
screen:blit(BX, BY, bullet)
screen:blit(400, 100, fly)
screen.waitVblankStart()
screen.flip()
BX = BX+7
end
end

while true do
if X>440 then X=440
end
if X<0 then X=0
end
if Y>232 then Y=232
end
if Y<0 then Y=0
end
checkhit()
screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
screen:blit(X, Y, smiley)
screen:blit(400, 100, fly)
screen.waitVblankStart()
screen.flip()
pad = Controls.read()
if pad:start() then
break
end
if pad:right() then
X=X+2
end
if pad:left() then
X=X-2
end
if pad:up() then
Y=Y-2
end
if pad:down() then
Y=Y+2
end
if pad:cross() then do
BX=X+40
BY=Y+18
shoot()
end
end
end
Squall333
Posts: 91
Joined: Thu Apr 28, 2005 5:32 am

Post by Squall333 »

try while BH == true do
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post by rinco »

i'd suspect the problem lies on line 17.. bx --> BX, by --> BY
i'm shiftkeyphobic
Post Reply