1.让一个物体任意移动
import pygame
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((600,400))
window.fill((255,255,255))
pygame.display.flip()
x = 0
y = 0
x_speed = 20
y_speed = 10
while True:
pygame.time.delay(50)
window.fill((255,255,255))
pygame.draw.rect(window,(255,0,0),(x,y,100,50),0)
pygame.display.update()
x += x_speed
y += y_speed
if x > 500 or y > 350:
x_speed,y_speed = -20,-10
elif x < 0 or y < 0:
x_speed,y_speed = 20,10
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()