# 导入pygame和sys模块 |
import pygame,sys |
# 初始化pygame,为访问pygame模块中的资源做准备 |
pygame.init() |
# 创建游戏窗口对象 |
SIZE = GAMEMIDTH,GAMEHEIGT = 480 , 360 |
game = pygame.display.set_mode(SIZE) |
# 设置窗口标题 |
pygame.display.set_caption( '弹球系列' ) |
# 创建变量 |
FPS = 60 |
x,y = 100 , 100 |
speedx,speedy = 2 , 2 |
r = 10 |
# 游戏主循环 |
while True : |
# 设置背景颜色 |
game.fill(( 255 , 255 , 255 )) |
# 计时器,设置帧率 |
pygame.time.Clock().tick(FPS) |
# 遍历事件列表 |
for event in pygame.event.get(): |
print (event) |
if event. type = = pygame.QUIT: |
pygame.quit() |
sys.exit() |
x + = speedx |
y + = speedy |
# 小球碰到边缘是反弹 |
if x < r or x > GAMEMIDTH - r: |
speedx = - speedx |
if y < r or y > GAMEHEIGT - r: |
speedy = - speedy |
# 画圆 |
pygame.draw.circle(game,( 255 , 0 , 0 ),(x,y),r, 0 ) |
# 刷新显示 |
pygame.display.flip() |