随机颜色 怎么写
时间: 2023-07-03 22:25:36 浏览: 29
你可以使用以下 JavaScript 代码生成一个随机颜色:
```javascript
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
```
该函数将生成一个随机的六位十六进制颜色代码,例如 "#3A5FCD"。你可以将其应用于任何需要随机颜色的地方,比如随机设置元素的背景色、字体颜色等。
相关问题
封装函数,功能获取一个随机颜色值 用js写
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Usage example:
var randomColor = getRandomColor();
console.log(randomColor); // #3E7EB1
帮我写一段vc 鼠标点击生成随机颜色速度上升的气球
好的,以下是一个满足您要求的VC++代码示例:
```cpp
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
using namespace std;
// 窗口类名
const char g_szClassName[] = "MyWindowClass";
// 气球结构体
struct Balloon {
int x, y; // 气球位置
int r; // 气球半径
int dx, dy; // 气球移动速度
COLORREF color; // 气球颜色
};
// 全局变量
vector<Balloon> g_balloons; // 气球数组
int g_bkWidth, g_bkHeight; // 背景大小
int g_speed = 5; // 气球移动速度
// 生成随机颜色
COLORREF RandomColor() {
return RGB(rand() % 256, rand() % 256, rand() % 256);
}
// 初始化气球
void InitBalloons() {
g_balloons.clear();
for (int i = 0; i < 20; i++) {
Balloon balloon;
balloon.x = rand() % (g_bkWidth - 100) + 50;
balloon.y = rand() % (g_bkHeight - 100) + 50;
balloon.r = rand() % 30 + 20;
balloon.dx = rand() % (g_speed * 2 + 1) - g_speed;
balloon.dy = rand() % (g_speed * 2 + 1) - g_speed;
balloon.color = RandomColor();
g_balloons.push_back(balloon);
}
}
// 绘制气球
void DrawBalloon(HDC hdc, Balloon &balloon) {
HBRUSH hBrush = CreateSolidBrush(balloon.color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, balloon.x - balloon.r, balloon.y - balloon.r, balloon.x + balloon.r, balloon.y + balloon.r);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
// 获取窗口大小
RECT rc;
GetClientRect(hwnd, &rc);
g_bkWidth = rc.right - rc.left;
g_bkHeight = rc.bottom - rc.top;
// 初始化随机数种子
srand(time(NULL));
// 初始化气球
InitBalloons();
break;
case WM_PAINT:
// 绘制背景
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
FillRect(hdc, &ps.rcPaint, hBrush);
DeleteObject(hBrush);
// 绘制气球
for (int i = 0; i < g_balloons.size(); i++) {
DrawBalloon(hdc, g_balloons[i]);
}
EndPaint(hwnd, &ps);
break;
case WM_LBUTTONDOWN:
// 鼠标点击事件,生成随机颜色气球并加速
g_speed += 2;
Balloon balloon;
balloon.x = rand() % (g_bkWidth - 100) + 50;
balloon.y = rand() % (g_bkHeight - 100) + 50;
balloon.r = rand() % 30 + 20;
balloon.dx = rand() % (g_speed * 2 + 1) - g_speed;
balloon.dy = rand() % (g_speed * 2 + 1) - g_speed;
balloon.color = RandomColor();
g_balloons.push_back(balloon);
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_TIMER:
// 移动气球
for (int i = 0; i < g_balloons.size(); i++) {
Balloon &balloon = g_balloons[i];
balloon.x += balloon.dx;
balloon.y += balloon.dy;
// 碰撞检测
if (balloon.x - balloon.r < 0 || balloon.x + balloon.r > g_bkWidth) {
balloon.dx = -balloon.dx;
}
if (balloon.y - balloon.r < 0 || balloon.y + balloon.r > g_bkHeight) {
balloon.dy = -balloon.dy;
}
}
// 重绘窗口
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_DESTROY:
// 退出程序
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// WinMain函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 创建窗口类
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "窗口类注册失败!", "错误", MB_ICONERROR);
return 0;
}
// 创建窗口
HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "随机颜色气球", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "窗口创建失败!", "错误", MB_ICONERROR);
return 0;
}
// 显示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 设置定时器
SetTimer(hwnd, 1, 10, NULL);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
这个代码会在窗口中绘制多个随机颜色的气球,每次鼠标点击会在随机位置生成一个随机颜色的气球,并将所有气球的移动速度加快。如果需要添加音效和背景音乐,您可以使用WinAPI提供的多媒体函数来实现。