import sys # 导入sys模块
import pygame # 导入pygame模块
pygame.init() # 初始化pygame
size = width, height = 800, 800 # 设置窗口
screen = pygame.display.set_mode(size) # 显示窗口
color = (0, 0, 0) # 设置窗口背景颜色
ball = pygame.image.load("ball.png") # 加载图片(可以进行替换你想要的图片)
ballrect = ball.get_rect() # 获取矩形区域
speed = [1, 1] # 设置移动的X轴、Y轴距离
# 执行死循环,确保窗口一直显示
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed) # 移动小球
# 碰到左右边缘
if ballrect.left < 0 or ballrect.right > width:
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() # 更新全部显示
pygame.quit() # 退出pygame
碰撞小球PYGAME
最新推荐文章于 2024-07-22 22:58:19 发布