0、前言
1、我使用的是编译软件是vc6.0 2、如果代码无法运行,你可以尝试吧文件xxx.c改为xxx.cpp 3、四个小游戏我都运行过,确保是可以运行的。虽然可玩性、操作性。。。
1、普普通通的五子棋
这是四个游戏中,个人感觉最好的一个了。
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#define N 65
int status[N][N]={{0},{0}};
int flag=0;
int direct[2];
int Value1[N][N]={{0},{0}};
int Value2[N][N]={{0},{0}};
int regrex,regrey,regrex1,regrey1;
int count=0;
void chess_board();
void red_movexy();
void white_movexy();
void red_chess(int x,int y);
void white_chess(int x,int y);
void man_man();
void man_machine();
int judge_chess(int x,int y);
int judge_winner(int x,int y,int temp);
void machine_attack();
void machine_defend();
void find_position();
void Regret();
void BackGround(unsigned int ForeColor, unsigned int BackGroundColor)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
}
void gotoxy(int x, int y)
{
HANDLE handle;
COORD coord;
coord.X = x;
coord.Y = y;
handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, coord);
}
void chess_board()
{
int i,j;
for(i=0;i<=30;i++)
for(j=0;j<=60;j+=4)
{
gotoxy(j,i);
printf("|");
}
for(i=0;i<=30;i+=2)
for(j=1;j<=57;j+=4)
{
gotoxy(j,i);
printf("---");
}
}
void chess_menu()
{
int i,j;
for(i=1;i<=29;i++)
{
gotoxy(67,i);
printf("||");
}
for(i=1;i<=29;i++)
{
gotoxy(89,i);
printf("||");
}
gotoxy(69,1);
printf("--------------------");
gotoxy(69,29);
printf("--------------------");
gotoxy(75,3);
printf("模 式");
gotoxy(75,20);
printf("提 示");
}
void red_movexy()
{
loop2:gotoxy(direct[0],direct[1]);
char key='y';
int temp;
while(key!=' ')
{
key=getch();
switch(key)
{
case 'W':
case 'w':
direct[1]-=2;
if(direct[1]<=1)
direct[1]=1;
break;
case 's':
case 'S':
direct[1]+=2;
if(direct[1]>=29)
direct[1]=29;
break;
case 'a':
case 'A':
direct[0]-=4;
if(direct[0]<=2)
direct[0]=2;
break;
case 'd':
case 'D':
direct[0]+=4;
if(direct[0]>=58)
direct[0]=58;
break;
case 'q':
case 'Q':
{
int message=MessageBox(NULL,"是否确定悔棋?","友情提示",MB_OKCANCEL);
if(IDCANCEL==message)
break;
if(IDOK==message)
{
Regret();
break;
}
}
}
gotoxy(direct[0],direct[1]);
}
temp=judge_chess(direct[1],direct[0]);
if(temp==1)
{
gotoxy(70,22);
BackGround(4, 0);
printf("这里已经被人下过了");
goto loop2;
}
}
void white_movexy()
{
loop1:gotoxy(direct[0],direct[1]);
char key='y';
int temp;
while(key!='0')
{
key=getch();
switch(key)
{
case 72:
direct[1]-=2;
if(direct[1]<=1)
direct[1]=1;
break;
case 80:
direct[1]+=2;
if(direct[1]>=29)
direct[1]=29;
break;
case 75:
direct[0]-=4;
if(direct[0]<=2)
direct[0]=2;
break;
case 77:
direct[0]+=4;
if(direct[0]>=58)
direct[0]=58;
break;
case 'B':
case 'b':
{
int message=MessageBox(NULL,"是否确定悔棋?","友情提示",MB_OKCANCEL);
if(IDCANCEL==message)
break;
if(IDOK==message)
{
Regret();
break;
}
}
}
gotoxy(direct[0],direct[1]);
}
temp=judge_chess(direct[1],direct[0]);
if(temp==1)
{
gotoxy(70,22);
BackGround(4, 0);
printf("这里已经被人下过了");
goto loop1;
}
}
void red_chess(int x,int y)
{
BackGround(4,0);
regrex=x;
regrey=y;
count++;
printf("●");
status[x][y]=1;
}
void white_chess(int x,int y)
{
BackGround(7,0);
regrex1=x;
regrey1=y;
printf("●");
count++;
status[x][y]=2;
}
void machine_chess(int x,int y)
{
BackGround(7,0);
status[x][y]=2;
regrex1=x;
regrey1=y;
count++;
gotoxy(y,x);
printf("●");
}
int judge_chess(int x,int y)
{
if(status[x][y]==0)
return 0;
else
return 1;
}
int judge_winner(int x,int y,int temp)
{
int i,j,n1,n2;
n1=n2=0;
for(i=x,j=y+4;j<=58;j+=4)
{
if(status[i][j]==temp)
n1++;
else
break;
}
for(i=x,j=y;j>=2;j-=4)
{
if(status[i][j]==temp)
n2++;
else
break;
}
if(n1+n2>=5)
return temp;
n1=n2=0;
for(i=x,j=y;i>=1;i-=2)
{
if(status[i][j]==temp)
n1++;
else
break;
}
for(i=x+2,j=y;i<=30;i+=2)
{
if(status[i][j]==temp)
n2++;
else
break;
}
if(n1+n2>=5)
return temp;
n1=n2=0;
for(i=x-2,j=y+4;i>=1&&j<=58;i-=2,j+=4)
{
if(status[i][j]==temp)
n1++;
else
break;
}
for(i=x,j=y;i<=30&&j>=2;i+=2,j-=4)
{
if(status[i][j]==temp)
n2++;
else
break;
}
if(n1+n2>=5)
return temp;
n1=n2=0;
for(i=x,j=y;i>=0&&j>=0;i-=2,j-=4)
{
if(status[i][j]==temp)
n1++;
else
break;
}
for(i=x+2,j=y+4;i<=30&&j<=58;i+=2,j+=4)
{
if(status[i][j]==temp)
n2++;
else
break;
}
if(n1+n2>=5)
return temp;
return 0;
}
void machine_attack()
{
int i1,j1;
int k1,k2,k;
for(int i=1;i<=30;i+=2)
{
for(int j=2;j<=58;j+=4)
{
if(status[i][j])
Value1[i][j]=0;
if(status[i][j]==0)
{
k1=k2=0;
for(i1=i,j1=j-4;j1>=2;j1-=4)
{
if(status[i1][j1]==2)
k1++;
else
break;
}
for(i1=i,j1=j+4;j1<=58;j1+=4)
{
if(status[i1][j1]==2)
k2++;
else
break;
}
k=k1>k2? k1:k2;
k1=k2=0;
for(i1=i-2,j1=j;i1>=1;i1-=2)
{
if(status[i1][j1]==2)
k1++;
else
break;
}
for(i1=i+2,j1=j;i1<=30;i1+=2)
{
if(status[i1][j1]==2)
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1? k:k1;
k1=k2=0;
for(i1=i-2,j1=j-4;i1>=0&&j1>=0;i1-=2,j1-=4)
{
if(status[i1][j1]==2)
k1++;
else
break;
}
for(i1=i+2,j1=j+4;i1<=30&&j1<=58;i1+=2,j1+=4)
{
if(status[i1][j1]==2 )
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1?k:k1;
k1=k2=0;
for(i1=i+2,j1=j-4;i1<=30&&j1>=2;i1+=2,j1-=4)
{
if(status[i1][j1]==2)
k1++;
else
break;
}
for(i1=i-2,j1=j+4;i1>=1&&j1<=58;i1-=2,j1+=4)
{
if(status[i1][j1]==2)
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1?k:k1;
switch(k)
{
case 3:
Value1[i][j]=15;break;
case 4:
Value1[i][j]=25;break;
default:
Value1[i][j]=3+2*k;break;
}
}
}
}
}
void machine_defend()
{
int i1, j1;
int k1,k2,k;
for(int i=1;i<=30;i+=2)
{
for(int j=2;j<=58;j+=4)
{
if(status[i][j])
Value2[i][j]=0;
if(status[i][j]==0)
{
k1=k2=0;
for(i1=i,j1=j-4;j1>=2;j1-=4)
{
if(status[i1][j1]==1)
k1++;
else
break;
}
for(i1=i,j1=j+4;j1<=58;j1+=4)
{
if(status[i1][j1]==1)
k2++;
else
break;
}
k=k1>k2? k1:k2;
k1=k2=0;
for(i1=i-2,j1=j;i1>=1;i1-=2)
{
if(status[i1][j1]==1)
k1++;
else
break;
}
for(i1=i+2,j1=j;i1<=30;i1+=2)
{
if(status[i1][j1]==1)
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1?k:k1;
k1=k2=0;
for(i1=i-2,j1=j-4;i1>=1&&j1>=2;i1-=2,j1-=4)
{
if(status[i1][j1]==1)
k1++;
else
break;
}
for(i1=i+2,j1=j+4;i1<=30&&j1<=58;i1+=2,j1+=4)
{
if(status[i1][j1]==1)
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1?k:k1;
k1=k2=0;
for(i1=i+2,j1=j-4;i1<=30&&j1>=2;i1+=2,j1-=4)
{
if(status[i1][j1]==1)
k1++;
else
break;
}
for(i1=i-2,j1=j+4;i1>=1&&j1<=58;i1-=2,j1+=4)
{
if(status[i1][j1]==1)
k2++;
else
break;
}
k1=k1>k2? k1:k2;
k=k>k1?k:k1;
switch(k)
{
case 3:
Value2[i][j]=10;break;
case 4:
Value2[i][j]=20;break;
default:
Value2[i][j]=2+k*2;
}
}
}
}
}
void find_position()
{
int k1=0, k2=0;
int i, j, max=0;
for( i=1;i<=30;i+=2)
for( j=2;j<=58;j+=4)
{
if(max<=Value1[i][j])
{
max=Value1[i][j];
k1=i;
k2=j;
}
}
for( i=1;i<=30;i+=2)
for( j=2;j<=58;j+=4)
{
if(max<=Value2[i][j])
{
max=Value2[i][j];
k1=i;
k2=j;
}
}
direct[1]=k1;
direct[0]=k2;
}
void man_man()
{
loop5:system("cls");
char key;
int control;
gotoxy(2, 3);
printf("1.红 子 先 手");
gotoxy(2, 5);
printf("2.白 子 先 手");
gotoxy(2, 7);
printf("(输入相应序号选择)");
key=getch();
system("cls");
if(key=='1')
control=1;
else if(key=='2')
control=-1;
else
goto loop5;
gotoxy(70,5);
printf(" 人 人 对 战 ");
direct[1]=15;
direct[0]=30;
chess_board();
chess_menu();
while(flag==0)
{
if(control==1)
{
gotoxy(70,22);
BackGround(6,0);
printf(" 红 子 执 手 ");
red_movexy();
red_chess(direct[1],direct[0]);
flag=judge_winner(direct[1],direct[0],1);
}
else
{
gotoxy(70,22);
BackGround(6,0);
printf(" 白 子 执 手 ");
white_movexy();
white_chess(direct[1],direct[0]);
flag=judge_winner(direct[1],direct[0],2);
}
control=-control;
}
if(flag==1)
{
BackGround(7,0);
MessageBox(NULL,"游戏结束,红子胜利","五子棋游戏",MB_OK);
}
if(flag==2)
{
MessageBox(NULL,"游戏结束,白子胜利","五子棋游戏",MB_OK);
}
if(count>=225)
{
MessageBox(NULL,"游戏结束,平局","五子棋游戏",MB_OK);
}
}
void man_machine()
{
loop6:system("cls");
char key;
int control;
gotoxy(2, 3);
printf("1.玩 家 先 手(玩家为红子)");
gotoxy(2, 5);
printf("2.电 脑 先 手(电脑为白子)");
gotoxy(2, 7);
printf("(输入相应序号选择)");
key=getch();
system("cls");
if(key=='1')
control=1;
else if(key=='2')
{
control=1;
machine_chess(13,26);
}
else
goto loop6;
gotoxy(70,5);
printf(" 人 机 对 战 ");
direct[1]=15;
direct[0]=30;
chess_board();
chess_menu();
while(flag==0)
{
if(control==1)
{
gotoxy(70,22);
BackGround(6,0);
printf(" 玩 家 执 手 ");
red_movexy();
red_chess(direct[1],direct[0]);
flag=judge_winner(direct[1],direct[0],1);
}
else
{
gotoxy(70,22);
BackGround(6,0);
printf(" 电 脑 执 手 ");
machine_defend();
machine_attack();
find_position();
machine_chess(direct[1],direct[0]);
flag=judge_winner(direct[1],direct[0],2);
}
control=-control;
}
gotoxy(8,18);
if(flag==1)
{
BackGround(7,0);
MessageBox(NULL,"太厉害了,您竟然战胜了电脑!","五子棋游戏",MB_OK);
}
if(flag==2)
{
MessageBox(NULL,"游戏结束,您输给了电脑","五子棋游戏",MB_OK);
}
if(count>=225)
{
MessageBox(NULL,"平局","五子棋游戏",MB_OK);
}
}
void Regret()
{
gotoxy(regrey,regrex);
BackGround(0,0);
printf(" ");
status[regrex][regrey]=0;
gotoxy(regrey1,regrex1);
BackGround(0,0);
printf(" ");
status[regrex1][regrey1]=0;
count-=2;
}
void welcome()
{
int k;
char choose;
system("cls");
for(k=2;k<=16;k+=2)
{
gotoxy(5,k);
printf("|-----------------|");
}
gotoxy(5, 3);
printf("| 五 子 棋 游 戏 |");
gotoxy(5, 5);
printf("| 菜 单 |");
gotoxy(5, 7);
printf("| 1.人 人 对 战 |");
gotoxy(5, 9);
printf("| 2.人 机 对 战 |");
gotoxy(5, 11);
printf("| 3.游 戏 帮 助 |");
gotoxy(5, 13);
printf("| 4.更 新 日 志 |");
gotoxy(5, 15);
printf("| 5.退 出 游 戏 |");
gotoxy(5, 18);
printf("温馨提示:输入菜单对应序号进行操作");
gotoxy(5, 20);
printf("祝您游戏愉快!");
gotoxy(13, 20);
}
char Gametips()
{
char choose;
int key;
system("cls");
gotoxy(2, 3);
printf("游戏操作:");
gotoxy(4, 5);
printf("① 红色棋子WASD移动光标选择下棋位置,按空格键确认,按Q悔棋");
gotoxy(4, 7);
printf("② 白色棋子↑↓←→移动光标选择下棋位置,按0确认,按B悔棋");
gotoxy(2, 19);
printf("(按任意键返回)");
return getch();
}
char Updatediary()
{
system("cls");
gotoxy(2, 3);
printf("(暂时没有)");
gotoxy(2, 5);
printf("(按任意键返回)");
return getch();
}
int main()
{
system("title 五子棋");
system("mode con cols=92 lines=33");
char choose,temp;
thetop: loop:welcome();
choose=getch();
switch(choose)
{
case '1':
man_man();
goto thetop;
break;
case '2':
man_machine();
goto thetop;
break;
case '3':
temp=Gametips();
goto thetop;
break;
case '4':
temp=Updatediary();
goto thetop;
break;
case '5':
int message=MessageBox(NULL,"是否退出?","友情提示",MB_OKCANCEL);
if(IDCANCEL==message)
goto thetop;
if(IDOK==message)
{
break;
}
}
}
2、好难操作的贪吃蛇
贪吃蛇的基本框架还是有的。
文件一:stdafx.h
#if !defined(AFX_STDAFX_H__20B660F3_64B9_4993_8071_269F806FAF71__INCLUDED_)
#define AFX_STDAFX_H__20B660F3_64B9_4993_8071_269F806FAF71__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
代码二:主程序
#include "stdafx.h"
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#define U 1
#define D 2
#define L 3
#define R 4
typedef struct snake
{
int x;
int y;
struct snake *next;
}snake;
int score=0;
int add=10;
int HighScore = 0;
int status;
int sleeptime=200;
snake *head;
snake *food;
snake *q;
int endgamestatus=0;
HANDLE hOut;
void gotoxy(int x,int y);
int color(int c);
void printsnake();
void welcometogame();
void createMap();
void scoreandtips();
void initsnake();
void createfood();
int biteself();
void cantcrosswall();
void speedup();
void speeddown();
void snakemove();
void keyboardControl();
void Lostdraw();
void endgame();
void choose();
void File_out();
void File_in();
void explation();
void gotoxy(int x,int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}
void printsnake()
{
color(2);
printf(" \n");
printf(" __________ ___ \n");
printf(" / \\ / \\ \\ |____ __\\__ \n");
printf(" / ________ \\ / ___ \\ _/ __ | | / \n");
printf(" | | |__| _/_ |_| / [|] |/ \n");
printf(" | | | | | / _|_ \\__/ \n");
printf(" \\ \\_______ / \\ |___/ ____ \n");
printf(" \\ \\ ____ ____ ____ __ | | ___ ______ \n");
printf(" \\_______ \\ | |/ \\ / \\_/ / | | / / / \\ \n");
printf(" \\ \\ | ___ \\ / ____ / | |/ / / ____ \\ \n");
printf(" __ | | | / \\ \\ | | | / | / | /____\\ | \n");
printf(" \\ \\_______| | | | | | | |__| | | \\ | ________/ \n");
printf(" \\ / | | | | \\ \\ | |\\ \\ \\ \\____ \n");
printf(" \\__________/ |__| |__| \\___/\\__\\ |__| \\__\\ \\______/ \n");
}
void welcometogame()
{
int n;
int i,j = 1;
gotoxy(43,18);
color(11);
printf("贪 吃 蛇 游 戏");
color(14);
for (i = 20; i <= 26; i++)
{
for (j = 27; j <= 74; j++)
{
gotoxy(j, i);
if (i == 20 || i == 26)
{
printf("-");
}
else if (j == 27 || j == 74)
{
printf("|");
}
}
}
color(10);
gotoxy(35, 22);
printf("1.开始游戏");
gotoxy(55, 22);
printf("2.游戏说明");
gotoxy(35, 24);
printf("3.退出游戏");
gotoxy(29,27);
color(3);
printf("请选择[1 2 3]:[ ]\b\b");
color(14);
scanf("%d", &n);
switch (n)
{
case 1:
system("cls");
createMap();
initsnake();
createfood();
keyboardControl();
break;
case 2:
explation();
break;
case 3:
exit(0);
break;
default:
color(12);
gotoxy(40,28);
printf("请输入1~3之间的数!");
getch();
system("cls");
printsnake();
welcometogame();
}
}
void createMap()
{
int i,j;
for(i=0;i<58;i+=2)
{
gotoxy(i,0);
color(6);
printf("□");
gotoxy(i,26);
printf("□");
}
for(i=1;i<26;i++)
{
gotoxy(0,i);
printf("□");
gotoxy(56,i);
printf("□");
}
for(i = 2;i<56;i+=2)
{
for(j = 1;j<26;j++)
{
gotoxy(i,j);
color(3);
printf("■\n\n");
}
}
}
void scoreandtips()
{
File_out();
gotoxy(64,4);
color(11);
printf("☆最高记录☆:%d",HighScore);
gotoxy(64,8);
color(14);
printf("当前得分:%d ",score);
color(15);
gotoxy(73,11);
printf("小 提 示");
gotoxy(60,13);
color(6);
printf("╬ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ ╬");
gotoxy(60,25);
printf("╬ ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ ╬");
color(3);
gotoxy(64,14);
printf("每个食物得分:%d分",add);
gotoxy(64,16);
printf("不能撞墙,不能咬到自己");
gotoxy(64,18);
printf("用↑ ↓ ← →分别控制蛇的移动");
gotoxy(64,20);
printf("F1键加速,F2键减速");
gotoxy(64,22);
printf("空格键暂停游戏");
gotoxy(64,24);
printf("Esc键退出游戏");
}
void initsnake()
{
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1;i<=4;i++)
{
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail!=NULL)
{
gotoxy(tail->x,tail->y);
color(14);
printf("◆");
tail=tail->next;
}
}
void createfood()
{
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
while((food_1->x%2)!=0)
{
food_1->x=rand()%52+2;
}
food_1->y=rand()%24+1;
q=head;
while(q->next==NULL)
{
if(q->x==food_1->x && q->y==food_1->y)
{
free(food_1);
createfood();
}
q=q->next;
}
gotoxy(food_1->x,food_1->y);
food=food_1;
color(12);
printf("●");
}
int biteself()
{
snake *self;
self=head->next;
while(self!=NULL)
{
if(self->x==head->x && self->y==head->y)
{
return 1;
}
self=self->next;
}
return 0;
}
void cantcrosswall()
{
if(head->x==0 || head->x==56 ||head->y==0 || head->y==26)
{
endgamestatus=1;
endgame();
}
}
void speedup()
{
if(sleeptime>=50)
{
sleeptime=sleeptime-10;
add=add+2;
}
}
void speeddown()
{
if(sleeptime<350)
{
sleeptime=sleeptime+30;
add=add-2;
}
}
void snakemove()
{
snake * nexthead;
cantcrosswall();
nexthead=(snake*)malloc(sizeof(snake));
if(status==U)
{
nexthead->x=head->x;
nexthead->y=head->y-1;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
score=score+add;
speedup();
createfood();
}
else
{
while(q->next->next!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==D)
{
nexthead->x=head->x;
nexthead->y=head->y+1;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
score=score+add;
speedup();
createfood();
}
else
{
while(q->next->next!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==L)
{
nexthead->x=head->x-2;
nexthead->y=head->y;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
score=score+add;
speedup();
createfood();
}
else
{
while(q->next->next!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==R)
{
nexthead->x=head->x+2;
nexthead->y=head->y;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
score=score+add;
speedup();
createfood();
}
else
{
while(q->next->next!=NULL)
{
gotoxy(q->x,q->y);
color(14);
printf("◆");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(biteself()==1)
{
endgamestatus=2;
endgame();
}
}
void keyboardControl()
{
status=R;
while(1)
{
scoreandtips();
if(GetAsyncKeyState(VK_UP) && status!=D)
{
status=U;
}
else if(GetAsyncKeyState(VK_DOWN) && status!=U)
{
status=D;
}
else if(GetAsyncKeyState(VK_LEFT)&& status!=R)
{
status=L;
}
else if(GetAsyncKeyState(VK_RIGHT)&& status!=L)
{
status=R;
}
if(GetAsyncKeyState(VK_SPACE))
{
while(1)
{
Sleep(300);
if(GetAsyncKeyState(VK_SPACE))
{
break;
}
}
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
endgamestatus=3;
break;
}
else if(GetAsyncKeyState(VK_F1))
{
speedup();
}
else if(GetAsyncKeyState(VK_F2))
{
speeddown();
}
Sleep(sleeptime);
snakemove();
}
}
void File_in()
{
FILE *fp;
fp = fopen("save.txt", "w+");
fprintf(fp, "%d", score);
fclose(fp);
}
void File_out()
{
FILE *fp;
fp = fopen("save.txt", "a+");
fscanf(fp, "%d", &HighScore);
fclose(fp);
}
void explation()
{
int i,j = 1;
system("cls");
color(15);
gotoxy(44,3);
printf("游戏说明");
color(2);
for (i = 6; i <= 22; i++)
{
for (j = 20; j <= 76; j++)
{
gotoxy(j, i);
if (i == 6 || i == 22) printf("=");
else if (j == 20 || j == 75) printf("||");
}
}
color(3);
gotoxy(30,8);
printf("tip1: 不能撞墙,不能咬到自己");
color(10);
gotoxy(30,11);
printf("tip2: 用↑.↓.←.→分别控制蛇的移动");
color(14);
gotoxy(30,14);
printf("tip3: F1 为加速,F2 为减速");
color(11);
gotoxy(30,17);
printf("tip4: 按空格键暂停游戏,再按空格键继续");
color(4);
gotoxy(30,20);
printf("tip5: Esc :退出游戏");
getch();
system("cls");
printsnake();
welcometogame();
}
void endgame()
{
system("cls");
if(endgamestatus==1)
{
Lostdraw();
gotoxy(35,9);
color(7);
printf("对不起,您撞到墙了。游戏结束!");
}
else if(endgamestatus==2)
{
Lostdraw();
gotoxy(35,9);
color(7);
printf("对不起,您咬到自己了。游戏结束!");
}
else if(endgamestatus==3)
{
Lostdraw();
gotoxy(40,9);
color(7);
printf("您已经结束了游戏。");
}
gotoxy(43,12);
color(14);
printf("您的得分是 %d",score);
if(score >= HighScore)
{
color(10);
gotoxy(33,16);
printf("创纪录啦!最高分被你刷新啦,真棒!!!");
File_in();
}
else
{
color(10);
gotoxy(33,16);
printf("继续努力吧~ 你离最高分还差:%d",HighScore-score);
}
choose();
}
void choose()
{
int n;
gotoxy(30,23);
color(12);
printf("重玩一局 [1]");
gotoxy(55,23);
printf("溜了溜了 [2]");
gotoxy(45,25);
color(11);
printf("选择:");
scanf("%d", &n);
switch (n)
{
case 1:
system("cls");
score=0;
sleeptime=200;
add = 10;
printsnake();
welcometogame();
break;
case 2:
exit(0);
break;
default:
gotoxy(35,27);
color(12);
printf("※※您的输入有误,请重新输入※※");
system("pause >nul");
endgame();
choose();
break;
}
}
void Lostdraw()
{
system("cls");
int i;
gotoxy(45,1);
color(6);
printf(" |-----| ");
gotoxy(45,2);
color(6);
printf(" | | ");
gotoxy(43,3);
color(6);
printf("-------------");
gotoxy(44,4);
color(14);
printf("(");
gotoxy(47,4);
color(15);
printf(" > <");
gotoxy(54,4);
color(14);
printf(")");
gotoxy(17,5);
color(11);
printf("+------------------------");
gotoxy(35,5);
color(14);
printf("oOOo");
gotoxy(39,5);
color(11);
printf("----------");
gotoxy(48,5);
color(14);
printf("| |");
gotoxy(48,6);
color(14);
printf("|_|");
gotoxy(51,5);
color(11);
printf("----------");
gotoxy(61,5);
color(14);
printf("oOOo");
gotoxy(65,5);
color(11);
printf("-----------------+");
for(i = 6;i<=19;i++)
{
gotoxy(17,i);
printf("|");
gotoxy(82,i);
printf("|");
}
gotoxy(17,20);
printf("+------------------------------------------");
gotoxy(60,20);
color(11);
printf("----------------------+");
}
int main(int argc, char* argv[])
{
system("mode con cols=110 lines=30");
printsnake();
welcometogame();
endgame();
return 0;
}
3、简单到炸的自制迷宫
本程序中的迷宫及其简单,你可以尝试修改地图,增加难度。
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int main()
{
char a[1000][1000] = {"##########",
"#o # # ",
"# ## ## ##",
"# # ##",
"## ## ##",
"##########" };
int i, x = 1, y = 1;
int count=0;
printf("\n\n\t请不要使用中文输入法!\n\t操作方式:上下左右=WSAD\n\t o 代表你操作的人物, # 代表墙体。\n\t开始你的游戏吧!\n\n\n");
for (i = 0; i < 11; i++)
puts(a[i]);
char ch;
while (x != 1|| y != 9)
{
ch = _getch();
count++;
if (ch == 's')
{
if (a[x + 1][y] != '#')
{
a[x][y] = ' ';
x++;
a[x][y] = 'o';
}
}
if (ch == 'w')
{
if (a[x - 1][y] != '#')
{
a[x][y] = ' ';
x--;
a[x][y] = 'o';
}
}
if (ch == 'a')
{
if (a[x][y - 1] != '#')
{
a[x][y] = ' ';
y--;
a[x][y] = 'o';
}
}
if (ch == 'd')
{
if (a[x][y + 1] != '#')
{
a[x][y] = ' ';
y++;
a[x][y] = 'o';
}
}
system("cls");
if (x == 1 && y == 9)
printf("成功过关\n");
for (i = 0; i < 6; i++)
puts(a[i]);
}
printf("你一共走了%d步\n", count);
Sleep(5000);
return 0;
}
4、不忍直视的双人飞机对战
芜湖,我愿称之为无与伦比。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define High 20
#define Width 100
int position_x,position_y,p_x,p_y,turn_a,turn_b,num_a,num_b,num_max,life_a = 10,life_b = 10;
int canvas[High][Width] = {0};
int next[8][2] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int bullet_a[21][4];
int bullet_b[21][4];
void gotoxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
num_a = 0;
num_b = 0;
turn_a = 0;
turn_b = 0;
p_x = High/2;
p_y = Width* 4 / 5;
canvas[p_x][p_y] = 5;
position_x = High/2;
position_y = Width/5;
canvas[position_x][position_y] = 1;
}
void show()
{
gotoxy(0,0);
int i,j;
for (i=0;i<High;i++)
{
for (j=0;j<Width;j++)
{
if( i == 0 || i == High -1 || j == 0 || j == Width -1){
canvas[i][j] = 4;
printf("0");
continue;
}
if (canvas[i][j]==0)
printf(" ");
else if (canvas[i][j]==1)
printf("A");
else if (canvas[i][j]==2)
printf("B");
else if (canvas[i][j]==3)
printf("o");
else if (canvas[i][j]==4)
printf("+");
else if (canvas[i][j]==5)
printf("+");
}
printf("\n");
}
printf("A:");
for( i = 1; i <= 10; i++ )
if( i <= life_a)
printf("■");
else printf(" ");
printf("\nB: ");
for( i = 1; i <= 10; i++ )
if( i <= life_b)
printf("■");
else printf(" ");
}
void updateWithoutInput()
{
int i,j,k;
num_max = num_a > num_b? num_a : num_b;
for( i = 1; i <= num_max; i++){
if( bullet_a[i][2] == 0 || bullet_a[i][2] == High - 1){
bullet_a[i][0] = -bullet_a[i][0];
}
else if( bullet_a[i][3] == 0 || bullet_a[i][3] == Width - 1){
bullet_a[i][1] = -bullet_a[i][1];
}
if( bullet_b[i][2] == 0 || bullet_b[i][2] == High - 1){
bullet_b[i][0] = -bullet_b[i][0];
}
else if( bullet_b[i][3] == 0 || bullet_b[i][3] == Width - 1){
bullet_b[i][1] = -bullet_b[i][1];
}
canvas[ bullet_a[i][2] ][ bullet_a[i][3] ] = 0;
canvas[ bullet_b[i][2] ][ bullet_b[i][3] ] = 0;
bullet_a[i][2] += bullet_a[i][0];
bullet_a[i][3] += bullet_a[i][1];
bullet_b[i][2] += bullet_b[i][0];
bullet_b[i][3] += bullet_b[i][1];
canvas[ bullet_a[i][2] ][ bullet_a[i][3] ] = 3;
canvas[ bullet_b[i][2] ][ bullet_b[i][3] ] = 3;
}
for (k=1;k<=num_max;k++)
{
if (((position_x==bullet_a[k][2]) && (position_y==bullet_a[k][3]))||((position_x==bullet_b[k][2]) && (position_y==bullet_b[k][3])))
{
life_a--;
if( life_a <= 0){
printf("A 玩家失败!\n");
Sleep(3000);
system("pause");
exit(0);
}
}
if (((p_x==bullet_a[k][2]) && (p_y==bullet_a[k][3]))||((p_x==bullet_b[k][2]) && (p_y==bullet_b[k][3])))
{
life_b--;
if( life_b <= 0){
printf("B 玩家失败!\n");
Sleep(3000);
system("pause");
exit(0);
}
}
}
}
void updateWithInput()
{
char input;
if(kbhit())
{
input = getch();
if (input == 'a' && position_y>1)
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
canvas[position_x][position_y] = 0;
position_y--;
canvas[position_x][position_y] = 1;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == 'd' && position_y<Width-2)
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
canvas[position_x][position_y] = 0;
position_y++;
canvas[position_x][position_y] = 1;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == 'w' && position_x > 1)
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
canvas[position_x][position_y] = 0;
position_x--;
canvas[position_x][position_y] = 1;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == 's'&& position_x < High - 2)
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
canvas[position_x][position_y] = 0;
position_x++;
canvas[position_x][position_y] = 1;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == ' ' && num_a <= 20)
{
num_a++;
bullet_a[num_a][0] = next[turn_a][0];
bullet_a[num_a][1] = next[turn_a][1];
bullet_a[num_a][2] = position_x + bullet_a[num_a][0];
bullet_a[num_a][3] = position_y + bullet_a[num_a][1];
canvas[ bullet_a[num_a][2] ][ bullet_a[num_a][3] ] = 3;
}
else if (input == 'q')
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
turn_a--;
if(turn_a < 0)
turn_a = 7;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == 'e')
{
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 0;
turn_a++;
if(turn_a > 7)
turn_a = 0;
canvas[position_x + next[turn_a][0]][position_y + next[turn_a][1]] = 4;
}
else if (input == '4' && position_y>1)
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
canvas[p_x][p_y] = 0;
p_y--;
canvas[p_x][p_y] = 2;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
else if (input == '6' && p_y<Width-2)
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
canvas[p_x][p_y] = 0;
p_y++;
canvas[p_x][p_y] = 2;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
else if (input == '8' && p_x > 1)
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
canvas[p_x][p_y] = 0;
p_x--;
canvas[p_x][p_y] = 2;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
else if (input == '5' && p_x < High - 2)
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
canvas[p_x][p_y] = 0;
p_x++;
canvas[p_x][p_y] = 2;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
else if (input == '0' && num_b <= 20)
{
num_b++;
bullet_b[num_b][0] = next[turn_b][0];
bullet_b[num_b][1] = next[turn_b][1];
bullet_b[num_b][2] = p_x + bullet_b[num_b][0];
bullet_b[num_b][3] = p_y + bullet_b[num_b][1];
canvas[ bullet_b[num_b][2] ][ bullet_b[num_b][3] ] = 3;
}
else if (input == '7')
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
turn_b--;
if(turn_b < 0)
turn_b = 7;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
else if (input == '9')
{
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 0;
turn_b++;
if(turn_b > 7)
turn_b = 0;
canvas[p_x + next[turn_b][0]][p_y + next[turn_b][1]] = 5;
}
}
}
int main()
{
startup();
system("color 30");
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
|