用C语言编写的小游戏代码是什么?

Python019

用C语言编写的小游戏代码是什么?,第1张

/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有c语言的编译器(vc++6.0、turbo????)上都能运行,你还可以进一步改进。这是一个类似贪吃蛇的小游戏。祝你好运*/\x0d\x0a/*贪吃蛇*/\x0d\x0a#include\x0d\x0a#include\x0d\x0a#include\x0d\x0a#include\x0d\x0aint head=3 ,tail=0\x0d\x0aint main()\x0d\x0a{\x0d\x0aint i,j,k=0\x0d\x0aint zuobiao[2][80]\x0d\x0along start\x0d\x0aint direction=77\x0d\x0aint gamespeed\x0d\x0aint timeover\x0d\x0aint change(char qipan[20][80],int zuobiao[2][80],char direction)\x0d\x0azuobiao[0][tail]=1zuobiao[1][tail]=1zuobiao[0][1]=1zuobiao[1][1]=2zuobiao[0][2]=1zuobiao[1][2]=3zuobiao[0][head]=1zuobiao[1][head]=4\x0d\x0a/*处理棋盘*/\x0d\x0achar qipan[20][80]//定义棋盘\x0d\x0afor(i=0ifor(j=0jqipan[i][j]=' '//初始化棋盘\x0d\x0afor(i=0iqipan[0][i]='_'\x0d\x0afor(i=0iqipan[i][0]='|'\x0d\x0afor(i=0iqipan[i][79]='|'\x0d\x0afor(i=0iqipan[19][i]='_'\x0d\x0aqipan[1][1]=qipan[1][2]=qipan[1][3]='*'//初始化蛇的位置\x0d\x0aqipan[1][4]='#'\x0d\x0aprintf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n")\x0d\x0aprintf("Input your game speed,please.(e.g.300)\n")\x0d\x0ascanf("%d",&gamespeed)\x0d\x0a\x0d\x0awhile(direction!='q')\x0d\x0a{\x0d\x0asystem("cls")\x0d\x0afor(i=0ifor(j=0jprintf("%c",qipan[i][j])\x0d\x0atimeover=1\x0d\x0astart=clock()\x0d\x0awhile(!kbhit()&&(timeover=clock()-startif(timeover)\x0d\x0a{\x0d\x0agetch()\x0d\x0adirection=getch()\x0d\x0a}\x0d\x0aelse\x0d\x0adirection=direction\x0d\x0aif(!(direction==72||direction==80||direction==75||direction==77))\x0d\x0a{\x0d\x0areturn 0\x0d\x0asystem("cls")\x0d\x0aprintf("GAME OVER!\n")\x0d\x0a}\x0d\x0aif(!change(qipan,zuobiao,direction))\x0d\x0a{\x0d\x0adirection='q'\x0d\x0asystem("cls")\x0d\x0aprintf("GAME OVER!\n")\x0d\x0a}\x0d\x0a}\x0d\x0areturn 0\x0d\x0a}\x0d\x0aint change(char qipan[20][80],int zuobiao[2][80],char direction)\x0d\x0a{\x0d\x0aint x,y\x0d\x0aif(direction==72)\x0d\x0ax=zuobiao[0][head]-1y=zuobiao[1][head]\x0d\x0aif(direction==80)\x0d\x0ax=zuobiao[0][head]+1y=zuobiao[1][head]\x0d\x0aif(direction==75)\x0d\x0ax=zuobiao[0][head]y=zuobiao[0][head]-1\x0d\x0aif(direction==77)\x0d\x0ax=zuobiao[0][head]y=zuobiao[1][head]+1\x0d\x0aif(x==0||x==18||y==78||y==0)\x0d\x0areturn 0\x0d\x0aif(qipan[x][y]!=' ')\x0d\x0areturn 0\x0d\x0aqipan[zuobiao[0][tail]][zuobiao[1][tail]]=' '\x0d\x0atail=(tail+1)%80\x0d\x0aqipan[zuobiao[0][head]][zuobiao[1][head]]='*'\x0d\x0ahead=(head+1)%80\x0d\x0azuobiao[0][head]=x\x0d\x0azuobiao[1][head]=y\x0d\x0aqipan[zuobiao[0][head]][zuobiao[1][head]]='#'\x0d\x0areturn 1\x0d\x0a}

“贪吃蛇”C代码:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78  //游戏框的宽,x轴

#define H 26  //游戏框的高,y轴

int dir=3   //方向变量,初值3表示向“左”

int Flag=0  //吃了食物的标志(1是0否)

int score=0  //玩家得分

struct food{ int x  //食物的x坐标

                  int y  //食物的y坐标

                 }fod //结构体fod有2个成员

struct snake{ int len  //身长

                   int speed  //速度

                   int x[100]

                   int y[100]

                  }snk  //结构体snk有4个成员

void gtxy( int x,int y)  //控制光标移动的函数

{ COORD coord

coord.X=x

coord.Y=y

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)

}

void gtxy( int x,int y)  //以下声明要用到的几个自编函数

void csh( )  //初始化界面

void keymove( )//按键操作移动蛇

void putFod( )  //投放食物

int  Over( )  //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q)//设定显示颜色

int main( )   //主函数

{ csh( )

  while(1)

    { Sleep(snk.speed)

      keymove( )

      putFod( )

      if(Over( ))

       {system(“cls”)

        gtxy(W/2+1,H/2)printf(“游戏结束!T__T”)

        gtxy(W/2+1,H/2+2)printf(“玩家总分:%d分”,score)

        getch( )

        break

       }

   }

   return 0

}

void csh( )  //初始化界面

{ int i

gtxy(0,0)

CONSOLE_CURSOR_INFO cursor_info={1,0}  //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info)

for(i=0i<=Wi=i+2)  //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0)  //设定打印颜色为绿字黑底

  gtxy(i,0) printf("■") //打印上边框

  gtxy(i,H)printf("■") //打印下边框

}

for(i=1i<Hi++)

{ gtxy(0,i)printf("■") //打印左边框

   gtxy(W,i)printf("■") //打印右边框

}

while(1)

  { srand((unsigned)time(NULL))  //初始化随机数发生器srand( )

  fod.x=rand()%(W-4)+2  //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

  fod.y=rand()%(H-2)+1  //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

  if (fod.x%2==0) break  //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0)  //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y)printf("●") //到食物坐标处打印初试食物

snk.len=3     //蛇身长

snk.speed=350  //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1  //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2    //蛇头纵坐标

setColor(9, 0)  //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]) printf("■") //打印蛇头

for(i=1i<snk.leni++)

    { snk.x[i]=snk.x[i-1]+2 snk.y[i]=snk.y[i-1]

      gtxy(snk.x[i],snk.y[i]) printf("■") //打印蛇身

   }

setColor(7, 0)  //恢复默认的白字黑底

return

}

void keymove( )  //按键操作移动蛇

{ int key

if( kbhit( ) )    //如有按键输入才执行下面操作

   { key=getch( )

     if (key==224)  //值为224表示按下了方向键,下面要再次获取键值

       { key=getch( )

         if(key==72&&dir!=2)dir=1  //72表示按下了向上方向键

         if(key==80&&dir!=1)dir=2  //80为向下

         if(key==75&&dir!=4)dir=3  //75为向左

         if(key==77&&dir!=3)dir=4  //77为向右

       }

   if (key==32)

      { while(1) if((key=getch( ))==32) break}  //32为空格键,这儿用来暂停

  }

if (Flag==0)  //如没吃食物,才执行下面操作擦掉蛇尾

  { gtxy(snk.x[snk.len-1],snk.y[snk.len-1]) printf("  ")}

int i

for (i = snk.len - 1i >0i--)  //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1] snk.y[i]=snk.y[i-1]}

switch (dir)  //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--break  //dir=1要向上移动

  case 2: snk.y[0]++break  //dir=2要向下移动

  case 3: snk.x[0]-=2break  //dir=3要向左移动

  case 4: snk.x[0]+=2break  //dir=4要向右移动

}

setColor(9, 0)

gtxy(snk.x[0], snk.y[0])printf("■") //打印蛇头

if (snk.x[0] == fod.x &&snk.y[0] == fod.y)  //如吃到食物则执行以下操作

   { printf("\007")snk.len++score += 100snk.speed -= 5Flag = 1} //007是响铃

else Flag = 0  //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5  //作弊码,不让速度无限加快

}

void putFod( )  //投放食物

{ if (Flag == 1)  //如吃到食物才执行以下操作,生成另一个食物

   { while (1)

  { int i,n= 1

   srand((unsigned)time(NULL))  //初始化随机数发生器srand( )

   fod.x = rand( ) % (W - 4) + 2  //产生在游戏框范围内的一个x坐标值

   fod.y = rand( ) % (H - 2) + 1  //产生在游戏框范围内的一个y坐标值

   for (i = 0i <snk.leni++)   //随机生成的食物不能在蛇的身体上

 { if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0break} }

   if (n &&fod.x % 2 == 0) break //n不为0且横坐标为偶数,则食物坐标取值成功

  }

 setColor(12, 0)

      gtxy(fod.x, fod.y) printf("●")  //光标到取得的坐标处打印食物

   }

return

}

int Over( )  //判断游戏是否结束的函数

{ int  i

setColor(7, 0)

gtxy(2,H+1)printf(“暂停键:space.”) //以下打印一些其它信息

gtxy(2,H+2)printf(“游戏得分:%d”,score)

if (snk.x[0] == 0 || snk.x[0] == W) return 1  //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1  //蛇头触碰上下边界

for (i = 1i <snk.leni++)

{ if (snk.x[0] == snk.x[i] &&snk.y[0] == snk.y[i]) return 1}  //蛇头触碰自身

return 0  //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{  HANDLE  handle = GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 )

}   //用来设定颜色的函数

// Calcu24.cpp : Defines the entry point for the console application.

//

/*

6-6

24点游戏

*/

#include "conio.h"

#include "stdlib.h"

#include "time.h"

#include "math.h"

#include "string.h"/*

从一副扑克牌中,任取4张。

2-10 按其点数计算(为了表示方便10用T表示),J,Q,K,A 统一按 1 计算

要求通过加减乘除四则运算得到数字 24。

本程序可以随机抽取纸牌,并用试探法求解。

*/void GivePuzzle(char* buf)

{

char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}for(int i=0i<4i++){

buf[i] = card[rand() % 13]

}

}

void shuffle(char * buf)

{

for(int i=0i<5i++){

int k = rand() % 4

char t = buf[k]

buf[k] = buf[0]

buf[0] = t

}

}

int GetCardValue(int c)

{

if(c=='T') return 10

if(c>='0' &&c<='9') return c - '0'

return 1

}

char GetOper(int n)

{

switch(n)

{

case 0:

return '+'

case 1:

return '-'

case 2:

return '*'

case 3:

return '/'

} return ' '

}double MyCalcu(double op1, double op2, int oper)

{

switch(oper)

{

case 0:

return op1 + op2

case 1:

return op1 - op2

case 2:

return op1 * op2

case 3:

if(fabs(op2)>0.0001)

return op1 / op2

else

return 100000

} return 0

}

void MakeAnswer(char* answer, int type, char* question, int* oper)

{

char p[4][3]

for(int i=0i<4i++)

{

if( question[i] == 'T' )

strcpy(p[i], "10")

else

sprintf(p[i], "%c", question[i])

}

switch(type)

{

case 0:

sprintf(answer, "%s %c (%s %c (%s %c %s))",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])

break

case 1:

sprintf(answer, "%s %c ((%s %c %s) %c %s)",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])

break

case 2:

sprintf(answer, "(%s %c %s) %c (%s %c %s)",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])

break

case 3:

sprintf(answer, "((%s %c %s) %c %s) %c %s",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])

break

case 4:

sprintf(answer, "(%s %c (%s %c %s)) %c %s",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])

break

}

}

bool TestResolve(char* question, int* oper, char* answer)

{

// 等待考生完成

int type[5]={0,1,2,3,4}//计算类型

double p[4]

double sum=0

//

for(int i=0i<4i++) //循环取得点数

{

p[i]=GetCardValue(int(question[i]))

} for(i=0i<5i++)

{

MakeAnswer(answer,type[i],question,oper) //获取可能的答案

switch(type[i])

{

case 0:

sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]) //A*(B*(c*D))

break

case 1:

sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]) //A*((B*C)*D)

break

case 2:

sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1])// (A*B)*(C*D)

break

case 3:

sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2])//((A*B)*C)*D

break

case 4:

sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2])//(A*(B*C))*D

break

}

if(sum==24) return true

}

return false

}

/*

采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法

提示:

1. 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) * ( 3 + A ), 实际上计算次序的数目是有限的:

A*(B*(c*D))

A*((B*C)*D)

(A*B)*(C*D)

((A*B)*C)*D

(A*(B*C))*D

2. 需要考虑计算结果为分数的情况:( 3 + (3 / 7) ) * 7

3. 题目中牌的位置可以任意交换

*/

bool TryResolve(char* question, char* answer)

{

int oper[3] // 存储运算符,0:加法 1:减法 2:乘法 3:除法

for(int i=0i<1000 * 1000i++)

{

// 打乱纸牌顺序

shuffle(question)

// 随机产生运算符

for(int j=0j<3j++)

oper[j] = rand() % 4 if( TestResolve(question, oper, answer) ) return true

} return false

}

int main(int argc, char* argv[])

{

// 初始化随机种子

srand( (unsigned)time( NULL ) )char buf1[4] // 题目

char buf2[30] // 解答

printf("***************************\n")

printf("计算24\n")

printf("A J Q K 均按1计算,其它按牌点计算\n")

printf("目标是:通过四则运算组合出结果:24\n")

printf("***************************\n\n")

for()

{

GivePuzzle(buf1) // 出题

printf("题目:")

for(int j=0j<4j++){

if( buf1[j] == 'T' )

printf("10 ")

else

printf("%c ", buf1[j])

} printf("\n按任意键参考答案...\n")

getch() if( TryResolve(buf1, buf2) ) // 解题

printf("参考:%s\n", buf2)

else

printf("可能是无解...\n") printf("按任意键出下一题目,x 键退出...\n")

if( getch() == 'x' ) break

} return 0

}