I have got am if-statement problem

Started by
2 comments, last by rip-off 10 years, 4 months ago

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?

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 ;)!

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

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.

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.

This topic is closed to new replies.

Advertisement