比如 一张地图的图片,我按方向键,图片向反方向移动,刷新屏幕实现滚动用什么方法啊?计时器?循环?。大侠们帮帮忙啊,给个简单代码启发下我啊,我才学几天

解决方案 »

  1.   

    使用Microsoft XNA可以做2D游戏,这也是C#呀,有很多现成例子可以参考。唯一缺点就是对显卡要求高了点
      

  2.   

    一般是移动摄像头,摄像头附在人上,地图是静止不动。但C#本就不是用来开发游戏的,也不是游戏引擎,一句话不适合。你真要做,那就把地图做成一张大的bitmap,存在内存,然后以人物所在位置为中心,计算出需要在屏幕上显示出来的那一部分地图。直接在这个方法中计算
          protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
            }
    或是新建个线程控制帧数来显示都行。
      

  3.   


    using System;
    using System.Drawing;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            // 设置DoubleBuffer使得Form刷新时不会闪烁
                this.SetStyle(ControlStyles.AllPaintingInWmPaint
                              | ControlStyles.OptimizedDoubleBuffer
                              | ControlStyles.UserPaint
                              | ControlStyles.ResizeRedraw,
                              true);
            }        protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                // 初始化背景图片
                m_BackgroundImage = new Bitmap(@"C:\1.jpg");            // 初始化背景图片位置
                m_Point = new Point(this.Width / 2 - m_BackgroundImage.Width / 2, this.Height / 2 - m_BackgroundImage.Height / 2);            // 初始化每按一下移动的长度
                m_Step = 2;
            }        protected override void OnKeyDown(KeyEventArgs e)
            {
                base.OnKeyDown(e);
                switch (e.KeyCode)
                {
                    // 按下左键,背景图右移一个step,并重绘背景
                    case Keys.Left:
                        if (m_Point.X < 0)
                        {
                            m_Point = new Point(m_Point.X + m_Step, m_Point.Y);
                            Invalidate();
                        }
                        break;
                    // 按下右键,背景图左移一个step,并重绘背景
                    case Keys.Right:
                        if (m_Point.X > (this.Width - m_BackgroundImage.Width))
                        {
                            m_Point = new Point(m_Point.X - m_Step, m_Point.Y);
                            Invalidate();
                        }
                        break;
                    // 按下上键,背景图下移一个step,并重绘背景
                    case Keys.Up:
                        if (m_Point.Y < 0)
                        {
                            m_Point = new Point(m_Point.X, m_Point.Y + m_Step);
                            Invalidate();
                        }
                        break;
                    // 按下下键,背景图上移一个step,并重绘背景
                    case Keys.Down:
                        if (m_Point.Y > (this.Height - m_BackgroundImage.Height))
                        {
                            m_Point = new Point(m_Point.X, m_Point.Y - m_Step);
                            Invalidate();
                        }
                        break;
                    default:
                        break;
                }
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);            // 画背景
                DrawBackground(e);
            }        private void DrawBackground(PaintEventArgs e)
            {
                e.Graphics.DrawImage(m_BackgroundImage, 
                    new Rectangle(m_Point.X, m_Point.Y, m_BackgroundImage.Width, m_BackgroundImage.Height), 
                    new Rectangle(0, 0, m_BackgroundImage.Width, m_BackgroundImage.Height), 
                    GraphicsUnit.Pixel);
            }        // 背景图片
            private Bitmap m_BackgroundImage;
            // 背景图片位置
            private Point m_Point;
            // 每按一下移动的长度
            private Int32 m_Step;
        }
    }