C#简单的打字游戏问题?

ily5242 2008-09-21 04:49:46
我用C#做了一个打字游戏的软件,字母从屏幕上随机落下时,按下键盘上的字母(A~Z)时,屏幕上落下的字母没有立即消失!请高手指点.....
...全文
377 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenzaiji 2009-05-21
  • 打赏
  • 举报
回复
将keypreview属性设置为true
Deathsign 2008-09-21
  • 打赏
  • 举报
回复
WM_KEYDOWN具体值是多少我记不了
搜索下把
Deathsign 2008-09-21
  • 打赏
  • 举报
回复
焦点必须在FORM上

你可以重载
WndProc然后
msg==WM_KEYDOWN
的消息里面处理 这个样子就无所谓焦点问题了
ily5242 2008-09-21
  • 打赏
  • 举报
回复
请问哪里出错了?
就是键盘响应不了按下的字母的消息!!!
ily5242 2008-09-21
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 我的个人打字游戏
{
public partial class Form1 : Form
{

#region 游戏结束时状态
public void stop()
{
this.timerlab.Enabled = false;
this.timerrate.Enabled = false;
foreach (DictionaryEntry de in _htlab)
{
Label l = (Label)de.Value;
l.Dispose();
}
this.Text = "游戏结束,请从新开始!";
}
#endregion
#region 让屏幕控件飘动方法(飘动速度,飘动范围-在软件上移动的范围)
public void flutter(int time, int value)
{
if (_life <= 0)
{
stop();
MessageBox.Show("生命值为0,游戏结束!", "对不起");

return;
}
this.timerrate.Interval = time;
ArrayList al = new ArrayList(); //存放已经沉淀到最底部的lab在hash中的key
foreach (DictionaryEntry de in _htlab)
{
Label lab = (Label)de.Value;
if (lab.Location.Y >= this.Size.Height - 80) //如果lab坐标大于本程序最底部-50执行
{
lab.Dispose(); //销毁lab
al.Add(de.Key); //把当前hash的key存入arraylist
_life--; //落地减生命
this.Text = "第" + ((_count / 50) + 1) + "关 您的生命值:" + _life + " 分数:" + _count;
}
else
lab.Location = new Point(lab.Location.X, lab.Location.Y + value); //让lab像屏幕下方移动
}
for (int i = 0; i < al.Count; i++) //循环从hash移除已经不显示的lab
{
_htlab.Remove(al[i]);
}
}
#endregion
#region 从屏幕产生字符(产生速度-毫秒)
public void create(int value, int font)
{
this.Text = "第" + ((_count / 50) + 1) + "关 您的生命值:" + _life + " 分数:" + _count;
this.timerlab.Interval = value; //设置产生速度毫秒
Label lab = new Label();
lab.AutoSize = true;
lab.Font = new System.Drawing.Font("Microsoft Sans Serif", font, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); //设置字体
lab.ForeColor = Color.FromArgb(int.Parse(new myRandom().getNumber(0, 255)), int.Parse(new myRandom().getNumber(0, 255)), int.Parse(new myRandom().getNumber(0, 255))); //随机生成颜色
lab.Location = new System.Drawing.Point((this.Size.Width - 50) - int.Parse(new myRandom().getNumber(0, (this.Size.Width - 50))), 28); //屏幕随机出现位置
string _char = new myRandom().getChar(1); //获取随机出来的字符
string labname = ""; //lab名字
int i = 0; //临时计数器存储当前界面上的lab是否存在本次生成的字母个数
foreach (DictionaryEntry de in _htlab)
{
if (de.Key.ToString().IndexOf(_char) != -1)
{
i++;
}
}
labname = _char + (i * DateTime.Now.Millisecond); //设置名字,防止打乱顺序
lab.Name = labname; //设置lab名字
lab.Text = _char; //让此lab显示生成的字符
this.Controls.Add(lab);
_htlab.Add(labname, lab); //把本次随机出的lab放入集合,方便验证
}
#endregion
#region 构造函数
public Form1()
{
InitializeComponent();
}
#endregion
#region 私有字段
Hashtable _htlab = new Hashtable(); //存放lab控件
int _count = 0; //游戏分数
int _life = 10; //生命值,小于0死亡,过关生命10
#endregion
#region 产生字符的线程
private void timerlap_Tick(object sender, EventArgs e)
{
create((_count / 50) > 0 ? 888 - (_count / 50) * 200 : 888, 20); //调用产生字符方法,每100毫秒产生一个字号20的字符
}
#endregion
#region 打字暂停
private void 打字暂停SToolStripMenuItem_Click(object sender, EventArgs e)
{
this.timerlab.Enabled = false;
this.timerrate.Enabled = false;

}
#endregion
#region 让字符飘动的线程
private void timerrate_Tick_1(object sender, EventArgs e)
{
flutter((_count / 50) * 2 + 10, (_count / 50) + 1); //调用飘动方法,设置飘动速度为10毫秒移动1个大小的位置
}
#endregion
#region 打字开始
private void 打字开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.timerlab.Enabled = true;
this.timerrate.Enabled = true;
this.timerrate.Interval =1 ;
this.timerlab.Interval = 1;
_count = 0;
_life = 20;
_htlab = new Hashtable();
}
#endregion
#region 判断用户输入字符
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
ArrayList al = new ArrayList(); //存放已经沉淀到最底部的lab在hash中的key
foreach (DictionaryEntry de in _htlab)
{
if (de.Key.ToString().IndexOf(Convert.ToChar(e.KeyCode)) != -1)
{
Label lab = (Label)_htlab[de.Key];
lab.BackColor = Color.White;
lab.Dispose(); //销毁lab
al.Add(de.Key); //把当前hash的key存入arraylist
_count++;
this.Text = "第" + ((_count / 50) + 1) + "关 您的生命值:" + _life + " 分数:" + _count;
break;
}
}
for (int j = 0; j < al.Count; j++) //循环从hash移除已经不显示的lab
{
_htlab.Remove(al[j]);
}
}
#endregion
JeffChung 2008-09-21
  • 打赏
  • 举报
回复
楼上发了好多代码,呵呵
Deathsign 2008-09-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace 打字游戏1
{
public static class PreSetting
{
/// <summary>
/// 文字下落时的时间间隔
/// </summary>
public static int CharDownTimeSpan = 25;
/// <summary>
/// 每次移动字母的坐标数
/// </summary>
public static int CharDownYOffSet = 2;
/// <summary>
/// 屏幕宽
/// </summary>
public static int ScreenWidth = 800;
/// <summary>
/// 屏幕高
/// </summary>
public static int ScreenHeight = 600;

/// <summary>
/// 添加字符的时间间隔
/// </summary>
public static int CreateCharTimeSpan =100;
}
}
Deathsign 2008-09-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace 打字游戏1
{
public static class Setting
{
/// <summary>
/// 文字下落时的时间间隔
/// </summary>
public static int CharDownTimeSpan = PreSetting.CharDownTimeSpan ;
/// <summary>
/// 每次移动字母的坐标数
/// </summary>
public static int CharDownYOffSet = PreSetting.CharDownYOffSet ;
/// <summary>
/// 屏幕宽
/// </summary>
public static int ScreenWidth = PreSetting.ScreenWidth ;
/// <summary>
/// 屏幕高
/// </summary>
public static int ScreenHeight = PreSetting.ScreenHeight ;

/// <summary>
/// 添加字符的时间间隔
/// </summary>
public static int CreateCharTimeSpan = PreSetting.CreateCharTimeSpan ;
public static void Back()
{
CreateCharTimeSpan = PreSetting.CreateCharTimeSpan;
ScreenHeight = PreSetting.ScreenHeight;
CharDownYOffSet = PreSetting.CharDownYOffSet;
CharDownTimeSpan = PreSetting.CharDownTimeSpan;
}
}
}
Deathsign 2008-09-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace 打字游戏1
{
class Char
{
float _x;
float _y;
char _char;

int _speed=Setting.CharDownYOffSet;
int timeEls = 0;
SpriteBatch sp;
int timeSpan = Setting.CharDownTimeSpan ;
bool Enable = true;
bool _hit = false;
public char GetChar
{
get
{
return _char;
}
}
public bool Hitted
{
get
{
return _hit;
}
}
public bool Missed
{
get
{
return !Enable;
}
}
public Char(float x, float y, char chr)
{
sp = new SpriteBatch(Game1.graphics.GraphicsDevice);
_char = chr;
_x = x; _y = y;
}
public void Update(GameTime gt)
{
timeEls += gt.ElapsedGameTime.Milliseconds;

if (timeEls > timeSpan)
{
timeEls = 0;
_y += _speed;
if (_y > Setting.ScreenHeight)
{
Enable = false;
}
}
}
public void Draw(GameTime gt)
{
sp.Begin();
sp.DrawString(Game1.Font , _char.ToString(), new Vector2(_x, _y), Color.White);
sp.End();
}
public bool GetKey(Keys key)
{
if ((char)key == _char)
{
_hit = true;
return true;
}
return false;
}
}
}
Deathsign 2008-09-21
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace 打字游戏1
{
class Engine
{
int _hitNum=0;
int _missNum=0;
int _downSpeed=10;
int totalCharNum = 20;
long totalTimePassed=1;
int keyPress = 0;
Random rnd = new Random();
SpriteBatch sp;
List<Char> charList = new List<Char>();
public Engine()
{

}
public void Init()
{
sp = new SpriteBatch(Game1.graphics.GraphicsDevice);
charList.Clear();
charList = new List<Char>();
_hitNum = 0;
_missNum = 0;
_downSpeed = 10;
totalCharNum = 20;
totalTimePassed = 1;

}
public void Update(GameTime gt)
{
UpLevel();
totalTimePassed += gt.ElapsedGameTime.Milliseconds ;
if (charList.Count<totalCharNum)
{
NewChar(gt);
}

foreach (Char chr in charList )
{
chr.Update(gt);
}
for (int i = charList.Count - 1; i >= 0; i--)
{
if (charList[i].Missed)
{
charList.RemoveAt(i);
_missNum++;
}

}
for (int i = charList.Count - 1; i >= 0; i--)
{
if (charList[i].Hitted)
{
charList.RemoveAt(i);
_hitNum++;
break;
}
}
}
int preLevelPro;
public void UpLevel()
{
int changeSpan = 50;
if ((preLevelPro / changeSpan) != (_hitNum / changeSpan))
{
preLevelPro = _hitNum;
int tmp = preLevelPro;
Setting.CharDownTimeSpan -= tmp / changeSpan;
Setting.CreateCharTimeSpan -= tmp / changeSpan;
Setting.CharDownYOffSet += tmp / changeSpan;
totalCharNum += tmp / changeSpan;
}
}
public void Draw(GameTime gt)
{

foreach (Char chr in charList)
{
chr.Draw (gt);
}
//Draw State
sp.Begin();
sp.DrawString(Game1.Font, "MISS: " + _missNum.ToString(), new Vector2(0, 0), Color.White);
sp.DrawString(Game1.Font, "HIT: " + _hitNum.ToString(), new Vector2(0, 16), Color.White);
sp.DrawString(Game1.Font, "KEYDOWN: " + (keyPress).ToString(), new Vector2(0, 50), Color.White);
sp.DrawString(Game1.Font, "TIME: " +(totalTimePassed/1000).ToString ()+" S", new Vector2(0, 32), Color.White);
sp.End ();
}
int cUpdateTimeSpan =100;
int cUpdateTimePass = 0;
public void ControlUpdate(GameTime gt, KeyboardState kb)
{
cUpdateTimePass += gt.ElapsedGameTime.Milliseconds;
if (cUpdateTimePass < cUpdateTimeSpan)
{
return;
}
else
{
cUpdateTimePass = 0;
}
Keys[] keys = kb.GetPressedKeys();
foreach (Keys k in keys)
{
keyPress++;
if (k == Keys.F1)
{
Init();
break;
}
foreach (Char chr in charList )
{
if (chr.GetKey(k))
{
return;
}
}
}
}
int newCharTimeSpan =Setting.CreateCharTimeSpan ;
int newCharTimePass = 0;
void NewChar(GameTime gt)
{
newCharTimePass += gt.ElapsedGameTime.Milliseconds;
if (newCharTimePass > newCharTimeSpan)
{
newCharTimePass = 0;
Char chr = new Char(rnd.Next(Setting.ScreenWidth), -20, (char)rnd.Next(65, 90));
charList.Add(chr);
}
}
}
}

Deathsign 2008-09-21
  • 打赏
  • 举报
回复
XNA写的打字游戏


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace 打字游戏1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
public static GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
public static SpriteFont Font;
Engine engine = new Engine();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
engine.Init();
Font = Content.Load<SpriteFont>("Font");
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
engine.ControlUpdate(gameTime, Keyboard.GetState());
engine.Update(gameTime);

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
engine.Draw(gameTime);
base.Draw(gameTime);
}
}
}

霜寒月冷 2008-09-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jiating520 的回复:]
去我空间看看就知道了!

[/Quote]
我去看看!
delphi_new 2008-09-21
  • 打赏
  • 举报
回复
根据键盘事件来响应还不简单吗,
hornbills 2008-09-21
  • 打赏
  • 举报
回复
不错!
花落_ 2008-09-21
  • 打赏
  • 举报
回复
那我说说原理吧:
下落的字母用 lable表示
用随机数产生 a-z
用哈希 表 存储字母,
在屏幕出现的位置随机

监视,用户按键,if(true)消失一个,
统计分数
lable.dispose();
哈希表.remove;

jiating520 2008-09-21
  • 打赏
  • 举报
回复
#region 判断用户输入字符
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
ArrayList al = new ArrayList(); //存放已经沉淀到最底部的lab在hash中的key
foreach (DictionaryEntry de in _htlab)
{
if (de.Key.ToString().IndexOf(Convert.ToChar(e.KeyCode)) != -1)
{
Label lab = (Label)_htlab[de.Key];
lab.BackColor = Color.White;
lab.Dispose(); //销毁lab
al.Add(de.Key); //把当前hash的key存入arraylist
_count++;
this.Text = "第" + ((_count / 50) + 1) + "关 您的生命值:" + _life + " 分数:" + _count;
break;
}
}
for (int j = 0; j < al.Count; j++) //循环从hash移除已经不显示的lab
{
_htlab.Remove(al[j]);
}
}
#endregion
spark_cao 2008-09-21
  • 打赏
  • 举报
回复
代码呢
aimeast 2008-09-21
  • 打赏
  • 举报
回复
不是有一个一样的问题吗?为什么还要再问一个?
jiating520 2008-09-21
  • 打赏
  • 举报
回复
去我空间看看就知道了!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧