用“vc”或“c++”编写的一个小游戏代码,源代码是什么?

用“vc”或“c++”编写的一个小游戏代码,源代码是什么?,第1张

#include\x0d\x0a#include\x0d\x0a#include\x0d\x0a#define LEFT 0x4b00\x0d\x0a#define RIGHT 0x4d00\x0d\x0a#define DOWN 0x5000\x0d\x0a#define UP 0x4800\x0d\x0a#define ESC 0x011b\x0d\x0aint i,key\x0d\x0aint score=0\x0d\x0aint gamespeed=32000\x0d\x0astruct Food /*食物的结构体*/\x0d\x0a{\x0d\x0aint x/*食物的横坐标*/\x0d\x0aint y/*食物的纵坐标*/\x0d\x0aint yes/*食物是否出现的变量*/\x0d\x0a}food\x0d\x0astruct Snack /*蛇的结构体*/\x0d\x0a{\x0d\x0aint x[N]\x0d\x0aint y[N]\x0d\x0aint node/*蛇的节数*/\x0d\x0aint direction/*蛇的方向*/\x0d\x0aint life/*蛇的生命,0活着,1死亡*/\x0d\x0a}snake\x0d\x0avoid Init(void)/*图形驱动*/\x0d\x0avoid Close(void)/*关闭游戏函数*/\x0d\x0avoid DrawK(void)/*画图函数*/\x0d\x0avoid GameOver(void)/*输出失败函数*/\x0d\x0avoid GamePlay()/*游戏控制函数 主要程序*/\x0d\x0avoid PrScore(void)/*分数输出函数*/\x0d\x0a\x0d\x0aDELAY(char ch)/*调节游戏速度*/\x0d\x0a{\x0d\x0aif(ch=='3')\x0d\x0a{\x0d\x0adelay(gamespeed)/*delay是延迟函数*/\x0d\x0adelay(gamespeed)\x0d\x0a}\x0d\x0aelse if(ch=='2')\x0d\x0a{\x0d\x0adelay(gamespeed)\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aMenu()/*游戏开始菜单*/\x0d\x0a{\x0d\x0achar ch\x0d\x0aprintf("Please choose the gamespeed:\n")\x0d\x0aprintf("1-Fast 2-Normal 3-Slow\n")\x0d\x0aprintf("\nPlease Press The numbers..\n")\x0d\x0ado\x0d\x0a{ch=getch()}\x0d\x0awhile(ch!='1'&&ch!='2'&&ch!='3')\x0d\x0aclrscr()\x0d\x0areturn(ch)\x0d\x0a}\x0d\x0a\x0d\x0a/*主函数*/\x0d\x0avoid main(void)\x0d\x0a{\x0d\x0aint ch\x0d\x0ach=Menu()\x0d\x0aInit()\x0d\x0aDrawK()\x0d\x0aGamePlay(ch)\x0d\x0aClose()\x0d\x0a}\x0d\x0a\x0d\x0avoid Init(void)\x0d\x0a{\x0d\x0aint gd=DETECT,gm\x0d\x0ainitgraph(&gd,&gm,"c:\\tc")\x0d\x0acleardevice()\x0d\x0a}\x0d\x0a\x0d\x0avoid DrawK(void)\x0d\x0a{\x0d\x0asetcolor(11)\x0d\x0asetlinestyle(SOLID_LINE,0,THICK_WIDTH)\x0d\x0afor(i=50i0i--) /*贪吃蛇的移动算法*/\x0d\x0a{\x0d\x0asnake.x[i]=snake.x[i-1]\x0d\x0asnake.y[i]=snake.y[i-1]/*贪吃蛇的身体移动算法*/\x0d\x0a}\x0d\x0a\x0d\x0aswitch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/\x0d\x0a{\x0d\x0acase 1:snake.x[0]+=10break\x0d\x0acase 2:snake.x[0]-=10break\x0d\x0acase 3:snake.y[0]-=10break\x0d\x0acase 4:snake.y[0]+=10break\x0d\x0a}\x0d\x0afor(i=3i595||snake.y[0]455)\x0d\x0a{\x0d\x0aGameOver()\x0d\x0asnake.life=1\x0d\x0a}\x0d\x0aif(snake.life==1) /*如果死亡就退出循环*/\x0d\x0abreak\x0d\x0aif(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/\x0d\x0a{\x0d\x0asetcolor(0)\x0d\x0arectangle(food.x,food.y,food.x+10,food.y-10)/*吃的食物后用黑色将食物擦去*/\x0d\x0asnake.x[snake.node]=-20snake.y[snake.node]=-20/*现把增加的一节放到看不到的地方去*/\x0d\x0a\x0d\x0asnake.node++\x0d\x0afood.yes=1\x0d\x0ascore+=10\x0d\x0aPrScore()\x0d\x0a}\x0d\x0asetcolor(4)/*每次移动后将后面的身体擦去*/\x0d\x0afor(i=0i 回答于 2022-11-16

/*也不知道你是什么级别的,我是一个新手,刚接触编程语言,以下是我自己变得一个小程序,在所有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}

#include<graphics.h>

#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=50i<=600i+=10)

{

rectangle(i,40,i+10,49)/*画出上边框*/

rectangle(i,451,i+10,460)/*画出下边框*/

}

for(i=40i<=450i+=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]=100snake.y[0]=100

snake.x[1]=110snake.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-1i>0i--) /*贪吃蛇的移动算法*/

{

snake.x[i]=snake.x[i-1]

snake.y[i]=snake.y[i-1]/*贪吃蛇的身体移动算法*/

}

switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/

{

case 1:snake.x[0]+=10break

case 2:snake.x[0]-=10break

case 3:snake.y[0]-=10break

case 4:snake.y[0]+=10break

}

for(i=3i<snake.nodei++) /*判断是否头部与身体相撞*/

{

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]=-20snake.y[snake.node]=-20/*现把增加的一节放到看不到的地方去*/

snake.node++

food.yes=1

score+=10

PrScore()

}

setcolor(4)/*每次移动后将后面的身体擦去*/

for(i=0i<snake.nodei++)

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()

}

贪吃蛇


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8061268.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存