【发布时间】:2016-09-22 03:14:56
【问题描述】:
我是 Python 的初学者。我正在尝试使用 pygame 模块移动图像。但我无法在 python 中移动图像的位置。你能帮我理解我做错了什么吗?
import pygame, sys
from pygame.locals import *
pygame.init()
image = pygame.image.load("ball.jpg")
image = pygame.transform.scale(image, (100, 100))
imgrect = image.get_rect()
Canvas = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Text Input')
imgrect.left = 200
imgrect.top = 200
Canvas.blit(image, imgrect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == KEYDOWN :
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_UP:
imgrect.top += 1
if event.key == K_DOWN:
imgrect.top -= 1
【问题讨论】:
-
对变量使用小写名称(如
canvas)而不是Canvas。第二个用于类,如果将两者混合使用,可能会使其他程序员感到困惑。