WPHZH--闲的蛋疼,打字小游戏,以供初学者参考
c#吧
全部回复
仅看楼主
level 6
kwwwvagaa 楼主
原理很简单 就是创建苹果然后一直向下运动,出了窗体后就移除掉 按下对应字母就消除掉一个
直接看代码吧
2014年10月29日 02点10分 1
level 6
kwwwvagaa 楼主
using System;
using System.Collections.Generic;
using System.Text;
namespace CharDown
{
class CharClass
{
public CharClass(int _x, int _y, char _char, bool _isChecked, int _moveSize)
{
X = _x;
Y = _y;
Char_key = _char;
IsChecked = _isChecked;
MoveSize = _moveSize;
}
private int x;
/// <summary>
/// X
/// </summary>
public int X
{
get { return x; }
set { x = value; }
}
private int y=0;
/// <summary>
/// Y
/// </summary>
public int Y
{
get { return y; }
set { y = value; }
}
private char char_key;
/// <summary>
/// 当前字母
/// </summary>
public char Char_key
{
get { return char_key; }
set { char_key = value; }
}
private bool isChecked=false;
/// <summary>
/// 是否击中
/// </summary>
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; }
}
private int moveSize=10;
/// <summary>
/// 移动距离
/// </summary>
public int MoveSize
{
get { return moveSize; }
set { moveSize = value; }
}
}
}
2014年10月29日 02点10分 2
level 6
kwwwvagaa 楼主
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CharDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<CharClass> charClassList = new List<CharClass>();
List<char> charlist = new List<char>();
static object _obj = new object();
int panelWidth = 0;
int panelHeigh = 0;
bool isStop = false;
int sleepMin = 0;
int sleepMax = 0;
bool islianxi = true;
private char GetChar()
{
lock (_obj)
{
char c;
string strs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
char[] chars = strs.ToCharArray();
Random r = new Random();
int _index = r.Next(0, chars.Length);
c = chars[_index];
return c;
}
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
switch (this.toolStripComboBox1.SelectedIndex)
{
case 0: sleepMin = 100; sleepMax = 200; break;
case 1: sleepMin = 50; sleepMax = 100; break;
case 2: sleepMin = 10; sleepMax = 50; break;
}
if (this.toolStripComboBox2.SelectedIndex == 0)
{
islianxi = true;
}
else
{
islianxi = false;
}
panelWidth = this.panel1.Width - 30;
panelHeigh = this.panel1.Height - 20;
this.toolStripLabel2.Text = "0";
this.toolStripLabel2.ForeColor = Color.Black;
this.Focus();
//this.panel1.Focus();
isStop = false;
Thread server = new Thread(new ThreadStart(Server));
server.IsBackground = true;
server.Start();
// Server();
}
private void Server()
{
while (true && !isStop)
{
Thread _th = new Thread(new ThreadStart(Begin));
_th.IsBackground = true;
_th.Start();
Thread.Sleep(500);
}
}
private void Begin()
{
char _char;
while (true)
{
_char = GetChar();
if (!charlist.Contains(_char))
{
break;
}
}
charlist.Add(_char);
Random r = new Random();
int x = r.Next(0, panelWidth);
int sleeptime = r.Next(sleepMin, sleepMax);
CharClass _class = new CharClass(x, 0, _char, false, 5);
charClassList.Add(_class);
Color c = this.panel1.BackColor;
Font font = new System.Drawing.Font("宋体", 20, FontStyle.Bold);
Graphics g = this.panel1.CreateGraphics();
while (true && !isStop)
{
Bitmap image = new Bitmap(Application.StartupPath + "\\app.png");
if (_class.IsChecked)
{
ClearImage(_class, g, c, image);
// ClearString(_class, g, c);
charlist.Remove(_class.Char_key);
charClassList.Remove(_class);
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
{
isStop = true;
MessageBox.Show("恭喜您通过了");
}
}
break;
}
DrawImage(_class, g, c, font, image);
//DrawString(_class, g, c, font);
if (_class.Y >= panelHeigh)
{
ClearImage(_class, g, c, image);
//ClearString(_class, g, c);
charlist.Remove(_class.Char_key);
charClassList.Remove(_class);
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
{
isStop = true;
MessageBox.Show("游戏结束");
}
}
return;
}
Thread.Sleep(sleeptime);
}
g.Clear(c);
// ClearString(_class, g, c);
}
private void DrawString(CharClass _class, Graphics g, Color c, Font font)
{
if (_class.Y != 0)
{
ClearString(_class, g, c);
}
_class.Y = _class.Y + _class.MoveSize;
g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), _class.X, _class.Y);
}
private void DrawImage(CharClass _class, Graphics gp, Color c, Font font, Bitmap image)
{
Graphics g = Graphics.FromImage(image);
g.DrawString(_class.Char_key.ToString(), font, new SolidBrush(Color.Black), image.Width / 2 - 13, image.Height / 2 - 10);
if (_class.Y != 0)
{
ClearImage(_class, gp, c, image);
}
_class.Y = _class.Y + _class.MoveSize;
gp.DrawImage(image, _class.X, _class.Y);
}
private void ClearImage(CharClass _class, Graphics g, Color c, Bitmap image)
{
g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, image.Width, image.Height);
}
private void ClearString(CharClass _class, Graphics g, Color c)
{
g.FillRectangle(new SolidBrush(c), _class.X, _class.Y, 30, 30);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
isStop = true;
Graphics g = this.panel1.CreateGraphics();
g.Clear(this.panel1.BackColor);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int key = e.KeyValue;
if (key >= 65 && key <= 90)
{
if (charlist.Contains(Convert.ToChar(e.KeyCode)))
{
foreach (CharClass c in charClassList)
{
if (c.Char_key.ToString().ToUpper() == e.KeyCode.ToString().ToUpper())
{
c.IsChecked = true;
if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) + 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) == 100)
{
isStop = true;
MessageBox.Show("恭喜您通过了");
}
}
return;
}
}
}
else if (!islianxi)
{
this.toolStripLabel2.Text = (Convert.ToInt32(this.toolStripLabel2.Text) - 1).ToString();
if (Convert.ToInt32(this.toolStripLabel2.Text) < 10)
{
this.toolStripLabel2.ForeColor = Color.Red;
}
if (Convert.ToInt32(this.toolStripLabel2.Text) < 0)
{
isStop = true;
MessageBox.Show("游戏结束");
}
}
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
this.toolStripComboBox1.SelectedIndex = 0;
this.toolStripComboBox2.SelectedIndex = 0;
}
}
}
2014年10月29日 02点10分 3
level 6
kwwwvagaa 楼主
源码[无效] http://pan.baidu.com/s/1ntBOT9f 密码: o8x3
源码程序
2014年10月29日 02点10分 4
level 8
java写过一个......
2014年10月29日 02点10分 5
level 9
好帖,看出你是闲的了
2014年10月29日 03点10分 6
level 12
此乃好帖!
还有什么好说的,鼓掌啊!
2014年10月29日 06点10分 7
level 10
为嘛运行楼主的源码没有那个苹果
2014年10月29日 06点10分 8
点击开始就可以了
2014年10月29日 08点10分
回复 kwwwvagaa :开始也没那个苹果的,只是有字母
2014年10月29日 08点10分
level 9
挺好的 [大拇指]
2014年10月29日 06点10分 9
level 9
不好意思,马上升级,只是为了那该死的十五个字!
2014年10月29日 08点10分 10
1