引擎(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/emuengine-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1329 个词 阅读量 0引擎(译文)
原文地址:https://www.codeproject.com/Articles/16413/EmuEngine
原文作者:sniffi
译文由本站 robot-v1.0 翻译
前言
With the EmuEngine, you can create simple 2D games in C#.
使用EmuEngine,您可以使用C#创建简单的2D游戏.
介绍(Introduction)
使用EmuEngine,您可以使用C#创建简单的2D游戏,这是一个具有以下类的小型图书馆:(With the EmuEngine, you can create simple 2D games in C#, it’s a small library with the following classes:) 使用EmuEngine,您可以使用C#创建简单的2D游戏,这是一个具有以下类的小型图书馆:(With the EmuEngine, you can create simple 2D games in C#, it’s a small library with the following classes:)
DrawingArea
:这是"主要"类别.它处理(: This is the “main” class. It handles)Picture
对象,(objects,)PictureWall
对象,以及(objects, and)PixelWall
对象.它还包括一个(objects. It also includes a)Collision
功能.仅此而已,但是对于示例项目中的测试游戏之类的简单2D游戏而言,已经足够了.(function. Nothing more, but it’s enough for a simple 2D game like the test game in the sample project.)Picture
:代表图片.的(: It represents a picture. The)DrawingArea
类具有一个称为(class has a function called)AddPicture
.使用该功能,您可以在游戏中添加图片.(. With that function, you can add a picture into your game.)PictureWall
:与Picture类几乎相同.您可以添加一个(: nearly the same as the Picture class. You can add a)PictureWall
与对象(object with the)AddPictureWall
的功能(function of the)DrawingArea
归类到您的游戏中.(class into your game.)PixelWall
:构造函数吃一个(: The constructor eats a)Color
,这是在游戏中添加彩色墙的好方法.(, it’s a nice way to add a colored wall into your game.) 项目中测试应用程序的代码:(The code of the test application in the project:)
public partial class Form1 : Form
{
public MediaPlayer status;
public MediaPlayer eat;
public delegate void Walk(int speed);
static int speed = 0;
static int score = 0;
Walk move;
DrawingArea area;
Picture player;
Picture enemy;
Picture gameOver;
Picture gameWin;
public Form1()
{
InitializeComponent();
area = new DrawingArea(doubleBufferedPanel1);
player = new Picture("pacmanUp.GIF", 300, 100, 30, 30);
enemy = new Picture("enemy.jpg", 100, 100, 20, 23);
area.AddPicture(new Picture("Food.gif", 200, 50, 10, 10));
area.AddPicture(new Picture("Food.gif", 200, 50, 10, 10));
area.AddPicture(new Picture("Food.gif", 100, 200, 10, 10));
area.AddPicture(new Picture("Food.gif", 300, 300, 10, 10));
area.AddPicture(new Picture("Food.gif", 400, 54, 10, 10));
area.AddPicture(new Picture("Food.gif", 150, 200, 10, 10));
area.AddPicture(new Picture("Food.gif", 500, 400, 10, 10));
area.AddPicture(new Picture("Food.gif", 123, 400, 10, 10));
area.AddPicture(new Picture("Food.gif", 345, 65, 10, 10));
area.AddPicture(new Picture("Food.gif", 234, 40, 10, 10));
area.AddPicture(new Picture("Food.gif", 46, 400, 10, 10));
area.AddPicture(new Picture("Food.gif", 123, 300, 10, 10));
area.AddPicture(new Picture("Food.gif", 321, 123, 10, 10));
gameOver = new Picture("gameover.gif", 140, 100, 280, 281);
gameWin = new Picture("gamewin.gif", 140, 100, 280, 281);
area.AddPixelWall(new PixelWall(Color.LightBlue, 30,30, 5,
doubleBufferedPanel1.Height - 60));
area.AddPixelWall(new PixelWall(Color.LightBlue, 30, 30,
doubleBufferedPanel1.Width - 60, 5));
area.AddPixelWall(new PixelWall(Color.LightBlue,
doubleBufferedPanel1.Width - 30, 30, 5,
doubleBufferedPanel1.Height - 60));
area.AddPixelWall(new PixelWall(Color.LightBlue, 30,
doubleBufferedPanel1.Height - 30,
doubleBufferedPanel1.Width - 55, 5));
area.AddPicture(player);
area.AddPicture(enemy);
timer1.Enabled = true;
move = new Walk(player.MoveUp);
speed = 1;
score = 0;
eat = new MediaPlayer(doubleBufferedPanel1.Handle);
eat.Open("pacchomp.wav");
status = new MediaPlayer(doubleBufferedPanel1.Handle);
status.Open(@"GAMEBEGINNING.wav");
status.Play(false);
}
private void timer1_Tick(object sender, EventArgs e)
{
area.UpdateScreen();
if (area.PixelWallCollision(player) != null)
{
status.Close();
eat.Dispose();
status.Open(@"killed.wav");
status.Play();
area.DeleteAllPictures();
timer1.Enabled = false;
area.AddPicture(gameOver);
}
if (area.Collision(player) != null)
{
eat.Stop();
eat.Play();
area.DeletePicture(area.Collision(player));
if (area.Pictures.Count == 2)
{
area.DeleteAllPictures();
area.AddPicture(gameWin);
timer1.Enabled = false;
}
speed++;
score++;
label1.Text = score.ToString();
}
if (enemy.Catch(player, 2))
{
status.Close();
eat.Dispose();
status.Open(@"killed.wav");
status.Play();
area.DeleteAllPictures();
timer1.Enabled = false;
area.AddPicture(gameOver);
doubleBufferedPanel1.Invalidate();
}
move(speed);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
player.Image = Image.FromFile("pacmanUp.gif");
move = new Walk(player.MoveUp);
}
if (e.KeyCode == Keys.Down)
{
player.Image = Image.FromFile("pacmanDown.gif");
move = new Walk(player.MoveDown);
}
if (e.KeyCode == Keys.Right)
{
player.Image = Image.FromFile("pacmanRight.gif");
move = new Walk(player.MoveRight);
}
if (e.KeyCode == Keys.Left)
{
player.Image = Image.FromFile("pacmanLeft.gif");
move = new Walk(player.MoveLeft);
}
}
}
我认为这是编写游戏代码的简便方法.您只需要知道(It’s an easy way to code a game I think. All that you have to know is that your) Form
应该有一个(should have a) Timer
检查是否发生任何碰撞,然后致电(to check if any collision has happened and then call) UpdateScreen()
(重画场景).((redraws the scene).)
更新资料(Update)
的(The) Picture
类获得了一个新函数,名为(class got a new function named) Catch(Picture target, int difficultLevel)
.使用此功能,您可以轻松添加(. With this function, you can easily add) enemy
s和(s, and the) Paint
控件的功能位于集成的绘图区域中,因此您只需调用(function of your control is in the drawing area integrated, so you just have to call) DrawingArea.UpdateScreen()
在里面(in the) TimerTick
功能绘制新场景.(function to draw the new scene.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET Windows GDI+ Visual-Studio Dev 新闻 翻译