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)
修复给定代码中的缩进,它将所有重要的内容放在 while 循环之外。\n您还可以添加一个函数,在每次移动精灵时重新绘制窗口:
\ndef redraw_game_window():\n screen.fill(black)\n screen.blit(\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2)\n pygame.display.update()\nRun Code Online (Sandbox Code Playgroud)\nfill将所有andblit语句放入函数内后,将其放在程序末尾:
redraw_game_window()\nRun Code Online (Sandbox Code Playgroud)\n此功能将使您更容易在屏幕上传输更多项目,并且这将防止对象在屏幕上绘制,因为每次移动对象时屏幕都会更新。
\n| 归档时间: |
|
| 查看次数: |
56 次 |
| 最近记录: |