Bottom Page

Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Problem with pygame not switching the mouse image
#1
So I've made a little thing to change my mouse's icon. It happens once in a blue moon, so I know it works. But it doesn't work 100% of the time.... which seems impossible.
There doesn't seem to be a way to get pygame to look at what image is currently being used as the mouse.
The fact that the prints both work perfectly but the image only switches once out of 79 clicks is extremely frustrating.

Anyone have any ideas for me?
I've tried this several different ways. All of the ways have negatives. Including slowing the program down to unresponding.
It's as if pygame.event.get() is so slow that it just forgets what it's trying to get done. If I put the mouse_cursor

This is the code that I use to get the image to change
gameDisplay = pygame.display.set_mode((display_w,display_h))
gameDisplay.blit(mouse_cursor, pygame.mouse.get_pos())
both are in the spots they should be.
Quote
#2
First of all you should never load an image more than once. As you have it here, you are loading the images every event, This is a big NO NO in gaming or in any GUI. You should never have a resource load within the while loop. In addition to that it is a massive bottleneck you have created. Which is why it slows down. This has nothing to do with python/pygame this is just straight GUI and gaming. So if you move on to another language the same rule applies.

Second of all... pygame supports basic cursor change
https://www.pygame.org/docs/ref/cursors.html

If you want complex or an image in place of your cursor, you load the image and center the image to the cursor position. Then you make the actual cursor disappear. Not sure how you are doing it as you dont show the whole code. But just figured i would add that.
mekire has an example here. Press the number keys to change to the various mouse cursors.
Quote
#3
I mimicked this. If you look in the comments you can see I reworked the first example.
I set this at the very top
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0)) ##invisible mouseColorblack = (0,0,0)

From what you're saying I should do something like
WnY=pygame.image.load('whitenyellow.png').convert_alpha()
WnR=pygame.image.load('whitenred.png').convert_alpha()

outside of my mouse function, then

def mouse():
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            print("mouse down")
            mouse_cursor=WnY
            return
        elif event.type == pygame.MOUSEBUTTONUP:
            print("mouse up")
            mouse_cursor=WnR
            return

to avoid reloading the images constantly.*
Is there a good way to read the mouse consistently and fast and accurate?

I've also tried
        global b1
        ex = pygame.mouse.get_pressed(0)
        if ex == (1, 0, 0)  and b1 == False:
            b1 = True
            print("mouse down")
            mouse_cursor=pygame.image.load('whitenyellow.png').convert_alpha()
            print(ex) 
b1 is used to keep track if the mouse has switched images yet. I dont know if python/pygame works like this, but I figured once the image is set to be the "mouse" it sticks. So there's no reason to keep setting it 60x a second. With b1, once it's set to True, it's set and the if statement should skip it next time it's read. Right?
Quote
#4
here is a very basic crude example. Didnt bother editing the image i got online or using a color surface for simplicity. 
import pygame as pg

screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
done = False
cursor_img = 'curs.jpg'
cursor = pg.image.load(cursor_img).convert()
cursor_rect = cursor.get_rect()
is_cursor = False

while not done:
    screen.fill((255,0,0))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN:
            pg.mouse.set_visible(False)
            is_cursor = True

    cursor_rect.center = pg.mouse.get_pos()
    if is_cursor:
        screen.blit(cursor, cursor_rect)
    pg.display.update()



Attached Files Image(s)
   
Quote
#5
Getting the cursor to display as an image isn't a problem at all.
I need to be able to switch the image inside a Def depending on the mouse being press or released.

I guess from your example I'll have to make a second if/else. 1 to set the variables, and 1 to assign the cursor.
But right now, it's 8:47am and I haven't been to bed yet.... so I'll try it todarrow.
Quote
#6
So I lied about going to be. Seems I have to double post too...
But I got this to work

######################################################
def mouse():
    global mouse_cursor
    global b1
    ex = pygame.mouse.get_pressed(0)
 
    if b1 == False: #not clicked
        if ex == (1, 0, 0):
            mouse_cursor=WnY
            b1 = True           
    elif b1 == True:  #clicked
        if ex == (0, 0, 0):
            mouse_cursor=WnR
            b1 = False  
There's very noticeable slowdown in the program on each click though.
Is there a way so it doesn't lag?? I mean it's just 2 variable checks, and 2 variable sets...
Quote
#7
(Jul-26-2017, 12:47 PM)Mondaythe1st Wrote: I need to be able to switch the image inside a Def depending on the mouse being press or released.

Then all you need to do is check if the mouse is down. This modification of my example shows the mouse cursor when not held down, and the image when is held down. 

import pygame as pg

screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
done = False
cursor_img = 'curs.jpg'
cursor = pg.image.load(cursor_img).convert()
cursor_rect = cursor.get_rect()
is_cursor = False

while not done:
    screen.fill((255,0,0))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    if pg.mouse.get_pressed()[0]:
        is_cursor = True
        pg.mouse.set_visible(False)
    else:
        is_cursor = False
        pg.mouse.set_visible(True)

    cursor_rect.center = pg.mouse.get_pos()
    if is_cursor:
        screen.blit(cursor, cursor_rect)
    pg.display.update()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Call a function when switching layouts 4096 0 884 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyGUI] Switching between two frames using Tkinter jenkins43 1 217 Jul-17-2019, 10:53 AM
Last Post: metulburr
  [Tkinter] switching frames nick123 2 562 Apr-18-2019, 04:50 PM
Last Post: francisco_neves2020
  [Tkinter] Problem loading an image from directory into a Canvas object tiotony 3 799 Sep-02-2018, 06:47 PM
Last Post: woooee

Forum Jump:


Users browsing this thread: 1 Guest(s)