一个垃圾游戏,不想做了, 不过太多
上图片static/image/hrline/1.gif
为了骗回复, 你们懂的
#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;
POSITION enemySpeed;
POSITION bulletPos;
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.x = rand() % WINDOW_WIDTH;
enemyPos.y = 0;
}
else if(rand()%10 > 3)
{
enemyPos.x = 0;
enemyPos.y = rand() % WINDOW_HEIGHT;
}
else if(rand()%10 > 3)
{
enemyPos.x = WINDOW_WIDTH;
enemyPos.y = rand() % WINDOW_HEIGHT;
}
else
{
enemyPos.x = rand() % WINDOW_WIDTH;
enemyPos.y = WINDOW_HEIGHT;
}
enemySpeed.x = rand() % MAX_ENEMY_MOVING_SPEED + MIN_ENEMY_MOVING_SPEED;
enemySpeed.y = rand() % MAX_ENEMY_MOVING_SPEED + MIN_ENEMY_MOVING_SPEED;
}
for(int i = 0; i < MAX_BULLET_NUMBER; i++)
{
/* 位置为-10则表示该子弹不存在 */
bulletPos.x = bulletPos.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.y < 0) enemySpeed.y = -enemySpeed.y;
if(enemyPos.x < 0) enemySpeed.x = -enemySpeed.x;
if(enemyPos.y > WINDOW_HEIGHT) enemySpeed.y = -enemySpeed.y;
if(enemyPos.x > WINDOW_WIDTH) enemySpeed.x = -enemySpeed.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.x == -10)
{
bulletPos.x = bulletPos.x;
bulletPos.y = bulletPos.y;
bulletPos.x = -10;
bulletPos.y = -10;
nowBulletNumber--;
}
}
for(int i = 0; i < nowEnemyNumber; i++)
{
if(enemyPos.x == -10)
{
enemyPos.x = enemyPos.x;
enemyPos.y = enemyPos.y;
enemyPos.x = -10;
enemyPos.y = -10;
nowEnemyNumber--;
}
}
}
void queueEnemyArrangement()
{
if(nowEnemyNumber <= 1) return;
for(int i = 0; i < nowEnemyNumber; i++)
{
if(enemyPos.x == -10)
{
enemyPos.x = enemyPos.x;
enemyPos.y = enemyPos.y;
enemyPos.x = -10;
enemyPos.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.x, enemyPos.y, enemyPos.x + ENEMY_SIZE, enemyPos.y + ENEMY_SIZE);
}
/* 画子弹 */
if(nowBulletNumber > 0)
{
const int bulletNumber = nowBulletNumber;
for(int i = 0; i < bulletNumber; i++)
{
Rectangle(hdc, bulletPos.x, bulletPos.y, bulletPos.x + BULLET_WIDTH, bulletPos.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.y += BULLET_SPEED;
}
}
TestingObjectCollision();
/* 敌人的移动 */
if(enemyNumber > 0)
{
for(int i = 0; i < enemyNumber; i++)
{
enemyPos.x += enemySpeed.x;
enemyPos.y += enemySpeed.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.x > enemyPos.x &&
bulletPos.x < enemyPos.x + ENEMY_SIZE &&
bulletPos.y > enemyPos.y &&
bulletPos.y < enemyPos.y + ENEMY_SIZE)
{
bulletPos.y = bulletPos.y = -10;
enemyPos.y = enemyPos.x = -10;
break;
}
}
return 0;
}
void postBullet()
{
if(nowBulletNumber = 19)
{
int number = bulletPos.y;
int n = 0;
for(int i = 1; i < nowBulletNumber; i++)
{
if(bulletPos.y < number) n = i;
}
bulletPos.x = mePos.x + 3;
bulletPos.y = mePos.y - 10;
}
else
{
bulletPos.x = mePos.x + 3;
bulletPos.y = mePos.y - 10;
nowBulletNumber++;
queueBulletArrangement();
}
} 这是什么游戏啊?小白刚进来,不知道怎么看 1
页:
[1]