• Advertisement
Sign in to follow this  

I have got am if-statement problem

This topic is 1503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have got a problem with my click detection.

 

Here is the code that manages that:

if click != []:

            for i in range(len(stars[0])):

                if (((click[0][0] >= (int(stars[0][i]) - 8)) and (click[0][0] <= (int(stars[0][i]) + 8))) and ((click[0][1] >= (int(stars[1][i]) - 8)) and (click[0][1] <= (int(stars[1][i]) + 8)))) and click[1][0] == 1:
                    stars[2][i] = 2
                    print("left")

                elif (((click[0][0] >= (int(stars[0][i]) - 8)) and (click[0][0] <= (int(stars[0][i]) + 8))) and ((click[0][1] >= (int(stars[1][i]) - 8)) and (click[0][1] <= (int(stars[1][i]) + 8)))) and click[1][2] == 1:

                    for j in range(len(stars[0])):

                        if stars[2][j] == 2 and j != i:
                            setUpStart = []
                            setUpStart.append(int(stars[0][j]))
                            setUpStart.append(int(stars[1][j]))
                            setUpEnd = []
                            setUpEnd.append(int(stars[0][i]))
                            setUpEnd.append(int(stars[1][i]))
                            setUpLine = []
                            setUpLine.append(setUpStart)
                            setUpLine.append(setUpEnd)
                            fleets.append(setUpLine)

                        stars[2][j] = 1
                    stars[2][i] = 1
                            
                    print("right")

                else:
                    stars[2][i] = 1

It should basically allowe me to select a star with a left click and then select another star with right click, whose coordinates are then stored for later. Clicks that do not land on stars, Just deselect any star that happens to be active.

 

But the problem is, that some stars some times just do nothing when I right click on them even though I have another star selected. I cannot find the reason that is causing it. Can anyone else find it?

Edited by Ironmaggot

Share this post


Link to post
Share on other sites
Advertisement
As one of the few members on here who uses Python for games:

What library are you using?

What does the Stars array do?

How does the click array work?

Some of these can be fixed if you *post full source code*. Then I can help you.

Cheers ;)!

Share this post


Link to post
Share on other sites

As one of the few members on here who uses Python for games:

What library are you using?

What does the Stars array do?

How does the click array work?

Some of these can be fixed if you *post full source code*. Then I can help you.

Cheers ;)!

 

I am using pygame library.

 

Stars array is just an array that stores information about stars that are on the map. Coordinates, resource value, garrison size, etc.

 

Click array when mouse is clicked returns: ((mousePosX, mousePosY), (1, 2, 3)). Numbers 1,2,3 represent mouse buttons. If left button is clicked, slot 1 will be "1" and all the other slots will be 0. Right click gives 1 in the slot 3 and 0 in the others.

 

How should I post the source code? It is quite long?

 

------------------------------

 

It seems that I have fixed the problem. I don't know what was the problem, but its gone now.

Edited by Ironmaggot

Share this post


Link to post
Share on other sites

for j in range(len(stars[0]))

Should this be stars[2]?

 

Your code is difficult to read because there are logs of magic numbers and repeated expressions. I'd recommend using named constants and pulling important expressions into a variable with a good name.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement