easyx 기반 C++탐식 뱀 구현

7346 단어 easyxC++탐식 사
본 논문 의 사례 는 easyx 를 바탕 으로 하 는 C++탐식 뱀 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
이 코드 는 easyx 토론 군의 공유 에서 나 왔 습 니 다.
먼저 효과 도 를 올 리 면 뱀 과 음식 을 대표 하 는 간단 한 동 그 라 미 를 그 렸 을 뿐 배경 은 검은색 이다.

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>
 
#define N 100
 
using namespace std;
 
enum moved { UP, DOWN, LEFT, RIGHT };
class Snake {
private:
 struct {   //      
 int x;
 int y;
 }snake[100];
 struct {
 int life;  // 1     , 0      
 int length; //      ,    3
 enum moved direction;  //    
 }snake_head;
 struct {    //     
 int x;
 int y;
 }food;
public:
 void display();  //    
 void initSnake(); //     
 void move();//   
 void boundary_check();//    
 void _food();//    
 int food_eatcheck();//        ,     1,    0
 int snake_eat();//           ,     1,    0
 void run();   //      
};
void Snake::display() {
 initgraph(800, 600);
 setbkcolor(WHITE);  //         
 cleardevice();    //           
 setfillcolor(BLACK); //          ,              
 solidrectangle(20, 560, 560, 20);//     20*20     ,   27*27   
}
//    
void Snake::initSnake() {
 srand((unsigned)time(NULL));
 //           ,                 
 int x = rand() % 22 + 3; //   3-24
 int y = rand() % 22 + 3; //           3,           
 this->snake[0].x = x * 20 + 10;//             
 this->snake[0].y = y * 20 + 10;
 //               y    
 this->snake[1].y = this->snake[2].y = this->snake[0].y;
 this->snake[1].x = this->snake[0].x - 20;
 this->snake[2].x = this->snake[0].x - 40;
 setfillcolor(GREEN);  //        
 solidcircle(this->snake[0].x, this->snake[0].y, 10); //  
 solidcircle(this->snake[1].x, this->snake[1].y, 10);
 solidcircle(this->snake[2].x, this->snake[2].y, 10);
 this->snake_head.length = 3;
 this->snake_head.life = 1;
 this->snake_head.direction = RIGHT;
}
void Snake::move() {
 char ch;
 if (_kbhit()) {  //          1,         0
 ch = _getch();//       
 switch (ch) {
  case 'w' :if (this->snake_head.direction != DOWN) this->snake_head.direction = UP; break;
  case 'W':if (this->snake_head.direction != DOWN) this->snake_head.direction = UP; break;
  case 's' :if (this->snake_head.direction != UP) this->snake_head.direction = DOWN; break;
  case 'S':if (this->snake_head.direction != UP) this->snake_head.direction = DOWN; break;
  case 'a':if (this->snake_head.direction != RIGHT) this->snake_head.direction = LEFT; break;
  case 'A':if (this->snake_head.direction != RIGHT) this->snake_head.direction = LEFT; break;
  case 'd':if (this->snake_head.direction != LEFT) this->snake_head.direction = RIGHT; break;
  case 'D':if (this->snake_head.direction != LEFT) this->snake_head.direction = RIGHT; break;
  default:break;
 }
 }
 //       
 int i = this->snake_head.length - 1;
 setfillcolor(BLACK);
 solidcircle(snake[i].x, snake[i].y, 10);
 //         ,             ,    
 for (; i > 0; i--) {
 this->snake[i].x = this->snake[i - 1].x;
 this->snake[i].y = this->snake[i - 1].y;
 }
 switch (this->snake_head.direction) {
 case RIGHT:this->snake[0].x += 20; break;
 case LEFT:this->snake[0].x -= 20; break;
 case UP:this->snake[0].y -= 20; break;
 case DOWN:this->snake[0].y += 20; break;
 default:break;
 }
 setfillcolor(GREEN);
 solidcircle(this->snake[0].x, this->snake[0].y, 10);//    
 Sleep(1000);
}
void Snake::boundary_check() {
 if (this->snake[0].x <= 30 || this->snake[0].x >= 550 || this->snake[0].y <= 30 || this->snake[0].y >= 550) {
 this->snake_head.life = 0; 
 }
}
void Snake::_food() {
 srand((unsigned)time(NULL));
 int x = rand() % 21 + 3;  //   3-23
 int y = rand() % 21 + 3;
 this->food.x = x * 20 + 10;
 this->food.y = y * 20 + 10;
 setfillcolor(YELLOW);
 solidcircle(this->food.x, this->food.y, 10);
}
int Snake::food_eatcheck() {
 if (this->snake[0].x == this->food.x && this->snake[0].y == this->food.y) {
 //             
 this->snake_head.length++;//    
 setfillcolor(GREEN);
 solidcircle(food.x, food.y, 10);
 int k = this->snake_head.length;
 //                  
 switch (this->snake_head.direction) {
  case RIGHT:this->snake[k - 1].x = this->snake[k - 2].x - 20; this->snake[k - 1].y = this->snake[k - 2].y; break;
  case LEFT:this->snake[k - 1].x = this->snake[k - 2].x += 20; this->snake[k - 1].y = this->snake[k - 2].y; break;
  case UP:this->snake[k - 1].x = this->snake[k - 2].x; this->snake[k - 1].y = this->snake[k - 2].y + 20; break;
  case DOWN:this->snake[k - 1].x = this->snake[k - 2].x; this->snake[k - 1].y = this->snake[k - 2].y - 20; break;
  default:break;
 }
 setfillcolor(GREEN);
 solidcircle(this->snake[k - 1].x, this->snake[k - 1].y, 10);
 return 1;
 }
 return 0;
}
int Snake::snake_eat() {
 int i;
 for (i = 1; i < this->snake_head.length; i++) {
 if (this->snake[i].x == this->snake[0].x && this->snake[i].y == this->snake[0].y) {
  return 1;
 }
 }
 return 0;
}
void Snake::run() {
 display(); //      
 initSnake();
 _food();  //       
 while (true) {
 move();  //   
 if (snake_eat() == 1) {
  //       ,    
  cout << "       ,    " << endl;
  break;
 }
 boundary_check();//      
 if (this->snake_head.life == 0) {
  //   
  cout << "   ,    " << endl;
  break;
 }
 if (food_eatcheck() == 1) {
  _food(); //             
 }
 }
}
 
int main() {
 Snake s;
 s.run();
 return 0;
}
더 많은 재 미 있 는 클래식 게임 을 통 해 주 제 를 실현 하고 여러분 에 게 공유 합 니 다.
C++클래식 게임 모음
python 클래식 게임 모음
python 러시아 블록 게임 집합
JavaScript 클래식 게임 을 계속 합 니 다.
javascript 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기