Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a game in Pygame and I want to be able to target enemy unit. I made it so when I click on them a variable "targeted" becomes true, and stays true until I click somewhere else on the screen. I also want targeted units to have a small green circle around them, so I made it in GEDIT. I have made a function that draws everything on the screen (the background, the player, objects, etc) and in the part where it draws the units it checks if the variable "targeted" is true and if it is it should move that little green circle over the enemy units.

here is the code that does that:

screen.blit(enemy_unit.pic, enemy_unit.rect) #draw the unit

if enemy_unit.targeted == True: #if the unit has been targeted then draw a circle over it 
    target_rect.move_ip(enemy_unit.pos)  #move the circle to the unit     #there are some bigger units and some smaller ones, so we have to "scale" the circle
    screen.blit(target_pic, target_rect) #actually draw the circle

This doesn't work, when I target the unit the circle just appears for a 1/5 of second next (not on, but just next) to the unit and then disappears. I am sure that I am keeping a good track of "enemy_unit.pos" because I tested it (I added a piece of code that would print one units position and mouse's position every time i clicked the mouse and when i was near him the numbers were same).

If you could give me a hint about what I'm doing wrong. I think its in move_ip function, but I tried just move and it didn't work either (the circle didn't even show at all)!

share|improve this question

migrated from Jan 11 '11 at 21:04

This question came from our site for professional and enthusiast programmers.

This would probably be better suited for – AttackingHobo Jan 11 '11 at 19:46
How is enemy_unit.targeted determined? – Nathon Jan 11 '11 at 19:52
i just have one question, could you make a seperate picture with the green circle and use that? so, if enemy.targeted == True, use the other picture? – Pip Apr 28 '13 at 13:59
 elif event.type == MOUSEBUTTONDOWN and event.button == 1 and pressed == False:
      pressed = True
      for enemie in lista_neprijatelja:
           if enemie.rect.collidepoint(pygame.mouse.get_pos()):
                enemie.targeted = True
                enemie.targeted = False

 elif event.type == MOUSEBUTTONUP and event.button == 1 and pressed == True:
      pressed = False

I am sure that this works as it should - I tested it.

share|improve this answer

in each game iteration you should check - if user clicked on other unit or surface, if yes - change selected unit or remove selection from currently selected

It will look something like this: somewhere in main loop:

    user_input.get_user_input() # grab user mouse input
    game.screen.event_handling(user_input)  # see below:

game.screen.event_handling function:

def event_handling(self,event_handler_object):
    for unit in self.unit_list:

In unit.update_state(event_handler_object) function you should check any mouse events like MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP. Also you should store last mouse event to compare current event, for example - usel clicks some unit, your actions:

1 - wait mouse event

2 - check if there any unit under this coordinates, if yes:

2.1 - set unit status "selected"

2.2 - set last_event = {"event":"click,"pos":pygame.mouse.get_pos()}

3 - get next (current mouse event)

4 - check if event is MOUSEBUTTONDOWN:


4.1 - goto #2

sorry, I'm little drunk in this moment, but logick will be like above. I you have any questions - tomorrow I will can share my code of the same mynctionality

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.