Python3 PyGame 如何移动图像?

low*_*low 5 python pygame python-3.x

我尝试使用.blit但出现问题,这是进一步解释我的问题的屏幕截图:

随着我的鼠标移动,图像似乎在屏幕上被弄脏了

它只是复制我的图像。

代码:

import pygame   
import keyboard
black = (0,0,0)
white = (255, 255, 255)

pygame.init()

screen = pygame.display.set_mode((600, 400))
screen.fill(black)
screen.convert()

icon = pygame.image.load('cross.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('MouseLoc')
cross = pygame.image.load('cross.png')


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or keyboard.is_pressed(' '):
            running = False
            
mx, my = pygame.mouse.get_pos()
screen.blit(cross, (mx-48, my-48))
print(my, mx)
pygame.display.update() 
Run Code Online (Sandbox Code Playgroud)

NMr*_*cks 1

修复给定代码中的缩进,它将所有重要的内容放在 while 循环之外。\n您还可以添加一个函数,在每次移动精灵时重新绘制窗口:

\n
def redraw_game_window():\n    screen.fill(black)\n    screen.blit(\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2)\n    pygame.display.update()\n
Run Code Online (Sandbox Code Playgroud)\n

fill将所有andblit语句放入函数内后,将其放在程序末尾:

\n
redraw_game_window()\n
Run Code Online (Sandbox Code Playgroud)\n

此功能将使您更容易在屏幕上传输更多项目,并且这将防止对象在屏幕上绘制,因为每次移动对象时屏幕都会更新。

\n