C#中的简单游戏只有本机库

Din*_*han 5 c# graphics game-loop winforms c#-native-library

我可以找到一组java 2D游戏教程Android游戏教程,它们使用原生图形库.
我在C#中寻找类似的东西(没有DirectX或XNA)
我发现这个游戏循环骨架,但它没有告诉如何渲染图形.

目标是模拟一个简单的电子设备.
当用户快速按下键盘上的某些键时,我需要显示一些图形输出.这看起来像一个街机游戏.

例如,当用户按下其中一个箭头键时,指针(图像)将相应地移动.

我想我不能用典型的Windows Forms Application做到这一点吗?
例如,使用PictureBox控件并在KeyPress发生时将其移动Form.

Geo*_*ett 15

这是一个简单的游戏使用WinForms和a Timer,Graphics用于绘制(封装GDI +).

它增加了一个每10毫秒"滴答"一次的计时器.每个勾选它执行游戏逻辑,然后绘制到屏幕外位图.这与使用链接中的示例中的连续循环相反.

表单分别处理关键事件(而不是做类似的事情GetKeyState)

调整窗体大小时,当它首次加载时,它将创建正确大小的后备缓冲区位图.


创建一个新表单并用下面的代码替换所有代码.使用箭头键控制球.没有死亡的概念.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsGame
{
    public partial class Form1 : Form
    {
        Bitmap Backbuffer;

        const int BallAxisSpeed = 2;

        Point BallPos = new Point(30, 30);
        Point BallSpeed = new Point(BallAxisSpeed, BallAxisSpeed);
        const int BallSize = 50;

        public Form1()
        {
            InitializeComponent();

            this.SetStyle(
            ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.DoubleBuffer, true);

            Timer GameTimer = new Timer();
            GameTimer.Interval = 10;
            GameTimer.Tick += new EventHandler(GameTimer_Tick);
            GameTimer.Start();

            this.ResizeEnd += new EventHandler(Form1_CreateBackBuffer);
            this.Load += new EventHandler(Form1_CreateBackBuffer);
            this.Paint += new PaintEventHandler(Form1_Paint);

            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
                BallSpeed.X = -BallAxisSpeed;
            else if (e.KeyCode == Keys.Right)
                BallSpeed.X = BallAxisSpeed;
            else if (e.KeyCode == Keys.Up)
                BallSpeed.Y = -BallAxisSpeed; // Y axis is downwards so -ve is up.
            else if (e.KeyCode == Keys.Down)
                BallSpeed.Y = BallAxisSpeed;
        }

        void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (Backbuffer != null)
            {
                e.Graphics.DrawImageUnscaled(Backbuffer, Point.Empty);
            }
        }

        void Form1_CreateBackBuffer(object sender, EventArgs e)
        {
            if (Backbuffer != null)
                Backbuffer.Dispose();

            Backbuffer = new Bitmap(ClientSize.Width, ClientSize.Height);
        }

        void Draw()
        {
            if (Backbuffer != null)
            {
                using (var g = Graphics.FromImage(Backbuffer))
                {
                    g.Clear(Color.White);
                    g.FillEllipse(Brushes.Black, BallPos.X - BallSize / 2, BallPos.Y - BallSize / 2, BallSize, BallSize);
                }

                Invalidate();
            }
        }

        void GameTimer_Tick(object sender, EventArgs e)
        {
            BallPos.X += BallSpeed.X;
            BallPos.Y += BallSpeed.Y;


            Draw();

            // TODO: Add the notion of dying (disable the timer and show a message box or something)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)