please explain these lines of code

Discuss any ports of LÖVE to different platforms.
Post Reply
blinmakerivan
Prole
Posts: 3
Joined: Tue Jul 21, 2020 4:05 pm

please explain these lines of code

Post 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
User avatar
FloatingBanana
Prole
Posts: 22
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

Re: please explain these lines of code

Post 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"

Code: Select all

if anyMistake(self.english) then
    print("Sorry, english is not my first language")
end
blinmakerivan
Prole
Posts: 3
Joined: Tue Jul 21, 2020 4:05 pm

Re: please explain these lines of code

Post by blinmakerivan »

I understand this. but shouldn't there be a number for the y value after the x value
pls help
User avatar
FloatingBanana
Prole
Posts: 22
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

Re: please explain these lines of code

Post 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)

Code: Select all

if anyMistake(self.english) then
    print("Sorry, english is not my first language")
end
blinmakerivan
Prole
Posts: 3
Joined: Tue Jul 21, 2020 4:05 pm

Re: please explain these lines of code

Post by blinmakerivan »

oh ok thanks
josephpayne
Prole
Posts: 2
Joined: Tue Nov 14, 2023 8:57 am

Re: please explain these lines of code

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests