📄 dj.txt
字号:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);//窗口函数说明
HFONT CreateFont(HDC hdc,int nCharHeight,BOOL bItalic);
POINT pt;
int x,y,i;
int a,b,c,d,count;
HDC hdcmem,hdcmem1,hdc;
HBITMAP hBm,hBm1;
BITMAP bm,bm1;
//--------以下初始化窗口类--------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hWnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="位图";
char lpszTitle[]="Example_For_BitMap";
//窗口类定义
wndclass.style =0;
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=lpszClassName;
//----------以下定义窗口类的注册----------
if (!RegisterClass(&wndclass))
{
MessageBeep (0);
return FALSE;
}
//创建窗口
hWnd = CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
hBm=LoadBitmap(hInstance,"pic7");
hBm1=LoadBitmap(hInstance,"pic1");
GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm);
GetObject(hBm1,sizeof(BITMAP),(LPVOID)&bm1);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage (&Msg);
DispatchMessage (&Msg);
}
return Msg.wParam;
}
char stri[8]={'0'};
char stringg[8]={'0'};
char stringggg[8]={'0'};
int timer=0;
//窗口函数
LRESULT CALLBACK WndProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
TEXTMETRIC tm;
char xianshi1[]="You Have Shot:";
char xianshi[10]=" ";
char well[]="Well Done,You Are Quite NB!";
char soso[]="Your Speed is SOSO!";
char bad[] ="I'am Sorry,You Are Quite SB!";
char over[]="GAME OVER";
char xianshi2[]="Time:";
char stringgg[8]={'0'};
time_t t;
struct tm *tp;
t=time(NULL);
tp=localtime(&t);
HFONT hF;
switch(message)
{
case WM_CREATE:
count=0;
a=rand()%800;b=rand()%400;
SetTimer(hWnd,100,40,NULL);
hdc=GetDC(hWnd);
hdcmem=CreateCompatibleDC(hdc);
hdcmem1=CreateCompatibleDC(hdc);
ReleaseDC(hWnd,hdc);
case WM_PAINT:
if(stringggg[6]!='3')
{
GetCursorPos(&pt);
x=pt.x;
y=pt.y;
hdc=BeginPaint(hWnd,&ps);
SelectObject(hdcmem,hBm);
BitBlt(hdc,x-44,y,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
//画出敌人位置
SelectObject(hdcmem1,hBm1);
BitBlt(hdc,a,b,bm1.bmWidth,bm1.bmHeight,hdcmem1,0,0,SRCCOPY);
}
//显示文本
SetTextColor(hdc,RGB(232,72,108));
hF=CreateFont(hdc,40,0);
SelectObject(hdc,hF);
GetTextMetrics(hdc,&tm);
TextOut(hdc,0,0,xianshi1,strlen(xianshi1));//you have shot:
itoa(count,xianshi,10);
GetTextMetrics(hdc,&tm);
TextOut(hdc,250,0,xianshi,strlen(xianshi)); //击中次数显示
GetTextMetrics(hdc,&tm);
TextOut(hdc,950,0,xianshi2,strlen(xianshi2));//显示timer:
//计时器
if(stringggg[6]!='3')
{
itoa((tp->tm_hour%100-tp->tm_hour%10)/10,&stringgg[0],10);
itoa(tp->tm_hour%10,&stringgg[1],10);
stringgg[2]=':';
itoa((tp->tm_min%100-tp->tm_min%10)/10,&stringgg[3],10);
itoa(tp->tm_min%10,&stringgg[4],10);
stringgg[5]=':';
itoa((tp->tm_sec%100-tp->tm_sec%10)/10,&stringgg[6],10);
itoa(tp->tm_sec%10,&stringgg[7],10);
if(timer==0)
strcpy(stri,stringgg);
if(stringgg[7]>=stri[7])
{
stringggg[7]=stringgg[7]-stri[7]+'0';
if(stringgg[6]>=stri[6])
stringggg[6]=stringgg[6]-stri[6]+'0';
else stringggg[6]=stringgg[6]-stri[6]+'0'+6;
}
else
{
stringggg[7]=stringgg[7]-stri[7]+'0'+10;
if(stringgg[6]>stri[6])
stringggg[6]=stringgg[6]-stri[6]+'0'-1;
else stringggg[6]=stringgg[6]-stri[6]+'0'+5;
}
}
for(i=6;i<8;i++) //显示所用时间
TextOut(hdc,1000+i*20,0,&stringggg[i],1);
timer++;
if(stringggg[6]=='3') //显示游戏结束时的状态
{
GetTextMetrics(hdc,&tm);TextOut(hdc,450,300,over,strlen(over));
if(count>19) {GetTextMetrics(hdc,&tm);TextOut(hdc,350,400,well,strlen(well));}
if(count<=19&&count>9) {GetTextMetrics(hdc,&tm);TextOut(hdc,400,400,soso,strlen(soso));}
if(count<=9) {GetTextMetrics(hdc,&tm);TextOut(hdc,350,400,bad,strlen(bad));}
}
return 0;
case WM_LBUTTONDOWN:
if (stringggg[6]!='3')
{
for (i=0;i<90;i++) //左键激发子弹
{
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
hPen=(HPEN)GetStockObject(BLACK_PEN);
hBrush=CreateHatchBrush(HS_CROSS,RGB(255,0,255));
SelectObject(hDC,hBrush);
SelectObject(hDC,hPen);
y=y-10;
Ellipse(hDC,pt.x-10,y+15,pt.x+10,y-15);
EndPaint (hWnd,&ps);
InvalidateRect(hWnd,NULL,1);
if (a<pt.x+10&&bm1.bmWidth+a>pt.x-10)
{
a=rand()%1024;b=rand()%400;count++;
}
}
}
return 0;
case WM_RBUTTONDOWN:
if (stringggg[6]!='3')
{
for (i=0;i<90;i++) //右键激发光束
{
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
hPen=CreatePen(PS_DASHDOT,3,RGB(255,0,0));
SelectObject(hDC,hPen);
MoveToEx(hDC,x,y,NULL);
LineTo (hDC,x,0);
hPen=CreatePen(PS_DASHDOT,3,RGB(0,255,0));
SelectObject(hDC,hPen);
MoveToEx(hDC,x-30,y,NULL);
LineTo (hDC,x-30,0);
hPen=CreatePen(PS_DASHDOT,3,RGB(0,0,255));
SelectObject(hDC,hPen);
MoveToEx(hDC,x+30,y,NULL);
LineTo (hDC,x+30,0);
EndPaint (hWnd,&ps);
DeleteObject(hPen);
InvalidateRect(hWnd,NULL,1);
}
if (a<x+30&&bm1.bmWidth+a>x-30)
{
a=rand()%1024;b=rand()%400;count++;
}
}
return 0;
case WM_TIMER:
InvalidateRect(hWnd,NULL,1);
break;
case WM_DESTROY:
PostQuitMessage(0);
KillTimer(hWnd,1);
return 0;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return (0);
}
HFONT CreateFont(HDC hdc,int nCharHeight,BOOL bItalic)
{
HFONT hFont;
hFont=CreateFont(nCharHeight,0,0,0,400,bItalic,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"Arial");
if(hFont==NULL) return NULL;
else return hFont;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -