Python/Pygame鼠标位置不更新(blit函数)
我试图使用Pygame制作一个简单的菜单,但我发现每当我使用Pygame.mouse.get_位置时,它都会满足我的需要,但我必须不断移动鼠标,使图片不断闪烁Python/Pygame鼠标位置不更新(blit函数),python,pygame,Python,Pygame,我试图使用Pygame制作一个简单的菜单,但我发现每当我使用Pygame.mouse.get_位置时,它都会满足我的需要,但我必须不断移动鼠标,使图片不断闪烁 import pygame import sys pygame.init() screen = pygame.display.set_mode((800,600)) pygame.display.set_caption('cursor test') cursorPng = pygame.image.load('resources/i
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('cursor test')
cursorPng = pygame.image.load('resources/images/cursor.png')
start = pygame.image.load('resources/images/menuStart.jpg')
enemy = pygame.image.load('resources/images/enemy-1.png')
white = (255,255,255)
black = (0,0,0)
clock = pygame.time.Clock()
FPS = 60
while True:
screen.fill(white)
pygame.mouse.set_visible(False)
x,y = pygame.mouse.get_pos()
x = x - cursorPng.get_width()/2
y = y - cursorPng.get_height()/2
screen.blit(cursorPng,(x,y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEMOTION:
if x < 50 and y < 250:
screen.blit(enemy,(100,100))
clock.tick(FPS)
pygame.display.update()
导入pygame
导入系统
pygame.init()
screen=pygame.display.set_模式((800600))
pygame.display.set_标题('光标测试')
cursorPng=pygame.image.load('resources/images/cursor.png')
start=pygame.image.load('resources/images/menuStart.jpg')
敌方=pygame.image.load('resources/images/敌方-1.png')
白色=(255255)
黑色=(0,0,0)
clock=pygame.time.clock()
FPS=60
尽管如此:
屏幕填充(白色)
pygame.mouse.set_可见(False)
x、 y=pygame.mouse.get_pos()
x=x-cursorPng.get_width()/2
y=y-cursorPng.get_height()/2
屏幕光点(光标,(x,y))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
pygame.quit()
sys.exit()
elif event.type==pygame.MOUSEMOTION:
如果x<50且y<250:
屏幕。光点(敌人,(100100))
时钟滴答声(FPS)
pygame.display.update()
怎么了?您需要在屏幕上快速显示一个曲面和一个矩形 首先,使用我用于加载图像的这个片段。它确保正确加载图像:
def loadImage(name, alpha=False):
"Loads given image"
try:
surface = pygame.image.load(name)
except pygame.error:
raise SystemExit('Could not load image "%s" %s' %
(name, pygame.get_error()))
if alpha:
corner = surface.get_at((0, 0))
surface.set_colorkey(corner, pygame.RLEACCEL)
return surface.convert_alpha()
第二,当你得到曲面时,得到它的矩形如下:
cursorSurf = loadImage('resources/images/cursor.png')
cursorRect = cursorSurf.get_rect()
screen.blit(cursorSurf, cursorRect)
然后,在更新中执行以下操作:
cursorRect.center = pygame.mouse.get_pos()
最后,请按如下方式浏览屏幕:
cursorSurf = loadImage('resources/images/cursor.png')
cursorRect = cursorSurf.get_rect()
screen.blit(cursorSurf, cursorRect)
现在,您将注意到,无需移动鼠标即可正确渲染鼠标。查看您的代码:
for event in pygame.event.get():
...
elif event.type == pygame.MOUSEMOTION:
if x < 50 and y < 250:
screen.blit(enemy,(100,100))