簡體   English   中英

如何用pygame繪制光標

[英]How to draw cursor with pygame

我試圖通過隱藏並繪制到某個位置來繪制光標。 我想使不同的圖像出現在我單擊時,但它似乎不能很好地工作。 請幫忙! 它只顯示為好像我單擊。

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()

問題在於您如何檢查用戶是否單擊。 pygame.MOUSEBUTTONDOWN實際上是pygame中的一個常量,用於分配給按下鼠標按鈕的數字值(嘗試將其打印出來)。 pygame.QUIT一樣, pygame.MOUSEBUTTONDOWN也是一種事件,因此可以在現有事件循環中像這樣檢查鼠標是向上還是向下:

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()

另外,如果您不想弄亂事件循環,可以使用pygame.mouse.get_pos()代替:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM