Home > OS >  mouse position not working when moving my mouse off the game window
mouse position not working when moving my mouse off the game window

Time:07-20

ok so im making a simon says game with 4 different colored squares and right now all im doing is detecting when you click on a box or hover over it cause when i hover over the box i want it to darken cause that makes it look more like a button but the problem i have is that when i start the program the upper right box (blue) is dark and when i move my mouse over it it becomes light and when i move it over the red box the blue box darkens even though the function im using to deteck click is telling me that IT IS red so aaaa idk what to do, last time i had a problme it was changing one character so i dont know if i made a really dumb mistake or if i should just abandan the project. anyway, thanks

from gettext import find
import pygame
import time
import sys
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor():
    mouse = pygame.mouse.get_pos()
    if mouse[0] >=margin and mouse[0]<= (width/2-(margin*2) margin):
        #first collumn
        if mouse[1] >= margin and mouse[1] <= (height/2-(margin*2) margin):
            return "red"
        else:return "green"
    elif  mouse[1] >= margin and mouse[1] <= (height/2-(margin*2) margin):
            return "blue"
    else:return "yellow"

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)
dred=(200,0,0)
black =(0,0,0)
blue = (0,0,255)
dblue = (0,0,200)
green = (0,255,0)
dgreen = (0,200,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:
            
            
            print(findColor())
            score =1
            pygame.display.set_caption("Score: " str(score))
            #print(mouse[0]," ",mouse[1])
     









    
    window.fill(black)
     
    #red
    if findColor() == "red":
        drawRect(dred,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    else:
        drawRect(red,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #blue
    if findColor() == "blue":
        drawRect(blue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    else:
        drawRect(dblue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #green
    if findColor() == "green":
        drawRect(dgreen,margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
    else: 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()

CodePudding user response:

First, you're missing the dark yellow portion, unless you wanted to leave that out.

Second, you swapped the blue selections. On red you have Dark on hover, on blue you have Light on hover, and on green you have Dark on hover, yellow stays the same.

CodePudding user response:

alr, thanks for the help guys, i must be dyslexic cause i didnt see that blue was flipped. anyway i got it working if anyone cares so.

from gettext import find
import pygame
import time
import sys
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor():
    mouse = pygame.mouse.get_pos()
    if mouse[0] >=0 and mouse[0]<= (345) and mouse[1] <= width:
        #first collumn
        #0,0,345,345
        if mouse[1] >= 0 and mouse[1] <= (345):
            return "red"
        else:return "green"
    elif  mouse[1] >= 0 and mouse[1] <= (345) and mouse[0] <= width:
            return "blue"
    elif  mouse[0] <= width:return "yellow"

width,height = 800,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)
dred=(200,0,0)
black =(0,0,0)
blue = (0,0,255)
dblue = (0,0,200)
green = (0,255,0)
dgreen = (0,200,0)
yellow = (255,255,0)
dyellow = (200,200,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:
            
            
            print(findColor())
            score =1
            pygame.display.set_caption("Score: " str(score))
            #print(mouse[0]," ",mouse[1])
     








    #drawRect(red,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    
    window.fill(black)
     
    #red
    if findColor() == "red":
        drawRect(dred,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    else:
        drawRect(red,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #blue
    if findColor() == "blue":
        drawRect(dblue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    else:
        drawRect(blue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #green
    if findColor() == "green":
        drawRect(dgreen,margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
    else: drawRect(green,margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
    #yellow
    if findColor() == "yellow":
        drawRect(dyellow,350,350,(width/2-(margin*2)),(height/2-(margin*2)))
    else:drawRect(yellow,350,350,(width/2-(margin*2)),(height/2-(margin*2)))


    
    #tmouse = pygame.mouse.get_pos()
   # print(tmouse)


    # Update our window
    pygame.display.flip()
     
pygame.quit()
  • Related