Mark Han
Mark Han

Reputation: 25

How to draw cursor with pygame

I'm trying to draw a cursor by hiding and drawing to a certain spot. I want to make a different image appear when I click it but it doesn't seem to work well. Please help! It only appears as if I clicked.

import pygame 

pygame.init()

white = [255,255,255]

size = [960,540]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("1a")

done = False
clock = pygame.time.Clock()

Cursor = pygame.image.load('Cursor_normal.png')
Cursor_Clicked = pygame.image.load('Cursor_Clicked.png')
def draw_cursor(screen,x,y):
    if pygame.MOUSEBUTTONDOWN:
        screen.blit(Cursor_Clicked,(x,y-48))
    else:
        screen.blit(Cursor,(x,y-48))

pygame.mouse.set_visible(0)

while done==False:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True     

    screen.fill(white)

    pos = pygame.mouse.get_pos()
    x=pos[0]
    y=pos[1]

    draw_cursor(screen,x,y)



    pygame.display.flip()

    clock.tick(20)

pygame.quit()

Upvotes: 0

Views: 2034

Answers (1)

CodeSurgeon
CodeSurgeon

Reputation: 2465

The problem is in how you are checking if the user has clicked. pygame.MOUSEBUTTONDOWN is actually a constant in pygame for the numerical value assigned to a mouse button being down (try printing it out). Like pygame.QUIT, pygame.MOUSEBUTTONDOWN is also a type of event, so whether the mouse is up or down could be checked like this in the existing event loop:

import pygame 

pygame.init()
white = [255,255,255]
size = [960,540]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("1a")
pygame.mouse.set_visible(0)
done = False
mouse_down = False
clock = pygame.time.Clock()

Cursor = pygame.image.load('Cursor_normal.png')
Cursor_Clicked = pygame.image.load('Cursor_Clicked.png')

def draw_cursor(screen,x,y):
    if mouse_down:
        screen.blit(Cursor_Clicked,(x,y-48))
    else:
        screen.blit(Cursor,(x,y-48))

while done==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_down = True
        elif event.type == pygame.MOUSEBUTTONUP:
            mouse_down = False
    screen.fill(white)
    pos = pygame.mouse.get_pos()
    x=pos[0]
    y=pos[1]
    draw_cursor(screen,x,y)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Alternatively, if you do not want to clutter up your event loop, you can use pygame.mouse.get_pos() instead:

import pygame 

pygame.init()
white = [255,255,255]
size = [960,540]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("1a")
pygame.mouse.set_visible(0)
done = False
mouse_down = False
clock = pygame.time.Clock()

Cursor = pygame.image.load('Cursor_normal.png')
Cursor_Clicked = pygame.image.load('Cursor_Clicked.png')

def draw_cursor(screen,x,y):
    if mouse_down:
        screen.blit(Cursor_Clicked,(x,y-48))
    else:
        screen.blit(Cursor,(x,y-48))

while done==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True
        """
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_down = True
        elif event.type == pygame.MOUSEBUTTONUP:
            mouse_down = False
        """
    screen.fill(white)
    pos = pygame.mouse.get_pos()
    mouse_down = pygame.mouse.get_pressed()[0]#note: returns 0/1, which == False/True
    x=pos[0]
    y=pos[1]
    draw_cursor(screen,x,y)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Upvotes: 1

Related Questions