The discussion title pretty much explains itself… I am a comple noob, and I am curious if anyone has any code for a 2D side slider game that can be explained to a newbie like myself. For those who submit code, I would like to try to build on it. (And if possible, get it into the App Store EVENTUALLY) I will be more than happy to include you in the credits of the game for providing the “bones” of the code. THANK YOU GUYS!! (And girls, lol)
Here it is, I think it’s what you’re asking for. Anyways, if you don’t understand, just ask and I will fill in comments (I may do it anyways, if I get bored).
The code:
supportedOrientations(PORTRAIT)
displayMode(OVERLAY)
displayMode(FULLSCREEN)
function setup()
shipPosition = vec2(WIDTH/2,50)
shipsprite = readImage("Space Art:Red Ship")
shipspriteL = readImage("Space Art:Red Ship Bank Left")
shipspriteR = readImage("Space Art:Red Ship Bank Right")
bulletsprite = readImage("Tyrian Remastered:Bullet Energy A")
enemysprite = {}
enemysprite[1] = readImage("Tyrian Remastered:Enemy Ship D")
enemysprite[2] = readImage("Space Art:Asteroid Small")
enemysprite[3] = readImage("Tyrian Remastered:Eye Mecha")
dx = 0
backingMode(RETAINED)
bullets = {}
lastBulletTime = 0
enemies = {}
lastEnemyTime = 0
score = 0
scoreStartTime = 0
end
function draw()
fill(0, 0, 0, 75)
rect(-1,-1,WIDTH+2,HEIGHT+2)
if ElapsedTime-lastEnemyTime > 2-math.min(1,(ElapsedTime-scoreStartTime)/100) then
makeEnemy()
end
for i,v in pairs(enemies) do
if v.body.collided == true then
v.body:destroy()
table.remove(enemies,i)
else
if v.body.position.y < -v.body.radius then
score = 0
scoreStartTime = ElapsedTime
elseif v.body.position.x < -v.body.radius or
v.body.position.x > WIDTH + v.body.radius then
v.body:destroy()
table.remove(enemies,i)
end
end
end
for i,v in ipairs(enemies) do
sprite(v.img,v.body.position.x,v.body.position.y)
end
if touch and ElapsedTime-lastBulletTime > 1 then
makeBullet()
end
for i,v in pairs(bullets) do
if v.collided == true then
v:destroy()
table.remove(bullets,i)
end
end
for i,v in ipairs(bullets) do
sprite(bulletsprite,v.position.x,v.position.y)
end
dx = Gravity.x * 15
if math.abs(dx) < 1 then
dx = 0
end
shipPosition.x = shipPosition.x + dx
shipPosition.x = math.max(0,math.min(shipPosition.x,WIDTH))
if dx == 0 then
sprite(shipsprite,shipPosition.x,shipPosition.y)
elseif dx > 0 then
sprite(shipspriteR,shipPosition.x,shipPosition.y)
else
sprite(shipspriteL,shipPosition.x,shipPosition.y)
end
fill(255,255,255,255)
textMode(CENTER)
textAlign(CENTER)
fontSize(20)
font("Futura-Medium")
text("Score: "..score,WIDTH/2,HEIGHT-20)
end
function touched(t)
if t.state == BEGAN and not touch then
touch = t
if ElapsedTime-lastBulletTime > .25 then
makeBullet()
end
elseif t.state == ENDED then
touch = nil
end
end
function makeBullet()
local bullet = physics.body(CIRCLE,5)
bullet.position = vec2(shipPosition.x,90)
bullet.bullet = true
bullet.gravityScale = 0
bullet.linearVelocity = vec2(dx,750)
bullet.type1 = "bullet"
bullets[#bullets+1] = bullet
lastBulletTime = ElapsedTime
end
function makeEnemy()
local enemy = {}
enemy.type = math.random(#enemysprite)
enemy.img = enemysprite[enemy.type]
local size = math.max(enemy.img.width,enemy.img.height)
enemy.body = physics.body(CIRCLE,size/2)
enemy.body.position = vec2(math.random(0+size/2,WIDTH-size/2),HEIGHT+size/2)
enemy.body.bullet = true
enemy.body.gravityScale = 0
enemy.body.linearVelocity = vec2(0,-math.random(30,125))
enemy.body.type1 = "enemy"
enemies[#enemies+1] = enemy
lastEnemyTime=ElapsedTime
end
function collide(contactinfo)
if (contactinfo.bodyA.type1 == "enemy" or
contactinfo.bodyB.type1 == "enemy") and
(contactinfo.bodyA.type1 == "bullet" or
contactinfo.bodyB.type1 == "bullet") then
contactinfo.bodyA.collided = true
contactinfo.bodyB.collided = true
score = score + 1
end
end
I tried to use a variety of beginner-intermediate techniques so as to challenge you to learn a few things from the code.
EDIT: made some modifications- namely enemies won’t destroy each other and an enemy that gets knocked off the screen on the left or right will be destroyed rather than being allowed to reset your score when it reaches the bottom
Ok cool! And a link. I’ll check it out. Thanks West!