主题:借个地方存一下
orangelegend
[专家分:860] 发布于 2007-02-17 14:34:00
#include<stdio.h>//sp1
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen){
int i;
int j=snakeLen;
for(i=1;i<=snakeLen;i++){
a[i].x=1;
a[i].y=j;
j--;
}
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>=cxScreen||a[1].y>=cyScreen) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(char c,int snakeLen){
switch(c){
case 'W':
case 'w':North(snakeLen);break;
case 'S':
case 's':South(snakeLen);break;
case 'D':
case 'd':East(snakeLen);break;
case 'A':
case 'a':West(snakeLen);break;
default:break;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
gotoxy(treasureX,treasureY);
printf("&");
}
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
printf("1.You must use tc to run this program\n");
printf("2.press A W S D to begin\n");
}
int main(){
char c;
int aliveFlag,snakeLen,treasureX,treasureY,treasureFlag;
srand((unsigned)time(NULL));
copyRight();
Init(INITLEN);
drawOut(INITLEN,-1,-1);
aliveFlag=1; c=getch(); snakeLen=10;treasureFlag=0;
treasurePosRand(snakeLen,&treasureX,&treasureY);
while(aliveFlag&&c){
snakeMove(c,snakeLen);
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(200);
c=getch();
}
return 0;
}
回复列表 (共9个回复)
沙发
orangelegend [专家分:860] 发布于 2007-02-17 17:30:00
#include<stdio.h>//sp2
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen);
void North(int snakeLen);
void South(int snakeLen);
void East(int snakeLen);
void West(int snakeLen);
int Detect(int snakeLen);
void snakeMove(char c,int snakeLen);
void drawOut(int snakeLen,int treasureX,int treasureY);
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos);
void copyRight();
void gameover();
int main(){
char c;
int aliveFlag,snakeLen,treasureX,treasureY,treasureFlag,reValue;
srand((unsigned)time(NULL));
Init(INITLEN);
aliveFlag=1; c=getch(); snakeLen=10;treasureFlag=0;
treasurePosRand(snakeLen,&treasureX,&treasureY);
while(aliveFlag&&c){
snakeMove(c,snakeLen);
reValue=Detect(snakeLen);
if(reValue){
gameover();
break;
}
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(200);
c=getch();
}
return 0;
}
void Init(int snakeLen){
int i;
char c;
int j=45;
for(i=1;i<=snakeLen;i++){
a[i].x=j;
a[i].y=20;
j--;
}
copyRight();
while((c=getch())!='y') continue;
drawOut(INITLEN,79,24);
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>cxScreen||a[1].x<1||a[1].y>cyScreen||a[1].y<1) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(char c,int snakeLen){
switch(c){
case 'W':
case 'w':North(snakeLen);break;
case 'S':
case 's':South(snakeLen);break;
case 'D':
case 'd':East(snakeLen);break;
case 'A':
case 'a':West(snakeLen);break;
default:return;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
gotoxy(treasureX,treasureY);
printf("&");
}
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
gotoxy(20,10);
printf("1.You must use tc to run this program\n");
gotoxy(20,11);
printf("2.press A W S D to begin\n");
gotoxy(20,12);
printf("3.press y to continue\n");
}
void gameover(){
clrscr();
gotoxy(30,10);
printf("Game Over\n");
}
板凳
orangelegend [专家分:860] 发布于 2007-02-17 17:31:00
/*sp3*/
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<bios.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen);
void North(int snakeLen);
void South(int snakeLen);
void East(int snakeLen);
void West(int snakeLen);
int Detect(int snakeLen);
void snakeMove(int c,int snakeLen);
void drawOut(int snakeLen,int treasureX,int treasureY);
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos);
void copyRight();
void gameover();
int main(){
int key;
int aliveFlag,snakeLen,treasureX,treasureY,treasureFlag,reValue;
srand((unsigned)time(NULL));
Init(INITLEN);
aliveFlag=1; key=bioskey(0); snakeLen=10;treasureFlag=0;
treasurePosRand(snakeLen,&treasureX,&treasureY);
while(aliveFlag){
snakeMove(key,snakeLen);
reValue=Detect(snakeLen);
if(reValue){
gameover();
break;
}
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(200);
key=bioskey(0);
}
return 0;
}
void Init(int snakeLen){
int i;
char c;
int j=45;
for(i=1;i<=snakeLen;i++){
a[i].x=j;
a[i].y=20;
j--;
}
copyRight();
while((c=getch())!='y') continue;
drawOut(INITLEN,79,24);
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>cxScreen||a[1].x<1||a[1].y>cyScreen||a[1].y<1) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(int c,int snakeLen){
switch(c){
case UP:North(snakeLen);break;
case DOWN:South(snakeLen);break;
case RIGHT:East(snakeLen);break;
case LEFT:West(snakeLen);break;
default:return;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
gotoxy(treasureX,treasureY);
printf("&");
}
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
gotoxy(20,10);
printf("1.You must use tc to run this program\n");
gotoxy(20,12);
printf("2.press y to start\n");
}
void gameover(){
clrscr();
gotoxy(30,10);
printf("Game Over\n");
}
3 楼
orangelegend [专家分:860] 发布于 2007-02-17 17:32:00
/*sp4*/
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<bios.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen);
void North(int snakeLen);
void South(int snakeLen);
void East(int snakeLen);
void West(int snakeLen);
int Detect(int snakeLen);
void snakeMove(int c,int snakeLen);
void drawOut(int snakeLen,int treasureX,int treasureY);
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos);
void copyRight();
void gameover();
int main(){
int key;
int aliveFlag,snakeLen,treasureX,treasureY,treasureFlag,reValue;
srand((unsigned)time(NULL));
Init(INITLEN);
aliveFlag=1; snakeLen=10;treasureFlag=0;
treasurePosRand(snakeLen,&treasureX,&treasureY);
while(aliveFlag){
key=bioskey(0);
while(bioskey(1)==0){
snakeMove(key,snakeLen);
reValue=Detect(snakeLen);
if(reValue){
gameover();
break;
}
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(200);
}
if(reValue) break;
}
system("pause");
return 0;
}
void Init(int snakeLen){
int i;
char c;
int j=45;
for(i=1;i<=snakeLen;i++){
a[i].x=j;
a[i].y=20;
j--;
}
copyRight();
while((c=getch())!='y') continue;
drawOut(INITLEN,79,24);
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>cxScreen||a[1].x<1||a[1].y>cyScreen||a[1].y<1) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(int c,int snakeLen){
switch(c){
case UP:North(snakeLen);break;
case DOWN:South(snakeLen);break;
case RIGHT:East(snakeLen);break;
case LEFT:West(snakeLen);break;
default:return;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
gotoxy(treasureX,treasureY);
printf("&");
}
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
gotoxy(20,10);
printf("1.You must use tc to run this program\n");
gotoxy(20,12);
printf("2.press y to start\n");
}
void gameover(){
clrscr();
gotoxy(30,10);
printf("Game Over\n");
}
4 楼
orangelegend [专家分:860] 发布于 2007-02-17 23:30:00
#include<stdio.h>/*sp5*/
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<bios.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
#define SPEED 300
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen);
void North(int snakeLen);
void South(int snakeLen);
void East(int snakeLen);
void West(int snakeLen);
int Detect(int snakeLen);
void snakeMove(int c,int snakeLen);
void drawOut(int snakeLen,int treasureX,int treasureY);
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos);
void copyRight();
char gameover();
void InputSpeed();
void firstDraw(int snakeLen);
int formerDirection=1;
int main(){
int key,againFlag;
int aliveFlag,snakeLen,rank,treasureX,treasureY,treasureFlag,reValue,delayTimes;
char c;
srand((unsigned)time(NULL));
while(againFlag){
Init(INITLEN);
InputSpeed();
scanf("%d",&rank);
delayTimes=SPEED/rank;
aliveFlag=1;snakeLen=10;treasureFlag=0;againFlag=1;
treasurePosRand(snakeLen,&treasureX,&treasureY);
firstDraw(snakeLen);
while(aliveFlag){
key=bioskey(0);
while(bioskey(1)==0){
snakeMove(key,snakeLen);
reValue=Detect(snakeLen);
if(reValue){
fflush(stdin);
c=gameover();
break;
}
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(delayTimes);
}
if(reValue) break;
}
if(c=='y'||c=='Y') againFlag=1;
else againFlag=0;
}
system("pause");
return 0;
}
void Init(int snakeLen){
int i;
char c;
int j=45;
for(i=1;i<=snakeLen;i++){
a[i].x=j;
a[i].y=20;
j--;
}
copyRight();
while((c=getch())!='y') continue;
drawOut(INITLEN,79,24);
}
void InputSpeed(){
clrscr();
printf(" Input your choices\n");
printf(" 1.Very Low Speed\n");
printf(" 2.Low Speed\n");
printf(" 3.Normal Speed\n");
printf(" 4.Fast Speed\n");
printf(" 5.Very Fast Speed\n");
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>cxScreen||a[1].x<1||a[1].y>cyScreen||a[1].y<1) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(int c,int snakeLen){
if(formerDirection==UP&&c==DOWN) c=UP;
if(formerDirection==DOWN&&c==UP) c=DOWN;
if(formerDirection==LEFT&&c==RIGHT) c=LEFT;
if(formerDirection==RIGHT&&c==LEFT) c=RIGHT;
if(c==UP||c==DOWN||c==LEFT||c==RIGHT)
formerDirection=c;
switch(c){
default:c=formerDirection;
case UP:North(snakeLen);break;
case DOWN:South(snakeLen);break;
case RIGHT:East(snakeLen);break;
case LEFT:West(snakeLen);break;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
gotoxy(treasureX,treasureY);
printf("&");
}
5 楼
orangelegend [专家分:860] 发布于 2007-02-17 23:30:00
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
clrscr();
gotoxy(20,12);
printf("Press 'Y' or 'y' to start\n");
gotoxy(60,20);
printf("xmu cs04 Feb 2007\n");
}
char gameover(){
char c;
clrscr();
gotoxy(30,10);
printf("Game Over\n");
printf("\t \t Press Y or y to continue\n");
printf("\t \t Press other key to quit\n");
c=getch();
return c;
}
void firstDraw(int snakeLen){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
}
6 楼
orangelegend [专家分:860] 发布于 2007-02-18 18:24:00
#include<stdio.h>/*sp6*/
#include<dos.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<bios.h>
#include<graphics.h>
#define MAX 50
#define INITLEN 10
#define cxScreen 80
#define cyScreen 25
#define SPEED 300
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
typedef struct{
int x;
int y;
}node;
node a[MAX];
void Init(int snakeLen);
void North(int snakeLen);
void South(int snakeLen);
void East(int snakeLen);
void West(int snakeLen);
int Detect(int snakeLen);
void snakeMove(int c,int snakeLen);
void drawOut(int snakeLen,int treasureX,int treasureY);
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos);
void copyRight();
char gameover();
void InputSpeed();
void firstDraw(int snakeLen);
int formerDirection=RIGHT;
int main(){
int key,againFlag,Drive,Mode=DETECT;
int aliveFlag,snakeLen,rank,treasureX,treasureY,treasureFlag,reValue,delayTimes;
char c;
srand((unsigned)time(NULL));
initgraph(&Drive,&Mode,"d:\\TC\\bgi");
textmode(C80);
highvideo();
while(againFlag){
Init(INITLEN);
InputSpeed();
scanf("%d",&rank);
delayTimes=SPEED/rank;
aliveFlag=1;snakeLen=10;treasureFlag=0;againFlag=1;
treasurePosRand(snakeLen,&treasureX,&treasureY);
firstDraw(snakeLen);
while(aliveFlag){
key=bioskey(0);
while(bioskey(1)==0){
snakeMove(key,snakeLen);
reValue=Detect(snakeLen);
if(reValue){
fflush(stdin);
c=gameover();
break;
}
if(treasureFlag) {
treasurePosRand(snakeLen,&treasureX,&treasureY);
treasureFlag=0;
}
drawOut(snakeLen,treasureX,treasureY);
if(a[1].x==treasureX&&a[1].y==treasureY) {
snakeLen++;
treasureFlag=1;
}
delay(delayTimes);
}
if(reValue) break;
}
if(c=='y'||c=='Y') againFlag=1;
else againFlag=0;
}
system("pause");
return 0;
}
void Init(int snakeLen){
int i;
char c;
int j=45;
for(i=1;i<=snakeLen;i++){
a[i].x=j;
a[i].y=12;
j--;
}
copyRight();
while((c=getch())!='y') continue;
}
void InputSpeed(){
clrscr();
gotoxy(1,10);
printf(" Input your choices\n");
printf(" 1.Very Low Speed\n");
printf(" 2.Low Speed\n");
printf(" 3.Normal Speed\n");
printf(" 4.Fast Speed\n");
printf(" 5.Very Fast Speed\n");
}
void North(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y-1;
}
void South(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x;
a[1].y=a[2].y+1;
}
void East(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x+1;
a[1].y=a[2].y;
}
void West(int snakeLen){
int i;
for(i=snakeLen;i>=2;i--){
a[i].x=a[i-1].x;
a[i].y=a[i-1].y;
}
a[1].x=a[2].x-1;
a[1].y=a[2].y;
}
int Detect(int snakeLen){
int i;
if(a[1].x>cxScreen||a[1].x<1||a[1].y>cyScreen||a[1].y<1) return 1;
for(i=2;i<=snakeLen;i++)
if(a[1].x==a[i].x&&a[1].y==a[i].y) return 1;
return 0;
}
void snakeMove(int c,int snakeLen){
if(formerDirection==UP&&c==DOWN) c=UP;
if(formerDirection==DOWN&&c==UP) c=DOWN;
if(formerDirection==LEFT&&c==RIGHT) c=LEFT;
if(formerDirection==RIGHT&&c==LEFT) c=RIGHT;
if(c==UP||c==DOWN||c==LEFT||c==RIGHT)
formerDirection=c;
if(c!=UP&&c!=DOWN&&c!=LEFT&&c!=RIGHT&&c!=LEFT)
c=formerDirection;
switch(c){
default:break;
case UP:North(snakeLen);break;
case DOWN:South(snakeLen);break;
case RIGHT:East(snakeLen);break;
case LEFT:West(snakeLen);break;
}
}
void drawOut(int snakeLen,int treasureX,int treasureY){
int i;
clrscr();
highvideo();
gotoxy(a[1].x,a[1].y);
putchar(15);
for(i=2;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
textattr(GREEN|BLACK*16);
printf("*");
}
gotoxy(treasureX,treasureY);
putchar(1);
gotoxy(treasureX,treasureY);
}
7 楼
orangelegend [专家分:860] 发布于 2007-02-18 18:25:00
void treasurePosRand(int snakeLen,int *treasurexPos,int *treasureyPos){
int i;
*treasureyPos=abs(rand()*200)%20+1;
*treasurexPos=abs(rand()*200)%75+1;
for(i=1;i<=snakeLen;i++)
if(*treasurexPos==a[i].x&&*treasureyPos==a[i].y)
treasurePosRand(snakeLen,treasurexPos,treasureyPos);
}
void copyRight(){
clrscr();
gotoxy(15,9);
printf(" Press 'Y' or 'y' to start\n");
gotoxy(60,24);
printf("xmu cs04 Feb 2007\n");
}
char gameover(){
char c;
clrscr();
gotoxy(30,10);
printf("Game Over\n");
printf("\t \t Press Y or y to continue\n");
printf("\t \t Press N or n to quit\n");
c=getch();
while(c!='y'&&c!='Y'&&c!='n'&&c!='N') c=getchar();
if(c=='n'||c=='N') exit(0);
return c;
}
void firstDraw(int snakeLen){
int i;
clrscr();
for(i=1;i<=snakeLen;i++){
gotoxy(a[i].x,a[i].y);
printf("*");
}
}
8 楼
雪光风剑 [专家分:27190] 发布于 2007-02-19 02:32:00
贪吃蛇?
9 楼
萧山·湘凌子 [专家分:10] 发布于 2007-02-20 19:35:00
呵呵,用东西南北左方向吗?我用vb编了一个用的上下左右
我来回复