Python 2.7 Pygame-Pygame.mouse.get\u pos问题

Python 2.7 Pygame-Pygame.mouse.get\u pos问题,python-2.7,pygame,Python 2.7,Pygame,问题不是错误,只是我试图让这个十字线图像跟随鼠标在屏幕上移动,我成功了,但它在后面留下了相同图像的痕迹,因此屏幕最终会被图像垃圾邮件,代码如下: import sys, pygame; from pygame.locals import *; spaceship = ('spaceship.png') mouse_c = ('crosshair.png') pygame.init() screen = pygame.display.set_mode((800, 600)) mousec = py

问题不是错误,只是我试图让这个十字线图像跟随鼠标在屏幕上移动,我成功了,但它在后面留下了相同图像的痕迹,因此屏幕最终会被图像垃圾邮件,代码如下:

import sys, pygame;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("test1")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("test3")
    pos = pygame.mouse.get_pos() #Here is the mouse following code
    screen.blit(mousec, (pos)) #Setting image to pos of mouse.. atleast I think I am
    pygame.display.update()

您的代码没有问题……它正在执行您告诉它的操作。
但是,对于绘制的任何图像,都会创建一个新的背景(与图像一起),并将其自身设置为新背景,因此在调用函数
pygame.display.update()
pygame.display.flip()
之前,需要擦除背景并将其设置为默认值。

更多信息

  • 所以基本上你所能做的就是通过拍摄一张图片(800x600)
    ('background.png')
    来创建一个新的背景

  • 加载它:
    bk=pygame.image.load('background.png')。convert_alpha()

  • 在闪烁其他移动对象之前闪烁它。
    screen.Blit(bk,(0,0))

    pos=pygame.mouse.get#pos()#以下是鼠标的代码

    screen.blit(mousec,(pos))#将图像设置为鼠标的pos

    pygame.display.update()


  • 为游戏添加一个背景可以使游戏具有完整的触感,但如果您不想添加背景,那么最简单的方法就是在每次调用
    while
    循环时用颜色填充屏幕。
    因此,在while循环开始后添加以下内容:
    屏幕填充(颜色(“黑色”))