'How to change a variable using a function?

I want to condense the following code:

 if self.GameGridSprite1.collidepoint(
                    pos) and self.GameGrid1 == self.GameGrid or self.GameGridSprite1.collidepoint(
                    pos) and self.GameGrid1 == self.CurrentToPlaySprite2:
                self.GameGrid1 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(0)
            if self.GameGridSprite2.collidepoint(
                    pos) and self.GameGrid2 == self.GameGrid or self.GameGridSprite2.collidepoint(
                pos) and self.GameGrid2 == self.CurrentToPlaySprite2:
                self.GameGrid2 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(1)
            if self.GameGridSprite3.collidepoint(
                    pos) and self.GameGrid3 == self.GameGrid or self.GameGridSprite3.collidepoint(
                pos) and self.GameGrid3 == self.CurrentToPlaySprite2:
                self.GameGrid3 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(2)
            if self.GameGridSprite4.collidepoint(
                    pos) and self.GameGrid4 == self.GameGrid or self.GameGridSprite4.collidepoint(
                pos) and self.GameGrid4 == self.CurrentToPlaySprite2:
                self.GameGrid4 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(3)
            if self.GameGridSprite5.collidepoint(
                    pos) and self.GameGrid5 == self.GameGrid or self.GameGridSprite5.collidepoint(
                pos) and self.GameGrid5 == self.CurrentToPlaySprite2:
                self.GameGrid5 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(4)
            if self.GameGridSprite6.collidepoint(
                    pos) and self.GameGrid6 == self.GameGrid or self.GameGridSprite6.collidepoint(
                pos) and self.GameGrid6 == self.CurrentToPlaySprite2:
                self.GameGrid6 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(5)
            if self.GameGridSprite7.collidepoint(
                    pos) and self.GameGrid7 == self.GameGrid or self.GameGridSprite7.collidepoint(
                pos) and self.GameGrid7 == self.CurrentToPlaySprite2:
                self.GameGrid7 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(6)
            if self.GameGridSprite8.collidepoint(
                    pos) and self.GameGrid8 == self.GameGrid or self.GameGridSprite8.collidepoint(
                pos) and self.GameGrid8 == self.CurrentToPlaySprite2:
                self.GameGrid8 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(7)
            if self.GameGridSprite9.collidepoint(
                    pos) and self.GameGrid9 == self.GameGrid or self.GameGridSprite9.collidepoint(
                pos) and self.GameGrid9 == self.CurrentToPlaySprite2:
                self.GameGrid9 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(8)

Here is my code and I want to know how to make it shorter like this:

if self.GameGridSprite1.collidepoint(
                    pos) and self.GameGrid1 == self.GameGrid or self.GameGridSprite1.collidepoint(
                    pos) and self.GameGrid1 == self.CurrentToPlaySprite2:
                self.GameGrid1 = self.CurrentToPlaySprite
                self.ClickOnGridCondense(0)

to something like this:

Shorter(self, self.GameSprite1, pos, self.GameGrid1, 0)

if Sprite.collidepoint(
                    pos) and Grid == self.GameGrid or Sprite.collidepoint(
                    pos) and Grid == self.CurrentToPlaySprite2:
                Grid = self.CurrentToPlaySprite
                self.ClickOnGridCondense(integer)

When I try to do so, it doesn't work Thanks for any help (and sorry I don't speak English very well)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source