pygame单击彩色弹球练习.py

"""pygame单击彩色弹球练习_事件USEREVENT,组group,sprite角色类练习程序"""

__author__ = "李兴球"
__date__ = "2018年8月"
__company__  = "风火轮少儿编程"

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

class Ball(pygame.sprite.Sprite):
def __init__(self,radius,x,y,screen,color):
pygame.sprite.Sprite.__init__(self)      # 初始化父类
self.radius  = radius                    # 弹球半径
self.screen_width = screen.get_width()   # 屏幕宽高
self.screen_height = screen.get_height() # 屏幕高度
self.image = pygame.Surface((radius * 2,radius * 2 )) # image图层
self.rect = self.image.get_rect()        #r ect属性,表示坐标与长高
self.rect.centerx = x                    # 设定中央x坐标
self.rect.centery = y                    # 设定中央y坐标
pygame.draw.circle(self.image,color,(radius,radius),radius) # 在self.image上画圆,颜色,位置,半径
self.image.set_colorkey((0,0,0))         # 设置不渲染的颜色(透明色)
self.xspeed = randint(-4,4)              # 初始x速度
self.yspeed = randint(-4,4)              # 初始y速度
def bounce(self):
"""碰到边缘就反弹,原理是x或y速度取负"""
if self.rect.right >= self.screen_width or self.rect.left<=0:self.xspeed = -self.xspeed
if self.rect.bottom >= self.screen_height or self.rect.top<=0:self.yspeed = -self.yspeed

def update(self):
"""更新坐标,move_ip是在原位移动rect"""
self.rect.move_ip(self.xspeed,self.yspeed)
self.bounce()

if __name__ == "__main__":

width,height = 800,600
screen = pygame.display.set_mode((width,height))    # 新建屏幕对象分辨率为800x600
pygame.display.set_caption("pygame事件USEREVENT,组group,sprite角色类练习参考答案")

group = pygame.sprite.Group()                        # 新建组
r = randint(0,255);g = randint(0,255);b = randint(0,255) # 生成rgb三元色
ball1 = Ball(20,width//2,height//2,screen,(r,g,b))   # 先生成一个球球

group.add(ball1)                                     # 把球球加到组中,以便统一更新坐标与重画.

produceball = USEREVENT + 1                          # 定义生成球事件
deleteball = USEREVENT + 2                           # 定义删除球事件
pygame.time.set_timer(produceball,1000)              # 让生成球事件每隔一秒发生一次
pygame.time.set_timer(deleteball,1400)               # 让删除球事件每隔一秒发生一次
clock = pygame.time.Clock()                          # 生成时钟对象
running = True                                       # 运行为真
while running:                                       # 当 running 为真时,执行循环体
clock.tick(30)                                   # fps为60,每秒渲染的图片为60张
for event in pygame.event.get():                 # 遍历每个事件
if event.type == QUIT : running = False      # 事件为关闭窗口的事件则让running为否
if event.type == produceball :               # 如果事件为产生球事件,那么生成一个球
r = randint(0,255);g = randint(0,255);b = randint(0,255)
group.add( Ball(20,width//2,height//2,screen,(r,g,b)))
#if event.type == deleteball and len(group)>0 : group.remove(choice(list(group)))  # 从group中随机选择一个球,移除它.

"答案在此,注意上面的if语句增加的代码"
b1,b2,b3 = pygame.mouse.get_pressed()            # 分别表示左,中,右鼠标键
mx,my = pygame.mouse.get_pos()                   # 获取鼠标指针坐标
for ball in group:                               # 遍历每个球
if b1 and ball.rect.collidepoint(mx,my):
group.remove(ball)# 按下鼠标左键并且鼠标指针的x,y坐标在矩形内,则删除

group.update()                                   # 更新球的坐标
screen.fill((0,0,0))                             # 先画背景
group.draw(screen)                               # 在screen上重画球
pygame.display.update()
pygame.quit()                                        # running为False时退出循环,执行退出pygame命令,程序结束