用vc或c++编写的一个小游戏代码,要有源代码,
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}
void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}
switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
贪吃蛇
毕竟控制台程序的这样类型的代码都值至少50分,更何况是可视化程序。
推荐楼主直接在度娘搜索吧,这样可以找到别人免费提供的用于教学的游戏代码,或者上知名的IT论坛也可以。
祝愿楼主能早日找到源代码。
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<cstdio>
#include<time.h>
using namespace std;
int x=0,y=0,v=0;
char a[30][15],d;
int n=100,ap=2,ad=1,sum=0;
void pan(int i,int j)
{
if(a[i][j]=='1')
{
int s1=5;
s1-=ap+sum/10;
while(s1>0)
{ if((4-(ad+sum/30))>0)
n-=(4-(ad+sum/30));
s1-=ap+sum/10;
s1++;
}
sum++;
}
else if(a[i][j]=='2')
{
int s2=30;
s2-=ap+sum/10-1;
while(s2>0)
{
if((6-(ad+sum/30))>0)
n-=(6-(ad+sum/30));
s2-=ap+sum/10-1;
s2+=2;
}
sum+=2;
}
else if(a[i][j]=='3')
{
int s3=60;
s3-=ap+sum/10-5;
while(s3>0)
{
if((10-(ad+sum/30))>0)
n-=(10-(ad+sum/30));
s3-=ap+sum/10-5;
s3+=5;
}
sum+=5;
}
else if(a[i][j]=='4')
{
int s4=60;
s4-=ap+sum/10-3;
s4++;
while(s4>0)
{
if((20-(ad+sum/30))>0)
n-=(20-(ad+sum/30));
s4-=ap+sum/10-3;
s4+=10;
}
sum+=4;
}
else if(a[i][j]=='5')
{
int s5=300;
s5-=ap+sum/10-2;
while(s5>0)
{
if((23-(ad+sum/30))>0)
n-=(23-(ad+sum/30));
s5-=ap+sum/10-2;
s5+=20;
}
sum+=5;
}
else if(a[i][j]=='6')
{
int s6=400;
s6-=ap+sum/10-20;
while(s6>0)
{
if((25-(ad+sum/30))>0)
n-=(25-(ad+sum/30));
n-=2;
s6-=ap+sum/10-20;
s6+=10;
}
sum+=5;
}
else
{
if(a[x][y]=='A')
ap++;
if(a[x][y]=='H')
n+=15;
}
}
void place(char shu)
{
a[x][y]=' ';
if(shu=='w')
y--;
else if(shu=='a')
x--;
else if(shu=='d')
x++;
else if(shu=='s')
y++;
else if(shu=='q'&&(ad+sum/30)>0)
{
ap+=4;
ad--;
n-=30;
}
else if(shu=='e'&&(ap+sum/10)>0)
{
ad+=1;
ap-=6;
n+=20;
}
if(x<0)
x++;
if(x>29)
x--;
if(y<0)
y++;
if(y>14)
y--;
pan(x,y);
a[x][y]='#';
}
int main(){
printf(" 数字魔塔\n");
scanf("%c",&d);
printf(" 你是#勇者 \n 你需要闯出去\n 出口为E\n 按下回车继续");
srand(time(0));
for(int x1=0;x1<30;x1++)
for(int x2=0;x2<15;x2++)
{
a[x1][x2]=rand()%30;
if(a[x1][x2]==0)
a[x1][x2]='A';
else if(a[x1][x2]==2)
a[x1][x2]='H';
else if(a[x1][x2]<9)
a[x1][x2]='1';
else if(a[x1][x2]<13)
a[x1][x2]='2';
else if(a[x1][x2]<15)
a[x1][x2]='3';
else if(a[x1][x2]<17)
a[x1][x2]='4';
else if(a[x1][x2]<18)
a[x1][x2]='5';
else
{
a[x1][x2]=' ';
}
}
char shu;
a[29][14]='E';a[28][14]='6';a[29][13]='6';a[27][14]='6';a[29][12]='6';a[28][13]='6';a[0][1]=' ';a[1][0]=' ';
while(1)
{
shu=getch();
system("cls");
if(n<=0||n>1000)
{
printf("\n\n\n\n Game Over\n 得分:%d\n\n\n\n\n",sum);
return 0;
}
v=n;
place(shu);
if(x==29&&y==14)
{
printf("\n\n\n\n\ 恭喜过关\n 得分:%d\n\n\n\n\n",sum);
return 0;
}
printf(" 请按下shift键开始\n wasd方向键\n q键和e键攻防转换(每次转换需要消耗30点生命值.)\n1,2,3,4,5,6都是不同等级的怪物\n\n");
printf("# 生命:%d 攻:%d 防:%d 等级:%d\n",n,ap+sum/10,ad+sum/30,sum/10);
for(int i=0;i<15;i++){
for(int j=0;j<30;j++){
printf("%c ",a[j][i]);
}
printf("\n");
}
printf("1:命:5 攻:4 防:0 回:1 A增加一点攻击力\n2:命:30 攻:6 防:1 回:2 H增加15点生命值\n3:命:60 攻:10防:5 回:5 \n4:命:60 攻:20防:3 回:10\n5:命:300攻:23防:2 回:20 \n6:命:400攻:25防:20回:10真:2");
if(v-n>0)
printf("\n损失%d点生命值\n",v-n);
}
}
//就随便写了一个
2019-04-17
太大方不下,放个文字游戏吧
反应太快。。
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cstring>
#include<windows.h>
using namespace std;
int i,H[3],S[3],K[3],p=1,Y,C;
string P[3];
bool game=1;
void a(int i){
for (;i;i--);
}
void help(){
cout<<"杀手:\nHP:100\nSP:5\nkill:3\n";
cout<<"医生:\nHP:75\nSP:7\nkill:2\n";
cout<<"法师:\nHP:144\nSP:3\nkill:1\n";
cin>>i;
}
void killer(){
P[p]+="杀手";
H[p]=100;
S[p]=5;
K[p]=3;
}
void doctor(){
P[p]+="医生";
H[p]=75;
S[p]=7;
K[p]=2;
}
void engineer(){
P[p]+="法师";
H[p]=144;
S[p]=3;
K[p]=1;
}
void e(int j){
if (j==2&&S[Y]>=2) {
int w=rand()%2;
if (Y==1) cout<<"你使用奖励\n";
else cout<<"电脑使用奖励\n";
K[Y]+=w;
cout<<"杀人数+"<<w<<endl;
w=rand()%5-1;
w=int(1.2*(K[Y]+w));
H[C]-=w;
if (H[C]<=0) game=0;
if (Y==1) cout<<"你杀了电脑 ";
else cout<<"你被电脑杀了 ";
cout<<w;
if (Y==1) cout<<endl;
S[Y]-=2;
}
}
void k(int j){
if (j==2&&S[Y]>=2) {
if (Y==1) cout<<"YOU 使用奖励\n";
else cout<<"The computer 使用奖励\n";
int w=rand()%4+1;
H[C]-=int(1.5*K[Y]+w);
if (H[C]<=0) game=0;
if (Y==1) cout<<"杀 ";
else cout<<"你被电脑杀了 ";
cout<<int(1.5*K[Y]+w);
if (Y==1) cout<<endl;
S[Y]-=2;
}
}
void d(int j){
if (j==2&&S[Y]>=2) {
if (Y==1) cout<<"YOU 使用奖励\n";
else cout<<"The computer 使用奖励\n";
int w=rand()%5;
H[Y]+=K[Y]+w;
if (Y==1) cout<<"YOU HP+";
else cout<<"The computer HP+";
cout<<K[Y]+w;
if (Y==1) cout<<endl;
S[Y]-=2;
}
}
void play(){
system("cls");
cout<<"Game start!!!!!!!!!!!!!!!!";
Sleep(3000);
system("cls");
int r=1,j;
// bool game=1;
while (game){
Y=1;
C=2;
cout<<"Round "<<r<<"\n";
Sleep(3000);
cout<<"The 电脑 HP:"<<H[2]<<" SP:"<<S[2]<<" kill:"<<K[2]<<"\t"<<P[2];
cout<<"\n\n\n\nYour HP:"<<H[1]<<" SP:"<<S[1]<<" kill:"<<K[1]<<"\t"<<P[1];
Sleep(3000);
cout<<"\nYou choose:\n1.杀人(1SP)\n2.奖励(2SP)\n3.大杀特杀(5SP)\n4.啥事都没(+1~3SP)\n5.干哈(+0~2kill)\n6.尝试(3SP)";
cin>>j;
if (j==1&&S[Y]>0) {
int w=rand()%5-1;
S[Y]--;
H[C]-=K[Y]+w;
if (H[C]<=0) game=0;
cout<<"You kill the computer"<<K[Y]+w<<endl;
} else {
if (j==4) {
int w=rand()%3+1;
S[Y]+=w;
cout<<"YOU SP+"<<w<<endl;
} else {
if (j==5) {
int w=rand()%3;
K[Y]+=w;
cout<<"YOU kill+"<<w<<endl;
} else {
if (P[Y]=="killer") k(j);
if (P[Y]=="doctor") d(j);
if (P[Y]=="engineer") e(j);
}
}
}
if (game){
j=rand()%6+1;
C=1;
Y=2;
while (j!=1&&j!=2&&j!=4&&j!=5) j=rand()%6+1;
if (j==1&&S[2]>1) {
int w=rand()%5-1;
S[2]--;
H[1]-=K[2]+w;
if (H[1]<=0) game=0;
cout<<"你被电脑杀了"<<K[2]+w;
} else {
if (j==4) {
int w=rand()%3+1;
S[2]+=w;
cout<<"The computer SP+"<<w;
} else {
if (j==5) {
int w=rand()%3;
K[2]+=w;
cout<<"The computer kill+"<<w;
} else {
if (P[Y]=="killer") k(j);
if (P[Y]=="doctor") d(j);
if (P[Y]=="engineer") e(j);
}
}
}
if (game) Sleep(3000);
}
r++;
system("cls");
}
}
int main(){
srand((unsigned)time(NULL));
cout<<"Welcome to my game!!!!!!!!!!!!\n";
Sleep(3000);
system("cls");
cout<<"Please choose:\n1.killer\n2.doctor\n3.engineer\n4.help";
cin>>i;
if (i==4) help();
cout<<"You choose ";
if (i==1) killer();
if (i==2) doctor();
if (i==3) engineer();
cout<<P[p]<<endl;
i=rand()%3+1;
p++;
cout<<"The computer chooses ";
if (i==1) killer();
if (i==2) doctor();
if (i==3) engineer();
cout<<P[p]<<endl;
system("pause");
play();
if (H[1]<=0) cout<<"YOU LOSE~~~~~~~~~~~~~~~~~";
else cout<<"YOU WIN!!!!!!!!!!!!!!!!!!!";
return 0;
}
广告 您可能关注的内容 |