My filters dont work even though they should work identically

Question:

I’m trying to make a simon says game. I have 4 different colored squares but to make it nice I made a margin of 10 pixels around each box. Right now I’m trying to get the mouse pos and see if when you click what box ur in, but it doesn’t work.

The main bit is this:

def findColor(mouse):
    if mouse[0] >=margin and mouse[0]<= (width/2-(margin*2)+margin):
        #first column
        if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):
            print("red")

where i am testing for the top left hand square, but it aslo detects as the bottom square counting as red.(its green). this shouldnt be happening, because the x filter to find what square it is works, but the y one doesnt. here is the rest of the code just in case im an idiot, but thank u guys in advance!

simon says game

import pygame
import time
import sys
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor(mouse):
    if mouse[0] >=margin and mouse[0]<= (width/2-(margin*2)+margin):
        #first column
        if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):
            print("red")

width,height = 690,690
window = pygame.display.set_mode((width,height))
pygame.display.set_caption("Score: "+str(score)) 

width = 700
height = 700 
def drawRect(color,x,y,width,height):
    pygame.draw.rect(window, color, pygame.Rect(x, y, width, height))

margin = 10


boxSizeX = (width/2-(margin*2))
boxSizeY = (height/2-(margin*2))

red=(255,0,0)
black =(0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
##redBox = {"x":boxSizeX,"y":boxSizeY,"color":red}
#blueBox = {"x":boxSizeX,"y":boxSizeY,"color":blue}
# keep game running till running is true
run = True
while run:
   
   
    for event in pygame.event.get():
         
        # if event is of type quit then set
        # running bool to false
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            
            mouse = pygame.mouse.get_pos()
            findColor(mouse)
            score+=1
            pygame.display.set_caption("Score: "+str(score))
            print(mouse[0]," ",mouse[1])
     









    
    window.fill(black)
     
    #red
    drawRect(red,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #blue
    drawRect(blue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #green
    drawRect(green,margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
    #yellow
    drawRect(yellow,350,350,(width/2-(margin*2)),(height/2-(margin*2)))





    # Update our window
    pygame.display.flip()
     
pygame.quit()
Asked By: ImBadAtMath

||

Answers:

There is a type in your code. The y-coordinate it mouse[1], not mouse[0]:

if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):/s>

if mouse[1] >= margin and mouse[1] <= (height/2-(margin*2)+margin):

However, I recommend simplifying your code. Use pygame.Rect objects and collidepoint

import pygame
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor(mouse, rect):
    if rect.collidepoint(mouse):
            print("red")

width,height = 690,690
window = pygame.display.set_mode((width,height))
pygame.display.set_caption("Score: "+str(score)) 

width = 700
height = 700 
def drawRect(color, rect):
    pygame.draw.rect(window, color, rect)

margin = 10


boxSizeX = (width/2-(margin*2))
boxSizeY = (height/2-(margin*2))

red=(255,0,0)
black =(0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)

red_rect = pygame.Rect(margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
blue_rect = pygame.Rect(350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
green_rect = pygame.Rect(margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
yellow_rect = pygame.Rect(350,350,(width/2-(margin*2)),(height/2-(margin*2)))

# keep game running till running is true
run = True
while run:
   
   
    for event in pygame.event.get():
         
        # if event is of type quit then set
        # running bool to false
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            findColor(event.pos, red_rect)
            score+=1
            pygame.display.set_caption("Score: "+str(score))
            print(event.pos[0]," ",event.pos[1])
         
    window.fill(black)
     
    #red
    drawRect(red, red_rect)
    #blue
    drawRect(blue, blue_rect)
    #green
    drawRect(green, green_rect)
    #yellow
    drawRect(yellow, yellow_rect)

    # Update our window
    pygame.display.flip()
     
pygame.quit()
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.