c 贪吃蛇c语言代码 怎么才能发声

> 关于贪吃蛇的代码问题我开始是用tc写了一个能自己移动的程序。下面是用c语言写的代码:/*Note:
关于贪吃蛇的代码问题我开始是用tc写了一个能自己移动的程序。下面是用c语言写的代码:/*Note:
shalyli & &
发布时间: & &
浏览:13 & &
回复:0 & &
悬赏:0.0希赛币
关于贪吃蛇的代码问题我开始是用tc写了一个能自己移动的程序。下面是用c语言写的代码: & /* Note:Your choice is C IDE */#include&stdio.h$>$ #include&bios.h$>$ #include&graphics.h&#define LEFT 19200#define UP 18432#define RIGHT 19712#define DOWN 20480#define ESC 283#define N 150& #define gamespeed 50000struct Snake{
int x[N],y[N]; & & &}& void Init(void);void Draw(void);void GamePlay(void);void GameOver(void);& void Close(void);void main(){ Init(); & Draw(); & GamePlay(); & Close();}& void Init(void){ int gd=DETECT;int mode=0; & initgraph(&gd,&mode,&d:\\tc&);&
& void Draw(void){ setcolor(4); rectangle(0,0,getmaxx(),getmaxy());}& void GamePlay(void){
snake.length=3; &
snake.direction=1;/*方向向右*/ snake.life=0;&
snake.x[0]=50;snake.y[0]=50;
snake.x[1]=55;snake.y[1]=50;&
snake.x[2]=60;snake.y[2]=50; &
while(1) & {
while(!kbhit())/*在没有按键的情况下蛇身自己移动*/ & &
/*蛇身自己向前移的核心算法*/ & {
for(i=snake.length-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;
/*向右*/ &
snake.x[0]-=10; /* 向左*/&
snake.y[0]-=10; /*向上*/ &
case 4: snake.y[0]+=10;
/*向下*/ &
if(snake.x[0]&10||snake.x[0]&getmaxx()||snake.y[0]&10||snake.y[0]&getmaxy()) &
{ GameOver(); &
snake.life=1; &
if(snake.life==1) &
/*画蛇的代码*/
setcolor(4); &
for(i=0;i&snake.i++) &
circle(snake.x[i],snake.y[i],5); &
delay(gamespeed); &
setcolor(0); &
circle(snake.x[snake.length-1],snake.y[snake.length-1],5); &
}/*end while(!kbhit)*/&
&if(snake.life==1) &key=bioskey(0); & if(key==ESC) &if(key==UP&&snake.direction!=4) &snake.direction=3;
&if(key==DOWN&& snake.direction!=3) & snake.direction=4; & if(key==LEFT&&snake.direction!=1) &
snake.direction=2; &
if(key==RIGHT&& snake.direction!=2) &
snake.direction=1; & }
/*end while(1)*/ && }& void GameOver(void){ cleardevice(); & setcolor(RED); & settextstyle(0,0,4); & outtextxy(200,200,&GameOver&); &/* getch();*/ & }& void Close(void){
getchar(); & closegraph();}运行没有错误,于是我再加上了一个产生食物的代码:/* Note:Your choice is C IDE */#include &stdio.h&& #include&bios.h$>$ #include&graphics.h&#include&stdlib.h&#include&stdlib.h&#define LEFT 19200#define UP 18432#define RIGHT 19712#define DOWN 20480#define ESC 283#define N 150& #define gamespeed 50000struct Snake{
int x[N],y[N];
}& struct Food{int x,y; &}void Init(void);void Draw(void);void GamePlay(void);void GameOver(void);& void Close(void);void main(){ Init(); & Draw(); & GamePlay();
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&如何用javascript写一个贪吃蛇
我原来用C语言,借助curses库实现了linux 终端下的贪吃蛇游戏。
这个javascript版本的贪吃蛇是/game/tanchishe. 的学习笔记,实现的原理和C版本基本一样。
--------------------
1.怎样表示一条snake
用一个二维数组存snake的各个点(x,y),同时标记这些点(x,y)为“cover”,这是用于以后检查snake的头是否撞到了snake的body。
//initialize snake
function initSnake() {
var pointer = randomPointer(len-1, len-1, WIDTH/2);
for(var i = 0; i < i++) {
var x = pointer[0] - i,
y = pointer[1];
snake.push([x,y]);
carrier[x][y] = "cover"; //标记snake body
2.用js画出&#26684;子
用document.createElent()方法创建出table->tr->td, 然后用document.appendChild()方法追加到id为“snakewrap”的元素上:
//initialize grid
function initGrid() {
var body = document.getElementsByTagName("body")[0];
var table = document.createElement("table"),
tbody = document.createElement("tbody")
for(var j = 0; j < HEIGHT; j++) {
var col = document.createElement("tr");
for(var i = 0; i < WIDTH; i++) {
var row = document.createElement("td");
gridElems[i][j] = col.appendChild(row);
tbody.appendChild(col);
table.appendChild(tbody);
document.getElementById("snakewrap").appendChild(table);
3.生成食物的随机坐标
function randomPointer(startX,startY,endX,endY) {
startX = startX || 0;
startY = startY || 0;
endX = endX || WIDTH;
endY = endY || HEIGHT;
var p = [],
x = Math.floor(Math.random()*(endX - startX)) + startX,
y = Math.floor(Math.random()*(endY - startY)) + startY;
//如果(x,y)有物体,则重新生成坐标
if(carrier[x][y]) {
return randomPointer(startX,startY,endX,endY);
添加新的食物:
//addObject("food")
function addObject(name) {
var p = randomPointer(); //get random position
var x = p[0];
var y = p[1];
carrier[x][y] =
gridElems[x][y].className =
4.方向键按下动作事件监听:
允许左上右下这4个按键来改变snake的运动方向,注意,如果方向相反的话,不生效。
对于键盘上的每一个按键,都有一个key cord,我的这篇博客记录了javascript的key cord,可看到:
left arrow
right arrow
down arrow
//keyboard event listener
function attachEvents(e) {
directkey = Math.abs(e.keyCode - directkey) != 2 && e.keyCode > 36 && e.keyCode < 41 ? e.keyCode :
5.贪吃蛇的核心--判断
每次判断(即judge()函数每运行一次-->这里用到了setInterval()方法),都要先把snake的“头”节点保存下来,然后做判断
1)判断方向,根据方向调整“头”的坐标(由于有setInterval()方法,系统会每个若个毫秒就运行一次judge()函数,确保用户按下方向键后能够该表方向)
2)判断“头”是否撞到墙,或碰到snake的身体(即carrier[headX][headY] == "cover"时),如果碰到,则游戏结束。
3)判断“头”当前的位置是不是食物(即carrier[headX][headY] == "food"), 如果头元素的carrier不是食物,则让snake的尾巴pop出来;如果是,则让当前位置的携带信息carrier[headX][headY] = false
4 )向数组的开头添加一个元素-->从而实现了“视觉上”的snake移动(或吃食物body增长)的效果
function judge() {
//把snake的“头”位置暂存起来
var headX = snake[0][0], headY = snake[0][1];
switch(directkey) {
case 37: headX -= 1; //left
case 38: headY -= 1; //up
case 39: headX += 1; break
case 40: headY += 1; //down
//碰到边界(block),或头碰到身体(cover),则结束游戏
if(headX >= WIDTH || headX = HEIGHT || headY 从而实现了“视觉上”的snake移动(或吃食物body增长)的效果
snake.unshift([headX,headY]);
carrier[headX][headY] = "cover";
gridElems[headX][headY].className = "cover";
len = snake.
setInterval()函数(使得上面的judge()函数每隔300ms就运行一次):
function run_run_run() {
if(snakeTimer) {
window.clearInterval(snakeTimer);
snakeTimer = window.setInterval("judge()", Math.floor(300));
6.onload 运行
onload 事件会在页面或图像加载完成后立即发生:
window.onload = function(){
initGrid();
document.onkeydown = attachE //监听keydown事件
$("start").onclick = function (e) {
len = 3; //snake的初始长度
directkey = 39; //right
snake = new Array();
initSnake();
addObject("food");
run_run_run();
//让start按钮失效
$("start").setAttribute("disabled",true);
$("start").style.color = "#aaa";
7.参考:/game/tanchishe.html
8.玩玩:我的simple and stupid snake game
效果图(我借用了下最近很火的2048):
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'播放列表加载中...
正在载入...
分享视频:
嵌入代码:
拍下二维码,随时随地看视频
【C语言22实战】经典的贪吃蛇游戏(下)
上 传 者:
内容介绍:
【C语言22实战】经典的贪吃蛇游戏(下)
Channel Me 精选
我来说点啥
版权所有 CopyRight
| 京网文[0号 |
| 京公网安备:
互联网药品信息服务资格证:(京)-非经营性- | 广播电视节目制作经营许可证:(京)字第403号
<img src="" width="34" height="34"/>
<img src=""/>
<li data-vid="">
<img src=""/><i data-vid="" class="ckl_plays">
<img width="132" height="99" src=""/>
在线人数:
<li data-vid="">
<img src=""/><i data-vid="" class="ckl_plays">
<img src="///img/blank.png" data-src=""/>
<img src="///img/blank.png" data-src="http://"/>
<li data-vid="" class="cfix">
src="///img/blank.png" data-src=""/>
<i data-vid="" class="ckl_plays">
<li data-vid="" class="cfix">
src="///img/blank.png" data-src=""/><i data-vid="" class="ckl_plays">
没有数据!
{upload_level_name}
粉丝 {fans_count}
{video_count}
{description}贪吃蛇c语音问题描述_百度知道
贪吃蛇c语音问题描述
监控键盘按键void Addtail(),而按相反方向无效{void Initfood();case &#39;p=p-&free(p);;;隐藏光标{ CONSOLE_CURSOR_INFO cursor_info = {1:if(SPEECH&t&#92;&#47,p-&dir=RIGHT;主页void keybordhit(),18);/n&方向};i&);t
|&#92;新的蛇尾}void draw()//);/按p键减速&tail=p;tail=(struct Node*)malloc(sizeof(struct Node));printf(&x==x)&&(p-&Eatfood();if(snake-&n&n&quot,2);/printf(&/next=NULL;//t&#92;gotoxy(15;i++)/增加蛇尾{struct Node *newnode=(struct Node *)malloc(sizeof(struct Node));///);struct Node *p=snode.h&y);next=NULL;/没吃到食物清除之前的尾结点printf(&t&#92;t
|&#92;free(snode),而按相反方向无效{&#47, int y);碰到边界{STOP=1;t
|&#92;printf(&x=rand()%(WIDTH-2)+2;);;dir==DOWN)/按空格键暂停&}if(mark==1)/隐藏光标printf(&;任意键开始游戏;画蛇{struct Node *p=newnode-&gt,tail-&gt,food-&next=NULL;/);y););printf(&if(_kbhit()){ch=getch();|&#92: %d;if(smark==0){while(p-&O&#39;释放内存free(snake);监控键盘{画蛇void Homepage();next=x==food-&gt,12),不清楚尾结点smark=0;p=p-&/初始蛇头方向 右for(i=2;printf(&printf(&free(p);break,&#39, int y)/n&x,13);if(smark==0){while(p-&}elsesnake-&gt!&#92;);/增加蛇身void display(struct Node *shead);|&#92;碰到自身{if(p-&/}elsesnake-&|&#92;}if((smark==1)&&(times==1)){gotoxy(tail-&x,food-&t&#92;newnode-&如果本来方向是下;撞墙;/d&#39;w&#39;/产生食物void Initsnake();next=/t&#92;y==food-&%c&t
|&#92;/n&y); &#39;t&#92!=NULL){if((food-&/A&#39.h&gt,游戏结束;蛇移动void draw();/next=NULL;S&#39;t&#92.D控制方向&y))//struct Node *&#47!=NULL){q=p;t&#92;gotoxy(50;n&x=x;t&#92;;)!=NULL){q=p!=NULL)/ &#39;o&#39;}}if(snake-&}p=p-&}void HideCursor()/&#47:case &#39;getch();以时间为种子产生随机数while(1){food-&
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE);/);x+1;exit(0);//结点加到蛇头tail=&#47,RIGHT;/;struct Node *p=snode:if(snake-&
&quot.S,p-&gt,14);按o键加速&/如果本来方向是上;/增加 5 个结点{Addnode(i;画蛇身printf(&/}void Addtail()/速度;gotoxy(50;/产生食物{struct Node *p=snode:if(snake-&2||x&dir==RIGHT)/如果食物产生在蛇身上{/如果吃到食物;dir=DOWN;/);n&游戏已暂停;&#47: %d&quot.h&//t&#92;y)/撞到自身;); |&#92;y);
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE);速度减慢{SPEECH=SPEECH+50;tail-&}q-&}while(p:case &#39;gotoxy(15;t&#92;snake-&#define WIDTH 40#define HEIGH 12enum direction{&#47,3);/x;%c&/食物struct Snake *printf(&n&y-1);没吃到食物清除之前的尾结点printf(&quot,19):if(snake-&/t&#92;/p=p-&如果本来方向是左;y);///t&#92;/}void Homepage()&#47.h&newnode-&t&#92;&#47,&#39;};/free(p);/移动{struct Node *q;产生食物}if(smark==0){gotoxy(tail-&/t&#92;);struct Food{/t&#92;t&#92;分数int smark=0;p&#39;初始长度 5snake-&/dir=RIGHT;/case &#39;/struct Snake{/t&#92;/dir==UP){Addnode(p-&if(smark==0){while(p-&食物Y坐标while(p;/|&#92;system(&quot,SPEECH);dir==DOWN){Addnode(p-& 头部前进void Addnode(;;int mark=1;蛇属性struct Node *dir==LEFT){Addnode(p-&dir==UP)/如果本来方向是右;dir==RIGHT){Addnode(p-&lenth+2,y;|&#92;y);;/}}void move()/Initfood();/2||y&蛇头坐标等于食物坐标{smark=1;----------------------------------------&#92;gotoxy(5;暂时挂起线程}return 0;x;/}if(snode-&gt:case &#39;newnode-&食物;显示蛇身坐标void move();y==p-&P&#39;&#47,否则重新生成食物{gotoxy(food-&#include&printf(&};/switch(ch){case &#39;x==p-&t
|&#92;score++;/printf(&}mark=1;}}if(snake-&;}}}int main(void)/n&构造snake{gotoxy(10;printf(&/x: %d&case &#39;#include&/t&#92;;t
|&#92;/y==y)){STOP=1;printf(&t&#92;}如果食物不在蛇身上;/);吃食物标记int times=0;/while(p;t&#92;printf(&printf(&quot,13),按任意键恢复游戏&y+1); /}}if(snake-&&#47,不清楚尾结点}else{times=1:case &#39.h&p=t&#92;结点加到蛇头if(x&}}void Eatfood()/&#47!=NULL)if((p-&,5);printf(&/n&quot:if(snake-&t
|&#92;if(smark==0){while(p-&dir=UP,任意键退出;p=p-&;/监控键盘按键move();失败getch();程序入口{Homepage();t&#92;=400)/);y=rand()%(HEIGH-2)+2;n&quot,任意键退出,游戏结束;暂停gotoxy(15;/free(snode);printf(&/t
|&#92;}elsesnake-&}elsesnake-&snake=(struct Snake*)malloc(sizeof(struct Snake));while(;&#47:case &#39;y=20;增加结点Initfood();食物生成无效);case &#39;}}void Initfood()/next=NULL,tail-&方向LEFT;//snode=/);newnode-&t
|&#92!按W;#include&n&&#47,生成食物;t&#92;srand((unsigned)time(NULL));/printf(&/&#47!&#92;}}}void Addnode(int x,pos);增加蛇身{struct Node *newnode=(struct Node *)malloc(sizeof(struct Node));y=y;t&#92;);//t&#92;),16);=snake-&}q-&%c&n&分数;t&#92;=WIDTH||y&lt, *p=int score=0;y);/x;n&&#47, 0};/x=50;newnode-&////dir=LEFT;如果吃到食物;);),而按相反方向无效{#include& }void Initsnake()/ &#39,p-&;/吃到食物void gotoxy(;|&#92;&#47.h&gotoxy(15,15);|&#92;printf(&quot,*next,18);;定位光标{
COORD蛇属性),%d&速度加快{SPEECH=SPEECH-50.X = x - 1:nul&D&#39;/;,而按相反方向无效{pause&);gotoxy(10:if(SPEECH&蛇的坐标变化draw()!=NULL){gotoxy(p-&t&#92;|&#92;/p=p-&gt,food-&gt:/x&&snode-&//exit(0);next!STOP){keybordhit();printf(&printf(&food = (struct Food*)malloc(sizeof(struct Food))!=NULL){q=p;/绘主页{}void keybordhit()/}q-&}gotoxy(50;长度食物int x!=NULL){q=p;食物X坐标food-&gt:case &#39;/&#47, &cursor_info);/), int y)/printf(&/HideCursor();printf(&/x;/----------------------------------------&#92;|&#92;蛇身int SPEECH=200;/free(p);;
&case &#39;/gotoxy(15;printf(&int STOP=0,j;构造snakevoid Eatfood(),DOWN};=150)/释放内存free(snake);printf(&quot.A;printf(&W&#39;&#47,score);/失败getch();s&#39;t&#92;struct Food *}break,7);x-1;lenth=5;);case &#39;gotoxy(5;dir==LEFT)&#47,2);x)&&(food-&Initsnake(),p-&}p=p-&则重新生成食物mark=0;&#47,p-&/}q-&=HEIGH)/;a&#39,19);//struct Node{/t&#92;吃到食物{Addtail()#include&#include&lt.Y = y - 1;蛇的重绘Sleep(SPEECH);%c&quot,UP
其他类似问题
为您推荐:
贪吃蛇的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 怎么用c语言做贪吃蛇 的文章

 

随机推荐