|
12鱼币
代码问题
头文件如下#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <time.h>
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 800
#define MAX_ENEMY_NUMBER 20
#define MAX_BULLET_NUMBER 20
#define BULLET_SPEED 10
#define ME_MOVING_SPEEDPX 5
#define MAX_ENEMY_MOVING_SPEED 13
#define MIN_ENEMY_MOVING_SPEED 4
#define BULLET_INTERVAL 3
#define ME_WIDTH 40
#define ME_HEIGHT 30
#define ENEMY_SIZE 40
#define BULLET_WIDTH 4
#define BULLET_HEIGHT 6
#define pass
struct POSITION
{
int x;
int y;
};
POSITION enemyPos[MAX_ENEMY_NUMBER];
POSITION enemySpeed[MAX_ENEMY_NUMBER];
POSITION bulletPos[MAX_BULLET_NUMBER];
POSITION mePos;
int nowEnemyNumber;
int nowBulletNumber;
long sFps;
long fps;
int game = 0;
void Initialization(); // 初始化函数
void userOperation(UINT message); // 检测用户的操作并处理
void startUpThreadEx(void *object); // 启动一条线程
void TestingEdgeCollision(); // 检测球体是否在窗口边缘,是则转向
// void enemySway(int pos); // 对传入的下表对应敌人进行转向
void TestingObjectCollision(); // 检测子弹是否和敌人有接触,是则双方消失
void queueBulletArrangement(); // 对子弹队列进行整理
void queueEnemyArrangement(); // 整理敌人队列
void drawObject(HWND hwnd); // 读取数据逐步进行绘图
void moveingObject(); // 物体的移动, 包括敌人, 子弹的移动
unsigned int __stdcall threadRun(void *object);
unsigned int __stdcall Contact(void *object);
main文件如下
#include "head.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MY WINDOW");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
return 0;
}
hwnd = CreateWindow(
szAppName,
TEXT("打飞机"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
WINDOW_HEIGHT,
WINDOW_WIDTH,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(message)
{
case WM_PAINT:
{
if(!game)
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);DrawText(hdc, TEXT("游戏即将开始!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
Sleep(100);
EndPaint(hwnd, &ps);
game = 1;
Initialization();
startUpThreadEx((void*)&hwnd);
}
} return 0;
case WM_KEYDOWN:
{
userOperation(message);
} return 0;
case WM_DESTROY:
{
PostQuitMessage(0);
} return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void Initialization()
{
srand(time(NULL));
/* 对敌人的坐标以及速度进行初始化 */
for(int i = 0; i < MAX_ENEMY_NUMBER; i++)
{
if(rand()%10 > 4)
{
enemyPos[i].x = rand() % WINDOW_WIDTH;
enemyPos[i].y = 0;
}
else if(rand()%10 > 3)
{
enemyPos[i].x = 0;
enemyPos[i].y = rand() % WINDOW_HEIGHT;
}
else if(rand()%10 > 3)
{
enemyPos[i].x = WINDOW_WIDTH;
enemyPos[i].y = rand() % WINDOW_HEIGHT;
}
else
{
enemyPos[i].x = rand() % WINDOW_WIDTH;
enemyPos[i].y = WINDOW_HEIGHT;
}
enemySpeed[i].x = rand() % MAX_ENEMY_MOVING_SPEED + MIN_ENEMY_MOVING_SPEED;
enemySpeed[i].y = rand() % MAX_ENEMY_MOVING_SPEED + MIN_ENEMY_MOVING_SPEED;
}
for(int i = 0; i < MAX_BULLET_NUMBER; i++)
{
/* 位置为-10则表示该子弹不存在 */
bulletPos[i].x = bulletPos[i].x = -10;
}
game = 0;
mePos.x = WINDOW_WIDTH / 2;
mePos.y = WINDOW_HEIGHT / 2;
nowBulletNumber = fps = sFps = 0;
nowEnemyNumber = MAX_ENEMY_NUMBER;
}
void userOperation(UINT message)
{
/* 左 上 右 下 */
switch(message)
{
case VK_LEFT:
{
if(mePos.x >= ME_MOVING_SPEEDPX) mePos.x -= ME_MOVING_SPEEDPX;
}
break;
case VK_UP:
{
if(mePos.y >= ME_MOVING_SPEEDPX) mePos.y -= ME_MOVING_SPEEDPX;
}
break;
case VK_RIGHT:
{
if(mePos.x < WINDOW_WIDTH-ME_MOVING_SPEEDPX) mePos.x += ME_MOVING_SPEEDPX;
}
break;
case VK_DOWN:
{
if(mePos.y < WINDOW_HEIGHT-ME_MOVING_SPEEDPX) mePos.y += ME_MOVING_SPEEDPX;
}
break;
case VK_SPACE:
{
if(fps - sFps > BULLET_INTERVAL) /* 调用子弹的发射函数 */
{
fps = sFps = 1;
}
}
break;
}
}
void startUpThreadEx(void *object)
{
/* 这条线程否则绘图 */
_beginthreadex(NULL, 0, threadRun, object, 0, NULL);
}
void TestingEdgeCollision()
{
const int maxEnemyNum = nowEnemyNumber;
for(int i = 0; i < maxEnemyNum; i++)
{
if(enemyPos[i].y < 0) enemySpeed[i].y = -enemySpeed[i].y;
if(enemyPos[i].x < 0) enemySpeed[i].x = -enemySpeed[i].x;
if(enemyPos[i].y > WINDOW_HEIGHT) enemySpeed[i].y = -enemySpeed[i].y;
if(enemyPos[i].x > WINDOW_WIDTH) enemySpeed[i].x = -enemySpeed[i].x;
}
}
/*
void enemySway(int pos)
{
pass
}
*/
void TestingObjectCollision()
{
if(nowBulletNumber > 1) return;
/* 检测子弹的碰撞 */
const int maxEnemyNum = nowEnemyNumber;
for(int i = 0; i < maxEnemyNum; i++)
{
Contact((void*)&i);
}
queueBulletArrangement(); // 整理子弹的队列
}
void queueBulletArrangement()
{
if(nowBulletNumber <= 1) return;
/* 对 bulletPos 队列进行整理 */
for(int i = 0; i < nowBulletNumber; i++)
{
if(bulletPos[i].x == -10)
{
bulletPos[i].x = bulletPos[nowBulletNumber-1].x;
bulletPos[i].y = bulletPos[nowBulletNumber-1].y;
bulletPos[nowBulletNumber-1].x = -10;
bulletPos[nowBulletNumber-1].y = -10;
nowBulletNumber--;
}
}
}
void queueEnemyArrangement()
{
if(nowEnemyNumber <= 1) return;
for(int i = 0; i < nowEnemyNumber; i++)
{
if(enemyPos[i].x == -10)
{
enemyPos[i].x = enemyPos[nowEnemyNumber-1].x;
enemyPos[i].y = enemyPos[nowEnemyNumber-1].y;
enemyPos[nowEnemyNumber-1].x = -10;
enemyPos[nowEnemyNumber-1].y = -10;
nowEnemyNumber--;
}
}
}
void drawObject(HWND hwnd)
{
/* 用一个白色的矩形填充窗口背景 */
HDC hdc = GetWindowDC(hwnd);
SelectObject(hdc, GetStockObject(NULL_PEN));
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Rectangle(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
/* 画自己 */
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
Rectangle(hdc, mePos.x, mePos.y, mePos.x + ME_WIDTH, mePos.y + ME_HEIGHT);
/* 画敌人 */
const int enemyNumber = nowEnemyNumber;
for(int i = 0; i < enemyNumber; i++)
{
Ellipse(hdc, enemyPos[i].x, enemyPos[i].y, enemyPos[i].x + ENEMY_SIZE, enemyPos[i].y + ENEMY_SIZE);
}
/* 画子弹 */
if(nowBulletNumber > 0)
{
const int bulletNumber = nowBulletNumber;
for(int i = 0; i < bulletNumber; i++)
{
Rectangle(hdc, bulletPos[i].x, bulletPos[i].y, bulletPos[i].x + BULLET_WIDTH, bulletPos[i].y + BULLET_HEIGHT);
}
}
moveingObject();
}
void moveingObject()
{
fps++;
/* 子弹的移动 */
const int bulletNumber = nowBulletNumber;
const int enemyNumber = nowEnemyNumber;
if(bulletNumber > 0)
{
for(int i = 0; i < bulletNumber; i++)
{
bulletPos[i].y += BULLET_SPEED;
}
}
TestingObjectCollision();
/* 敌人的移动 */
if(enemyNumber > 0)
{
for(int i = 0; i < enemyNumber; i++)
{
enemyPos[i].x += enemySpeed[i].x;
enemyPos[i].y += enemySpeed[i].y;
}
}
TestingEdgeCollision();
}
unsigned int __stdcall threadRun(void *object)
{
/* object = 父窗口句柄, */
HWND hwnd = *(HWND*)object;
while(game)
{
drawObject(hwnd);
}
return 0;
}
unsigned int __stdcall Contact(void *object)
{
const int pos = *(int*)object;
for(int i = 0; i < nowEnemyNumber; i++)
{
if(bulletPos[pos].x > enemyPos[i].x &&
bulletPos[pos].x < enemyPos[i].x + ENEMY_SIZE &&
bulletPos[pos].y > enemyPos[i].y &&
bulletPos[pos].y < enemyPos[i].y + ENEMY_SIZE)
{
bulletPos[pos].y = bulletPos[pos].y = -10;
enemyPos[i].y = enemyPos[i].x = -10;
break;
}
}
return 0;
}
程序的功能是打算做一个如图所示的游戏
,
开始代码是可以正常编译通过的, 直到我改变了一句代码, 头文件红色部分, 即把 int game; 改为了 ing game = 0;之后,编译器出现错误,
c-free和DEV-cpp的报错如下
你的问题里写错字了 ing game =0; 是int game = 0把! 在头文件里最好是声明变量 永远不要去定义变量。如果声明有初始化式,就被当作定义,即使前面加了extern。
一般情况下头文件中只放变量的声明,因为头文件要被其他文件包含(即#include),如果把定义放到头文件的话,就不能避免多次定义变量,C++不允许多次定义变量,一个程序中对指定变量的定义只有一次,声明可以无数次。
|
最佳答案
查看完整内容
你的问题里写错字了 ing game =0; 是int game = 0把! 在头文件里最好是声明变量 永远不要去定义变量。如果声明有初始化式,就被当作定义,即使前面加了extern。
一般情况下头文件中只放变量的声明,因为头文件要被其他文件包含(即#include),如果把定义放到头文件的话,就不能避免多次定义变量,C++不允许多次定义变量,一个程序中对指定变量的定义只有一次,声明可以无数次。
|