PDA

View Full Version : Firing Bullets Help



splodger15
February 17th, 2007, 18:36
I seem to be having some trouble with firing bullets. I used the snippet over at evilmana (www.evilmana.com) My bullet seems to fire although it doesnt go very far. In fact it doesnt even move. It just appears on the screen then disappears. Here is my code.

I would have used the code tags but then it becomes all screwed up.

Thanks
:thumbup:

-- Created by splodger15


----------detect collision--------------
function detectCollision(player, opponent)
if (player.x + character:width() > opponent.x) and (player.x < opponent.x + opponent.pic:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.pic:height()) then
screen.waitVblankStart(5)
end
end

green=Color.new(0,255,0)

Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
Town=Image.load("images/Town.png")
character=Image.load("images/Kenny.PNG")
player1=Kenny
pic1=Image.load("images/pic1.PNG")
pic2=Image.load("images/pic2.PNG")
pic3=Image.load("images/pic3.PNG")
pic4=Image.load("images/pic4.PNG")
pic5=Image.load("images/pic5.PNG")
pic6=Image.load("images/pic6.PNG")
pic7=Image.load("images/pic7.PNG")
pic8=Image.load("images/pic8.PNG")

--------This is the bullet----------
bullet = Image.createEmpty(4,4)
bullet:clear(green)



----------walls------------

wall1 = Image.createEmpty(480,2)
wall2 = Image.createEmpty(2,272)
wall3 = Image.createEmpty(2,272)
wall4 = Image.createEmpty(480,2)

wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
wall2 = {x = 450, y = 0, height = wall2:height(), width = wall2:width() }
wall3 = {x = 0, y = 0, height = wall3:height(), width = wall3:width() }
wall4 = {x = 0, y = 299, height = wall4:height(), width = wall4:width() }



player = {}
player.x = 25
player.y = 90
playerHeight = 63
playerWidth = 33

pic1 = {}
pic1.x = 299
pic1.y = 177
pic1Height = 63
pic1Width = 33
pic1.pic = Image.load("images/pic1.PNG")

pic2 = {}
pic2.x = 228
pic2.y = 90
pic2Height = 63
pic2Width = 33
pic2.pic = Image.load("images/pic2.PNG")

pic3 = {}
pic3.x = 366
pic3.y = 200
pic3Height = 63
pic3Width = 33
pic3.pic = Image.load("images/pic3.PNG")


pic4 = {}
pic4.x = 95
pic4.y = 68
pic4Height = 63
pic4Width = 33
pic4.pic = Image.load("images/pic4.PNG")


pic5 = {}
pic5.x = 302
pic5.y = 250
pic5Height = 63
pic5Width = 33
pic5.pic = Image.load("images/pic5.PNG")


pic6 = {}
pic6.x = 250
pic6.y = 176
pic6Height = 63
pic6Width = 33
pic6.pic = Image.load("images/pic6.PNG")

pic7 = {}
pic7.x = 100
pic7.y = 200
pic7Height = 63
pic7Width = 33
pic7.pic = Image.load("images/pic7.PNG")

pic8 = {}
pic8.x = 277
pic8.y = 262
pic8Height = 63
pic8Width = 33
pic8.pic = Image.load("images/pic8.PNG")

character = character



------------------player sprite loading--------------

function rands1()
rand2 = math.random(1,3)
end

function rands2()
rand3 = math.random(1,3)
end

function rands3()
rand4 = math.random(1,3)
end

function rands4()
rand5 = math.random(1,3)
end

function rands5()
rand6 = math.random(1,3)
end

function rands6()
rand7 = math.random(1,3)
end

function rands7()
rand8 = math.random(1,3)
end


function rands8()
rand9 = math.random(1,3)
end

XScrollPosition = 480

numberOfScrolls = 0

while true do
math.randomseed(os.time() )
rand=math.random(1,3)
random1 =math.random(20,115)
random2 =math.random(90,195)
random3 =math.random(192,225)
random4 =math.random(60,165)
random5 =math.random(200,366)
random6 =math.random(144,420)
random7 =math.random(250,344)
random8 =math.random(110,300)


pad = Controls.read()
screen:clear()


currentBullet = 0
direction = "right"

--Create Array for Bullets
BulletInfo = {}
for a = 1,5 do
BulletInfo[a] = { pic = bullet , firing = false, direction = "right", x = player.x + 32,
y = player.y + 16 }
end




function bulletSetup()

if currentBullet < 5 then
currentBullet = currentBullet + 1
else
currentBullet = 1
end

if direction == "left" then
BulletInfo[currentBullet].x = player.x
BulletInfo[currentBullet].y = player.y + 16
end
if direction == "right" then
BulletInfo[currentBullet].x = player.x + 32
BulletInfo[currentBullet].y = player.y + 16
end
if direction == "up" then
BulletInfo[currentBullet].x = player.x + 16
BulletInfo[currentBullet].y = player.y
end
if direction == "down" then
BulletInfo[currentBullet].x = player.x + 16
BulletInfo[currentBullet].y = Player.y + 32
end

BulletInfo[currentBullet].direction = direction
BulletInfo[currentBullet].firing = true
end

function bulletFire()
for i = 1,5 do
if BulletInfo[i].firing == true then
if BulletInfo[i].direction == "right" then BulletInfo[i].x = BulletInfo[i].x + 10 end
if BulletInfo[i].direction == "left" then BulletInfo[i].x = BulletInfo[i].x - 10 end
if BulletInfo[i].direction == "up" then BulletInfo[i].y = BulletInfo[i].y - 10 end
if BulletInfo[i].direction == "down" then BulletInfo[i].y = BulletInfo[i].y + 10 end
screen:blit(BulletInfo[i].x,BulletInfo[i].y,BulletInfo[i].pic)
end
if BulletInfo[i].x < 0 or BulletInfo[i].x > 480 or BulletInfo[i].y < 0 or BulletInfo[i].y > 272 then BulletInfo[i].firing = false end
end
end



oldx = player.x
oldy = player.y
screen:clear()

if pad:left() then
character = car1
player.x = player.x - 2
end

if pad:right() then
character = character
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
end
end

if pad:cross() and oldpad:cross() ~= pad:cross() then
bulletSetup()
end


if pad:up() then
character = character
player.y = player.y - 2
end

if pad:down() then
character = character
player.y = player.y + 2
end


if pad:square() then
Image:save("screenshot.png")
end

if pad:start() then
dofile("menu.lua")
end


if rand ==1 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==2 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==3 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==4 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==5 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==6 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==7 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end

if rand ==8 then
pic1.x = pic1.x -10
pic2.x = pic2.x-10
pic3.x = pic3.x-10
pic4.x = pic4.x-10
pic5.x = pic5.x-10
pic6.x = pic6.x-10
pic7.x = pic7.x-10
pic8.x = pic8.x-10
end



if pic1.x <=0 then
pic1.x =510
pic1.y = random1
rands1()
end

if pic2.x <=0 then
pic2.x = 520
pic2.y = random2
rands2()
end

if pic3.x <=0 then
pic3.x = 500
pic3.y = random3
rands3()
end

if pic4.x <=0 then
pic4.x = 540
pic4.y = random4
rands4()
end

if pic5.x <=0 then
pic5.x = 530
pic5.y = random5
rands5()
end

if pic6.x <=0 then
pic6.x = 560
pic6.y = random6
rands6()
end

if pic7.x <=0 then
pic7.x = 550
pic7.y = random7
rands7()
end

if pic8.x <=0 then
pic8.x = 570
pic8.y = random8
rands8()
end

bulletFire()




screen:clear()

screen:blit(XScrollPosition, 0, Town)

if XScrollPosition > 0 then
screen:blit(XScrollPosition - 480, 0, Town)
else
screen:blit(XScrollPosition + 480, 0, Town)
end

if numberOfScrolls > 500 then
dofile("eol.lua")
end

if player.x > 448 then
Player.x = 448
end
if player.x < 0 then
player.x = 0
end

if player.y > 240 then
player.y = 240
end
if player.y < 0 then
player.y = 0
end


detectCollision(player, pic1)
detectCollision(player, pic2)
detectCollision(player, pic3)
detectCollision(player, pic4)
detectCollision(player, pic5)
detectCollision(player, pic6)
detectCollision(player, pic7)
detectCollision(player, pic8)


-----------blit background and player------------

screen:blit(player.x, player.y, character)
screen:blit(pic1.x, pic1.y, pic1.pic)
screen:blit(pic2.x, pic2.y, pic2.pic)
screen:blit(pic3.x, pic3.y, pic3.pic)
screen:blit(pic4.x, pic4.y, pic4.pic)
screen:blit(pic5.x, pic5.y, pic5.pic)
screen:blit(pic6.x, pic6.y, pic6.pic)
screen:blit(pic7.x, pic7.y, pic7.pic)
screen:blit(pic8.x, pic8.y, pic8.pic)

bulletFire()

screen.flip()
oldpad = pad
screen.waitVblankStart()
end

Zion
February 18th, 2007, 16:36
why not just ask on evilmana, the site where you got the code from :confused:

yaustar
February 18th, 2007, 17:20
The first problem is that the bullet will always fire right of the player.

splodger15
February 18th, 2007, 20:32
I would like to have it like that as he is only going to be shooting right to start off with.

Zion
February 18th, 2007, 20:46
work it out then :rofl:

seriously though, i have learned that working these things out for yourself makes you a far better coder than having people bail you out all the time.

like be honest, your code at the top says "By splodger 15" when in reality 90% was coded by members of evilmana.........
im not criticising you i just want to help you force yourself to learn it and work it out on your own mate..... its hard but worth it

ive been tempted many a time to post and get someone to work it out for me but i have prevailed and worked it out and felt much better about myself as a result.

and improved of course....


also do you really want to be known as a "copy and paste coder" ?

splodger15
February 19th, 2007, 15:16
I dont know everything in Lua so there are still parts i am unclear of.

yaustar
February 19th, 2007, 15:42
10 pixels per frame is pretty fast. Maybe you can't see them?

splodger15
February 19th, 2007, 17:50
you can seem them . But they do go quite fast across the screen

yaustar
February 20th, 2007, 13:09
My bullet seems to fire although it doesnt go very far. In fact it doesnt even move.


you can seem them . But they do go quite fast across the screen

Which is it?

mexicansnake
March 10th, 2007, 21:01
if pad:left() then
character = car1
player.x = player.x - 2
direction = "left"
end

if pad:right() then
character = character
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
direction = "right"
end
end

if pad:up() then
character = character
player.y = player.y - 2
direction = "up"
end

if pad:down() then
character = character
player.y = player.y + 2
direction = "down"
end

This should work...