Log In  

I have done a game code for a red ball that I can move in a line. I have figured out the code for that. However, I am stuck on making it change a color when it hits the end of the pico 8 screen. I have searched it online and have had no luck. Can anyone explain what I need to do to get the ball to change color? Thank you
Here is my code so far without changing the color.

col=0
function _init()
cls()
xpos = 64
ypos = 64
col = 8
size = 5
end

function _update60()
if (btn(0) and xpos > 0) xpos -= 1
if (btn(1) and xpos < 127) xpos += 1
end
if xpos < 33 then col = 5
end
function _draw()
cls()
circfill(xpos, ypos, size, col)
end

P#67310 2019-09-05 23:38

Some of your IF statements are missing the THEN clause and END, otherwise it worked perfectly. The ball turns from red to grey (color 8 to color 5) when posx is less than 33. I added some code to turn it back to red when it is greater than or equal to 33.

col=0 --Not strictly necessary. Lua variables are global by default unless preceded with the 'local' keyword 

function _init()
 cls()
 xpos = 64
 ypos = 64
 col = 8
 size = 5
end

function _update60()
 if (btn(0) and xpos > 0) then 
  xpos -= 1
 end
 if (btn(1) and xpos < 127) then
  xpos += 1
 end

 if xpos < 33 then 
  col = 5
 else
  col = 8
 end
end 

function _draw()
 cls()
 circfill(xpos, ypos, size, col)
end
P#67313 2019-09-06 00:39

A correction to my previous post:
Someone corrected my misunderstanding of how Pico-8 handles IF statements as opposed to standard lua in another post. Pico-8 does not require THEN or END on if statements as long as the condition is in parentheses and the code to run on a true condition is on the same line. Therefore, the following code will work as well:

function _init()
 cls()
 xpos = 64
 ypos = 64
 col = 8
 size = 5
end

function _update60()
 if (btn(0) and xpos > 0) xpos -= 1

 if (btn(1) and xpos < 127) xpos += 1

 if (xpos < 33) col = 5 else col = 8

end 

function _draw()
 cls()
 circfill(xpos, ypos, size, col)
end
P#67319 2019-09-06 03:53 ( Edited 2019-09-06 03:53)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 19:17:54 | 0.006s | Q:11