탐식 사 (C + 버 전)
실행 가능 한 파일 다운로드 링크:http://download.csdn.net/detail/txgc0/5304710
main.cpp:
#include "main.h"
int foodx,foody;
int main(){
snak s;
food f;
srand((int )time(0));//
f.init();
s.initsnak();
s.runing();
return 0;
}
food.cpp:
#include "main.h"
void food::init(){
makefood();
putfood();
}
void food::makefood(){
foodx=rand()%MAXLINE;
foody=rand()%MAXROW;
if(foodx == 0){
foodx=1;
}
if(foody == 0){
foody=1;
}
}
void food::putfood(){
food::gotoxy(foodx,foody);
cout<<"O";
}
void food::gotoxy(int x,int y){
HANDLE h1;
COORD pos;
pos.X=x;
pos.Y=y;
h1=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h1,pos);
}
int food::getx(){
return foodx;
}
int food::gety(){
return foody;
}
snak.cpp:
#include "main.h"
void snak::judgeEnd(){
int end=0;
if(level >25){
printf(" , !");
level =0;
Sleep(1000);
exit(0);
}
for(i=lens-1;i>0;i--){
if(X[0] == X[i] && Y[0] == Y[i]){
end = 1;
}
}
if(end == 1 || \
X[0] >= (MAXLINE) || Y[0] >= (MAXROW) || X[0] <=0 ||Y[0] <=0){
end=0;
gotoxy(11,4);
printf("game over
");
gotoxy(8,5);
printf("play agin:(Y or N)");
if(getchar() == 'Y'){
system("cls");
fd.init();
initsnak();
runing();
}
else exit(0);
}
}
void snak::movesnak(){
for(i=lens-1;i>0;i--){
X[i]=X[i-1];
Y[i]=Y[i-1];
}
if(dir == LEFT){
X[0]--;
}
else if(dir == RIGHT){
X[0]++;
}
else if(dir == UP){
Y[0]--;
}
else if(dir == DOWN){
Y[0]++;
}
}
void snak::initsnak(){
int i;
X[0]=10;Y[0]=6;
X[1]=9;Y[1]=6;
dir = RIGHT;
totalscore=0;
lens=2;score=0;level=0;
gotoxy(0,0);
for(i=0;i<MAXLINE;i++){
gotoxy(i,0);
cout<<"-";
gotoxy(i,MAXROW);
cout<<"-";
}
for(i=0;i<MAXROW;i++){
gotoxy(MAXLINE,i);
cout<<"|";
gotoxy(0,i);
cout<<"|";
}
gotoxy(MAXLINE+5,3);
cout<<" :w,s,a,d: 、 、 、 ;
";
gotoxy(MAXLINE+5,4);
cout<<" :
";
gotoxy(MAXLINE+5,5);
cout<<" !
";
gotoxy(MAXLINE+5,6);
cout<<" ,
";
gotoxy(MAXLINE+5,7);
cout<<" , !
";
gotoxy(MAXLINE+5,9);
cout<<" ,
";
}
void snak::gotoxy(int x,int y){
HANDLE h1;
COORD pos;
pos.X=x;
pos.Y=y;
h1=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h1,pos);
}
int snak::getdir(int *p){
char c=getch();
if(c == 'w' && *p != DOWN)
return UP;
else if(c == 's' && *p != UP)
return DOWN;
else if(c == 'a' && *p != RIGHT)
return LEFT;
else if(c == 'd' && *p != LEFT)
return RIGHT;
else if(c == 'p'){
gotoxy(8,4);
cout<<"!pause!";
while(getch() != ' ');
gotoxy(8,4);
cout<<" ";
return *p;
}
return *p;
}
void snak::dispmenu(){
gotoxy(10,20);
cout<<"head(x,y)=("<<X[0]<<","<<Y[0]<<")";
gotoxy(10,21);
cout<<"totalscore: "<<totalscore<<", level: "<<(level+1);
gotoxy(10,22);
cout<<"the snak lens :"<<lens;
}
void snak::dispsnak(){
for(i=0;i<lens;i++){
gotoxy(X[i],Y[i]);
cout<<"X";
}
Sleep(300-level*10);
gotoxy(X[lens-1],Y[lens-1]);
cout<<" ";
}
void snak::ifeatfood(){
gotoxy(10,19);
cout<<"food(x,y)=("<<foodx<<","<<foody<<")";
if(X[0] == fd.getx() && Y[0] == fd.gety()){
fd.makefood();
fd.putfood();
gotoxy(X[lens-1],Y[lens-1]);
cout<<" ";
lens++;
score++;
totalscore++;
if(score >= 2){
score = 0;
level++;
}
}
}
void snak::runing(){
while(1){
if(kbhit()){
dir=getdir(&dirlast);
}
dirlast=dir;
dispsnak();
judgeEnd();
ifeatfood();
movesnak();
dispmenu();
}
}
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <conio.h>
#include "food.h"
#include "snak.h"
extern int foodx;
extern int foody;
#define MAXLINE 32
#define MAXROW 16
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
class food;
class snak;
using std::cout;
using std::endl;
#endif
food.h:
#ifndef __FOOD_H__
#define __FOOD_H__
#include "main.h"
class sank;
class food{
private:
public:
//food(int x,int y);//
void init();
void makefood();// ,
void putfood();//
void gotoxy(int x,int y);
int getx();
int gety();
// void eatfood();
};
#endif
snak.h:
#ifndef __SNAK_H__
#define __SNAK_H__
#include "main.h"
class food;
class snak{
private:
int X[256],Y[256];
int totalscore,score,lens,level,dir,i,dirlast;
food fd;
public:
void gotoxy(int x,int y);
int getdir(int *p);
void initsnak();
void judgeEnd();
void dispmenu();
void runing();
void dispsnak();
void ifeatfood();
void movesnak();
};
#endif
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.