|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
上图片
为了骗回复, 你们懂的
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <process.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 1000
#define MAX_ENEMY_NUMBER 20
#define MAX_BULLET_NUMBER 20
#define BULLET_SPEED -3
#define ME_MOVING_SPEEDPX 10
#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(WPARAM 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);
void postBullet();
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_WIDTH,
WINDOW_HEIGHT,
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(1000);
Initialization();
startUpThreadEx((void*)&hwnd);
EndPaint(hwnd, &ps);
// MessageBox(hwnd, TEXT("xx"), TEXT("xx"), MB_OK);
}
} return 0;
case WM_KEYDOWN:
{
userOperation(wParam);
} 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 = 1;
mePos.x = WINDOW_WIDTH / 2;
mePos.y = WINDOW_HEIGHT / 2;
nowBulletNumber = fps = sFps = 0;
nowEnemyNumber = MAX_ENEMY_NUMBER;
}
void userOperation(WPARAM wParam)
{
/* 左 上 右 下 */
switch(wParam)
{
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) /* 调用子弹的发射函数 */
{
postBullet();
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--;
}
}
for(int i = 0; i < nowEnemyNumber; i++)
{
if(enemyPos[i].x == -10)
{
enemyPos[i].x = enemyPos[nowBulletNumber-1].x;
enemyPos[i].y = enemyPos[nowBulletNumber-1].y;
enemyPos[nowEnemyNumber-1].x = -10;
enemyPos[nowEnemyNumber-1].y = -10;
nowEnemyNumber--;
}
}
}
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)
{
Sleep(10);
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;
}
void postBullet()
{
if(nowBulletNumber = 19)
{
int number = bulletPos[0].y;
int n = 0;
for(int i = 1; i < nowBulletNumber; i++)
{
if(bulletPos[i].y < number) n = i;
}
bulletPos[n].x = mePos.x + 3;
bulletPos[n].y = mePos.y - 10;
}
else
{
bulletPos[nowBulletNumber].x = mePos.x + 3;
bulletPos[nowBulletNumber].y = mePos.y - 10;
nowBulletNumber++;
queueBulletArrangement();
}
} |
|