在pygame中移动图像

0 投票
1 回答
12018 浏览
提问于 2025-04-18 13:02

我刚开始学习pygame。最近我想用pygame移动一张图片,但遇到了问题。我的代码是:

import pygame
from pygame.locals import*
img = pygame.image.load('tank.jpg')

white = (255, 64, 64)

w = 640
h = 480
    screen = pygame.display.set_mode((w, h))

screen.fill((white))

running = 1

while running:

  x = 5
  x++
  y  = 10
  y++

  screen.fill((white))
  screen.blit(img,(x,y))
  pygame.display.flip()
  pygame.update()

我尝试了这个代码来移动图片,但没有成功。

1 个回答

2

1) 你在每次循环的时候都在重新给 xy 赋值。

2) 在 Python 里不能用 x++,应该用 x+=1 来增加 1。

3) pygame.update() 这个可能是你想写 pygame.display.update()

试试这个:

running = 1                     
x= 5                            
y = 5                           
while running:                  
    x +=1                       
    y +=1                       
    screen.fill((white))        
    screen.blit(img,(x,y))      
    pygame.display.flip()       
    pygame.display.update()

撰写回答