How do I change card graphic?

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.
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

How do I change card graphic?

Post by Lacotemale »

Hey all,

So ive been busy porting over a game I made in c programming to Lua and it has even made more progress than that version. However, I have hit a wall this time. Its such a simple thing, I want to change the card graphic from blue to red if the player card beats another. If the enemy beats a player card change graphic from red version to blue.

Im not sure what is going on but I just know its not working right for me. I could be going at this completely wrong, I just don't know. Im posting the love file as this game is only for practice. Its a clone of the minigame found in FFVIII - Triple Triad.

Any help would be greatly appreciated, so I can continue to learn Lua and Love2d. :)

updateCardImage is the function to look at.
Attachments
TTU-rev16.tar.gz
(2.73 MiB) Downloaded 206 times
User avatar
zorfmorf
Prole
Posts: 35
Joined: Sat Mar 10, 2012 12:31 pm

Re: How do I change card graphic?

Post by zorfmorf »

I took a look but for now I'm afraid I can't help you as I've no idea whats going on. I can place blue cards on the field, red cards appear and move around and sooner or later blue cards start to turn red.

Maybe you can explain a little bit more? When does a card beat another? And do you want to actually change the card (is it like capturing cards) or do you just want to visually indicate, that one card beats the other?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How do I change card graphic?

Post by kikito »

Lacotemale wrote: I want to change the card graphic from blue to red if the player card beats another. If the enemy beats a player card change graphic from red version to blue.
It would be easier to help you if you explained what "a card beats another" means.
Lacotemale wrote:Its a clone of the minigame found in FFVIII - Triple Triad.
I have not played that, and I'm sure I am not the only one. I suggest you explain the rules. Otherwise the only people able to help you would be the ones that have played it.
When I write def I mean function.
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: How do I change card graphic?

Post by Lacotemale »

Ah right, somehow I thought everyone must have played it. :D Let me break down some details to make it easier.

So in Triple Triad each card has 4 static values. These values are compared to adjacent card values if next to an enemy card. (blue is playerCard, red is enemyCard) I named them like North, West, East and South so..

wAtk (is west attack/value on the left of the card)
nAtk (is north attack/value on the top of the card)
eAtk (east attack/value on the right of the card)
sAtk (south attack/value on the bottom of the card)

When you place a card the values of the card are set on the cardSpot. We have 9 cardSpots on the playmat. Note cardSpot Objects start with a c.

Eg.. c1 equals cardspot 1. c2 equals cardspot 2.. etc.

To set a card to compare we need to select a card from the player hand and place it on a cardspot. (cardSelected and spotSelected required)

So I compare values by using this function:

Code: Select all

function compareCardValues()

    print("COMPARE: cardSelected "..cardSelected.." and ".."spotSelected "..spotSelected)
  
    if(spotSelected==1) then  //the first cardspot c1
        if((c1.eAtk>0 and c2.wAtk>0) and (c1.eAtk>c2.wAtk)) then  //check if adjacent spots have values set from card and if c1 beats c2
            if(c1.owner==1 and c2.owner==2) then  //check if cardspot 1 owner is player(1) and cardspot 2 owner is enemy(2)
                playerScore = playerScore+1  //increment player score if player wins comparison
                print("PLAYER: c1 win card c2")  //spit it out to console that cardspot 1 wins cardspot 2
                updateCardImage(c2) //update the card image of the losing entity (player or enemy)
                updateScore(s1,playerScore,c1)  //update the score of the player since the player wins
            elseif(c1.owner==2 and c2.owner==1) then  //do the same kinda thing for the enemy
                enemyScore = enemyScore+1
                print("ENEMY: c1 win card c2")
                updateCardImage(c2)
                updateScore(s2,enemyScore,c1)
            end
        end

//etc...
Let me know if ye have more questions. I will gladly explain. The comparison and updating of the score works fine. Updating the graphic is the part that does not.
User avatar
DrCicero
Prole
Posts: 19
Joined: Sun Jun 01, 2014 8:56 pm
Location: Germany

Re: How do I change card graphic?

Post by DrCicero »

Actually im not sure why the images are not updated. However your code is quite a mess ;)

A simple indicator that your making yourself too much work is when you start naming variables with indices. Remember to stay DRY (Dont repeat yourself), much code you wrote is simply copy-pasted 10 times in a row! I simplified the functions love.load, love.draw and getCard for you and you should simplify/adapt the other functions, too, because that will make reasoning about the actual problem much easier. (Also i renamed some variables so that i actually understand what they are used for.)
You dont have to use my code as written, but i hope that it servers as an inspiration to what you are actually able to do in lua!

If you don't understand what i changed, please ask. :ultrahappy: I hope i could help you.

Code: Select all

function love.load()
    love.window.setTitle("TripleTriad Ultimate")
    bg = love.graphics.newImage( '/images/playmat.png' )

    spots = {}
    for i=1,9 do
      spots[i] = Cardspot:new("fancy", 0, 0, 0, 0)
      spots[i].x = 220 + (i%3)*130
      spots[i].y = 120 + math.floor(i/3)*150
    end

    playerHand = {"bitebug","blobra","bloodsoul","caterchipillar","cockatrice"}
    enemyHand = {}
    for i=0,5 do
        enemyHand[i] = Cards[love.math.random(1, 11)]
    end

    cardsOnTable = {}
    for i=1,5 do
      cardsOnTable[i] = getCard(playerHand[i])
      cardsOnTable.x = 40
      cardsOnTable.y = 100+i*60
      cardsOnTable.owner = "blue"
    end
    for i=6,10 do
      e[i] = getCard(enemyHand[i-5])
      e.x = 660
      e.y = 100+i*60
      e.owner = "red"
    end

    s = {
      Score:new(playerScore), 
      Score:new(enemyScore)
    }
    s[1].x, s[1].y = 80, 50
    s[2].x, s[2].y = 700, 50

    if gameStarted == 0 then
        turn = love.math.random(1,2)
        print(turn)
    end

    if turn == 2 then
        enemyTurn()
    end
end

Cards = {"bitebug", "blobra", "bloodsoul", "caterchipillar", "cockatrice", "fastitocalon-f", "funguar", "gayla", "geezard", "gesper", "redbat"}
-- to get PlayerCards do: Cards[i]
-- to get EnemyCards do: 'r' .. Cards[i]

local cardTemplates = {
  bitebug        = {5,1,3,3},
  blobra         = {5,2,3,1},
  bloodsoul      = {1,2,1,6},
  caterchipillar = {3,4,2,4},
  cockatrice     = {6,2,1,2},
  fastitocalon-f = {1,3,5,2},
  funguar        = {3,5,1,1},
  gayla          = {4,2,1,4},
  geezard        = {5,1,4,1},
  gesper         = {1,1,5,4},
  redbat         = {2,6,1,1},
}
function getCard(name, owner)
  local filename = name
  if owner == "red" then filename = "r" .. filename end
  return Card:new(filename, cardTemplate[0], cardTemplate[1], cardTemplate[2], cardTemplate[3])
end

function love.draw()
    love.graphics.draw(bg, 0, 0)

    for i, o in ipairs(spots) do
      love.graphics.draw(o.image, o.x, o.y)
    end
    for i, o in ipairs(playerHand) do
      love.graphics.draw(o.image, o.x, o.y)
    end
    for i, o in ipairs(enemyHand) do
      love.graphics.draw(o.image, o.x, o.y)
    end
    for i, o in ipairs(cardsOnTable) do
      love.graphics.draw(o.image, o.x, o.y)
    end
end
Xeldas Saga - my ca. 10min action-platformer (contains sound effects, music, graphics and a boss fight)
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: How do I change card graphic?

Post by Lacotemale »

Thank you so very much! Maybe a step in the right direction is taking a step back first. I know my original code is trash pretty much. :D In game development I do it for fun so I just do simple code and im still new to Lua.

In the past I would set the attack values on the cardObject. Like
c1.wAtk = 3
, what way should I do this going forward? Like spots[0].wAtk = 3 ?
User avatar
DrCicero
Prole
Posts: 19
Joined: Sun Jun 01, 2014 8:56 pm
Location: Germany

Re: How do I change card graphic?

Post by DrCicero »

If i understand that game correctly, the atk never changes and only depends on the type of the card. So you could have a associative array that is indexed by the type of card and gives you the Atk values of all four directions:

Code: Select all

local cardTemplates = {
  bitebug        = {5,1,3,3},
  blobra         = {5,2,3,1},
  bloodsoul      = {1,2,1,6},
  caterchipillar = {3,4,2,4},
  cockatrice     = {6,2,1,2},
  fastitocalon-f = {1,3,5,2},
  funguar        = {3,5,1,1},
  gayla          = {4,2,1,4},
  geezard        = {5,1,4,1},
  gesper         = {1,1,5,4},
  redbat         = {2,6,1,1},
}
You can then simply access the atk values:

Code: Select all

spot[1].type = "bitebug"

local atks = cardTemplate[spot[1].type]
local nAtk = atks[1]
local eAtk = atks[2]
local sAtk = atks[3]
local wAtk = atks[4]
This way you only need to change the 'type' of a card (a string value).
If cardTemplate is to long for you to type you could also use one of the following variable names: monsters, cards, templates, stats
Actually i like stats, very short but still tells you what it is about!

Oh and: :monocle:
Lacotemale wrote:, what way should I do this going forward? Like spots[0].wAtk = 3 ?
Please note that in lua the first element of an array has index 1, while in most other languages start counting at 0. Its supposed to be easier to understand, but i find it quite confusing. (Lua uses indexes, others use offsets). That means array[0] is almost always an error in lua.

Any questions? :ultrahappy:

EDIT: spots[1].wAtk would obviously work, too :awesome:
Xeldas Saga - my ca. 10min action-platformer (contains sound effects, music, graphics and a boss fight)
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: How do I change card graphic?

Post by Lacotemale »

Cool! Umm just one thing. I put your code into my project but im getting a weird bug on draw.
Error: main.lua:1001: bad argument #1 to 'draw' (Drawable expected, got nil)
stack traceback:
[C]: in function 'draw'
main.lua:1001: in function 'draw'
[string "boot.lua"]:438: in function <[string "boot.lua"]:399>
[C]: in function 'xpcall'
Any ideas? It happens with all of the draw methods you showed me. :?
Attachments
rewriteTTU.tar.gz
(2.73 MiB) Downloaded 173 times
User avatar
DrCicero
Prole
Posts: 19
Joined: Sun Jun 01, 2014 8:56 pm
Location: Germany

Re: How do I change card graphic?

Post by DrCicero »

Oh, i'm sorry. I looked at your old code and saw, that those variables (playerHand, enemyHand) were never supposed to be drawn :roll: So just remove those 6 lines. Or do it with even less code:

Code: Select all

function love.draw()
    love.graphics.draw(bg, 0, 0)

    for _, array in ipairs({s, spots, playercardsOnTable, enemycardsOnTable}) do
        for _, element in ipairs(array) do
            love.graphics.draw(element.image, element.x, element.y)
        end
    end
end
EDIT: :shock: In line 1001 i gave you cardTemplate[0], The first element is cardTemplate[1]!
And in line 943 i gave you a for-loop starting at 0 (for i=0,5 do), you probably wanted to go from 1 to 5.
Xeldas Saga - my ca. 10min action-platformer (contains sound effects, music, graphics and a boss fight)
User avatar
Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

Re: How do I change card graphic?

Post by Lacotemale »

Umm.. are you sure about this? It looks a bit wrong to me. xD
hmm.png
hmm.png (637.83 KiB) Viewed 7298 times
I have attached the fixed code from your fix suggestions.
rewriteTTU-updated.tar.gz
(2.73 MiB) Downloaded 244 times
Post Reply

Who is online

Users browsing this forum: No registered users and 85 guests