2016-11-05 50 views
0

我想通過隱藏和繪製到某個地方來繪製光標。 我想單擊時顯示不同的圖像,但看起來效果不好。請幫忙! 它只顯示爲我點擊。如何用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() 

回答

0

問題在於如何檢查用戶是否點擊過。 pygame.MOUSEBUTTONDOWN實際上是pygame中的一個常量,用於分配給鼠標按鈕的數值(嘗試打印出來)。像pygame.QUITpygame.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() 
+0

您的意思是pygame.mouse.get_pressed()對不對? –

+0

和謝謝!我不知道它是一個常量值。 –

+0

實際上'pygame.mouse.get_pressed()'按順序返回鼠標左鍵,中鍵和右鍵的按鈕狀態,所以我只把第一個元素獲取鼠標左鍵狀態。這就是爲什麼我在我的代碼中使用'pygame.mouse.get_pressed()[0]'而不是! – CodeSurgeon