0

i am working on a program in Python and using Pygame. this is what the basic code looks like:

while 1:

   screen.blit(background, (0,0))
   for event in pygame.event.get():

      if event.type == QUIT:
        pygame.quit()
        sys.exit()

      if event.type == KEYDOWN and event.key == K_c:
        circle_create = True
        circle_list.append(Circle())

      if event.type == MOUSEBUTTONDOWN and circle_create == True:
        if clicks == 0:
            circle_list[i].center()
        clicks += 1


      if event.type == MOUSEMOTION and clicks == 1 and circle_create == True:
        circle_list[i].stretch()

   if circle_create == True:
     circle_list[i].draw_circle()

   if clicks == 2:
     clicks = 0
     i += 1
     circle_create = False    

 pygame.display.update()

what i want to do is have the object's function of draw_circle() to be constantly updated by the loop so that the drawn circle is shown for all objects in the list, but since the list is iterated it updates the new object added and the objects already appended are not updated.

The program, works, it draws the circles upon user input but the update problem is the only issue i need to solve. Is there any possible way to have all elements in the list of objects being updated by the while loop? i have tried for many days and i have not been able to find a good solution. any ideas are appreciated. Thanks

11
  • Is your code indented correctly? It looks like your while loop will never terminate, since you have no break.
    – BrenBarn
    Jun 23, 2012 at 20:28
  • yes. the code works fine and everything, it all works good. the only problems is updating the draw_circle() for every object in the list... that way the circle drawn is visible
    – lambda
    Jun 23, 2012 at 20:45
  • the loop ends when event.type == QUIT
    – lambda
    Jun 23, 2012 at 20:46
  • But when event.type==QUIT you call sys.exit, so it looks like that will end the whole program, not just the loop.
    – BrenBarn
    Jun 23, 2012 at 20:47
  • i need the loop in order to make multiple circles(as many as the user wants). thats where the problem is at. how to have multiple circles drawn with different variables being constantly updated because the screen is being blitted all the time during the loop
    – lambda
    Jun 23, 2012 at 20:52

3 Answers 3

1

You need to redraw the whole list after your blit(it covers the whole screen with the surface 'background' and 'erases' it), its not conditional, you need to iterate over the whole list and draw it. Them in the event part you decides who enters and who leaves the list.

Loop:
  Blit,starting new

  Event, here you decide who moves, who begin or cease to exist(append/remove)

  Redraw whole list, everyone in the circle_list.
2
  • yes.. i know. im tryign to figure out a method to redraw the entire growing list effetively.
    – lambda
    Jun 23, 2012 at 21:19
  • I can suggest to you the sprite groups, pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite, you only need your instance of Circle to have a .image (surface instance) and a .rect (rect instance) attributes. Then you add your circle instances on them, and call .draw method, which will make each item of the group to be drawn. Jun 23, 2012 at 21:46
1

To draw all the circles in your list, just iterate through them and draw them before each call to update:

for circle in circle_list:
    circle.draw_circle()

Edit: the OP had posted incorrectly formatted code, but says the actual code is fine, so removed that suggestion

5
  • the code is indented correctly just when i typed it in this blog/forum the indention were off. the program works fine . it draws the circle but then it disappears because circle_list[i].draw_circle() only draws the circle in the current iteration. i need a way to tell the program update circle_list[i].draw_circle() for all elements and not the current iteration
    – lambda
    Jun 23, 2012 at 21:00
  • no. in order to call draw_circle, it must be accessed by the list circle_list[i].draw_circle()
    – lambda
    Jun 23, 2012 at 21:35
  • You are not accessing draw_circle with the list in that statement, you are accessing it with a list element. In the for loop above, circle is a list-element just like circle_list[i] is
    – Dhara
    Jun 23, 2012 at 21:41
  • I would suggest not to copy the list unless you have to: the [:] expression will copy the list before iterating through it, and that is an extra overhead. Also, please accept the answer if it solves your problem :)
    – Dhara
    Jun 23, 2012 at 21:51
  • alright. yes that works. i didnt see it at first because C++ mentality made me forget the details of python's for loops. many thanks
    – lambda
    Jun 23, 2012 at 22:01
0

edit: I thought you already tried : https://stackoverflow.com/a/11172885/341744

The program, works, it draws the circles upon user input but the update problem is the only issue i need to solve. Is there any possible way to have all elements in the list of objects being updated by the while loop?

You could iterate on a temporary list, for example, if you kill actors while iterating.

for circle in circles[:]:
    circle.update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.