Page 1 of 1

please explain these lines of code

Posted: Tue Jul 21, 2020 4:14 pm
by blinmakerivan
i am taking the cs50 game development course and came across the following code for one project. i believe it is to print a flipped sprite

function Pipe:render()
love.graphics.draw(PIPE_IMAGE, self.x,
(self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y),
0, 1, self.orientation == 'top' and -1 or 1)
end

can some one please explain the draw part. there should be a y value after self.x but (self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y) will return true or false
pls send help
here is the full code in question
Pipe = Class{}

-- since we only want the image loaded once, not per instantation, define it externally
local PIPE_IMAGE = love.graphics.newImage('pipe.png')

-- speed at which the pipe should scroll right to left
PIPE_SPEED = 60

-- height of pipe image, globally accessible
PIPE_HEIGHT = 288
PIPE_WIDTH = 70

function Pipe:init(orientation, y)
self.x = VIRTUAL_WIDTH
self.y = y

self.width = PIPE_IMAGE:getWidth()
self.height = PIPE_HEIGHT

self.orientation = orientation
end

function Pipe:update(dt)

end

function Pipe:render()
love.graphics.draw(PIPE_IMAGE, self.x,
(self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y),
0, 1, self.orientation == 'top' and -1 or 1)
end

heres the link
https://github.com/games50/fifty-bird/b ... 6/Pipe.lua

Re: please explain these lines of code

Posted: Tue Jul 21, 2020 11:28 pm
by FloatingBanana
The "and" operator returns the right value if the left value is "true", otherwise it will return the right value. The "or" operator returns the right value if the left value is "false", otherwise it will return the left value. Remember that any value except "nil" and "false" are considered "true".

Example:

Code: Select all

local value = "something"

print(true and value) --output: something
print(false and value) --output: false

print(true or value) --output: true
print(false or value) --output: "something"

Re: please explain these lines of code

Posted: Wed Jul 22, 2020 2:55 pm
by blinmakerivan
I understand this. but shouldn't there be a number for the y value after the x value
pls help

Re: please explain these lines of code

Posted: Wed Jul 22, 2020 5:33 pm
by FloatingBanana
self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y

" self.orientation == 'top' " will be evaluated first and will return a boolean value. If the value is "true", then "self.y + PIPE_HEIGHT" is returned, but if the value is "false" then "self.y" is returned.

This is the same as writting this:

Code: Select all

local pipe_y = 0

if self.orientation == 'top' then
    pipe_y = self.y + PIPE_HEIGHT
else
    pipe_y = self.y
end

love.graphics.draw(PIPE_IMAGE, self.x, pipe_y, 0, 1, self.orientation == 'top' and -1 or 1)

Re: please explain these lines of code

Posted: Thu Jul 23, 2020 2:21 pm
by blinmakerivan
oh ok thanks

Re: please explain these lines of code

Posted: Tue Nov 14, 2023 9:01 am
by josephpayne
In the love.graphics.draw() function, (self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y) is not returning true or false, but rather it's using Lua's version of a ternary operation.

This can be read as: If self.orientation equals 'top', then use self.y + PIPE_HEIGHT, else use self.y. This provides the y value for the draw function. It's determining where to draw the pipe based on its orientation.

Similarly, self.orientation == 'top' and -1 or 1 is setting the scale factor for the sprite. If the pipe's orientation is 'top', it will flip the sprite upside down (scale -1), else it will keep it right-side up (scale 1).

So, if the pipe's orientation is 'top', the pipe image will be drawn flipped vertically at position self.y + PIPE_HEIGHT. If the orientation is not 'top', the pipe image will be drawn at position self.y without flipping.