,,,大神,求份visual c++ 游戲代碼,,,(替朋友上 課程設計,,,看到人家 啪啪啪地敲個不停,,,連一個妹子都用鄙視的眼光看我,,,真心尴尬,,,)
#include<stdio.h>
#include<bios.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<alloc.h>
#include<stdlib.h>
#include<time.h>
#define LEFT 0x4B00
#define RIGHT 0x4D00
#define UP 0x4800
#define DOWN 0x5000
#define ESC 0x011B
#define ENTER 0x1C0D
char s1_title[]="Snake game";
char s1_choose[3][11]={"start game","author","exit"};
char s2_title[]="Snake game,made by HungryAnt!";
char s2_fail[]="Game over!";
char s2_win[]="You win!!!";
int s1_x=320-45;
int s1_y=240-33;
int s2_x=320-150;
int s2_y=240-150;
int s3_x=320-120;
int s3_y=240-50;
char map[30][30];/*空地0,蛇身1,食物2*/
int length;
int direction=2;/*上,下,左,右:0,1,2,3*/
int delay_time=20;/*延時,單位10毫秒*/
int difficult=0;/*游戲難度*/
int game_out=0;
struct snake{/*************蛇結構體**************/
char x;
char y;
struct snake *previous;/*前*/
struct snake *next;
}*head,*tail;
void initsnake(){/*************蛇初始化**************/
struct snake *sn;
head=sn=(struct snake *)malloc(sizeof(struct snake));/*蛇頭*/
sn->x=14;
sn->y=15;
map[15][14]=1;
sn->previous=NULL;
sn->next=(struct snake *)malloc(sizeof(struct snake));
sn->next->previous=sn;
sn=sn->next;
sn->x=15;
sn->y=15;
map[15][15]=1;
sn->next=(struct snake *)malloc(sizeof(struct snake));
sn->next->previous=sn;
sn=sn->next;
sn->x=16;
sn->y=15;
map[15][16]=1;
sn->next=NULL;
tail=sn;/*蛇尾*/
}
void barbox(int x,int y,int color,int width,int height){/*填充一定范圍的函數*/
setfillstyle(SOLID_FILL,color);
bar(x,y,x+width-1,y+height-1);
}
void box(int x,int y,int color){/*填充地圖小方格函數*/
barbox(x*10+1,y*10+1,color,9,9);
}
int choose(){/*在s1窗口裡的選擇框裡進行選擇*/
int key,i=0,j;
do{
j=i;
barbox(1,i*(240-s1_y)*2/3+1,LIGHTGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*設置淺灰色的選擇條*/
setcolor(BLUE);
outtextxy(4,i*(240-s1_y)*2/3+9,s1_choose[i]);
setcolor(WHITE);
outtextxy(3,i*(240-s1_y)*2/3+8,s1_choose[i]);
while(bioskey(1)==0);
key=bioskey(0);
switch(key){
case UP:
if(i>0)j=i--;
break;
case DOWN:
if(i<2)j=i++;
break;
case ESC:
exit(0);
break;
case ENTER:
return i;
}
if(j!=i){
barbox(1,j*(240-s1_y)*2/3+1,DARKGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*設置淺灰色的選擇條*/
setcolor(WHITE);
outtextxy(3,j*(240-s1_y)*2/3+8,s1_choose[j]);
}
}while(1);
}
void s1_window(){/*進入程序的第一個界面--中間的窗口*/
int i=0;
setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0);
setcolor(LIGHTBLUE);/*畫兩個連在一起的框架*/
rectangle(-1,-26,(320-s1_x)*2,-1); /*標題部分*/
rectangle(-1,-1,(320-s1_x)*2,(240-s1_y)*2+1);/*選擇部分*/
barbox(0,-25,BLUE,(320-s1_x)*2,24);/*設置標題框架填充*/
settextstyle(0,0,1);
setcolor(WHITE);
outtextxy(5,-17,s1_title);/*輸出標題*/
while(i<3){
barbox(1,i*(240-s1_y)*2/3+1,DARKGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*設置深灰色的條*/
setcolor(WHITE);
outtextxy(3,i*(240-s1_y)*2/3+8,s1_choose[i]);
i++;
}
}
void s2_window(){/*第二個界面--游戲界面*/
int x,y;
setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0);
clearviewport();
setcolor(LIGHTBLUE);/*畫兩個連在一起的框架*/
rectangle(-1,-30,(320-s2_x)*2+1,-1); /*分值框架*/
barbox(0,-29,BLUE,(320-s2_x)*2+1,28);/*設置分值框架填充*/
settextstyle(0,0,1);
setcolor(WHITE);
outtextxy(5,-17,s2_title);/*輸出標題*/
setcolor(LIGHTGRAY);
rectangle(-1,0,(320-s2_x)*2+1,(240-s2_y)*2+1);/*地圖框架*/
rectangle(0,0,(320-s2_x)*2,(240-s2_y)*2);
for(y=0;y<300;y+=10)/*地圖繪制*/
for(x=0;x<300;x+=10)
barbox(x+1,y+1,DARKGRAY,9,9);
}
void initmap(){/*初始化地圖*/
int x,y;
for(y=0;y<30;y++)
for(x=0;x<30;x++)
map[y][x]=0;
}
void setfood(){/*隨機產生一個食物*/
int x,y;
do{
x=rand()%30;
y=rand()%30;
}while(map[y][x]==1);
map[y][x]=2;
box(x,y,YELLOW);
}
void printsnake(){/*顯示出蛇*/
struct snake *sn=head;
box(sn->x,sn->y,LIGHTBLUE);
sn=head->next;
while(sn!=NULL){
box(sn->x,sn->y,LIGHTGREEN);
sn=sn->next;
}
}
void cleartime(){/*時間設置為0*/
struct time time_0;
time_0.ti_min=0;
time_0.ti_hour=0;
time_0.ti_hund=0;
time_0.ti_sec=0;
settime(&time_0);
}
void freesnake(){/***************釋放蛇所占的內存**************/
struct snake *sn,*sn1;
sn=head;
head=NULL;
while(sn!=NULL){/*free蛇所占的內存*/
sn1=sn->next;
free(sn);
sn=sn1;
}
}
void snakemove(){/***蛇移動***/
struct snake *sn;
sn=head;
head=(struct snake *)malloc(sizeof(struct snake));/*蛇移動一格*/
head->x=sn->x;
head->y=sn->y;
switch(direction){
case 0:
head->y--;
break;
case 1:
head->y++;
break;
case 2:
head->x--;
break;
case 3:
head->x++;
break;
}
if(head->x<0 || head->x>29 || head->y<0 || head->y>29 || map[head->y][head->x]==1){/*如果蛇超出地圖或者撞到自己,游戲失敗*/
freesnake();
setviewport(0,0,639,479,0);
settextstyle(0,0,3);
setcolor(CYAN);
outtextxy(205,210,s2_fail);/****************顯示game over****************/
getch();
clearviewport();/*清除屏幕內容*/
game_out=1;
return;
}
else{
head->next=sn;
sn->previous=head;
head->previous=NULL;
if(map[head->y][head->x]==2){/*如果遇到食物*/
length++;/*長度增加*/
if(length==30){
freesnake();
setviewport(0,0,639,479,0);
settextstyle(0,0,3);
setcolor(WHITE);
outtextxy(206,211,s2_win);/****************顯示you win****************/
setcolor(LIGHTRED);
outtextxy(205,210,s2_win);/****************顯示you win****************/
getch();
clearviewport();/*清除屏幕內容*/
game_out=1;
return;
}
difficult=(length-3)/3;
delay_time=20-difficult;
map[head->y][head->x]=1;
setfood();/*再添加一個食物*/
}
else{/*如果沒有遇到食物則清除蛇尾*/
map[tail->y][tail->x]=0;
sn=tail;
tail=tail->previous;
tail->next=NULL;
map[sn->y][sn->x]=0;
box(sn->x,sn->y,DARKGRAY);
free(sn);
map[head->y][head->x]=1;
}
}
}
void gamestart(){/*****游戲進行中*****/
int key;
char l[4];
struct time t_last;
setfood();
while(1){
barbox((320-s2_x)*2-28,-29,BLUE,27,28);/*覆蓋上次蛇長*/
sprintf(l,"%d",length);
setcolor(YELLOW);
outtextxy((320-s2_x)*2-28,-17,l);/********************輸出蛇長*/
printsnake();
cleartime();
key=0;
while(1){
gettime(&t_last);
if(t_last.ti_hund>delay_time)break;/*如果超過延時時間就退出*/
if(bioskey(1)){
key=bioskey(0);
switch(key){
case UP:
if(direction!=1)direction=0;
break;
case DOWN:
if(direction!=0)direction=1;
break;
case LEFT:
if(direction!=3)direction=2;
break;
case RIGHT:
if(direction!=2)direction=3;
break;
}
break;
}
}
snakemove();
if(key==ESC){
freesnake();
game_out=1;
setviewport(0,0,639,479,0);
clearviewport();
return;
}
if(game_out==1)return;
}
}
void s2(){/*游戲*/
length=3;
direction=2;
game_out=0;
s2_window();/*界面*/
initmap();/*地圖初始化*/
initsnake();/*蛇初始化*/
gamestart();/*游戲開始*/
if(game_out==1)return;
}
void s3_window(){
char author[]="Author: HungryAnt";
char email[]="E-mail: [email protected]";
char blog[]="http://hi.baidu.com/zhongji";
char QQ[]="QQ:517377100";
setviewport(0,0,639,479,0);/*首先清屏*/
clearviewport();
setviewport(s3_x,s3_y,640-s3_x-1,480-s3_y-1,0);
setcolor(LIGHTBLUE);
rectangle(-1,-1,(320-s3_x)*2,(240-s3_y)*2);/*個人信息*/
barbox(0,0,BLUE,(320-s3_x)*2,(240-s3_y)*2);/*設置標題框架填充*/
settextstyle(0,0,1);/*輸出作者信息*/
setcolor(BLACK);
outtextxy(6,10,author);
outtextxy(6,35,email);
outtextxy(6,60,blog);
outtextxy(6,85,QQ);
setcolor(WHITE);
outtextxy(5,9,author);
outtextxy(5,34,email);
outtextxy(5,59,blog);
outtextxy(5,84,QQ);
getch();
setviewport(0,0,639,479,0);/*清屏*/
clearviewport();
}
void s3(){/*顯示作者信息*/
s3_window();
}
void s1(){
int i;
while(1){
s1_window();
i=choose();
switch(i){
case 0:
s2();/*游戲*/
break;
case 1:
s3();/*作者*/
break;
case 2:
exit(0);
}
}
}
void s_detectgraph(){/*自定義圖形檢測*/
int gdriver,gmode,errorcode;
gdriver=VGA;
gmode=VGAHI;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
errorcode = graphresult();
if (errorcode !=0){
printf("\n\t\t\tGame : Snake Game\n");
printf("\t\t\tAuthor : HungryAnt\n");
printf("\n\terror:\t%s\n", grapherrormsg(errorcode));
getch();
exit(1);
}
}
int main(){
srand((unsigned) time(NULL));/*設置隨機數不同*/
s_detectgraph(); /*圖形檢測*/
s1();
getch();
closegraph();
}
貪吃蛇 照著敲吧_(:зゝ∠)_