Can't make my pygame colour change stop at the end of the loop

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 4448 Views - Last Post: 27 May 2017 - 04:07 PM Rate Topic: -----

#1 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Can't make my pygame colour change stop at the end of the loop

Posted 21 April 2017 - 04:27 PM

I tried to make a candle program previously with pygame and it looked like something a kid in kindergarten would've drawn using crayons. I decided to try something else. I am trying to make each letter of the string that appears on the screen randomly flash a different colour and then at the end of the loop stop at the colour it was flashing. I know using regular python I could do something like this:

def String():
	import random, time
	string=["red","blue","green","white","black","yellow"]
	for i in range(10):#amount of times I want the letter to flash
		#before stopping
		ran=random.choice(string)
		print ran
		time.sleep(.05)


I have actually wanted to use..
self.rancol=(random.randint(0,255),random.randint(0,255),random.randint(0,255)
But I when I was trying to figure out how to make it work (and the only way I could figure out was what I did using python) I thought this would be the easiest way to do it.

I have two questions actually.
1. Any suggestions how to make each letter randomly loop different colours and then stop at the end of the loop?.
2. Is there an easier way to do this?. I'm using a method for each letter of the string and at this rate it just seems like I'm doing something wrong. I'm trying to spell out happy birthday and then below my wifes name.
This is my program below.
import pygame, random, time
 
# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue =(0, 0, 225)
lightblue=(153,204,255)
dodgerblue=(30,144,255)
gray =(224,224,224)
darkgray = (128,128,128)
yellow=(255,255,0)
lightorange =(255,178,102)
orange=(225,165,0)
orangered=(255,69,0)
tomato =(255,99,71)
gold =(255,215,0)
hotpink=(255,105,180)
deeppink=(255,20,147)
pink=(255,192,203)

pygame.init()

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("animate text")
 
# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

class Stuff(object):
    def __init__(self):
        self.col=(black,green,red,blue,lightblue,dodgerblue,yellow,lightorange,orange,orangered,
                  tomato,hotpink,deeppink,pink)
        self.ran=random.choice(self.col)

    def firstlet(self):
        for i in range(10):
            self.ran=random.choice(self.col)
            text=font.render("H",True,self.ran)
            screen.blit(text,[150,150])

    def secondlet(self):
        for i in range(10):
            self.ran=random.choice(self.col)
            text=font.render("a",True,self.ran)
            screen.blit(text,[177,150])
            

    def thirdlet(self):
        for i in range(10):
            self.ran=random.choice(self.col)
            text=font.render("p",True,self.ran)
            screen.blit(text,[200,150])

    def fourthlet(self):
        for i in range(10):
            self.ran=random.choice(self.col)
            text=font.render("p",True,self.ran)
            screen.blit(text,[224,150])

    def fifthlet(self):
        for i in range(10):
            self.ran=random.choice(self.col)
            text=font.render("y",True,self.ran)
            screen.blit(text,[248,150])
            time.sleep(.05)

    def Main(self):
        a.firstlet()
        a.secondlet()
        a.thirdlet()
        a.fourthlet()
        a.fifthlet()
        
    #This will be the function that will handle everything
font = pygame.font.Font(None,60)

while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Drawing code should go here
    screen.fill(white)
    a=Stuff()
    a.Main()
    pygame.display.flip()
 
    # --- Limit to 60 frames per second
    clock.tick(60)
 
# Close the window and quit.
pygame.quit()




Is This A Good Question/Topic? 0
  • +

Replies To: Can't make my pygame colour change stop at the end of the loop

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 563
  • Posts: 2,315
  • Joined: 13-June 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 21 April 2017 - 09:58 PM

There's a lot of duplication there with all those methods. It seems like you should just write a single method that does all the things that all of those have in common, taking parameters for the things that vary (like the letter).
Was This Post Helpful? 1
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 06:01 AM

The comment by ndc85430 is VERY important. Look at it and come back if you cannot figure it out.

But moreover, you need to decide where to loop.
In the letter-method you have a 10-fold loop without an update of the display - you don't ever see this loop in action! It executes in milliseconds and is invisible; and then stops.
In you main, however, you keep creating new instances of Stuff() and call the Main() method on this. This is why you see the color-changing letters. And this loop don't stop!

My advice:

Rename 'Stuff()' to 'Letter()'
Create instances of Letter(), one for each letter in you text. Those instances you keep in a list 'letters'.
You can use a loop to create the instances:
my_text = "Happy"
letters = []
for n, letter in enumerate(my_text):
    x_pos = 150 + n * 25
    letters.append(Letter(letter, x_pos)) 


The Letter class should have a method named 'draw()' and maybe one named 'update()' where the color is changed.

Finally the main goes:
for i in range(10):
    screen.fill(white)
    for letter in letters:
        letter.update()
        letter.draw()
    pygame.display.flip()
    pygame.time wait(200)


I'm sure you can work out the details, - otherwise you know where to ask for assistance...

This post has been edited by DK3250: 22 April 2017 - 06:02 AM

Was This Post Helpful? 1
  • +
  • -

#4 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 09:18 AM

I got inspired by albert003:

View Postalbert003, on 22 April 2017 - 12:27 AM, said:

I tried to make a candle program previously with pygame and it looked like something a kid in kindergarten would've drawn using crayons.


Take a look at this; hopefully it may be of inspiration for future projects.

Posted Image

"""
Demo program by DK3250
Flickering Candle

April 2017
"""
import pygame, sys, random
pygame.init()

X = 600
Y = 400

screen = pygame.display.set_mode((X, Y))
pygame.display.set_caption("Flickering Candle")

BLACK = (0, 0, 0)

pix = pygame.surface.Surface((4, 4))  #the individual 'pixels' in candle
surf = pygame.surface.Surface((20, 200))  # the candle surface
for i in range(0, 20, 4):
    for j in range(0, 200, 4):
        ran = random.randint(200, 255)
        pix.fill([ran]*3)  # creation of random grey colors
        surf.blit(pix, (i, j))
screen.blit(surf, (100, 100))

surf = pygame.surface.Surface((30, 60))  # the flame surface
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
    for i in range(5):
        ratio = random.randint(0, 100)
        # creation of random colors within (200, 100, 60) - (255, 255, 0)
        color = ((200 + (255-200)/100*ratio),
                 (100 + (255-100)/100*ratio),
                 (60 - (0-60)/100*ratio))
        # the flame top
        pygame.draw.polygon(surf, color, ((i*3,45), (15, i*3), (30-i*3, 45)), 0)
        # the flame body
        pygame.draw.circle(surf, color, (15, 45), 15-i*3, 0)

    # mask to get flame shape
    pygame.draw.circle(surf, BLACK, (-33, 0), 50, 0)
    pygame.draw.circle(surf, BLACK, (63, 0), 50, 0)

    screen.blit(surf, (95, 40))
    pygame.display.flip()
    pygame.time.wait(500)



EDIT:
..and an even more natural-looking version:
"""
Demo program by DK3250
Flickering Candle

April 2017
"""
import pygame, sys, random
pygame.init()

def get_flame_color():
    """
    Generates colors linear distributed in the range
    (255, 255, 0) to (200, 100, 30)

    The colors will smooth and gradually
    turn from one shade to another.
"""
    color = [255, 255, 0]              # start color
    colEnd = random.randint(200, 255)  # the red color end point

    while True:
        dif = colEnd - color[0]
        if abs(dif) > 1:
            color[0] += dif/abs(dif)
            color[1] = 100 + 155/55*(color[0] - 200)
            color[2] = 30 - 30/55*(color[0] - 200)
            yield color
        else:
            colEnd = random.randint(200, 255)

X = 600
Y = 400

screen = pygame.display.set_mode((X, Y))
pygame.display.set_caption("Flickering Candle")

BLACK = (0, 0, 0)

class Candle():
    def __init__(self, x):
        self.get_color = get_flame_color()
        self.x = x  # position
        pix = pygame.surface.Surface((4, 4))  #the individual 'pixels' in candle
        self.candle_surf = pygame.surface.Surface((20, 200))  # the candle surface
        for i in range(0, 20, 4):
            for j in range(0, 200, 4):
                ran = random.randint(200, 255)
                pix.fill([ran]*3)  # creation of random grey colors
                self.candle_surf.blit(pix, (i, j))

        self.flame_surf = pygame.surface.Surface((30, 60))  # the flame surface

    def make_flame(self):        
        flick = random.randint(-3, 3)  # variation  of flame top position

        for i in range(0, 13):
            color = next(self.get_color)  # get color from generator
            # the flame top
            pygame.draw.polygon(self.flame_surf, color, ((i, 45), (15+flick, i), (30-i, 45)), 0)
            # the flame body
            pygame.draw.circle(self.flame_surf, color, (15, 45), 15-i, 0)

        # mask to get flame shape
        pygame.draw.circle(self.flame_surf, BLACK, (-33+flick, flick//2), 50, 0)
        pygame.draw.circle(self.flame_surf, BLACK, (63+flick, -flick//2), 50, 0)

    def draw(self):
        self.make_flame()
        screen.blit(self.candle_surf, (self.x, 100))
        screen.blit(self.flame_surf, (self.x-5, 100))


candles = []
for i in [100, 200, 300, 400]:
    candles.append(Candle(i))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(BLACK)
    for c in candles:
        c.draw()
    pygame.display.flip()
    pygame.time.wait(300)


The masking in line 64/65 defines the flame shape variation.
For a more steady flame use (or an intermediate):
pygame.draw.circle(self.flame_surf, BLACK, (-33+flick, -flick), 50, 0)
pygame.draw.circle(self.flame_surf, BLACK, (63+flick, flick), 50, 0)


This post has been edited by DK3250: 22 April 2017 - 12:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 05:43 PM

Thanks for the advice. You did a better job with the candle than I did.

I knew I was doing something wrong. I couldn't figure a way to draw each letter using just one method. After I sent this request for help I thought about trying to slice the string to make it print each letter one at a time. But I think your suggestion is cleaner and easier.


I have a question, so I tried to finish off the method Draw so it would render the string "Happy birthday". When I run the program, I get a blank screen. Can you give me a hint why it didn't render the string?. This is what I did to the program after the suggestions you guys gave me.

import pygame, random, time, string
 
# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue =(0, 0, 225)
lightblue=(153,204,255)
dodgerblue=(30,144,255)
gray =(224,224,224)
darkgray = (128,128,128)
yellow=(255,255,0)
lightorange =(255,178,102)
orange=(225,165,0)
orangered=(255,69,0)
tomato =(255,99,71)
gold =(255,215,0)
hotpink=(255,105,180)
deeppink=(255,20,147)
pink=(255,192,203)

pygame.init()

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("animate text")
 
# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

class Letter(object):
    def __init__(self):
        self.col=(black,green,red,blue,lightblue,dodgerblue,yellow,lightorange,orange,orangered,
                  tomato,hotpink,deeppink,pink)
        self.ran=random.choice(self.col)

    def Draw(self):
        #This will render the text on the screen
        my_text = "Happy birthday"
        for n, letter in enumerate(my_text):
            x_pos = 150 + n *25
            letters.append(Letter(letter,x_pos))
            text=font.render(my_text,True,self.col)
            screen.blit(text,[x_pos,150])

    def update(self):
        #This will control the colour change of the text
        #It will make the letter stop flashing at the end
        #of the loop and it will stay on the last colour it rendered
        for i in range(10):
            self.ran=random.choice(self.col)
            
        
    def Main(self):
        letters = []
        for i in range(10):
            screen.fill(white)
            for letter in letters:
                letter.update()
                letter.draw()
            pygame.display.flip()
            pygame.time.wait(200)

        
    #This will be the function that will handle everything
font = pygame.font.Font(None,60)

while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Drawing code should go here
    screen.fill(white)
    a=Letter()
    a.Main()
    pygame.display.flip()
 
    # --- Limit to 60 frames per second
    clock.tick(60)
 
# Close the window and quit.
pygame.quit()

This post has been edited by albert003: 22 April 2017 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 06:09 PM

You misunderstood my wording 'main'; this refers to the true main of the code, not the method 'Main()' in the class.

Likewise with the small loop for creation of Letter instances; you have placed it in the 'Draw()' method, I meant to place it in the main (not Main()).

The Letter() class, is very simple, only 3 methods: __init__(), update() and draw()
In my version 9 lines all in all.

Give it a try.

I have placed a working version under spoiler protection. But do try for yourself first!

Spoiler

I used the spoiler option to be sure you'll have a working code for the birthday. Congratulate your wife from me.

EDIT:
This type of text works best with a monospaced font (all letters have same width) like Courier.
You can define it by
font = pygame.font.SysFont("Courier", 60)

Other monospaced fonts can be downloaded as .ttf files
I recommend downloading fonts; the font-part of pygame is not very good, to say the least!
I normally don't use downloaded fonts in my examples, - to reduce complexity. But very often in my own programs.

This post has been edited by DK3250: 22 April 2017 - 06:27 PM

Was This Post Helpful? 1
  • +
  • -

#7 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 06:55 PM

I get what you mean. I have a question for you. I took a peek at your spoiler and I have a few questions for you:
1. Why didn't you put all_colours in the constructor?
2. I understand how enumerate works, I get this line...
x_pos = 150 + n * 25

but I don't understand why you took n and multiplied it by 25?. Is that 25 to tab the position 25 times?.
3. I thought to make a pygame program work you had to put what you wanted to blit or draw inside the while loop?. For example, couldn't I do this?.
while not done:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
		    done = True
	for i in range(10):
	    screen.fill(white)
	    for letter in letters:
		letter.update()
		letter.draw()
	pygame.display.flip()
	pygame.time.wait(500)


Sorry if question 4 is a dumb question. I've never thought of, considered or seen in the pygame tutorial I used to learn pygame a game loop done this way.
Was This Post Helpful? 0
  • +
  • -

#8 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 07:07 PM

1.
I could have put all_colors in the __init__() method.
However, as the colors are defined in main, I prefer the list in the same place.
But this is a matter of taste.
2.
The enumerate will give you a number sequence: 0, 1, 2, 3,..
This I want to convert to a space distance, in this case I want 25 pixels between the starting point of each letter.
3.
Normally you want the program to loop 'for ever'.
But in this case you only want 10 flash (as I understand it).
Thus, no while-loop.
4.
Your suggestion combines a while-loop (running forever) with a for-loop.
But you do not show the output from the for-loop, - only the last situation will be "flip()'ed" to screen.
If you want a forever loop, you don't need a for-loop.
If you want a limited number of flashes, you don't need a while-loop.
Was This Post Helpful? 1
  • +
  • -

#9 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 22 April 2017 - 07:14 PM

Further to point 2 above, the enumerate can be avoided by:
my_text = "Happy Birthday"
letters = []
x_pos = 150
for letter in my_text:
    letters.append(Letter(letter, x_pos))
    x_pos += 25

Actually, I think this is more clean.
Was This Post Helpful? 1
  • +
  • -

#10 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 23 April 2017 - 01:32 AM

One last comment.
It is possible to account for the variation in width of letters:
font = pygame.font.SysFont("CourierBold", 60)
my_text = "Happy Birthday"
letters = []
x_pos = 150
for letter in my_text:
    letters.append(Letter(letter, x_pos))
    x_pos += font.render(letter, 1, black).get_width()

In this way we advance the x_pos value according to the individual letter width; the requirement for monospace font is no longer necessary.
Just observe, that for some (few) fonts, pygame have bad data and readout wrong width. If this happens, just find another font.

As mentioned before: It is better to import .ttf files and use
font = pygame.font.Font("file_name.ttf", font_size)

Was This Post Helpful? 0
  • +
  • -

#11 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 24 April 2017 - 12:50 PM

I've never heard of ttf files before. So not to sound dumb, if I was to use ttf for the pygame program. Would I then do this?.

font = pygame.font.Font("Happy birthday.ttf", font_size)

I'm guessing because it has a string I would need to do this way.
Was This Post Helpful? 0
  • +
  • -

#12 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 24 April 2017 - 01:34 PM

.ttf is an acronym for TrueTypeFont.
This is the most usual file format for fonts.
You can download ttf files from the internet, many for free, more for a small fee.
Just search 'ttf files'.

So, if you download the font named X-Cryption.ttf (link: http://www.webpagepu...X-Cryption.ttf) you tell pygame to use it by:
font = pygame.font.Font("X-Cryption.ttf", size)  # size can be replaced by an integer
The font must be located in the same directory as the code, or you can indicate the full path in the font string.

You can rename a font to "Happy_Birthday.ttf" but that is only making things unnecessary complicated.

This post has been edited by DK3250: 24 April 2017 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#13 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 24 April 2017 - 07:16 PM

When I tried to go to the ttf link it gave me the error 404 not found. Ill look for it and read about it after I finish perfecting this program.

I got inspired and added a few things to the program. I made it so that when the program finishes randomly looping each letter of the string, it makes the entire string randomly loop through colours. I had an idea to make the next loop go through each letter of the text one by one and only randomly flashing different colours. My last idea and I'm pretty certain I can do it, is to use that pygame snow program you wrote during Christmas. I was going to use what I learned from your matrix tutorial and have the word spread out through the screen, falling and randomly changing colours. I haven't gotten to the falling text (I wanted to do that last). I was able to make the program randomly loop through each letter more or less, but instead of making each letter flash and then moving on to the next letter, it over lapped the text and each text is flashing random colours. I've been working on this since Saturday. This is the program.
import pygame, random, time

# Define some colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue =(0, 0, 225)
lightblue=(153,204,255)
dodgerblue=(30,144,255)
gray =(224,224,224)
darkgray = (128,128,128)
yellow=(255,255,0)
lightorange =(255,178,102)
orange=(225,165,0)
orangered=(255,69,0)
tomato =(255,99,71)
gold =(255,215,0)
hotpink=(255,105,180)
deeppink=(255,20,147)
pink=(255,192,203)
Navy = (0,0,128)
cyan = (0,255,255)
coral = (255,127,80)
violet = (238,130,238)
purple = (160,32,240)

pygame.init()

size = (700, 500)
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("animate text")
font = pygame.font.Font(None,60)

class Letter(object):
    def __init__(self, letter, x_pos):
        self.letter = letter
        self.x = x_pos
        self.all_colours = (black,green,red,blue,lightblue,dodgerblue,yellow,lightorange,orange,orangered,
                  tomato,hotpink,deeppink,pink,cyan,coral,violet,purple)
        
    def update(self):        
        self.colour = random.choice(self.all_colours)

    def draw(self):
        text = font.render(self.letter, True, self.colour)
        screen.blit(text,[self.x, 150])

    def alltext(self):#all text randomly flashes
        text = font.render(my_text, True, self.colour)
        screen.blit(text,[150,150])

    def oneattime(self):
        text = font.render(my_text, True, self.colour)
        screen.blit(text,[self.x,150])

        
letters = []
my_text = "Happy birthday"
done = False

while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(white)
    
    for n, letter in enumerate(my_text):
        x_pos = 150 + n * 25
        letters.append(Letter(letter, x_pos))
    
    for i in range(10):
        #screen.fill(white)
        for letter in letters:
            letter.update()
            letter.draw()
        pygame.display.flip()
        pygame.time.wait(500)

    screen.fill(white)
    
    for i in range(10):
        for letter in letters:
            letter.update()
            letter.alltext()
        pygame.display.flip()
        pygame.time.wait(500)

    screen.fill(white)
    for i in range(len(my_text)):
        x_pos =150 +i,

    for i in range(10):
        for letter in letters:
            letter.update()
            letter.oneattime()
        pygame.display.flip()
        pygame.time.wait(500)

    screen.fill(white)
            
                
        

pygame.quit()


Was This Post Helpful? 0
  • +
  • -

#14 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: Can't make my pygame colour change stop at the end of the loop

Posted 25 April 2017 - 02:02 AM

What you want is to execute a number of different 'stages' of the program.
First stage is the simultaneous blinking of letters in individual colors.
Second stage is blinking of all letters in same (but changing) colors.
Third stage is some kind of falling letters (not 100% sure how you want this).

This kind of structure is very easy to organize in a game.stage structure as described in my tutorials.
Then you can even (randomly) loop between stages and have the different effects happen randomly.

Stage one you already have.
Stage two you try to implement in the Letter class, - but this is wrong. Stage two acts upon the full message (Happy Birthday) and should be handled completely separately; you don't need the Letter class for this.

Stage three, however, is probably a new method in the Letter class; it applies a fall() method to the individual letters.

Even if you do not want to introduce a Game() object, you should organize the code to sequentially run through the three step described here.

This post has been edited by DK3250: 25 April 2017 - 02:07 AM

Was This Post Helpful? 0
  • +
  • -

#15 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 348
  • Joined: 15-December 14

Re: Can't make my pygame colour change stop at the end of the loop

Posted 25 April 2017 - 07:47 PM

When you say stage two should be handled completely separately; you don't need the Letter class for this. I wasn't sure what you meant by that. Two things came to mind after reading it a few times... Either move all_colours to the main outside of the class or make a second class to handle it. Because I hope I'm not over thinking it, but after reading your suggestion (I love that idea of having a game control to handle the random loop so it never goes through the same order every time.) I was thinking what if I had two classes and a main function. I could have one handle the text, the second would handle how the text flashes and the function could handle the game loop. That way it wouldn't be messy like I have it now. The only other solution I could come up with was to have a separate class handle each animation and then use inheritance to the main class that would handle the game loop. Please tell me if I did understand your suggestions correctly. I will be working on a flow chart and a skeleton program to show how I plan to make both of my ideas I mentioned so I can show my ideas.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3