修改如下, 说明在每行右边的批注
import pygame
import sys
# set everything to where started
pygame.init()
size = width, height = 650, 650
speed = [-2,1]
bg = (255, 255, 255)
# create a window with a determined size
screen = pygame.display.set_mode(size)
# create window title
pygame.display.set_caption("Hello!")
ball = pygame.image.load("D://soccer.jpg")
# position of the image
position = ball.get_rect()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # quit from pygame
sys.exit()
# move the image # From this line
position = position.move(speed) # should indent
screen.fill(bg) # clear screen
screen.blit(ball, position) # draw ball at position
pygame.display.update() # update screen
clock.tick(100) # 100 frames/second
if position.left < 0 or position.right > height:
# flip image
ball = pygame.transform.flip(ball, True, False) # flip image to ball
# move to the other side
speed[0] = -speed[0] # index 0 for x
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]