2016-11-05 121 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