You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.5 KiB
2.5 KiB
废弃方法修复记录
修复日期
2024年
修复的废弃方法
1. Go 标准库 - math/rand
修复内容:
- 废弃方法:
rand.Seed(time.Now().UnixNano())
- 修复方案: 完全移除该调用
- 原因: Go 1.20+ 中随机数生成器自动种子化,不再需要手动调用
rand.Seed()
- 文件:
main.go
变更:
// 修复前
import (
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
// ...
}
// 修复后
func main() {
// 不再需要 rand.Seed() 调用
// ...
}
2. Ebitengine v2.5+ - 图形绘制 API
修复内容:
- 废弃方法:
ebitenutil.DrawRect()
和ebitenutil.DrawLine()
- 新方法:
vector.DrawFilledRect()
和vector.StrokeLine()
- 原因: Ebitengine v2.5 后推荐使用性能更好的 vector 包
- 文件:
internal/game/game.go
主要变更:
矩形绘制:
// 修复前
ebitenutil.DrawRect(screen, x, y, width, height, color)
// 修复后
vector.DrawFilledRect(screen, float32(x), float32(y), float32(width), float32(height), color, false)
线条绘制:
// 修复前
ebitenutil.DrawLine(screen, x1, y1, x2, y2, color)
// 修复后
vector.StrokeLine(screen, float32(x1), float32(y1), float32(x2), float32(y2), 1, color, false)
修复位置:
- 游戏背景绘制
- 预览区域背景
- 网格线绘制
- 方块绘制 (
drawBlock
方法) - 幽灵方块绘制
- 下一个方块预览
3. 导入包更新
添加了新的导入:
import (
// ... 其他导入
"github.com/hajimehoshi/ebiten/v2/vector"
)
性能优势
Vector API 优势:
- 更好的性能: vector 包使用 GPU 加速绘制
- 更准确的渲染: 减少了像素对齐问题
- 未来兼容性: 与 Ebitengine 的发展方向一致
随机数优化:
- 自动优化: Go 1.20+ 的随机数生成器自动优化
- 更好的随机性: 不需要手动种子化
- 代码简化: 减少了样板代码
验证结果
- ✅ 编译成功
- ✅ Lint 检查通过 (无废弃警告)
- ✅ 功能测试正常
- ✅ 多平台构建正常
影响评估
- 兼容性: 完全向前兼容
- 功能: 无功能变化,纯技术升级
- 性能: 理论上性能有所提升
- 维护性: 符合最新最佳实践
建议
- 定期运行
make check
检查代码质量 - 关注 Ebitengine 官方更新,及时采用新 API
- 保持 Go 版本更新以享受最新优化