My first love2d game

Show off your games, demos and other (playable) creations.
Post Reply
nick_banana
Prole
Posts: 1
Joined: Wed Aug 05, 2015 11:35 pm

My first love2d game

Post by nick_banana »

I just started learning programming this year. I started with python, but I have never used lua. I started learning lua this week and then came across love2d. I decided to give it a go and I make my first ever video game. Its a pong like game where the player moves a platform around and bounces a ball off the platform to destroy blocks. I probably used terrible coding structure, and i probably could have simplified it a lot with functions, but I'm new to game programming and I'm kind of confused on the best way to do things. Any constructive criticism would be appreciated!
I'll attach the compressed .love game and ill put the source code below. Rate my first video game!

Code: Select all

function love.load()
  -- WINDOW SETUP
  love.window.setTitle("Block Buster")
  height = love.graphics.getHeight()
  width = love.graphics.getWidth()

  -- SOUND SOURCES
  hit = love.audio.newSource("hit.mp3")
  bounce = love.audio.newSource("bounce.mp3")
  loss = love.audio.newSource("loss.mp3")

  -- PLAYER SETUP
  player = {}
  function player.load()
    player.width = 70
    player.height = 20
    player.x = width/2 - player.width/2
    player.y = height - player.height
    player.speed = 400
    player.lives = 5
    player.points = 0
  end
  player.load()

  -- BLOCKS
  blocks = {}
  blocks.draw = {}

  -- LOAD BLOCKS
  function blocks.load()
    column = 0; row = 1
    while 5 >= row do
      block = {}
      block.width = width/10 - 5
      block.height = 20
      block.x = column * (block.width + 5)
      block.y = row * (block.height + 5)
      table.insert(blocks.draw, block)
      column = column + 1
      if column == 10 then column = 0; row = row + 1 end
    end
  end
  blocks.load()

  -- BALL
  ball = {}
  function ball.load()
    ball.radius = 5
    ball.x = width/2
    ball.y = player.y - 200
    ball.speed = 200
    ball.direction = "d"
    ball.cooldown = 200
  end
  ball.load()

  -- CHECK TOP FOR BOUNCE
  function topbounce()
    if ball.direction == "ull" then ball.direction = "dll"
    elseif ball.direction == "ul" then ball.direction = "dl"
    elseif ball.direction == "uul" then ball.direction = "ddl"
    elseif ball.direction == "u" then ball.direction = "d"
    elseif ball.direction == "uur" then ball.direction = "ddr"
    elseif ball.direction == "ur" then ball.direction = "dr"
    elseif ball.direction == "urr" then ball.direction = "drr"
    end
  end

end


------ UPDATE ------

function love.update(dt)
  if ball.cooldown > 0 then ball.cooldown = ball.cooldown - 1 end

  -- Player movement
  if love.keyboard.isDown("right") and player.x <= (width - player.width) then
    player.x = player.x + (dt * player.speed)
  elseif love.keyboard.isDown("left") and player.x >= 0 then
    player.x = player.x - (dt * player.speed)
  elseif love.keyboard.isDown("r") then
    ball.load()
  end

  -- Hitbox for player
  if ball.y >= player.y and ball.y <= height and ball.x >= player.x and
    ball.x <= (player.x + player.width) then
    if ball.x >= player.x and ball.x < (player.x + 10) then
      ball.direction = "ull"
    elseif ball.x >= (player.x + 10) and ball.x < (player.x + 20) then
      ball.direction = "ul"
    elseif ball.x >= (player.x + 20) and ball.x < (player.x + 30) then
      ball.direction = "uul"
    elseif ball.x >= (player.x + 30) and ball.x < (player.x + 40) then
      ball.direction = "u"
    elseif ball.x >= (player.x + 40) and ball.x < (player.x + 50) then
      ball.direction = "uur"
    elseif ball.x >= (player.x + 50) and ball.x < (player.x + 60) then
      ball.direction = "ur"
    elseif ball.x >= (player.x + 60) and ball.x < (player.x + 70) then
      ball.direction = "urr"
    end
    love.audio.play(bounce)
  end


  -- Hitbox for blocks
  for i,v in ipairs(blocks.draw) do
    if ball.y <= (v.y + v.height) and ball.y >= v.y then
      if ball.x <= (v.x + v.width) and ball.x >= v.x then
        topbounce()
        love.audio.play(hit)
        table.remove(blocks.draw, i)
        player.points = player.points + 1
      end
    end
  end

  -- Bounces ball off walls
  if (ball.x <= 0) or (ball.x >= width) then
    if ball.direction == "uur" then ball.direction = "uul"
    elseif ball.direction == "ur" then ball.direction = "ul"
    elseif ball.direction == "urr" then ball.direction = "ull"
    elseif ball.direction == "drr" then ball.direction = "dll"
    elseif ball.direction == "dr" then ball.direction = "dl"
    elseif ball.direction == "ddr" then ball.direction = "ddl"
    elseif ball.direction == "ddl" then ball.direction = "ddr"
    elseif ball.direction == "dl" then ball.direction = "dr"
    elseif ball.direction == "dll" then ball.direction = "drr"
    elseif ball.direction == "ull" then ball.direction = "urr"
    elseif ball.direction == "ul" then ball.direction = "ur"
    elseif ball.direction == "uul" then ball.direction = "uur"
    end
    love.audio.play(bounce)
  end

  -- Bounce ball off ceiling
  if ball.y <= 0 then topbounce() end

  -- Move ball
  if ball.cooldown == 0 then
    if ball.direction == "u" then
      ball.y = ball.y - 2 * (dt * ball.speed)
    elseif ball.direction == "uur" then
      ball.y = ball.y - 2 * (dt * ball.speed)
      ball.x = ball.x + 1 * (dt * ball.speed)
    elseif ball.direction == "ur" then
      ball.y = ball.y - 2 * (dt * ball.speed)
      ball.x = ball.x + 2 * (dt * ball.speed)
    elseif ball.direction == "urr" then
      ball.y = ball.y - 1 * (dt * ball.speed)
      ball.x = ball.x + 2 * (dt * ball.speed)
    elseif ball.direction == "drr" then
      ball.y = ball.y + 1 * (dt * ball.speed)
      ball.x = ball.x + 2 * (dt * ball.speed)
    elseif ball.direction == "dr" then
      ball.y = ball.y + 2 * (dt * ball.speed)
      ball.x = ball.x + 2 * (dt * ball.speed)
    elseif ball.direction == "ddr" then
      ball.y = ball.y + 2 * (dt * ball.speed)
      ball.x = ball.x + 1 * (dt * ball.speed)
    elseif ball.direction == "d" then
      ball.y = ball.y + 2 * (dt * ball.speed)
    elseif ball.direction == "ddl" then
      ball.y = ball.y + 2 * (dt * ball.speed)
      ball.x = ball.x - 1 * (dt * ball.speed)
    elseif ball.direction == "dl" then
      ball.y = ball.y + 2 * (dt * ball.speed)
      ball.x = ball.x - 2 * (dt * ball.speed)
    elseif ball.direction == "dll" then
      ball.y = ball.y + 1 * (dt * ball.speed)
      ball.x = ball.x - 2 * (dt * ball.speed)
    elseif ball.direction == "ull" then
      ball.y = ball.y - 1 * (dt * ball.speed)
      ball.x = ball.x - 2 * (dt * ball.speed)
    elseif ball.direction == "ul" then
      ball.y = ball.y - 2 * (dt * ball.speed)
      ball.x = ball.x - 2 * (dt * ball.speed)
    elseif ball.direction == "uul" then
      ball.y = ball.y - 2 * (dt * ball.speed)
      ball.x = ball.x - 1 * (dt * ball.speed)
    end
  end

  if ball.y >= height then
    love.audio.play(loss)
    player.lives = player.lives - 1; ball.load()
  end

  if player.lives < 0 then
    love.graphics.print("GAME OVER", width/2, height/2)
    love.load()
  end

end



------ DRAW ------

function love.draw()
  -- Cooldown
  if ball.cooldown > 0 then
    love.graphics.print("Get ready!", width/2, height/2)
  end

  -- Points/Lives
  love.graphics.print("Lives: " .. player.lives, 10, height/3)
  love.graphics.print("Points: " .. player.points, 10, height/3 + 20)
  -- Draw player
  love.graphics.setColor(255, 255, 255)
  love.graphics.rectangle("fill", player.x, player.y, player.width, player.height - 10)

  -- Draw blocks
  love.graphics.setColor(255, 0, 0)
  iter = 0
  for _,v in pairs(blocks.draw) do
    love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
  end

  -- Draw ball
  love.graphics.setColor(255, 255, 255)
  love.graphics.circle("fill", ball.x, ball.y, ball.radius)
end
I'm 16 and my name is Nick by the way! my Github is github.com/nickgardne if you want to follow me :)
Attachments
firstgame.love
(92.51 KiB) Downloaded 188 times
Poliku
Prole
Posts: 3
Joined: Wed Aug 05, 2015 2:46 am

Re: My first love2d game

Post by Poliku »

First off, welcome to the forums and Love2d in general!

I want to start off by saying that I can't use the love file you sent. I had to extract it, rearrange things, then repackage it in order for it to work. Anyways, the game's pretty fun, and the coding looks solid. I noticed that the ball would occasionally just pass right though the platform and the walls, so collision detection could use some work. Other than that, it's a really good first game :) Keep it up!
Stay cool! :cool:
User avatar
CanadianGamer
Party member
Posts: 132
Joined: Tue Jun 30, 2015 1:23 pm
Location: Canada
Contact:

Re: My first love2d game

Post by CanadianGamer »

Hello and to start with I'm sorry to say that when I tried to run your game and I got:

Error

boot.lua:378: No code to run
Your Game Might be packaged incorrectly
Make sure main.lua is at the top level of the zip

Traceback

[C]: in function 'error'
[C]: in function 'xpcall'

Other than that it looks Great keep up the work
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest