Is there a way to get pygame to detect what color my cursor is on

Viewed 113

I'm trying to make a game where it spawns another circle every time you click on a circle. And the error i'm getting is "TypeError: 'bool' object is not callable". I'm looking for a solution that doesn't completly change the code since i'm new and want to understand the code myself. But at this point i'll take any help.

import pygame
import random
import time
from pygame.math import Vector2

# Define some colors
BLACK = (0, 0, 0)
WHITE = (247, 247, 247)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (225,225,0)
tuple1 = (247, 247, 247, 255)
# Setup
pygame.init()
# Set the width and height of the screen [width,height]
surface = pygame.display.set_mode( (2560, 1440) ) 
pygame.display.set_caption("My Game") 
# Loop until the user clicks the close button.
done = False 
# Used to manage how fast the screen updates
clock = pygame.time.Clock() 
# Hide the mouse cursor
pygame.mouse.set_visible(0) 
# Speed in pixels per frame
x_speed = 0
y_speed = 0 
# Current position
cursor = pygame.image.load('cursor.png').convert_alpha()
pygame.image.load("pattern.jpg")
background_image = pygame.image.load("pattern.jpg").convert()
circposy = 770
circposx = 1280
# -------- Main Program Loop -----------
while done ==False:
    # --- Event Processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:           
            done = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # --- Drawing Code
    surface.fill(WHITE)
    # First, clear the screen to WHITE. Don't put other drawing commands
    # above this, or they will be erased with this command.\
    player_position = pygame.mouse.get_pos()
    a = 0
    b = 1
    p=player_position[a]
    o=player_position[b]
    player_position = (p,o)
    pygame.draw.circle(surface,RED,[circposx,circposy], 40)
    tuple2 = surface.get_at(pygame.mouse.get_pos())
    print (tuple2)
    q = p - 2545
    w = o - 2545
    surface.blit( cursor, (q, w) )
    a=0
    result = tuple(map(int, tuple2)) > tuple1
    print (result)
    while event.type == pygame.MOUSEBUTTONDOWN:
        done = True
        if result == True():
            a+1
            surface.fill(WHITE)
            pygame.draw.circle(surface,RED,[circposx + randint, circposy+randint],40)
    print (a)
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # Limit frames per second
    clock.tick(144) 
# Close the window and quit.
pygame.quit()

cursor.png

2 Answers

The short answer is that the code is trying to call True(), which isn't a function.

while event.type == pygame.MOUSEBUTTONDOWN:
    done = True
    if result == True():          # <<-- HERE
        a+1
        surface.fill(WHITE)
        pygame.draw.circle(surface,RED,[circposx + randint, circposy+randint],40)

Simply change this to True.

But you will also need to define what randint is a few lines lower. Did you mean random.randint( 0, 500 ) or suchlike? And fixing this leads to another error, because the surrounding loop to this code, is an infinite loop:

while event.type == pygame.MOUSEBUTTONDOWN:    # <<-- HERE
    done = True
    if result == True:         
        a+1
        surface.fill(WHITE)
        rand_x = random.randint( 0, 500 )
        rand_y = random.randint( 0, 500 )
        pygame.draw.circle(surface,RED,[circposx + rand_x, circposy+rand_y],40)

Because there is no way event.type can ever change inside that loop. This should probably read:

if event.type == pygame.MOUSEBUTTONDOWN:

If I may make some suggestions:

  • Put all your event handling to a single place.
    • There's some doubling-up of event handling, and it would have prevented that infinite loop.
  • Move your screen dimensions into variables
    • SCREEN_WIDTH = 2560
    • SCREEN_HEIGHT = 1440
  • Instead of having constant numbers through the code (e.g.: 2545) make these functions of the screen size.
    • SCREEN_MARGIN = SCREEN_WIDTH - round( SCREEN_WIDTH * 0.10 )
    • q = p - SCREEN_MARGIN

If you want to detect what color your cursor is on, you can use pyautogui. Make sure you have pyautogui installed. type pip install pyautogui. If it doesn't install successfully, you already have it installed.

# Import everything
import pygame
import pyautogui
from pyautogui import *

# Initialize
pygame.init()

# Get mouse position
mouse_pos = pygame.mouse.get_pos()
x = mouse_pos[0]
y = mouse_pos[1]


# Get Color
r = pyautogui.pixel(x,y)[0]
g = pyautogui.pixel(x,y)[1]
b = pyautogui.pixel(x,y)[2]
color = [r,g,b]


Hopefully, you found this helpful!

Related