Problem with user input in turn-based battle

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dichotommy
Prole
Posts: 4
Joined: Thu Nov 22, 2018 11:09 am

Problem with user input in turn-based battle

Post by dichotommy »

Hi all! First time poster and novice coder/game-maker here. I am having a problem with keyboard input in a small project I started to throw together. Code below for those who want to skip down to that. No external files or images, so you should be able to just plug it in and run it yourself.

Brief explanation of project: I have been playing a lot of Persona 5 and wanted to create a simple prototype game with an "overworld" in which you can move around and a classic JRPG "turn-based battle" mode that occurs when you run into an enemy.

Intended result: In "battle mode", player can use arrow keys to move a selector around a menu and choose an action with "enter" key (not yet implemented except for "Give Up" to exit battle).

Problem: After entering battle mode, I cannot move the selector around the menu with the arrow keys. Nothing happens. The strange thing is that if I set battle = true in the code and start the game in battle mode, I can move the selector around the menu using the arrow keys. However if I then select "give up" and re-enter the battle, the same problem will occur. I suspect that I am misunderstanding something about love.keypressed. Thanks for your help!

Code: Select all

function love.load()
  love.graphics.setBackgroundColor(135/255, 206/255, 235/255)
  player = {}
  enemy = {}
  
  player.width = 10
  player.height = 20
  player.x = 10
  player.y = 300 - player.height
  player.speed =1
  
  enemy.width = 15
  enemy.height = 40
  enemy.x = 150
  enemy.y = 300 - enemy.height
  
  battle = true
  
  select = {}
  select.position = 1
  select.width = 20
  select.height = 10
  select.x = 350 + 112 - select.width - 2
  select.y = 450 + 2
end

function love.update()
  if battle == false then
    worldUpdate()
  end
end

function love.draw()
  if battle == false then
    worldDraw()
    love.graphics.print("battle = false", 100, 100)
  else
    battleDraw()
    love.graphics.print("battle = true", 100, 100)
  end
end

function love.keypressed(key)
  if battle == true then
    if key == "right" then
      if select.position == 1 or select.position == 3 then
        select.position = select.position + 1
        select.x = select.x + 225
      else
        select.position = select.position - 1
        select.x = select.x - 225
      end    
    end
  
    if key == "left" then
      if select.position == 2 or select.position == 4 then
        select.position = select.position - 1
        select.x = select.x - 225
      else
        select.position = select.position + 1
        select.x = select.x + 225
      end
    end
    
    if key == "down" then
      if select.position == 1 or select.position == 2 then
        select.position = select.position + 2
        select.y = select.y + 100
      else
        select.position = select.position - 2
        select.y = select.y - 100
      end
    end
    
    if key == "up" then
      if select.position == 3 or select.position == 4 then
        select.position = select.position - 2
        select.y = select.y - 100
      else
        select.position = select.position + 2
        select.y = select.y + 100
      end
    end
    
    if key == "return" then
      if select.position == 1 then
        --Attack
      elseif select.position == 2 then
        --Prepare
      elseif select.position == 3 then
        --Item
      elseif select.position == 4 then
        --Give Up
        battle = false
      end
    end
  end
end


function worldUpdate()
  if love.keyboard.isDown("right") then
    player.x = player.x + player.speed
  end
  
  if love.keyboard.isDown("left") then
    player.x = player.x - player.speed
  end
  
  if checkCollision(player, enemy) == true then
    battle = "true"
    player.x = 100
  end
end

function worldDraw()
  --platform
  love.graphics.rectangle("fill", 0, 300, 800, 600)
  
  --player
  love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
  
  --enemy
  love.graphics.rectangle("line", 150, 260, 15, 40)
end

function battleDraw()
  love.graphics.setColor(255, 255, 255)
  
  --player
  love.graphics.rectangle("line", 50, 250, player.width * 25, player.height * 25)
  
  --enemy
  love.graphics.rectangle("line", 600, 150, enemy.width * 5, enemy.height * 5)
  
  --box
  love.graphics.rectangle("fill", 350, 400, 450, 200)
  
  --text
  love.graphics.setColor(0, 0, 0)
  love.graphics.print("Attack", 350 + 112, 450)
  love.graphics.print("Item", 350 + 112, 550)
  love.graphics.print("Prepare", 350 + 337, 450)
  love.graphics.print("Give Up", 350 + 337, 550)
  
  --selector
  love.graphics.rectangle("fill", select.x, select.y, select.width, select.height)
  love.graphics.print(select.position, 100, 120)
end

function checkCollision(a, b)
  local a_left = a.x
  local a_right = a.x + a.width
  local a_top = a.y
  local a_bottom = a.y + a.height
  
  local b_left = b.x
  local b_right = b.x + b.width
  local b_top = b.y
  local b_bottom = b.y + b.height
  
  if a_right > b_left and
  a_left < b_right and
  a_bottom > b_top and
  a_top < b_bottom then
    return true
  else
    return false
  end
end
Last edited by dichotommy on Fri Nov 23, 2018 5:31 pm, edited 1 time in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Problem with user input in turn-based battle

Post by Nixola »

Hi! First of all, welcome to the forum!
Second, it would be very helpful to us if you enclosed your code in [code][/code] tags, so it would be displayed indented, monospaced, highlighted and in a box so that it doesn't span several screens of height.
Third, uploading a .love file is even more useful as it allows people to quickly run the code with modifications, to figure out faster what the issue is and how to help you.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
dichotommy
Prole
Posts: 4
Joined: Thu Nov 22, 2018 11:09 am

Re: Problem with user input in turn-based battle

Post by dichotommy »

Thank you Nixola! I edited my first post, and in the course of doing so, found my problem: some stray quotation marks changing what should've been a boolean into a string. Case closed.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with user input in turn-based battle

Post by pgimeno »

Funnily enough, it wouldn't have been a problem if it weren't because you're comparing values that are assumed to be boolean, with their boolean values. This is generally considered bad style. Using the boolean variable itself as the condition helps with code readability, especially if you name your boolean variables and functions appropriately

For example:

Code: Select all

if isColliding(player, enemy) then
  battleMode = true
end

...

if battleMode then
  -- do stuff that is done when in battle mode
end

...

if not battleMode then
  -- do stuff that is done when not in battle mode
end
dichotommy
Prole
Posts: 4
Joined: Thu Nov 22, 2018 11:09 am

Re: Problem with user input in turn-based battle

Post by dichotommy »

I see! I had noticed this in others' code but didn't totally understand why they were writing it that way. Thank you very much pgimeno :)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 13 guests