4 疯狂的小圆圈

图书简介可以看这里:

童晶:《Python游戏趣味编程》新书上架了

本章我们将实现一个好玩的程序,鼠标点击后,会在点击处出现一些同心圆圈,并在窗口中四处反弹,效果如图4-1所示。首先我们学习列表的概念,用列表记录实现多个小球的反弹;然后学习一种新的鼠标交互方式,用鼠标移动点击来增加绘制同心圆。

本章案例最终代码一共38行,代码参看:配套资源\第4章\4-5.py,视频效果参看:配套资源\第4章\疯狂的小圆圈.mp4。

import pgzrun  # 导入游戏库
import random  # 导入随机库
WIDTH = 800   # 设置窗口的宽度
HEIGHT = 600  # 设置窗口的高度
balls = []  # 存储所有小球的信息,初始为空列表

def draw():   # 绘制模块,每帧重复执行
    screen.fill('white')  # 白色背景
    for ball in balls:  # 绘制所有的圆
        screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7]))
        for x in range(1, ball[4], 3): # 用同心圆填充
            screen.draw.filled_circle((ball[0], ball[1]), ball[4]-x, (random.randint(
                ball[5], 255), random.randint(ball[6], 255), random.randint(ball[7], 255)))

def update():  # 更新模块,每帧重复操作
    for ball in balls:
        ball[0] = ball[0] + ball[2]   # 利用x方向速度更新x坐标
        ball[1] = ball[1] + ball[3]   # 利用y方向速度更新y坐标
        if ball[0] > WIDTH-ball[4] or ball[0] < ball[4]:  # 当小球碰到左右边界时,x方向速度反转
            ball[2] = -ball[2]
        if ball[1] > HEIGHT-ball[4] or ball[1] < ball[4]:  # 当小球碰到上下边界时,y方向速度反转
            ball[3] = -ball[3]

def on_mouse_move(pos, rel, buttons): # 当鼠标移动时
    if mouse.LEFT in buttons:         # 当鼠标左键按下时
        x = pos[0]   # 鼠标的x坐标,设为小球的x坐标
        y = pos[1]   # 鼠标的y坐标,设为小球的y坐标
        speed_x = random.randint(1, 5)  # 小球x方向的速度
        speed_y = random.randint(1, 5)  # 小球y方向的速度
        r = random.randint(5, 50)      # 小球的半径
        colorR = random.randint(10, 255)  # 小球的三个颜色分量
        colorG = random.randint(10, 255)
        colorB = random.randint(10, 255)
        # 存储小球所有信息的列表
        ball = [x, y, speed_x, speed_y, r, colorR, colorG, colorB]
        balls.append(ball)  # 把第i号小球的信息添加到balls中

pgzrun.go()   # 开始执行游戏

 

练习4-4:尝试修改4-5.py,实现用鼠标写字画图的程序,得到类似的效果:

import pgzrun  # 导入游戏库
import random  # 导入随机库
WIDTH = 1200   # 设置窗口的宽度
HEIGHT = 800  # 设置窗口的高度
balls = []  # 存储所有小球的信息,初始为空列表

def draw():   # 绘制模块,每帧重复执行
    screen.fill('white')  # 白色背景
    for ball in balls:  # 绘制所有的圆
        screen.draw.filled_circle((ball[0], ball[1]), ball[2], (ball[3], ball[4], ball[5]))
        for x in range(1, ball[2], 3): # 用同心圆填充
            screen.draw.filled_circle((ball[0], ball[1]), ball[2]-x, (random.randint(
                ball[3], 255), random.randint(ball[4], 255), random.randint(ball[5], 255)))

def on_mouse_move(pos, rel, buttons): # 当鼠标移动时
    if mouse.LEFT in buttons:         # 当鼠标左键按下时
        x = pos[0]   # 鼠标的x坐标,设为小球的x坐标
        y = pos[1]   # 鼠标的y坐标,设为小球的y坐标
        r = random.randint(10, 20)      # 小球的半径
        colorR = random.randint(10, 255)  # 小球的三个颜色分量
        colorG = random.randint(10, 255)
        colorB = random.randint(10, 255)
        # 存储小球所有信息的列表
        ball = [x, y, r, colorR, colorG, colorB]
        balls.append(ball)  # 把第i号小球的信息添加到balls中

pgzrun.go()   # 开始执行游戏

分步骤代码、讲解视频可以从异步社区下载:

https://www.epubit/bookDetails?id=UB72096d97d6149

分步骤代码也可以直接从这里下载:

联想Filez

 

这一章主要学习了列表的基本语法与应用、鼠标交互等知识点。利用列表,我们就可以记录、处理大量的数据;用户使用鼠标和程序互动,可以带来更加直观多样的交互效果。利用这些知识点,开发了疯狂小圆圈的趣味程序,读者也可以尝试实现更加有趣的交互、可视化效果。

 

更多推荐

《Python游戏趣味编程》 第4章 疯狂的小圆圈