## please explain these lines of code

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

### please explain these lines of code

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

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

FloatingBanana
Prole
Posts: 19
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

### Re: please explain these lines of code

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

I understand this. but shouldn't there be a number for the y value after the x value
pls help

FloatingBanana
Prole
Posts: 19
Joined: Sat Mar 02, 2019 4:57 pm
Contact:

### Re: please explain these lines of code

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

oh ok thanks

### Who is online

Users browsing this forum: No registered users and 9 guests