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()