110,533
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
}
}
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;
}
}
}
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);
}
}
}
}
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);
}
}
}