C + + implementation based on the console interface Pacman game _c language

Source: Internet
Author: User

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:

The program running interface looks like this:

ESC key to exit the game.

Main.cpp source files are as follows:

#include "lib.h"
#pragma once
extern int level;
int main ()
{
 FOOD FOOD;
 WALL WALL;
 Body CurPos;
 Hall Hall;
 int iexit = 0;
 while (1)
 {
  if (iexit) break
   ;
  Init (&food,&wall,&curpos,&hall);
  Makewall (&wall);
  while (1)
  {sleep
   ();
   int res = move (&food,&wall,&curpos);
   if (1 = res)//start break again
    ;
   else if ( -1 = res)//user exits
   {
    iexit = 1;
    break;
   Showscore (&curpos,&food);
  }
 return 0;
}

Lib.h header files are as follows:

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h>//identify user-pressed arrow keys 
Define KEY_UP Wm_user + 1 #define Key_down Wm_user + 2 #define KEY_LEFT Wm_user + 3 #define Key_right Wm_user + 4 Identifies the user's current movement direction #define DIRECTION_UP Wm_user + 5 #define Direction_down Wm_user + 6 #define Direction_left Wm_user + 7 #d  Efine direction_right Wm_user + 8//Identifies the element type to be printed #define Print_wall Wm_user + 9 #define Print_food Wm_user + #define Print_body Wm_user + #define Print_clean wm_user + #define Knock_wall wm_user + #define Knock_food Wm_us
 ER + struct POS {int x;
int y;
};
 struct WALL {POS pos[9999];
int Len;
};
 struct FOOD {POS pos[8];
 int Len;
int Ishidden;
};
 struct Body {pos pos;
int Direction;
};
 struct hall{POS pos[200];
int Len;
};
void Init (FOOD *pfood,wall *pwall,body *pbody,hall *phall);
Perform some initialization operations void Print (POS *pos,int Type,int ix = 0);
Complete the printing function void Makewall (WALL *pwall); Build wall void Makefood (HALL *phall,food *pfood);
Generates food int move (FOOD *pfood,wall *pwall,body *pbody);
User mobile void Showscore (body *pbody,food *pfood);
Show rank and remaining beans int isover (POS pos1,pos pos2,int TYPE);
Determine whether 2 points coincide with int isknock (WALL *pwall,body *pbody,food *pfood,int TYPE);
Judge whether to wall, eat beans int getkey (); Get the user button

The

Lib.cpp source files are as follows:

#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-&GT;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-&GT;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-&GT;LEN,PBODY-&GT;POS.X,PBODY-&GT;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-&GT;POS[TOT].Y = phall->pos[ixfood].y;
} Pfood->len = 7; }

I hope this article will help you with your C + + programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.