Pygame question2

Thread Solved

Join Date: Mar 2009
Posts: 10
Reputation: FengG is an unknown quantity at this point 
Solved Threads: 1
FengG FengG is offline Offline
Newbie Poster

Pygame question2

 
0
  #1
26 Days Ago
Python beginner try to learn pygame. Here is a program I made for random color circles popping out.

  1. import pygame, random
  2. pygame.init()
  3. X = 680
  4. Y = 460
  5. def main():
  6. screen = pygame.display.set_mode((X,Y))
  7. pygame.display.set_caption('circles')
  8. background = pygame.Surface(screen.get_size())
  9. background = background.convert()
  10. background.fill(pygame.color.Color('white'))
  11. run = True
  12. clock = pygame.time.Clock()
  13. while run:
  14. clock.tick(30)
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT:
  17. run = False
  18.  
  19. elif event.type == pygame.KEYDOWN:
  20. if event.key == pygame.K_q:
  21. run = False
  22.  
  23.  
  24. for count in range(50):
  25. r = random.randint(1,40)
  26. a = random.randint(1,X)
  27. b = random.randint(1, Y)
  28. c = random.randint(1, 255)
  29. d = random.randint(1, 255)
  30. e = random.randint(1, 255)
  31. pygame.draw.circle(background, (c,d,e), (a,b), r, 0)
  32. screen.blit(background, (0,0))
  33. pygame.display.flip()
  34. if __name__ == "__main__":
  35. 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.

  1. import pygame, random
  2. pygame.init()
  3. X = 680
  4. Y = 460
  5. def main():
  6. screen = pygame.display.set_mode((X,Y))
  7. pygame.display.set_caption('circles')
  8. background = pygame.Surface(screen.get_size())
  9. background = background.convert()
  10. background.fill(pygame.color.Color('white'))
  11. run = True
  12. clock = pygame.time.Clock()
  13.  
  14. a = random.randint(1,X)
  15. b = random.randint(1, Y)
  16. color1 = random.randint(1, 255)
  17. color2 = random.randint(1, 255)
  18. color3 = random.randint(1, 255)
  19. radius = random.randint(10,40)
  20.  
  21. while run:
  22.  
  23. clock.tick(30)
  24. background.fill(pygame.color.Color('white'))
  25.  
  26. for event in pygame.event.get():
  27. if event.type == pygame.QUIT:
  28. run = False
  29.  
  30. elif event.type == pygame.KEYDOWN:
  31. if event.key == pygame.K_q:
  32. run = False
  33.  
  34.  
  35.  
  36. pygame.draw.circle(background, (color1,color2,color3),\
  37. (a,b), radius, 0)
  38.  
  39. b-=10
  40.  
  41. screen.blit(background, (0,0))
  42. pygame.display.flip()
  43.  
  44. if __name__ == "__main__":
  45. main()
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: FengG is an unknown quantity at this point 
Solved Threads: 1
FengG FengG is offline Offline
Newbie Poster
 
0
  #2
26 Days Ago
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

  1. import pygame, random
  2. pygame.init()
  3. X = 680
  4. Y = 460
  5.  
  6. def draw(background,screen):
  7. a = random.randint(1,X)
  8. b = random.randint(1, Y)
  9. color1 = random.randint(1, 255)
  10. color2 = random.randint(1, 255)
  11. color3 = random.randint(1, 255)
  12. radius = random.randint(10,40)
  13.  
  14. while b > 0:
  15. background.fill(pygame.color.Color('white'))
  16. pygame.draw.circle(background, (color1,color2,color3),\
  17. (a,b), radius, 0)
  18.  
  19. b -= 1
  20. screen.blit(background, (0,0))
  21. pygame.display.flip()
  22.  
  23. def main():
  24. screen = pygame.display.set_mode((X,Y))
  25. pygame.display.set_caption('circles')
  26. background = pygame.Surface(screen.get_size())
  27. background = background.convert()
  28. background.fill(pygame.color.Color('white'))
  29. run = True
  30.  
  31. while run:
  32. clock = pygame.time.Clock()
  33.  
  34. clock.tick(30)
  35.  
  36. for event in pygame.event.get():
  37. if event.type == pygame.QUIT:
  38. run = False
  39.  
  40. elif event.type == pygame.KEYDOWN:
  41. if event.key == pygame.K_q:
  42. run = False
  43.  
  44. draw(background,screen)
  45.  
  46. if __name__ == "__main__":
  47. main()
Last edited by FengG; 26 Days Ago at 1:13 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #3
26 Days Ago
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 10
Reputation: FengG is an unknown quantity at this point 
Solved Threads: 1
FengG FengG is offline Offline
Newbie Poster
 
0
  #4
25 Days Ago
Originally Posted by vegaseat View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC