前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Python实现水波纹效果:逼真的水波动画

使用Python实现水波纹效果:逼真的水波动画

作者头像
屿小夏
发布2024-09-20 08:36:48
550
发布2024-09-20 08:36:48
举报
文章被收录于专栏:IT杂谈学习

引言

水波纹效果是一种常见且迷人的视觉效果,广泛应用于游戏和图形设计中。在这篇博客中,我们将使用Python创建一个逼真的水波动画。通过利用Pygame库和基于网格的算法,我们可以实现动态水波纹效果。

准备工作

前置条件

在开始之前,你需要确保你的系统已经安装了Pygame库。如果你还没有安装它,可以使用以下命令进行安装:

代码语言:javascript
复制
pip install pygame

Pygame是一个跨平台的Python模块,用于编写视频游戏。它包括计算机图形和声音库,使得游戏开发更加简单。

代码实现与解析

导入必要的库

我们首先需要导入Pygame库和其他必要的模块:

代码语言:javascript
复制
import pygame
import numpy as np
初始化Pygame

我们需要初始化Pygame并设置屏幕的基本参数:

代码语言:javascript
复制
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("水波纹效果")
clock = pygame.time.Clock()
定义水波类

我们创建一个Water类来定义水波的属性和行为:

代码语言:javascript
复制
class Water:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.current = np.zeros((height, width))
        self.previous = np.zeros((height, width))

    def update(self):
        damping = 0.99
        for y in range(1, self.height - 1):
            for x in range(1, self.width - 1):
                self.current[y, x] = (
                    (self.previous[y-1, x] +
                     self.previous[y+1, x] +
                     self.previous[y, x-1] +
                     self.previous[y, x+1]) / 2
                    - self.current[y, x]
                ) * damping

    def disturb(self, x, y, radius=5, intensity=1000):
        if 1 < x < self.width - 1 and 1 < y < self.height - 1:
            self.previous[y-radius:y+radius, x-radius:x+radius] += intensity
绘制水波

我们定义一个函数来绘制水波效果:

代码语言:javascript
复制
def draw_water(screen, water):
    for y in range(water.height):
        for x in range(water.width):
            color_value = min(max(int(water.current[y, x]), 0), 255)
            color = (0, 0, color_value)
            screen.set_at((x, y), color)
主循环

我们在主循环中更新水波的状态并绘制:

代码语言:javascript
复制
water = Water(800, 600)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            water.disturb(x, y)

    water.update()

    screen.fill((0, 0, 0))
    draw_water(screen, water)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

完整代码

代码语言:javascript
复制
import pygame
import numpy as np

# 初始化Pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("水波纹效果")
clock = pygame.time.Clock()

# 水波类定义
class Water:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.current = np.zeros((height, width))
        self.previous = np.zeros((height, width))

    def update(self):
        damping = 0.99
        for y in range(1, self.height - 1):
            for x in range(1, self.width - 1):
                self.current[y, x] = (
                    (self.previous[y-1, x] +
                     self.previous[y+1, x] +
                     self.previous[y, x-1] +
                     self.previous[y, x+1]) / 2
                    - self.current[y, x]
                ) * damping

    def disturb(self, x, y, radius=5, intensity=1000):
        if 1 < x < self.width - 1 and 1 < y < self.height - 1:
            self.previous[y-radius:y+radius, x-radius:x+radius] += intensity

# 绘制水波函数
def draw_water(screen, water):
    for y in range(water.height):
        for x in range(water.width):
            color_value = min(max(int(water.current[y, x]), 0), 255)
            color = (0, 0, color_value)
            screen.set_at((x, y), color)

# 主循环
water = Water(800, 600)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            water.disturb(x, y)

    water.update()

    screen.fill((0, 0, 0))
    draw_water(screen, water)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-09-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • 准备工作
    • 前置条件
    • 代码实现与解析
      • 导入必要的库
        • 初始化Pygame
          • 定义水波类
            • 绘制水波
              • 主循环
              • 完整代码
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档