Python精选200Tips:146-150


运行系统:macOS Sonoma 14.6.1
Python编译器:PyCharm 2024.1.4 (Community Edition)
Python版本:3.12

往期链接:

1-5 6-10 11-20 21-30 31-40 41-50
51-60:函数 61-70:类 71-80:编程范式及设计模式
81-90:Python编码规范 91-100:Python自带常用模块-1
101-105:Python自带模块-2 106-110:Python自带模块-3
111-115:Python常用第三方包-频繁使用 116-120:Python常用第三方包-深度学习
121-125:Python常用第三方包-爬取数据 126-130:Python常用第三方包-为了乐趣
131-135:Python常用第三方包-拓展工具1 136-140:Python常用第三方包-拓展工具2
Python项目实战
141-145
P146–Boid模型用于模拟鸟群行为
技术栈:pygame(版本2.6.0)+Boid模型
import pygame
import random

# 屏幕设置
WIDTH, HEIGHT = 800, 600
BACKGROUND_COLOR = (255, 192, 203)  # 粉色背景

# 矩形类,用于范围查询
class Rectangle:
    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def contains(self, position):
        return (self.x <= position.x < self.x + self.w and
                self.y <= position.y < self.y + self.h)

    def intersects(self, other):
        return not (other.x + other.w < self.x or
                    other.x > self.x + self.w or
                    other.y + other.h < self.y or
                    other.y > self.y + self.h)

# Boid 类
class Boid:
    def __init__(self, x, y):
        self.position = pygame.Vector2(x, y)
        self.velocity = pygame.Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
        self.velocity.scale_to_length(random.uniform(2, 4))  # 随机速度

    def update(self, boids):
        alignment = self.align(boids) 
        cohesion = self.cohere(boids) 
        separation = self.separate(boids)
        
        self.velocity += alignment + cohesion + separation
        self.velocity = self.velocity.normalize() * 4  # 限制速度
        self.position += self.velocity
        
        self.wrap_around()

    def align(self, boids):
        perception_radius = 50
        steering = pygame.Vector2(0, 0)
        total = 0
        
        for other in boids:
            if other != self and self.position.distance_to(other.position) < perception_radius:
                steering += other.velocity
                total += 1
        
        if total > 0:
            steering /= total
            steering = steering.normalize() * 4
            steering -= self.velocity
            return steering
        
        return pygame.Vector2(0, 0)

    def cohere(self, boids):
        perception_radius = 100
        steering = pygame.Vector2(0, 0)
        total = 0
        
        for other in boids:
            if other != self and self.position.distance_to(other.position) < perception_radius:
                steering += other.position
                total += 1
        
        if total > 0:
            steering /= total
            steering -= self.position
            steering = steering.normalize() * 4
            steering -= self.velocity
            return steering
        
        return pygame.Vector2(0, 0)

    def separate(self, boids):
        perception_radius = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AnFany

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值