模块化编程概述与拦球动画

```"""
Ball类的代码
"""

import pygame
from random import randint
from pygame.locals import *

class Ball(pygame.sprite.Sprite):
def __init__(self,screen,board):
"""
screen:所在的屏幕,board:球所要访问的拦板
"""
self.board = board
pygame.sprite.Sprite.__init__(self)# 初始化父类
r = randint(30,50)
self.sw = screen.get_width()       # 获取屏幕宽度
self.sh = screen.get_height()      # 获取屏幕高度
self.image = pygame.Surface((r*2,r*2),SRCALPHA)
pos = r,r                          # 设定圆心坐标
color = randint(0,255),randint(0,255),randint(0,255)
pygame.draw.circle(self.image,color,pos,r)
self.rect = self.image.get_rect()  # 获取矩形对象
self.rect.center = self.sh//2,self.sw//2
self.speed = [randint(-5,5),randint(-5,5)]

def update(self):
"""根据speed更新矩形对象"""
self.rect.move_ip(self.speed)      # 移动矩形对象
self.bounce_on_board()             # 碰到板子就反弹
self.bounce_on_edge()              # 碰到边缘就反弹

def bounce_on_edge(self):
"""碰到边缘就反弹"""
# 如果矩形的最右边x坐标大于屏幕宽度或者最左边x坐标小于等于0
if self.rect.right>=self.sw or \
self.rect.left<=0 : self.speed[0] = -self.speed[0]

if self.rect.bottom>=self.sh or \
self.rect.top<=0 : self.speed[1] = -self.speed[1]

def bounce_on_board(self):
"""碰到板子就反弹"""
tf = pygame.sprite.collide_rect(self,self.board)
if tf :                             # 如果碰到板子
self.speed[1] = -self.speed[1]

if __name__ == "__main__":

pass
```

Board类就比较简单了，它是跟随着鼠标指针左右移动的。下面是board.py的代码

```"""
Board类的代码,跟随鼠标指针左右移动的矩形。
"""

import pygame

class Board(pygame.sprite.Sprite):
def __init__(self,size,pos):
"""初始化方法,size:宽高,pos:坐标"""
pygame.sprite.Sprite.__init__(self) # 初始化父类
self.image = pygame.Surface(size)   # 新建图形
self.image.fill((13,223,223))       # 填充图形
self.rect = self.image.get_rect(center=pos)

def update(self):
"""更新拦板的坐标"""
mpos = pygame.mouse.get_pos() # 获取鼠标指针坐标
self.rect.centerx = mpos[0]   # 设置矩形中央x坐标

if __name__ == "__main__":

pass
```

```"""
拦球动画,本程序导入Ball和Board类,让球碰到拦板会反弹。
"""

from ball import *
from board import *

def main():
"""
主要函数
"""
size = 480,360
screen = pygame.display.set_mode(size)
pygame.display.set_caption("碰到边缘就反弹的球类演示代码")

# 生成拦板,前面是板子的宽高,后面是坐标
b = Board((230,10),(200,size[1]-50))      # 生成拦板

balls = [Ball(screen,b) for i in range(3)]# 生成3个球
group = pygame.sprite.Group()

clock = pygame.time.Clock()              # 新建时钟对象
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:running=False

group.update()        # 调用组中所有对象的update方法

screen.fill((110,10,60))
group.draw(screen)   # 在screen上渲染组中的每个对象的surface

pygame.display.update()
clock.tick(60)
pygame.quit()

if __name__ == "__main__":

main()
```