📄 fffff.cpp
字号:
#define WIN32_LEAN_AND_MEAN // include all macros
#define INITGUID // include all GUIDs
#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include "resource.h"
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <ddraw.h> // directX includes
#include "blackbox.h" // game library includes
// DEFINES ////////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WIN3DCLASS" // class name
#define WINDOW_WIDTH 640 // size of window
#define WINDOW_HEIGHT 480
#define MAX_X 32
#define MAX_Y 23
//defines
#define INITX 4
#define INITY 4
#define RIGHT 0
#define DOWN 1
#define LEFT 2
#define UP 3
//globles
int MoveTo = RIGHT;
int NextMoveTo = RIGHT;
int NEXT_X;
int NEXT_Y;
int KeyDown = 0;
bool isFoodSet = false;
bool isKeyDown = false;
bool isGameOver= false;
//Time Values
DWORD Time_Elapse;
DWORD Start_Time;
//Score
int Score = 0;
char buffer[80];
//Frame Values
int CurrFrame = 0;
int FramePerMove = 2;
//Sruct
typedef struct Snake_Node
{
int x;
int y;
Snake_Node * next;
}SNAKE;
//Snake Head and Tail;
SNAKE *SnakeHead = NULL;
SNAKE *SnakeTail = NULL;
//The screen matrix
int block[MAX_X][MAX_Y];
//My functions
void Init_Snake();
void Init_Block();
void Set_Food();
void Nor_Move();
void Eat_Food();
void Delete_Snake();
void Draw_Blocks();
// PROTOTYPES /////////////////////////////////////////////////
// game console
int Game_Init(void *parms=NULL);
int Game_Shutdown(void *parms=NULL);
int Game_Main(void *parms=NULL);
// GLOBALS ////////////////////////////////////////////////////
HWND main_window_handle = NULL; // save the window handle
HINSTANCE main_instance = NULL; // save the instance
//int game_state = GAME_STATE_INIT; // starting state
// FUNCTIONS //////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
return(0);
} break;
case WM_PAINT:
{
// start painting
hdc = BeginPaint(hwnd,&ps);
// the window is now validated
// end painting
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY:
{
// kill the application
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN ////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
// this is the winmain function
WNDCLASS winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // generic dc
PAINTSTRUCT ps; // generic paintstruct
// first fill in the window class stucture
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL,"icon1.ico");
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
// register the window class
if (!RegisterClass(&winclass))
return(0);
// create the window, note the use of WS_POPUP
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"WIN3D Game Console", // title
WS_POPUP | WS_VISIBLE,
0,0, // initial x,y
GetSystemMetrics(SM_CXSCREEN), // intial width
GetSystemMetrics(SM_CYSCREEN), // initial height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance
NULL))) // creation parms
return(0);
// hide mouse
ShowCursor(FALSE);
// save the window handle and instance in a global
main_window_handle = hwnd;
main_instance = hinstance;
// perform all game console specific initialization
Game_Init();
Start_Time = ::GetTickCount();
// enter main event loop
while(1)
{
if( isGameOver )
goto ShutDonw;
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
Time_Elapse = ::GetTickCount() - Start_Time;
if( !isFoodSet )
Set_Food();
if(Time_Elapse < 33){
Sleep(1);
continue;
}
//Start_Time = ::GetTickCount();
//if(CurrFrame < FramePerMove){
// CurrFrame++;
if(!isKeyDown){
if( KEY_DOWN(VK_RIGHT) )
KeyDown = RIGHT;
if( KEY_DOWN(VK_LEFT) )
KeyDown = LEFT;
if( KEY_DOWN(VK_UP) )
KeyDown = UP;
if( KEY_DOWN(VK_DOWN) )
KeyDown = DOWN;
isKeyDown = true;
}
// continue;
//}
if(CurrFrame < FramePerMove){
CurrFrame++;
Start_Time = ::GetTickCount();
continue;
}
CurrFrame = 0;
// main game processing goes here
Game_Main();
isKeyDown = false;
} // end while
// shutdown game and release all resources
ShutDonw:
Game_Shutdown();
// show mouse
ShowCursor(TRUE);
// return to Windows like this
return(msg.wParam);
} // end WinMain
// T3DX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////////
int Game_Init(void *parms)
{
// this function is where you do all the initialization
// for your game
Init_Snake();
Init_Block();
DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
// return success
return(1);
} // end Game_Init
int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated
Delete_Snake();
DD_Shutdown();
// return success
return(1);
} // end Game_Shutdown
int Game_Main(void *parms)
{
Draw_Rectangle(0,0,SCREEN_WIDTH-1, SCREEN_HEIGHT-1,200);
NextMoveTo = MoveTo;
if(isKeyDown)
{
switch( MoveTo ){
case RIGHT:
if( KeyDown == UP || KeyDown == DOWN )
NextMoveTo = KeyDown;
break;
case LEFT:
if( KeyDown == UP || KeyDown == DOWN )
NextMoveTo = KeyDown;
break;
case UP:
if( KeyDown == LEFT || KeyDown == RIGHT )
NextMoveTo = KeyDown;
break;
case DOWN:
if( KeyDown == LEFT || KeyDown == RIGHT )
NextMoveTo = KeyDown;
break;
default:
NextMoveTo = MoveTo;
break;
}
}
switch( NextMoveTo ){
case RIGHT:
NEXT_X = SnakeTail->x +1;
NEXT_Y = SnakeTail->y;
break;
case LEFT:
NEXT_X = SnakeTail->x -1;
NEXT_Y = SnakeTail->y;
break;
case UP:
NEXT_X = SnakeTail->x;
NEXT_Y = SnakeTail->y -1;
break;
case DOWN:
NEXT_X = SnakeTail->x;
NEXT_Y = SnakeTail->y +1;
break;
}
MoveTo = NextMoveTo;
if( NEXT_X == MAX_X || NEXT_Y == MAX_Y || NEXT_X == -1 || NEXT_Y == -1 ){
//Delete_Snake();
//exit(1);
isGameOver = true;
}
if( block[NEXT_X][NEXT_Y] == 1 ){
//Delete_Snake();
//exit(2);
isGameOver = true;
}
if( block[NEXT_X][NEXT_Y] == 0 )
Nor_Move();
if( block[NEXT_X][NEXT_Y] == 2 )
Eat_Food();
if( isGameOver )
goto GameOver;
Draw_Blocks();
Draw_Rectangle(0, 460, 639, 479, 3);
sprintf(buffer,"S N A K E 贪吃蛇 得分: %d", Score);
Draw_Text_GDI(buffer, 8,SCREEN_HEIGHT-16, 127);
Flip:
DD_Flip();
if( isGameOver )
Sleep(3000);
if (KEY_DOWN(VK_ESCAPE))
PostMessage(main_window_handle, WM_DESTROY,0,0);
return(1);
////////////////////////////////////
//Code about the GameOver
GameOver:
Draw_Rectangle( (screen_width/2 - 80) , (screen_height/2 - 60), (screen_width/2 + 80), (screen_height/2 + 60) , 171);
sprintf(buffer,"GAME OVER 得分: %d", Score);
Draw_Text_GDI(buffer, 250, 210, 127);
sprintf(buffer," 天天天晴 " );
Draw_Text_GDI(buffer, 250, 250, 127);
goto Flip;
////////////////////////////////////
}
//
void Init_Snake(){
SnakeHead = new SNAKE;
SnakeTail = SnakeHead;
SnakeHead->x = INITX;
SnakeHead->y = INITY;
for( int i=0; i<4; i++ )
{
SNAKE * p = new SNAKE;
SnakeTail->next = p;
SnakeTail = p;
p->x = INITX+1+i;
p->y = INITY;
}
SnakeTail->next = NULL;
}
void Init_Block(){
for(int i=0; i<MAX_X; i++)
for(int j=0; j<MAX_Y; j++)
{
block[i][j] = 0;
}
SNAKE *p = SnakeHead;
while( p != NULL )
{
block[p->x][p->y] = 1;
p = p->next;
}
}
void Set_Food(){
start:
int tempx = rand()%MAX_X;
int tempy = rand()%MAX_Y;
if( block[tempx][tempy] == 1 )
goto start;
if( block[tempx][tempy] == 2 )
return;
block[tempx][tempy] = 2;
isFoodSet = true;
}
void Nor_Move(){
SNAKE * p = SnakeHead;
if(p)
block[p->x][p->y] = 0;
while( p->next != NULL )
{
p->x = p->next->x;
p->y = p->next->y;
p = p->next;
}
p->x = NEXT_X;
p->y = NEXT_Y;
block[NEXT_X][NEXT_Y] = 1;
}
void Eat_Food(){
SNAKE * p = new SNAKE;
SnakeTail->next = p;
SnakeTail = p;
p->x = NEXT_X;
p->y = NEXT_Y;
SnakeTail->next = NULL;//尾指针一定要设NULL
/*
SNAKE * p = new SNAKE;
SnakeTail->next = p;
SnakeTail = p;
SnakeTail->x = NEXT_X;
SnakeTail->y = NEXT_Y;
*/
block[NEXT_X][NEXT_Y] = 1;
Score += 50;
isFoodSet = false;
}
void Delete_Snake(){
SNAKE *p = SnakeHead;
while(p != NULL)
{
SnakeHead = p->next;
delete p;
p = SnakeHead;
}
SnakeHead = NULL;
SnakeTail = NULL;
}
void Draw_Blocks(){
int color = rand()%255;
for(int i=0; i<MAX_X; i++)
{
for(int j=0; j<MAX_Y; j++)
{
if(block[i][j] == 1)
Draw_Rectangle(i*20+1,j*20+1,i*20+19,j*20+19,123);
else if(block[i][j] == 2){
Draw_Rectangle(i*20+7,j*20+2,i*20+13,j*20+7,color);
Draw_Rectangle(i*20+2,j*20+7,i*20+7,j*20+13,color);
Draw_Rectangle(i*20+7,j*20+13,i*20+13,j*20+18,color);
Draw_Rectangle(i*20+13,j*20+7,i*20+18,j*20+13,color);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -