简单的游戏在C#中,只有本地库 [英] Simple Game in C# with only native libraries

查看:149
本文介绍了简单的游戏在C#中,只有本地库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能找到一套的Java 2D游戏的教程并的安卓游戏教程它使用的唯一的本地图形库

我正在寻找在C#中类似的东西(不包括DirectX或XNA)

我发现的这个游戏圈骨架,但它并没有告诉如何渲染图形。


目标就是模拟一个简单的电子设备。
我需要表现出一定的图形输出为用户按下quickly.Hence它看起来像一个街机游戏键盘上的某些键。


例如的当用户按下方向键中的一个指针(图像)将随之移动。


我猜我不能用典型的Windows做到这一点窗体应用程序可以吗?

例如使用图片框控制和表格的的KeyPress 事件。


I could find a set of java 2D game tutorials and android game tutorials which uses only native graphics libraries.
I'm looking for something similar in C# ( without DirectX or XNA)
I found this game loop skeleton , but it doesn't tell how to Render the Graphics.

Target is to simulate a simple Electronic device.
I need to show some graphical output as user "presses" some keys on the keyboard quickly.Hence it looks like an arcade game.

For example As the user presses one of the arrow keys a pointer(an image) will move accordingly.

I guess I can't do this with typical Windows Forms Application can I?
For example use a PictureBox control and move it in KeyPress event of the Form.

推荐答案

下面是一个使用的WinForms一个简单的游戏 定时 ,使用 图形 画(封装了GDI +)。

Here's a simple game using WinForms and a Timer, using Graphics to draw (encapsulates GDI+).

它增加了一个计时器,'滴答'每10毫秒。每个剔它执行的游戏逻辑,然后绘制到离屏位图。这是相对于使用连续循环作为在链接的例子。

It adds a timer that 'ticks' every 10 milliseconds. Each tick it performs game logic, then draws to an off screen bitmap. This is as opposed to using a continual loop as in the example in the link.

的形式单独处理关键事件(而不是做这样的事情的GetKeyState

The form handles key events separately (as opposed to doing something like GetKeyState)

当窗体大小,并首次加载时,它会创建大小合适的后备缓冲位图。

When the form is resized, and when it first loads it'll create the backbuffer bitmap of the right size.

创建一个新的形式和下面替换所有代码。使用方向键控制球。有没有死亡的概念

Create a new form and replace all code with below. Control the ball using arrow keys. There's no notion of dying.

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

这篇关于简单的游戏在C#中,只有本地库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆