Results 1 to 4 of 4

Thread: Pygame conditional help

  1. #1
    Join Date
    Sep 2012
    Beans
    91

    Pygame conditional help

    Hi I'm tyring to check for event in a function in a class I made the only problem is that I don't want to keep writing down each if and elif conditional over and over again for different conditions that I test against a list of rect coords. Is there anyway I could make it so I could just pass a bunch a parameters through list and test against that? How would I ? Heres the function .
    Code:
        def check_mouse(self):
            for rect in self.text_pos_list:
                if rect.collidepoint(pygame.mouse.get_pos()):
                    if rect == self.text_pos_list[0]:
                        return self.item[0]
                    elif rect == self.text_pos_list[1]:
                        return self.item[1]
                    elif rect == self.text_pos_list[2]:
                        return self.item[2]
                    else:
                        return False

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,696
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Thumbs up Re: Pygame conditional help

    Quote Originally Posted by snowlizard31 View Post
    Hi I'm tyring to check for event in a function in a class I made the only problem is that I don't want to keep writing down each if and elif conditional over and over again for different conditions that I test against a list of rect coords. Is there anyway I could make it so I could just pass a bunch a parameters through list and test against that? How would I ? Heres the function .
    Code:
        def check_mouse(self):
            for rect in self.text_pos_list:
                if rect.collidepoint(pygame.mouse.get_pos()):
                    if rect == self.text_pos_list[0]:
                        return self.item[0]
                    elif rect == self.text_pos_list[1]:
                        return self.item[1]
                    elif rect == self.text_pos_list[2]:
                        return self.item[2]
                    else:
                        return False
    Code:
        def check_mouse(self):
            for rect in self.text_pos_list:
                if rect.collidepoint(pygame.mouse.get_pos()):
                    for i in range(len(self.text_pos_list)):
                       if rect == self.text_pos_list[i]:
                          return self.item[i]
                    return False

  3. #3
    Join Date
    Jun 2010
    Location
    Loznica Serbia
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Pygame conditional help

    You could insert another for loop to replace if/elif blocks
    Code:
    def check_mouse(self):
        for rect in self.text_pos_list:
            if rect.collidepoint(pygame.mouse.get_pos()):
                for listItem in self.text_pos_list[:3]:
                    if rect == listItem:
                        return listItem
            return False
    Thic code would check only the first 3 items just like your if/elif part and if the loop exits it returns False just like your else part
    Windows is not user friendly,it's just user familiar

  4. #4
    Join Date
    Sep 2012
    Beans
    91

    Re: Pygame conditional help

    Sweeeeeet thanks guys! I really appreciate the help!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •