实验代码:
# -*- coding:utf-8 -*- import sys #导入sys模块 import pygame #导入pygame模块 pygame.init() #初始化pygame size = widch, height = 1500, 900 #设置窗口 screen = pygame.display.set_mode(size) #显示窗口 color = (172,52,92) #设置窗口 ball = pygame.image.load("ball.png") #加载图片 ballrect = ball.get_rect() #获取矩形区域 speed = [12,12] clock = pygame.time.Clock() #执行死循环,确保窗口一直显示 while True: clock.tick(160) #每秒执行120次 #检查事件 for event in pygame.event.get(): if event.type == pygame.QUIT: #如果单击关闭窗口,则退出 pygame.quit() #退出pygame sys.exit() ballrect = ballrect.move(speed) #移动小球 #碰到左右边缘 if ballrect.left < 0 or ballrect.right > widch: speed[0] = -speed[0] #碰到上下边缘 if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(color) #填充颜色 screen.blit(ball, ballrect) #将图片画到窗口上 pygame.display.flip() #更新全部显示
实验结果:
标签:ball,窗口,ballrect,小球,跳动,pygame,speed,event From: https://www.cnblogs.com/zhuangzhuang123/p/16947214.html