Python beginner try to learn pygame. Here is a program I made for random color circles popping out.

import pygame, random
pygame.init()
X = 680
Y = 460
def main():
    screen = pygame.display.set_mode((X,Y))
    pygame.display.set_caption('circles')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill(pygame.color.Color('white'))
    run = True
    clock = pygame.time.Clock()
    while run:
        clock.tick(30)
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                run = False

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    run = False


        for count in range(50):
            r = random.randint(1,40)
            a = random.randint(1,X)
            b = random.randint(1, Y)
            c = random.randint(1, 255)
            d = random.randint(1, 255)
            e = random.randint(1, 255)
            pygame.draw.circle(background, (c,d,e), (a,b), r, 0)
            screen.blit(background, (0,0))
            pygame.display.flip()        
if __name__ == "__main__":
    main()

It is pretty, I was excited. Now I want to make all circle fly, my goal is, to make random color circles randomly pop out and then fly toward the top of the window. So of course it will include the code that y -= something. However I tried different ways, none worked out, so here is my code for only one circle going up. Please give me some advice on how to make a lot of these circles pop out from random place and then go up.

Thanks. Here is my code for one sucessful flying circle.

import pygame, random
pygame.init()
X = 680
Y = 460
def main():
    screen = pygame.display.set_mode((X,Y))
    pygame.display.set_caption('circles')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill(pygame.color.Color('white'))
    run = True
    clock = pygame.time.Clock()

    a = random.randint(1,X)
    b = random.randint(1, Y)
    color1 = random.randint(1, 255)
    color2 = random.randint(1, 255)
    color3 = random.randint(1, 255)
    radius = random.randint(10,40)

    while run:

        clock.tick(30)
        background.fill(pygame.color.Color('white'))
        
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                run = False

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    run = False



        pygame.draw.circle(background, (color1,color2,color3),\
                               (a,b), radius, 0)
                               
        b-=10

        screen.blit(background, (0,0))      
        pygame.display.flip()        

if __name__ == "__main__":
    main()

Recommended Answers

All 3 Replies

I kind of got what I wanted. But it's still one circle floating up at a time. Please give advice on how to make multiple circles. Thanks

import pygame, random
pygame.init()
X = 680
Y = 460

def draw(background,screen):
    a = random.randint(1,X)
    b = random.randint(1, Y)
    color1 = random.randint(1, 255)
    color2 = random.randint(1, 255)
    color3 = random.randint(1, 255)
    radius = random.randint(10,40)

    while b > 0:
        background.fill(pygame.color.Color('white'))
        pygame.draw.circle(background, (color1,color2,color3),\
                               (a,b), radius, 0)
        
        b -= 1
        screen.blit(background, (0,0))      
        pygame.display.flip()

def main():
    screen = pygame.display.set_mode((X,Y))
    pygame.display.set_caption('circles')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill(pygame.color.Color('white'))
    run = True

    while run:
        clock = pygame.time.Clock()

        clock.tick(30)
      
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                run = False

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    run = False
        
        draw(background,screen)

if __name__ == "__main__":
    main()

Looks like you have made nice progress with coding pygame. The pygame module has something called sprites that, as far as I remember, are more suitable for what you have in mind.

Looks like you have made nice progress with coding pygame. The pygame module has something called sprites that, as far as I remember, are more suitable for what you have in mind.

Thanks vegaseat, I will keeping learning,
http://www.cs.iupui.edu/~aharris/pygame/
this is the source I learn from, good lessons for pygame with videos and powerpoints, maybe we can put the link somewhere so people who are interested in pygame can get something out of it.
Any advice on weather learning pygame is a good path to master the python language?
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.