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 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
OpenCV && AKAZE && Windows10 && VisualStudio2017작동하는 버전의 조합을 찾고 ... OpenCV에서 AKAZE로 이미지 간의 특징점을 일치시키고 싶다면, OS(Windows10 64bit)와 OpenCV 버전과 Visual Studio 버전의 조합을 찾는데 힘들었...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.