| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 472 人关注过本帖
标题:帮解决个问题!!!
只看楼主 加入收藏
using110
Rank: 1
来 自:成都
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-7
结帖率:0
收藏
 问题点数:0 回复次数:2 
帮解决个问题!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Snake
{
    public class Game
    {
        static char foodChar = '$';
        static char snakeChar = '#';
        static char blank = ' ';
        //游戏界面边界表示数组
        static int[] border = new int[4] { 0, 24, 0, 79 };

        /// <summary>
        /// 在相应坐标绘制字符
        /// </summary>
        /// <param name="left">纵坐标</param>
        /// <param name="top">横坐标</param>
        /// <param name="c">字符</param>
        public static void Draw(int left, int top, char c)
        {
            Console.SetCursorPosition(left, top);
            Console.Write(c);
        }

        /// <summary>
        /// 根据字符在蛇的相应坐标绘制图形
        /// </summary>
        /// <param name="s">蛇</param>
        /// <param name="c">字符</param>
        public static void Draw(Snake s, char c)
        {
            for (int i = s.Length - 1; i >= 0; --i)
            {
                Draw(s.Left[i], s.Top[i], c);
            }
        }

        /// <summary>
        /// 绘制蛇
        /// </summary>
        /// <param name="s">蛇</param>
        public static void SnakeDraw(Snake s)
        {
            Draw(s, snakeChar);
        }

        /// <summary>
        /// 清除已绘制的蛇
        /// </summary>
        /// <param name="s">蛇</param>
        public static void ClearSnake(Snake s)
        {
            Draw(s, blank);
        }

        /// <summary>
        /// 设置食物坐标
        /// </summary>
        /// <param name="f">食物</param>
        public static void SetFood(Food f)
        {
            f.SetFood(border);
        }

        /// <summary>
        /// 蛇吃食物
        /// </summary>
        /// <param name="s">蛇</param>
        /// <param name="f">食物</param>
        /// <returns>蛇是否吃了食物</returns>
        public static bool SnakeEatFood(Snake s, Food f)
        {
            if (s.Left[s.Length - 1] == f.Left & s.Top[s.Length - 1] == f.Top)
            {
                s.EatFood(f);

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 绘制食物
        /// </summary>
        /// <param name="s">蛇</param>
        /// <param name="f">食物</param>
        public static void FoodDraw(Snake s, Food f)
        {
            if (SnakeEatFood(s, f))
            {
                Draw(f.Left, f.Top, blank);

                SetFood(f);
            }
            else
            {
                Draw(f.Left, f.Top, foodChar);
            }
        }

        /// <summary>
        /// 判断游戏是否结束
        /// </summary>
        /// <param name="s">蛇</param>
        /// <returns>是否结束的结果</returns>
        public static bool IsEnd(Snake s)
        {
            if (s.Top[s.Length - 1] < border[0] || s.Top[s.Length - 1] > border[1] || s.Left[s.Length - 1] < border[2] || s.Left[s.Length - 1] > border[3])
            {
                return true;
            }

            for (int i = 0; i < s.Length - 1; i++)
            {
                if (s.Left[i] == s.Left[s.Length - 1] & s.Top[i] == s.Top[s.Length - 1])
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 蛇运动
        /// </summary>
        /// <param name="s">蛇</param>
        /// <param name="k">变化的按键</param>
        /// <param name="key">上一次的运动方向键</param>
        public static void SnakeRun(Snake s, ref ConsoleKey k, ref ConsoleKey key)
        {
            if (Console.KeyAvailable)
            {
                k = Console.ReadKey(true).Key;
            }

            if (k == ConsoleKey.UpArrow)
            {
                s.UpRun();
            }
            else if (k == ConsoleKey.DownArrow)
            {
                s.DownRun();
            }
            else if (k == ConsoleKey.LeftArrow)
            {
                s.LeftRun();
            }
            else if (k == ConsoleKey.RightArrow)
            {
                s.RightRun();
            }
            else
            {
                k = key;
            }

            key = k;
        }

        /// <summary>
        /// 运行
        /// </summary>
        public static void Start()
        {
            Console.CursorVisible = false;
            ConsoleKey key = ConsoleKey.LeftArrow;
            ConsoleKey k = ConsoleKey.RightArrow;
            Snake s = new Snake();
            Food f = new Food();

            SetFood(f);
            while (true)
            {
                FoodDraw(s, f);
                SnakeDraw(s);

                Thread.Sleep(800);

                ClearSnake(s);
                SnakeRun(s, ref k, ref key);

                if (IsEnd(s))
                {
                    Console.Clear();
                    Console.WriteLine("Game Over !!");

                    break;
                }

            }
        }
    }
}
是控制台的 红色这个地方要怎么控制蛇的速度啊,如果吃了食物就加速度,一直都是800我尝试了也不行啊  帮下。。。
搜索更多相关主题的帖子: blank 游戏 
2011-04-30 16:39
fily1314
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1190
注 册:2007-7-18
收藏
得分:0 
设置个全局变量作为sleep的时间,吃了食物就修改那个变量的值
2011-05-03 12:51
李龙才
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-3
收藏
得分:0 
贪吃蛇的毕业设计!!
2011-05-03 13:54
快速回复:帮解决个问题!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017061 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved