This article describes the C + + implementation based on the console interface Pacman game. Share to everyone for your reference. The specific analysis is as follows:
ESC key to exit the game.
#include "lib.h" #pragma once HANDLE hmain_out;
HANDLE hmain_in;
Console_cursor_info INFO;
int Ibeans = 0;
int level = 1;
extern short wall[17][24]; Short wall[17][24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,-1,0,0,0,0,0,0,0,0,0,0,0,- 1,0,0,0,0,0,0,0,0,0,0, 0,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,0,0, 0,-1,0,0,0,-1,0,0,0,0,0,0,0,-1 , 0,0,0,0,0,-1,0,0,0,0, 0,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,0,-1,0,-1,-1,-1,-1,-1,-1,-1,0,0, 0,-1,0,0,-1,0,-1,0,0,0 , 0,-1,0,-1,-1,-1,0,0,0,0,0,0,0,0, 0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,0, 0,-1,0,0,0,0,0,0,0,0, 0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,-1,0,0,0,0, 0,0,0,0,0 , 0,-1,0,0,0,0,0,0,0,-1,0,-1,0,0,-1,0,0,0,0, 0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,-1,-1,-1,0,-1,-1,0, 0,0,0,0,0,- 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0, 0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0, 0,0,-1,-1 , -1,-1,0,0,0, 0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0, 0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0, 0,0,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /********************************** * Function: * Judge the user to collide with the wall or eat food ***********************************/int Isknock (WALL *pWall
, body *pbody,food *pfood,int type) {if (Knock_wall = = type) {int i;
for (i = 0; i < Pwall->len ++i) {if (Isover (Pbody->pos,pwall->pos[i],knock_wall)) return 1;//collide with the wall
} else if (Knock_food = = TYPE) {int i; for (i = 1; I <= pfood->len. ++i) {if (Isover (pfood->pos[i],pbody->pos,knock_wall)) return I;//with food
Hit} return 0; /********************************** * Features: * user moves ***********************************/int move (FOOD *pfood,wall *pWall
, body *pbody) {The body Prepos = *pbody;//POS Prepos = pbody->pos;
int ikey = Getkey ();
if ( -1 = = Ikey)//user exit return-1; if (Ikey) {pbody->Direction = Ikey + 4;
Ikey = 0;
} if (0 = ikey) {if (direction_up = = pbody->direction)--pbody->pos.y;
else if (Direction_down = = pbody->direction) ++pbody->pos.y;
else if (Direction_left = = pbody->direction)--pbody->pos.x;
else if (direction_right = = pbody->direction) ++pbody->pos.x;
} if (Isknock (Pwall,pbody,pfood,knock_wall)) {*pbody = Prepos;
return 0;
int IX = Isknock (Pwall,pbody,pfood,knock_food);
if (ix) {++ibeans;
Remove the first eaten food int i;
for (i = IX; I <= (pfood->len-1); ++i) Pfood->pos[i] = pfood->pos[i + 1];
--(Pfood->len);
if (0 = = Pfood->len)//authorities complete {++level;
return 1; } Print (&prepos.pos,print_clean);
Deletes the previous output print (&pbody->pos,print_body) first;
return 0; /********************************** * Function: * To determine whether 2 points are coincident ***********************************/int isover (POS Pos1,POS Pos2 , int TYPE) {if (Knock_wall = = type)//pos1,the body. Pos2,the WALL if (pos1.x = pos2.x && pos1.y = pos2.y) | |
(pos2.x + 1 = pos1.x && pos2.y = = pos1.y))
return 1;
return 0; /********************************** * Function: * Build wall ***********************************/void Makewall (WALL *pWall) {int x
, y;
int IX = 0; for (x = 0; x <= ++x) {for (y = 0; y <=; ++y) {if (0 = = Wall[x][y]) {pwall->pos[ix].x =
2 * y;
PWALL->POS[IX].Y = x;
Print (&pwall->pos[ix++],print_wall); }} Pwall->len = IX; Update the number of walls}/********************************** * Function: * Complete initialization operation ***********************************/void Init (FOOD *
Pfood,wall *pwall,body *pbody,hall *phall) {//Get the console standard input output handle hmain_out = GetStdHandle (Std_output_handle);
hmain_in = GetStdHandle (Std_input_handle);
Hidden cursor getconsolecursorinfo (hmain_out,&info);
info.bvisible = FALSE;
Setconsolecursorinfo (Hmain_out,&info);
Initialize the structure body Pfood->len = 0;
Pwall->len = 0;
Phall->len = 0;
Change the console size system ("mode con cols=50 lines=21");Corridor Structure body assignment int x,y;
int IX = 0; for (x = 0; x < ++x) {for (y = 0; y < ++y) {if (wall[x][y))//non-wall {phall->pos[ix].x = 2
* y;
PHALL->POS[IX++].Y = x;
}} Phall->len = IX;
Pbody->pos.x = 2;
Pbody->pos.y = 1;
Pbody->direction = Direction_down;
printf ("%d%d\n", phall->pos[0].x,phall->pos[0].y);
output food int i;
Makefood (Phall,pfood);
for (i = 1; I <= 7; ++i) {Print (&pfood->pos[i],print_food,i); }/********************************** * Features: * Get user key ***********************************/int getkey () {if (GetAsyncKey
State (VK_UP)) return key_up;
if (Getasynckeystate (Vk_down)) return key_down;
if (Getasynckeystate (vk_left)) return key_left;
if (Getasynckeystate (vk_right)) return key_right; if (Getasynckeystate (vk_escape)) return-1; User exits return 0;//user does not have a valid key}/********************************** * Function: * Complete the printing function ***********************************/Voi D Print (POS *p,int type,int ix) {COORD POs Pos.
X = p->x; Pos.
Y = p->y;
SetConsoleCursorPosition (Hmain_out,pos);
if (Print_wall = = TYPE) printf ("");
else if (Print_food = = TYPE) printf ("%d", ix);
else if (print_body = = TYPE) printf ("\1");
else if (Print_clean = = TYPE) printf ("");} /********************************** * Function: * Display level and remaining beans number ***********************************/void Showscore (Body *pBody
, FOOD *pfood) {COORD pos; Pos.
X = 0; Pos.
Y = 18;
SetConsoleCursorPosition (Hmain_out,pos); printf ("\tbeans Left:%d |
Pos:x=%d,y=%d\n ", PFOOD->LEN,PBODY->POS.X,PBODY->POS.Y);
printf ("\t\tlevel:%d", level);
for (int i = 1; I <= pfood->len ++i)//printf ("(x=%d,y=%d)", pfood->pos[i].x,pfood->pos[i].y); /********************************** * Function: * Generate food ***********************************/void Makefood (HALL *pHall,FOOD *
Pfood) {srand (unsigned) time (NULL));
int tot;
for (tot = 1; tot <= 7; ++tot) {int ixfood = rand () * phall->len/rand_max; pfood->pos[tot].x =phall->pos[ixfood].x;
PFOOD->POS[TOT].Y = phall->pos[ixfood].y;
} Pfood->len = 7; }
I hope this article will help you with your C + + programming.