c++双人射击小游戏代码
时间: 2023-07-09 21:07:09 浏览: 252
c++小游戏代码
以下是一个简单的 C++ 双人射击小游戏代码示例,使用了 Windows API 和 Winsock2 库来实现网络连接。注意:本代码仅供学习参考,不保证完全正确和稳定,使用时请自行测试和修改。
```cpp
// 双人射击小游戏代码示例
#include <iostream>
#include <windows.h>
#include <winsock2.h>
using namespace std;
const int PORT = 9000;
const int BUF_SIZE = 1024;
// 定义玩家类
class Player {
public:
Player(int x, int y) : posX(x), posY(y) {}
int posX;
int posY;
void move(int dx, int dy) {
posX += dx;
posY += dy;
}
};
// 初始化 Winsock2 库
void initWinsock() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cerr << "WSAStartup failed." << endl;
exit(1);
}
}
// 创建服务器端套接字
SOCKET createServerSocket() {
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
cerr << "Failed to create socket." << endl;
exit(1);
}
// 绑定端口号
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
cerr << "Failed to bind socket." << endl;
closesocket(serverSocket);
WSACleanup();
exit(1);
}
// 监听连接请求
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
cerr << "Failed to listen to socket." << endl;
closesocket(serverSocket);
WSACleanup();
exit(1);
}
return serverSocket;
}
// 创建客户端套接字
SOCKET createClientSocket(const char* ipAddr) {
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
cerr << "Failed to create socket." << endl;
exit(1);
}
// 连接服务器
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, ipAddr, &serverAddr.sin_addr) != 1) {
cerr << "Invalid IP address." << endl;
closesocket(clientSocket);
WSACleanup();
exit(1);
}
if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
cerr << "Failed to connect to server." << endl;
closesocket(clientSocket);
WSACleanup();
exit(1);
}
return clientSocket;
}
// 发送数据
void sendData(SOCKET socket, const char* data, int len) {
int sentBytes = 0;
while (sentBytes < len) {
int ret = send(socket, data + sentBytes, len - sentBytes, 0);
if (ret == SOCKET_ERROR) {
cerr << "Failed to send data." << endl;
closesocket(socket);
WSACleanup();
exit(1);
}
sentBytes += ret;
}
}
// 接收数据
int recvData(SOCKET socket, char* buf) {
int recvBytes = recv(socket, buf, BUF_SIZE, 0);
if (recvBytes == SOCKET_ERROR) {
cerr << "Failed to receive data." << endl;
closesocket(socket);
WSACleanup();
exit(1);
}
buf[recvBytes] = '\0';
return recvBytes;
}
// 主函数
int main() {
// 初始化 Winsock2 库
initWinsock();
// 创建玩家
Player player1(0, 0);
Player player2(10, 10);
// 创建服务器端套接字
SOCKET serverSocket = createServerSocket();
cout << "Waiting for player 2 to connect..." << endl;
// 接受连接请求
SOCKET clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
cerr << "Failed to accept connection." << endl;
closesocket(serverSocket);
WSACleanup();
exit(1);
}
cout << "Player 2 connected." << endl;
// 创建客户端套接字
char ipAddr[BUF_SIZE];
cout << "Please enter the IP address of player 1: ";
cin >> ipAddr;
SOCKET player1Socket = createClientSocket(ipAddr);
cout << "Connected to player 1." << endl;
// 进入游戏循环
while (true) {
// 向对方发送自己的位置
char buf[BUF_SIZE];
sprintf_s(buf, "%d %d", player1.posX, player1.posY);
sendData(player2Socket, buf, strlen(buf));
sprintf_s(buf, "%d %d", player2.posX, player2.posY);
sendData(player1Socket, buf, strlen(buf));
// 接受对方的位置
int recvBytes = recvData(player2Socket, buf);
int posX, posY;
sscanf_s(buf, "%d %d", &posX, &posY);
player2.move(posX - player2.posX, posY - player2.posY);
recvBytes = recvData(player1Socket, buf);
sscanf_s(buf, "%d %d", &posX, &posY);
player1.move(posX - player1.posX, posY - player1.posY);
// 显示当前状态
system("cls");
cout << "Player 1: (" << player1.posX << ", " << player1.posY << ")" << endl;
cout << "Player 2: (" << player2.posX << ", " << player2.posY << ")" << endl;
// 处理输入
if (GetAsyncKeyState(VK_UP) & 0x8000) {
player1.move(0, -1);
}
if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
player1.move(0, 1);
}
if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
player1.move(-1, 0);
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
player1.move(1, 0);
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
// 射击
}
// 延时
Sleep(50);
}
// 关闭套接字和 Winsock2 库
closesocket(player1Socket);
closesocket(player2Socket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
阅读全文