1 # -*- coding:utf-8 -*- 2 import sys # 导入sys模块 3 import pygame # 导入pygame模块 4 5 pygame.init() # 初始化pygame 6 size = width, height = 1000, 700 # 设置窗口 7 screen = pygame.display.set_mode(size) # 显示窗口 8 color = (0, 0, 0) # 设置颜色 9 10 ball = pygame.image.load("ball.png") # 加载图片 11 ballrect = ball.get_rect() # 获取矩形区域 12 13 speed = [25, 25] # 设置移动的X轴、Y轴距离 14 clock = pygame.time.Clock() # 设置时钟 15 # 执行死循环,确保窗口一直显示 16 while True: 17 clock.tick(20) # 每秒执行60次 18 # 检查事件 19 for event in pygame.event.get(): 20 if event.type == pygame.QUIT: # 如果点击关闭窗口,则退出 21 sys.exit() 22 23 ballrect = ballrect.move(speed) # 移动小球 24 # 碰到左右边缘 25 if ballrect.left < 0 or ballrect.right > width: 26 speed[0] = -speed[0] 27 # 碰到上下边缘 28 if ballrect.top < 0 or ballrect.bottom > height: 29 speed[1] = -speed[1] 30 31 screen.fill(color) # 填充颜色 32 screen.blit(ball, ballrect) # 将图片画到窗口上 33 pygame.display.flip() # 更新全部显示 34 35 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
标签:25,ball,窗口,ballrect,Pythontext,pygame,speed From: https://www.cnblogs.com/kimtaeyoan/p/16960555.html