您的位置 首页 java

给大家分享一个贪吃蛇小游戏代码,有需要的朋友可以收藏一下!

#include <windows.h>

#include <cstdio>

#include <ctime>

#include <cmath>

#define Window_FullWidth 610

#define Window_FullHeight 630

#define Screen_FullWidth GetSystemMetrics(SM_CXSCREEN)

#define Screen_FullHeight GetSystemMetrics(SM_CYSCREEN)

#define Game_Menu

#define Game_Ready

#define Game_Play

#define Game_Pause

#define Game_Over

#define up 1

#define down 2

#define left 3

#define right 4

HFONT hFont=CreateFont(

-50/*高度*/, -25/*宽度*/, 0, 0, 400 /*一般这个值设为400*/,

FALSE /*不带斜体*/, FALSE/*不带下划线*/, FALSE/*不带删除线*/,

DEFAULT_CHARSET, //使用默认字符集

OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, //这行参数不用管

DEFAULT_QUALITY, //默认输出质量

FF_DONTCARE, //不指定字体族*/

TEXT(“微软雅黑”) //字体名

);

struct o{

int tail;

int head;

int x[1000];

int y[1000];

}s;

intfx=0,len=4,movex[5]={0,0,0,-20,20},movey[5]={0,-20,20,0,0},foodx,foody;

bool can[610][630],start=0,stop=0;

PAINTSTRUCT ps;

HBRUSH black,red, green , blue ,white;

HDC hdc;

void game_Init(HWND hwnd);

void game_Play(HWND hwnd);

void game_check(HWND hwnd);

void game_Paint(HWND hwnd);

void game_reset(HWND hwnd);

void game_Clear(HWND hwnd);

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

HWND hwnd;

MSG msg ;

WNDCLASSEX wc;

memset(&wc,0,sizeof(wc));

wc.cbSize = sizeof(WNDCLASSEX);

wc.lpfnWndProc = WndProc;

wc.hInstance = hInstance;

wc.hCursor = NULL;

wc.hbrBackground = NULL;

wc.lpszClassName = “WindowClass”;

wc.hIcon = NULL;

wc.hIconSm = NULL;

if(!RegisterClassEx(&wc)) {

MessageBox(NULL, “Window Registration Failed!”,”Error!”,MB_ICONEXCLAMATION|MB_OK);

return 0;

}

hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,”WindowClass”,”贪吃蛇 帮助:上下左右键移动,空格暂停 作者QQ:1458038842″,WS_VISIBLE|WS_OVERLAPPEDWINDOW^WS_THICKFRAME^WS_MINIMIZEBOX^WS_MAXIMIZEBOX,

(Screen_FullWidth-Window_FullWidth)/2,(Screen_FullHeight-Window_FullHeight)/2,Window_FullWidth,Window_FullHeight,NULL,NULL,hInstance,NULL);

if(hwnd == NULL) {

MessageBox(NULL, “Window Creation Failed!”,”Error!”,MB_ICONEXCLAMATION|MB_OK);

return 0;

}

game_Init(hwnd);

ShowWindow(hwnd,nCmdShow);

UpdateWindow(hwnd);

srand(time(NULL));

game_reset(hwnd);

while (msg.message != WM_QUIT) {

if (PeekMessage(&msg,0,0,0,PM_REMOVE))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

if(start)

if(!stop){

game_Play(hwnd);

game_Paint(hwnd);

}

}

return msg.wParam;

}

void game_Init(HWND hwnd)

{

}

void game_Paint(HWND hwnd)

{

int x1=s.x[s.head],x2=s.x[s. tail ],y1=s.y[s.head],y2=s.y[s.tail];

SelectObject (hdc,blue);

Rectangle(hdc,x1-10,y1-10,x1+10,y1+10);

SelectObject(hdc,black);

Rectangle(hdc,x2-10,y2-10,x2+10,y2+10);

SelectObject(hdc,green);

Ellipse(hdc,foodx-5,foody-5,foodx+5,foody+5);

Sleep(300);

}

void game_Play(HWND hwnd)

{

s.head++;

if(s.head==10001)

s.head=1;

s.x[s.head]=s.x[s.head-1]+movex[fx];

s.y[s.head]=s.y[s.head-1]+movey[fx];

if(s.x[s.head]!=foodx||s.y[s.head]!=foody){

s.tail++;

if(s.tail==10001)

s.tail=1;

}

else{

len++;

foodx=(rand()%29+1)*20;

foody=(rand()%29+1)*20;

}

can[s.x[s.tail]][s.y[s.tail]]=true;

if(can[s.x[s.head]][s.y[s.head]]==false)

game_Clear(hwnd);

else can[s.x[s.head]][s.y[s.head]]=false;

}

void game_check(HWND hwnd)

{

}

void game_reset(HWND hwnd)

{

}

void game_Clear(HWND hwnd)

{

TCHAR ll[20];

HWND over=CreateWindow(

TEXT(“static”), //静态文本框的类名

TEXT(“”), //控件的文本

WS_CHILD /*子窗口*/ | WS_VISIBLE /*创建时显示*/ | WS_BORDER /*带边框*/|ES_CENTER,

150,100,300,140,hwnd,

(HMENU)1,

NULL,

NULL

);

SendMessage(over,WM_SETFONT,(WPARAM)hFont,(LPARAM)hFont);

wsprintf(ll,”游戏结束!\n你的分数:%d”,len-4);

SetWindowText(over,ll);

Sleep(3000);

PostQuitMessage(0);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

switch(Message) {

case WM_CREATE:{

for(int i=0;i<610;i++)

for(int j=0;j<630;j++)

can[i][j]=false;

for(int i=10;i<=590;i++)

for(int j=10;j<=590;j++)

can[i][j]=true;

fx=0;

black=CreateSolidBrush(RGB(0,0,0));

red=CreateSolidBrush(RGB(255,0,0));

green=CreateSolidBrush(RGB(0,255,0));

blue=CreateSolidBrush(RGB(0,0,255));

white=CreateSolidBrush(RGB(255,255,255));

s.head=4;

s.tail=0;

for(int i=1;i<=4;i++){

s.x[i]=300;

s.y[i]=240+i*20;

can[s.x[i]][s.y[i]]=false;

}

foodx=(rand()%29+1)*20;

foody=(rand()%29+1)*20;

break ;

}

case WM_PAINT:{

hdc=BeginPaint(hwnd,&ps);

SelectObject(hdc,red);

Rectangle(hdc,0,0,600,600);

SelectObject(hdc,black);

Rectangle(hdc,10,10,590,590);

SelectObject(hdc,green);

Ellipse(hdc,foodx-5,foody-5,foodx+5,foody+5);

SelectObject(hdc,blue);

for(int i=s.tail+1;i<=s.head;i++)

Rectangle(hdc,s.x[i]-10,s.y[i]-10,s.x[i]+10,s.y[i]+10);

break;

}

/* Upon destruction, tell the main thread to stop */

case WM_KEYDOWN:{

if(start==0){

start=1;

fx=2;

}

else

switch(wParam){

case VK_UP:{

fx=up;

break;

}

case VK_DOWN:{

fx=down;

break;

}

case VK_LEFT:{

fx=left;

break;

}

case VK_RIGHT:{

fx=right;

break;

}

case VK_SPACE:{

stop=!stop;

break;

}

default: break;

}

break;

}

case WM_DESTROY: {

PostQuitMessage(0);

break;

}

/* All other messages (a lot of them) are processed using default procedures */

default:

return DefWindowProc(hwnd, Message, wParam, lParam);

}

return 0;

}

文章来源:智云一二三科技

文章标题:给大家分享一个贪吃蛇小游戏代码,有需要的朋友可以收藏一下!

文章地址:https://www.zhihuclub.com/174504.shtml

关于作者: 智云科技

热门文章

网站地图