示例游戏第一部分(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/sample-game-part-i-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 4 分钟阅读 - 1737 个词 阅读量 0示例游戏第一部分(译文)
原文地址:https://www.codeproject.com/Articles/19227/Sample-Game-Part-I
原文作者: Muammar©
译文由本站 robot-v1.0 翻译
前言
Basic game designing issues
基本游戏设计问题
介绍(Introduction)
这是一个示例游戏,演示了如何阅读键盘按键和移动角色.(This is a sample game to demonstrate reading keyboard strokes and moving a character.) 不知何故,这是一款类似于太空侵略者的游戏,除了没有什么可以射击的!(Somehow, it’s a space invaders-like game, except that there’s nothing to shoot!!) 您还可以控制角色和项目符号的速度.(You can also control the speed of the character and bullet.)
初始化游戏(Initializing the Game)
游戏首先初始化子弹和角色的速度:(The game starts by initializing the bullet and character’s speed:)
bullet_speed = 4;
character_speed = 2;
注意,我在数字列表中使用了相同的值.(Notice that I’ve used the same values in the numlists.)
移动角色(Moving the Character)
为了移动字符,我们首先读取当前按下的键,然后确定要在表单边界内移动的方向(稍后说明).(In order to move the character, we start by reading the currently pressed key and determine which direction to move within the form boundaries (explained later).)
通过每次在键值的相同方向上创建一个新点并添加(The character is moved by creating a new point each time in the same direction of the key’s value and adding the) character_speed
到这一点.(to that point.)
private void MainCourse_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode.ToString())
{
case "Up":
...
}
}
在表格内移动(Moving Within the Form)
为了限制字符在表格边界内的移动,我必须首先计算允许的区域.只需通过从一种形式的尺寸中减去一个字符尺寸并将该数字设置为沿所述方向移动的条件,即可完成此操作:(To restrict character movement within the form boundaries, I had to first calculate the allowed area. This was performed simply by subtracting one character dimension from one form’s dimension and setting that number as a condition to move in the said direction:)
case "Right":
if(Character.Location.X < (this.Size.Width - (Character.Width+10)))
Character.Location = new Point(Character.Location.X
+ character_speed, Character.Location.Y);
break;
case "Down":
if(Character.Location.Y < (this.Size.Height - (Character.Height+29)))
Character.Location = new Point(Character.Location.X,
Character.Location.Y + character_speed);
break;
注意只有(Notice that only) Right
和(and) Down
必须注意方向.(directions have to be watched.) Left
和(and) Up
当然是已知的(0,0).(are of course already known (0,0).)
射击!!(Shoot!!)
我知道我们没什么可射击的!但是,请足够好考虑这一部分.(I know we have nothing to shoot!! However, please be nice enough to consider this part.)
首先,我们需要初始化(First, we need to initialize the) bullet location
(在这种情况下,((in this case, it’s the) character
的(’s) location
).().)
Bullet.Location = Character.Location;
现在我们设置(Now we set our) bullet
一个出现的位置,我们可以叫她别再害羞了!(a location to appear, we can call her to stop being shy and come!!)
Bullet.Visible = true;
接下来的两行将以起点为基础(The next two lines are to have a base for the starting) bullet
位置,否则角色将不会退出!!(position, otherwise, it won’t quit following the character!!)
bullet_x = Convert.ToInt32(Character.Location.X);
bullet_y = Convert.ToInt32(Character.Location.Y);
杀死杀手(Killing the Killer)
可怜的小(Poor little) bullet
,我认为这是唯一在游戏中被杀死的东西!(, I think it’s the only thing that gets killed in this game!!)
再一次,我们必须确定(Once again, we have to determine the) bullet
的(’s) location
为了终止它:(in order to terminate it:)
if(Bullet.Location.Y >= Bullet.Height)
{
Bullet.Location = new Point(bullet_x,bullet_y);
bullet_y -= bullet_speed;
}
else
kill_bullet();
的(The) bullet
只需停止计时器并将其可见性重新设置为(is simply killed by stopping its timer and setting its visibility back again to) false
.(.)
Shoot.Stop();
Bullet.Visible = false;
我认为现在是时候我们一起来看这件事了.(I think it’s about time we look at it all together.)
代码(The Code)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using tonysound;
namespace MoveMe
{
public partial class Game_sample : Form
{
int bullet_speed;
int character_speed;
int bullet_x;
int bullet_y;
public Game_sample()
{
InitializeComponent();
bullet_speed = 4;
character_speed = 2;
}
private void MainCourse_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode.ToString())
{
case "Up":
if (Character.Location.Y > 1)
Character.Location = new Point
(Character.Location.X, Character.Location.Y -
character_speed);
break;
case "Left":
if (Character.Location.X > 1)
Character.Location = new Point
(Character.Location.X - character_speed,
Character.Location.Y);
break;
case "Right":
if (Character.Location.X <
(this.Size.Width - (Character.Width+10)))
Character.Location = new Point
(Character.Location.X + character_speed,
Character.Location.Y);
break;
case "Down":
if (Character.Location.Y <
(this.Size.Height - (Character.Height+29)))
Character.Location = new Point
(Character.Location.X, Character.Location.Y +
character_speed);
break;
case "Space":
if (!Shoot.Enabled)
{
Bullet.Location = Character.Location;
Bullet.Visible = true;
//getting the current location of
bullet_x = Convert.ToInt32(Character.Location.X);
//the character to start with
bullet_y = Convert.ToInt32(Character.Location.Y);
Sound.Play(Environment.GetFolderPath
(Environment.SpecialFolder.ProgramFiles)
+ @"\Windows NT\Pinball\SOUND8.WAV",
PlaySoundFlags.SND_ASYNC);
Shoot.Start();
}
break;
case "Escape":
btnHide_Options.Text = "&Hide";
Options_panel.Location= new Point
((this.Width - Options_panel.Width) - 15, 5);
Options_panel.Visible = true;
btnHide_Options.Focus();
break;
default:
MessageBox.Show(@"
Up: Move up
Right: Move right
Left: Move left
Down: Move down
Space Bar: Fire
Esc: Game Options","Sample
game",MessageBoxButtons.OK,
MessageBoxIcon.Information);
break;
}
}
private void kill_bullet()
{
Shoot.Stop();
Bullet.Visible = false;
}
private void Shoot_Tick(object sender, EventArgs e)
{
if(Bullet.Location.Y >= Bullet.Height)
{
Bullet.Location = new Point(bullet_x, bullet_y);
bullet_y -= bullet_speed;
}
else
kill_bullet();
}
private void Close_Options_Click(object sender, EventArgs e)
{
bullet_speed = Convert.ToInt32(numBullSpeed.Value);
character_speed = Convert.ToInt32(numCharSpeed.Value);
Options_panel.Visible = false;
this.Focus();
}
private void Game_sample_Load(object sender, EventArgs e)
{
string strSystem = "system32";
//replace the music path in the following line to your background music
Sound.Play(Environment.GetFolderPath
(Environment.SpecialFolder.System).TrimEnd
(strSystem.ToCharArray())
+ @"\Help\Tours\WindowsMediaPlayer\Audio\Wav\wmpaud3.wav",
PlaySoundFlags.SND_ASYNC);
}
private void chMute_CheckedChanged(object sender, EventArgs e)
{
if(chMute.Checked)
Sound.Play(@"Nothing.wav", PlaySoundFlags.SND_ASYNC);
else
Sound.Play(Environment.GetFolderPath
(Environment.SpecialFolder.System)
+ @"\oobe\images\title.wma", PlaySoundFlags.SND_ASYNC);
}
private void lnkAbout_LinkClicked
(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("Sample Game\nBy Muammar Yacoob",
"The CodeProject", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private void numCharSpeed_ValueChanged(object sender, EventArgs e)
{
Sound.Play(Environment.GetFolderPath
(Environment.SpecialFolder.ProgramFiles) +
@"\Windows NT\Pinball\SOUND16.WAV", PlaySoundFlags.SND_ASYNC);
}
private void numBullSpeed_ValueChanged(object sender, EventArgs e)
{
Sound.Play(Environment.GetFolderPath
(Environment.SpecialFolder.ProgramFiles) +
@"\Windows NT\Pinball\SOUND16.WAV", PlaySoundFlags.SND_ASYNC);
}
}
}
局限性(Limitations)
我无法同时播放两个声音,任何类似的体验都将不胜感激.(I couldn’t cope up with playing two sounds simultaneously, any similar experience will be highly appreciated.)
笔记(Notes)
为了减小演示的大小,我使用了Windows的2种音效和1种背景音乐,因此请确保它们已存在于您的PC上,否则请用有效的替代它们.(In order to decrease the demo size, I’ve used 2 sound effects and 1 background music from Windows, so make sure they exist on your PC, otherwise replace them with valid ones.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 WinXP Windows .NET .NET2.0 Visual-Studio VS2005 Dev 新闻 翻译