用C++在VC6环境下写程序:设置两个小球,在窗口中进行运动 要求:当二者进行碰撞时,随机方向进行弹开,运动小球不能出窗口。
时间: 2024-02-22 20:59:23 浏览: 23
好的,以下是一份简单的C++代码,可以在VC6环境下实现你的需求。你可以在自己的项目中创建一个.cpp文件,命名为"ball.cpp",并在VC6中编译运行。
```c++
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
#define ID_TIMER 1
#define BALL_RADIUS 50
// 小球结构体
struct Ball {
int x, y; // 小球圆心坐标
int vx, vy; // 小球速度
int color; // 小球颜色
};
// 定义全局变量
Ball ball1, ball2;
int ball1Color, ball2Color;
int ball1Speed, ball2Speed;
int ball1Direction, ball2Direction;
HWND hwnd;
// 函数声明
void InitBall(Ball& ball);
void MoveBall(Ball& ball);
void DrawBall(HDC hdc, Ball ball);
void CheckCollision();
// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 初始化随机数生成器
srand((unsigned)time(NULL));
// 创建窗口
HWND hwnd = CreateWindow("static", "", WS_VISIBLE | WS_BORDER | SS_CENTER, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
// 初始化小球1
InitBall(ball1);
ball1Color = RGB(rand() % 255, rand() % 255, rand() % 255);
ball1Speed = rand() % 10 + 1;
ball1Direction = rand() % 360;
// 初始化小球2
InitBall(ball2);
ball2Color = RGB(rand() % 255, rand() % 255, rand() % 255);
ball2Speed = rand() % 10 + 1;
ball2Direction = rand() % 360;
// 设置定时器,每50毫秒移动小球
SetTimer(hwnd, ID_TIMER, 50, NULL);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// 初始化小球
void InitBall(Ball& ball) {
ball.x = rand() % (800 - BALL_RADIUS * 2) + BALL_RADIUS;
ball.y = rand() % (600 - BALL_RADIUS * 2) + BALL_RADIUS;
ball.vx = rand() % 10 + 1;
ball.vy = rand() % 10 + 1;
}
// 移动小球
void MoveBall(Ball& ball) {
// 根据速度和方向移动小球
ball.x += ball.vx * cos(ball1Direction * 3.14 / 180);
ball.y += ball.vy * sin(ball1Direction * 3.14 / 180);
// 检测小球是否超出边界,如果是则弹回
if (ball.x - BALL_RADIUS < 0 || ball.x + BALL_RADIUS > 800) {
ball1Direction = 180 - ball1Direction;
ball.x = ball.x - ball.vx * cos(ball1Direction * 3.14 / 180);
}
if (ball.y - BALL_RADIUS < 0 || ball.y + BALL_RADIUS > 600) {
ball1Direction = 360 - ball1Direction;
ball.y = ball.y - ball.vy * sin(ball1Direction * 3.14 / 180);
}
}
// 绘制小球
void DrawBall(HDC hdc, Ball ball) {
HBRUSH hBrush = CreateSolidBrush(ball.color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, ball.x - BALL_RADIUS, ball.y - BALL_RADIUS, ball.x + BALL_RADIUS, ball.y + BALL_RADIUS);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
// 检测碰撞
void CheckCollision() {
int dx = ball2.x - ball1.x;
int dy = ball2.y - ball1.y;
int distance = sqrt(dx * dx + dy * dy);
if (distance < BALL_RADIUS * 2) {
// 碰撞后随机改变方向
ball1Direction = rand() % 360;
ball2Direction = rand() % 360;
}
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
break;
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
DrawBall(hdc, ball1);
DrawBall(hdc, ball2);
EndPaint(hwnd, &ps);
break;
case WM_TIMER:
// 移动小球
MoveBall(ball1);
MoveBall(ball2);
// 检测碰撞
CheckCollision();
// 重绘窗口
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
```
注意,以上代码是一个简单的实现,你可以根据自己的实际需求进行修改和优化。