Sliding Text

Category: PyAngelo

Last updated 1 year ago

Thisum Wijendra

Thisum Wijendra Asked 1 year ago

How do you make the text slide across the screen on a canvas then suddenly stop in the middle? With cycling rbg colours for the text if possible?

NOTE: This is for the top down car race video game.


Jeff Plumb

Jeff Plumb Answered 1 year ago

Hi Thisum,

Here's one way you could achieve that effect:

from sprite import *
setCanvasSize(640, 360)
title = TextSprite("Racing Game", -100, 160)
red = 0
title.setColour(red, 100, 200)

while title.x < 260:
    background(220, 220, 220)
    title.draw()
    title.moveBy(1, 0)
    red = (red + 1) % 256
    title.setColour(red, 100, 200)
    sleep(0.005)

We create an object called "title" from the TextSprite class. This means that wth the "title" object we can use many methods including draw(), moveBy(), and setColour(). To change the colour we are simply changing the red value by 1 each time through the loop and using the modulus operator to set it back to zero if it passes 256. Let me know if that helps.


Notify me of updates
Add to Favourites
Back to Questions

No comments yet!


Become a free member to post a comment about this question.