当前位置: 首页 > news >正文

0391-Raylib-按钮动画和声音

环境

  • Time 2024-03-01
  • Zig 0.12.0-dev.3076+6e078883e
  • WSL-Ubuntu 22.04.3 LTS
  • Raylib 5.0

前言

说明

参考资料:

  1. https://www.raylib.com/examples.html

目标

渲染一个按钮的不同状态,当点击按钮时,会有音效。

main.zig

const std = @import("std");
const ray = @import("raylib.zig");const NUM_FRAMES = 3;pub fn main() !void {const screenWidth: c_int = 800;const screenHeight: c_int = 450;ray.InitWindow(screenWidth, screenHeight, "raylib [texture] example");defer ray.CloseWindow();ray.SetTargetFPS(60);ray.InitAudioDevice(); // Initialize audio devicedefer ray.CloseAudioDevice();const fxButton = ray.LoadSound("res/buttonfx.wav"); // Load button sounddefer ray.UnloadSound(fxButton);const button = ray.LoadTexture("res/button.png"); // Load button texturedefer ray.UnloadTexture(button);// Define frame rectangle for drawingconst frameHeight = @divTrunc(button.height, NUM_FRAMES);var sourceRec = ray.Rectangle{.width = @floatFromInt(button.width),.height = @floatFromInt(frameHeight),};// Define button bounds on screenconst screenWidthf = @as(f32, @floatFromInt(screenWidth));const screenHeightf = @as(f32, @floatFromInt(screenHeight));const y: f32 = @floatFromInt(@divTrunc(button.height, NUM_FRAMES));const btnBounds = ray.Rectangle{.x = screenWidthf / 2.0 - @as(f32, @floatFromInt(button.width)) / 2.0,.y = screenHeightf / 2.0 - y / 2.0,.width = @floatFromInt(button.width),.height = @floatFromInt(frameHeight),};var btnState: c_int = 0; // Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSEDvar btnAction = false; // Button action should be activatedvar mousePoint = ray.Vector2{};while (!ray.WindowShouldClose()) {// UpdatemousePoint = ray.GetMousePosition();btnAction = false;// Check button stateif (ray.CheckCollisionPointRec(mousePoint, btnBounds)) {if (ray.IsMouseButtonDown(ray.MOUSE_BUTTON_LEFT)) btnState = 2 else btnState = 1;if (ray.IsMouseButtonReleased(ray.MOUSE_BUTTON_LEFT)) btnAction = true;} else btnState = 0;if (btnAction) {ray.PlaySound(fxButton);// TODO: Any desired action}// Calculate button frame rectangle to draw depending on button statesourceRec.y = @floatFromInt(btnState * frameHeight);// Drawray.BeginDrawing();defer ray.EndDrawing();ray.ClearBackground(ray.RAYWHITE);ray.DrawTextureRec(button, sourceRec, .{ .x = btnBounds.x, .y = btnBounds.y }, ray.WHITE); // Draw button frameray.DrawFPS(10, 10);}
}

效果

按钮动画

总结

渲染一个按钮的不同状态,当点击按钮时,会有音效。

附录

http://www.jsqmd.com/news/1364953/

相关文章:

  • 3分钟快速修复洛雪音乐:六音音源修复版完全指南
  • OpenClaw容器化部署实战:Docker与CUDA环境配置指南
  • 在VS2010中从零实现FFT算法:原理、代码与性能优化实战
  • XUnity.AutoTranslator:Unity游戏一键翻译的终极解决方案
  • 信息系统项目管理师备考全攻略:从教材精读到论文实战
  • Python实战:基于规则与NER的军事战报信息抽取与结构化处理
  • AI文章转PPT视频:本地部署与自动化流程全解析
  • Python高级特性实战:提升代码效率与性能
  • SSM+Vue英语学习网站开发全解析
  • Palantir Ontology 如何重塑半导体晶圆厂:从数据孤岛到业务操作系统
  • 终极飞书文档批量导出工具:告别手动下载,25分钟完成700+文档迁移
  • 2026年8月市面上佛山广告灯箱制造厂家哪家靠谱测评,超薄灯箱、拉布灯箱及异形定制厂家分析 - 海棠依旧大
  • 为什么都说网站建设属于软件开发其实这是一项复杂的系统工程的真相
  • 2026独立站搭建平台有哪些,跨境卖家建站工具选择指南
  • Houdini Engine for Unreal V2 安装配置与核心工作流程全解析
  • UE5 GameInstance核心职责与架构设计:从新手困惑到最佳实践
  • 2026年7月广州市越秀区二手房价格深度分析报告
  • Office安装神器,流批了
  • 我把 10 万行祖传代码喂给了 AI:用 RAG 搭建“代码考古“助手,新人 1 天看懂老项目
  • Python a0-baas-sdk包解析与BaaS开发实战
  • 【Python20260808】03
  • 2026年8月正规的净化板翻新源头厂家找哪家测评,净化板翻新服务与源头企业分析 - 海棠依旧大
  • Spring IoC与DI核心原理及最佳实践解析
  • 终极Mac鼠标滚动优化方案:让外接鼠标如触控板般顺滑的完整指南
  • NOOA框架:面向对象设计简化AI智能体开发与工程化实践
  • 苹果中国官网“千问”文档“一日游”:谁先说为何这么敏感?
  • UE5 C++开发必备:5个VS2022工作区优化技巧,告别卡顿与漫长编译
  • 2026年7月广州市黄埔区二手房价格深度分析报告
  • Codex实战:基于LLM的PR自动化安全审查如何提升开发效率与代码安全
  • 2026做网站的平台,企业建站工具与官网搭建方案指南