#region File Description
//-----------------------------------------------------------------------------
// Game1.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BeginnersGuide_2DGame_Windows
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Texture2D backgroundTexture;
Rectangle viewportRect;
SpriteBatch spriteBatch;
GameObject cannon;
const int maxCannonBalls = 30;
GameObject[] cannonBalls;
GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
KeyboardState previousKeyboardState = Keyboard.GetState();
const int maxEnemies = 80;
const int maxEnemies1 = 80;
const float maxEnemyHeight = 0.1f;
const float minEnemyHeight = 0.6f;
const float maxEnemyVelocity = 7.0f;
const float minEnemyVelocity = 3.0f;
Random random = new Random();
GameObject[] enemies;
GameObject[] enemies1;
int score;
SpriteFont font;
Vector2 scoreDrawPoint = new Vector2(0.1f, 0.1f);
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()
{
base.Initialize();
}
/// <summary>
/// Load your graphics content
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
backgroundTexture =
Content.Load<Texture2D>("Sprites\\background");
cannon = new GameObject(Content.Load<Texture2D>("Sprites\\cannon"));
cannon.position = new Vector2(50, graphics.GraphicsDevice.Viewport.Height - 80);
cannonBalls = new GameObject[maxCannonBalls];
for (int i = 0; i < maxCannonBalls; i++)
{
cannonBalls[i] = new GameObject(Content.Load<Texture2D>(
"Sprites\\cannonball"));
}
enemies = new GameObject[maxEnemies];
for (int i = 0; i < maxEnemies; i++)
{
enemies[i] = new GameObject(
Content.Load<Texture2D>("Sprites\\enemy"));
}
enemies1 = new GameObject[maxEnemies];
for (int i = 0; i < maxEnemies; i++)
{
enemies[i] = new GameObject(
Content.Load<Texture2D>("Sprites\\enemy"));
}
font = Content.Load<SpriteFont>("Fonts\\GameFont");
//drawable area of the game screen.
viewportRect = new Rectangle(0, 0,
graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
base.LoadContent();
}
public void UpdateCannonBalls()
{
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
ball.position += ball.velocity;
if (!viewportRect.Contains(new Point(
(int)ball.position.X,
(int)ball.position.Y)))
{
ball.alive = false;
continue;
}
Rectangle cannonBallRect = new Rectangle(
(int)ball.position.X,
(int)ball.position.Y,
ball.sprite.Width,
ball.sprite.Height);
foreach (GameObject enemy in enemies)
{
Rectangle enemyRect = new Rectangle(
(int)enemy.position.X,
(int)enemy.position.Y,
enemy.sprite.Width,
enemy.sprite.Height);
if (cannonBallRect.Intersects(enemyRect))
{
ball.alive = true;
enemy.alive = false;
{
score += 7;
break;
}
}
foreach (GameObject enemy1 in enemies)
{
Rectangle enemy1Rect = new Rectangle(
(int)enemy.position.X,
(int)enemy.position.Y,
enemy.sprite.Width,
enemy.sprite.Height);
if (cannonBallRect.Intersects(enemyRect))
{
ball.alive = true;
enemy.alive = false;
{
score += 10000;
break;
}
}
}
}
}
}
}
/// <summary>
/// Unload any content not managed by the Content Manager
/// </summary>
protected override void UnloadContent()
{
base.UnloadContent();
}
/// <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();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
cannon.rotation += gamePadState.ThumbSticks.Left.X * 0.1f;
if (gamePadState.Buttons.A == ButtonState.Pressed &&
previousGamePadState.Buttons.A == ButtonState.Released)
{
FireCannonBall();
}
#if !XBOX
KeyboardState keyboardState = Keyboard.GetState();
if(keyboardState.IsKeyDown(Keys.Left))
{
cannon.rotation -= 0.05f;
}
if(keyboardState.IsKeyDown(Keys.Right))
{
cannon.rotation += 0.05f;
}
if (keyboardState.IsKeyDown(Keys.Space) &&
previousKeyboardState.IsKeyUp(Keys.Space))
{
FireCannonBall();
}
#endif
cannon.rotation = MathHelper.Clamp(cannon.rotation, -MathHelper.PiOver2, 0);
UpdateCannonBalls();
UpdateEnemies();
// TODO: Add your update logic here
previousGamePadState = gamePadState;
#if !XBOX
previousKeyboardState = keyboardState;
#endif
base.Update(gameTime);
}
public void UpdateEnemies()
{
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
enemy.position += enemy.velocity;
if (!viewportRect.Contains(new Point(
(int)enemy.position.X,
(int)enemy.position.Y)))
{
enemy.alive = false;
}
}
else
{
enemy.alive = true;
enemy.position = new Vector2(
viewportRect.Right,
MathHelper.Lerp(
(float)viewportRect.Height * minEnemyHeight,
(float)viewportRect.Height * maxEnemyHeight,
(float)random.NextDouble()));
enemy.velocity = new Vector2(
MathHelper.Lerp(
-minEnemyVelocity,
-maxEnemyVelocity,
(float)random.NextDouble()), 0);
}
}
}
public void FireCannonBall()
{
foreach (GameObject ball in cannonBalls)
{
if (!ball.alive)
{
ball.alive = true;
ball.position = cannon.position - ball.center;
ball.velocity = new Vector2(
(float)Math.Cos(cannon.rotation),
(float)Math.Sin(cannon.rotation)) * 8.0f;
return;
}
}
}
/// <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);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
//Draw the backgroundTexture sized to the width
//and height of the screen.
spriteBatch.Draw(backgroundTexture, viewportRect,
Color.White);
foreach (GameObject ball in cannonBalls)
{
if (ball.alive)
{
spriteBatch.Draw(ball.sprite,
ball.position, Color.White);
}
}
spriteBatch.Draw(cannon.sprite,
cannon.position,
null,
Color.White,
cannon.rotation,
cannon.center, 1.0f,
SpriteEffects.None, 0);
foreach (GameObject enemy in enemies)
{
if (enemy.alive)
{
spriteBatch.Draw(enemy.sprite,
enemy.position, Color.White);
}
}
spriteBatch.DrawString(font,
"Score: " + score.ToString(),
new Vector2(scoreDrawPoint.X * viewportRect.Width,
scoreDrawPoint.Y * viewportRect.Height),
Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
The World of MingHua
2010年12月10日 星期五
2010年11月11日 星期四
小算盤進階版(未完成)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
System.Windows.Forms.TextBox[] TextBoxs;
int Case, i;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBoxs = new System.Windows.Forms.TextBox[1];
TextBoxs[0] = new TextBox();
this.Controls.Add(TextBoxs[0]);
TextBoxs[0].Size = new Size(190, 10);
TextBoxs[0].Location = new System.Drawing.Point(10, 60);
TextBoxs[0].Enabled = false;
Buttons = new System.Windows.Forms.Button[16];
for (int i = 0; i < 16; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
Buttons[i].Size = new Size(40, 40);
this.Controls.Add(Buttons[i]);
if (i <= 3)
Buttons[i].Location = new System.Drawing.Point(10 + i * 50, 250);
else if (i > 3 && i <= 7)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 4) * 50, 200);
else if (i > 7 && i <= 11)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 8) * 50, 150);
else if (i > 11 && i <= 15)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 12) * 50, 100);
}
Buttons[0].Text = "0";
Buttons[1].Text = "00";
Buttons[2].Text = "=";
Buttons[3].Text = "+";
Buttons[4].Text = "1";
Buttons[5].Text = "2";
Buttons[6].Text = "3";
Buttons[7].Text = "-";
Buttons[8].Text = "4";
Buttons[9].Text = "5";
Buttons[10].Text = "6";
Buttons[11].Text = "*";
Buttons[12].Text = "7";
Buttons[13].Text = "8";
Buttons[14].Text = "9";
Buttons[15].Text = "/";
}
public void Buttons_Click(object sender, EventArgs e)
{
Buttons[0].Click+=new EventHandler( Buttons0_Click) ;
Buttons[4].Click += new EventHandler(Buttons4_Click);
}
public void Buttons0_Click(object sender, EventArgs e)
{
TextBoxs[0].Text+=0;
}
public void Buttons4_Click(object sender, EventArgs e)
{
TextBoxs[0].Text = TextBoxs[0].Text + "1";
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
System.Windows.Forms.TextBox[] TextBoxs;
int Case, i;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBoxs = new System.Windows.Forms.TextBox[1];
TextBoxs[0] = new TextBox();
this.Controls.Add(TextBoxs[0]);
TextBoxs[0].Size = new Size(190, 10);
TextBoxs[0].Location = new System.Drawing.Point(10, 60);
TextBoxs[0].Enabled = false;
Buttons = new System.Windows.Forms.Button[16];
for (int i = 0; i < 16; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
Buttons[i].Size = new Size(40, 40);
this.Controls.Add(Buttons[i]);
if (i <= 3)
Buttons[i].Location = new System.Drawing.Point(10 + i * 50, 250);
else if (i > 3 && i <= 7)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 4) * 50, 200);
else if (i > 7 && i <= 11)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 8) * 50, 150);
else if (i > 11 && i <= 15)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 12) * 50, 100);
}
Buttons[0].Text = "0";
Buttons[1].Text = "00";
Buttons[2].Text = "=";
Buttons[3].Text = "+";
Buttons[4].Text = "1";
Buttons[5].Text = "2";
Buttons[6].Text = "3";
Buttons[7].Text = "-";
Buttons[8].Text = "4";
Buttons[9].Text = "5";
Buttons[10].Text = "6";
Buttons[11].Text = "*";
Buttons[12].Text = "7";
Buttons[13].Text = "8";
Buttons[14].Text = "9";
Buttons[15].Text = "/";
}
public void Buttons_Click(object sender, EventArgs e)
{
Buttons[0].Click+=new EventHandler( Buttons0_Click) ;
Buttons[4].Click += new EventHandler(Buttons4_Click);
}
public void Buttons0_Click(object sender, EventArgs e)
{
TextBoxs[0].Text+=0;
}
public void Buttons4_Click(object sender, EventArgs e)
{
TextBoxs[0].Text = TextBoxs[0].Text + "1";
}
}
}
小算盤基本版
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int x,y,z;
Boolean a = false;
Boolean b = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
button1.Text = "0";
button2.Text = "00";
button3.Text = "=";
button4.Text = "1";
button5.Text = "2";
button6.Text = "3";
button7.Text = "4";
button8.Text = "5";
button9.Text = "6";
button10.Text = "7";
button11.Text = "8";
button12.Text = "9";
button13.Text = "+";
button14.Text = "-";
button15.Text = "*";
button16.Text = "/";
}
private void button1_Click(object sender, EventArgs e)
{
if (a == false)
{
}
else
{
textBox1.Text += 0;
}
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
a = true;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += 2;
a = true;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += 3;
a = true;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += 4;
a = true;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += 5;
a = true;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += 6;
a = true;
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += 7;
a = true;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += 8;
a = true;
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += 9;
a = true;
}
private void button16_Click(object sender, EventArgs e)
{
if (b == true)
{
}
else
{
x = int.Parse(textBox1.Text);
textBox1.Text = "";
a = false;
b = true;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (a == true & b == true)
{
y = int.Parse(textBox1.Text);
z = x / y;
textBox1.Text=""+z;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int x,y,z;
Boolean a = false;
Boolean b = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
button1.Text = "0";
button2.Text = "00";
button3.Text = "=";
button4.Text = "1";
button5.Text = "2";
button6.Text = "3";
button7.Text = "4";
button8.Text = "5";
button9.Text = "6";
button10.Text = "7";
button11.Text = "8";
button12.Text = "9";
button13.Text = "+";
button14.Text = "-";
button15.Text = "*";
button16.Text = "/";
}
private void button1_Click(object sender, EventArgs e)
{
if (a == false)
{
}
else
{
textBox1.Text += 0;
}
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += 1;
a = true;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += 2;
a = true;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += 3;
a = true;
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += 4;
a = true;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += 5;
a = true;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += 6;
a = true;
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += 7;
a = true;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += 8;
a = true;
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += 9;
a = true;
}
private void button16_Click(object sender, EventArgs e)
{
if (b == true)
{
}
else
{
x = int.Parse(textBox1.Text);
textBox1.Text = "";
a = false;
b = true;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (a == true & b == true)
{
y = int.Parse(textBox1.Text);
z = x / y;
textBox1.Text=""+z;
}
}
}
}
2010年11月5日 星期五
井字遊戲
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Buttons = new System.Windows.Forms.Button[9];
//Buttons[i].Size=new Size(50,50);
for (int i = 0; i < 9; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
this.Controls.Add(Buttons[i]);
if (i <= 2)
Buttons[i].Location = new System.Drawing.Point(10 + i * 100, 10);
else if (i > 2 && i <= 5)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
else if (i > 5 && i <= 9)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 100, 70);
}
}
public void Buttons_Click(object sender, EventArgs e)
{
int bno = 0;
int i = 0;
Buttons[i].Text = Buttons[bno].Text;
bno = i;
switch (bno)
{
case 0:
Buttons[0].Image = pictureBox2.Image;
break;
case 1:
Buttons[1].Image = pictureBox2.Image;
break;
case 2:
Buttons[2].Image = pictureBox2.Image;
break;
case 3:
Buttons[3].Image = pictureBox2.Image;
break;
case 4:
Buttons[4].Image = pictureBox2.Image;
break;
case 5:
Buttons[5].Image = pictureBox2.Image;
break;
case 6:
Buttons[6].Image = pictureBox2.Image;
break;
case 7:
Buttons[7].Image = pictureBox2.Image;
break;
case 8:
Buttons[8].Image = pictureBox2.Image;
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
Random Rd = new Random();
int Rdno = Rd.Next(9);
label1.Text = "" + Rdno;
Buttons[Rdno].Image =pictureBox1.Image;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Buttons = new System.Windows.Forms.Button[9];
//Buttons[i].Size=new Size(50,50);
for (int i = 0; i < 9; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
this.Controls.Add(Buttons[i]);
if (i <= 2)
Buttons[i].Location = new System.Drawing.Point(10 + i * 100, 10);
else if (i > 2 && i <= 5)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
else if (i > 5 && i <= 9)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 100, 70);
}
}
public void Buttons_Click(object sender, EventArgs e)
{
int bno = 0;
int i = 0;
Buttons[i].Text = Buttons[bno].Text;
bno = i;
switch (bno)
{
case 0:
Buttons[0].Image = pictureBox2.Image;
break;
case 1:
Buttons[1].Image = pictureBox2.Image;
break;
case 2:
Buttons[2].Image = pictureBox2.Image;
break;
case 3:
Buttons[3].Image = pictureBox2.Image;
break;
case 4:
Buttons[4].Image = pictureBox2.Image;
break;
case 5:
Buttons[5].Image = pictureBox2.Image;
break;
case 6:
Buttons[6].Image = pictureBox2.Image;
break;
case 7:
Buttons[7].Image = pictureBox2.Image;
break;
case 8:
Buttons[8].Image = pictureBox2.Image;
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
Random Rd = new Random();
int Rdno = Rd.Next(9);
label1.Text = "" + Rdno;
Buttons[Rdno].Image =pictureBox1.Image;
}
}
}
2010年11月4日 星期四
4x4矩陣試做版
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
System.Windows.Forms.TextBox[] TextBoxes;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBoxes = new System.Windows.Forms.TextBox[46];
for (int i = 1; i <= 16; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 4)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 1) * 106, 12);
else if (i > 4 && i <= 8)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 5) * 106, 40);
else if (i > 8 && i <= 12)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 9) * 106, 68);
else if (i > 12 && i <= 16)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 13) * 106, 96);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
for (int i = 17; i <= 33; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 20)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 17) * 106, 12);
else if (i > 20 && i <= 24)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 21) * 106, 40);
else if (i > 24 && i <= 28)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 25) * 106, 68);
else if (i > 28 && i <= 32)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 29) * 106, 96);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
for (int i = 34; i <= 48; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 37)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 33) * 106, 512);
else if (i > 37 && i <= 41)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 37) * 106, 540);
else if (i > 41 && i <= 45)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 41) * 106, 568);
else if (i > 45 && i <= 48)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 45) * 106, 596);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
System.Windows.Forms.TextBox[] TextBoxes;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TextBoxes = new System.Windows.Forms.TextBox[46];
for (int i = 1; i <= 16; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 4)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 1) * 106, 12);
else if (i > 4 && i <= 8)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 5) * 106, 40);
else if (i > 8 && i <= 12)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 9) * 106, 68);
else if (i > 12 && i <= 16)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 13) * 106, 96);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
for (int i = 17; i <= 33; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 20)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 17) * 106, 12);
else if (i > 20 && i <= 24)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 21) * 106, 40);
else if (i > 24 && i <= 28)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 25) * 106, 68);
else if (i > 28 && i <= 32)
TextBoxes[i].Location = new System.Drawing.Point(500 + (i - 29) * 106, 96);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
for (int i = 34; i <= 48; ++i)
{
TextBoxes[i] = new TextBox();
this.Controls.Add(TextBoxes[i]);
if (i <= 37)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 33) * 106, 512);
else if (i > 37 && i <= 41)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 37) * 106, 540);
else if (i > 41 && i <= 45)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 41) * 106, 568);
else if (i > 45 && i <= 48)
TextBoxes[i].Location = new System.Drawing.Point(12 + (i - 45) * 106, 596);
//產生出16個TextBox,並排列整齊成為4x4的空格方陣
}
}
}
}
2010年10月29日 星期五
數字遊戲完成板
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
System.Windows.Forms.TextBox[] TextBoxs;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Buttons = new System.Windows.Forms.Button[9];
TextBoxs = new System.Windows.Forms.TextBox[9];
for (int i = 0; i < 9; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
this.Controls.Add(Buttons[i]);
if (i <= 2)
Buttons[i].Location = new System.Drawing.Point(10 + i * 120, 110);
else if (i > 2 && i <= 5)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 120, 140);
else if (i > 5 && i <= 9)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 120, 170);
}
for (int i = 0; i < 9; ++i)
{
TextBoxs[i] = new TextBox();
this.Controls.Add(TextBoxs[i]);
if (i <= 2)
TextBoxs[i].Location = new System.Drawing.Point(10 + i * 110, 10);
else if (i > 2 && i <= 5)
TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 3) * 110, 40);
else if (i > 5 && i <= 9)
TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 6) * 110, 70);
}
TextBoxs[0].Text = "7";
TextBoxs[1].Text = "2";
TextBoxs[2].Text = "4";
TextBoxs[3].Text = "5";
TextBoxs[4].Text = "0";
TextBoxs[5].Text = "6";
TextBoxs[6].Text = "8";
TextBoxs[7].Text = "3";
TextBoxs[8].Text = "1";
}
public void Buttons_Click(object sender, EventArgs e)
{
// System.Windows.Forms.MessageBox.Show("You have clicked button " +
// ((System.Windows.Forms.Button)sender).Tag.ToString());
int bno=0;
String tnum = sender.ToString();
int tlen = tnum.Length;
String no = tnum.Substring(tlen - 1);
for (int i = 0; i < 9; i++)
{
if (Buttons[i].Text == no)
bno = i;
}
switch (bno)
{
case 0:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[0].Text;
Buttons[0].Text = temp;
}
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[0].Text;
Buttons[0].Text = temp;
}
break;
case 1:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
if (Buttons[0].Text == "0")
{
String temp;
temp = Buttons[0].Text;
Buttons[0].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
if (Buttons[2].Text == "0")
{
String temp;
temp = Buttons[2].Text;
Buttons[2].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
break;
case 2:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[2].Text;
Buttons[2].Text = temp;
}
if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[2].Text;
Buttons[2].Text = temp;
}
break;
case 3:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[3].Text;
Buttons[3].Text = temp;
}
if (Buttons[0].Text == "0")
{
String temp;
temp = Buttons[0].Text;
Buttons[0].Text = Buttons[3].Text;
Buttons[3].Text = temp;
}
if (Buttons[6].Text == "0")
{
String temp;
temp = Buttons[6].Text;
Buttons[6].Text = Buttons[6].Text;
Buttons[3].Text = temp;
}
break;
case 4:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
break;
case 5:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
if (Buttons[2].Text == "0")
{
String temp;
temp = Buttons[2].Text;
Buttons[2].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
if (Buttons[8].Text == "0")
{
String temp;
temp = Buttons[8].Text;
Buttons[8].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
break;
case 6:
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[6].Text;
Buttons[6].Text = temp;
}
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[6].Text;
Buttons[6].Text = temp;
}
break;
case 7:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
if (Buttons[6].Text == "0")
{
String temp;
temp = Buttons[6].Text;
Buttons[6].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
if (Buttons[8].Text == "0")
{
String temp;
temp = Buttons[8].Text;
Buttons[8].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
break;
case 8:
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[8].Text;
Buttons[8].Text = temp;
} if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[8].Text;
Buttons[8].Text = temp;
}
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
Buttons[i].Text = Convert.ToString(TextBoxs[i].Text);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Windows.Forms.Button[] Buttons;
System.Windows.Forms.TextBox[] TextBoxs;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Buttons = new System.Windows.Forms.Button[9];
TextBoxs = new System.Windows.Forms.TextBox[9];
for (int i = 0; i < 9; ++i)
{
Buttons[i] = new Button();
Buttons[i].Click += new System.EventHandler(Buttons_Click);
this.Controls.Add(Buttons[i]);
if (i <= 2)
Buttons[i].Location = new System.Drawing.Point(10 + i * 120, 110);
else if (i > 2 && i <= 5)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 120, 140);
else if (i > 5 && i <= 9)
Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 120, 170);
}
for (int i = 0; i < 9; ++i)
{
TextBoxs[i] = new TextBox();
this.Controls.Add(TextBoxs[i]);
if (i <= 2)
TextBoxs[i].Location = new System.Drawing.Point(10 + i * 110, 10);
else if (i > 2 && i <= 5)
TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 3) * 110, 40);
else if (i > 5 && i <= 9)
TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 6) * 110, 70);
}
TextBoxs[0].Text = "7";
TextBoxs[1].Text = "2";
TextBoxs[2].Text = "4";
TextBoxs[3].Text = "5";
TextBoxs[4].Text = "0";
TextBoxs[5].Text = "6";
TextBoxs[6].Text = "8";
TextBoxs[7].Text = "3";
TextBoxs[8].Text = "1";
}
public void Buttons_Click(object sender, EventArgs e)
{
// System.Windows.Forms.MessageBox.Show("You have clicked button " +
// ((System.Windows.Forms.Button)sender).Tag.ToString());
int bno=0;
String tnum = sender.ToString();
int tlen = tnum.Length;
String no = tnum.Substring(tlen - 1);
for (int i = 0; i < 9; i++)
{
if (Buttons[i].Text == no)
bno = i;
}
switch (bno)
{
case 0:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[0].Text;
Buttons[0].Text = temp;
}
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[0].Text;
Buttons[0].Text = temp;
}
break;
case 1:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
if (Buttons[0].Text == "0")
{
String temp;
temp = Buttons[0].Text;
Buttons[0].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
if (Buttons[2].Text == "0")
{
String temp;
temp = Buttons[2].Text;
Buttons[2].Text = Buttons[1].Text;
Buttons[1].Text = temp;
}
break;
case 2:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[2].Text;
Buttons[2].Text = temp;
}
if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[2].Text;
Buttons[2].Text = temp;
}
break;
case 3:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[3].Text;
Buttons[3].Text = temp;
}
if (Buttons[0].Text == "0")
{
String temp;
temp = Buttons[0].Text;
Buttons[0].Text = Buttons[3].Text;
Buttons[3].Text = temp;
}
if (Buttons[6].Text == "0")
{
String temp;
temp = Buttons[6].Text;
Buttons[6].Text = Buttons[6].Text;
Buttons[3].Text = temp;
}
break;
case 4:
if (Buttons[1].Text == "0")
{
String temp;
temp = Buttons[1].Text;
Buttons[1].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[4].Text;
Buttons[4].Text = temp;
}
break;
case 5:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
if (Buttons[2].Text == "0")
{
String temp;
temp = Buttons[2].Text;
Buttons[2].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
if (Buttons[8].Text == "0")
{
String temp;
temp = Buttons[8].Text;
Buttons[8].Text = Buttons[5].Text;
Buttons[5].Text = temp;
}
break;
case 6:
if (Buttons[3].Text == "0")
{
String temp;
temp = Buttons[3].Text;
Buttons[3].Text = Buttons[6].Text;
Buttons[6].Text = temp;
}
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[6].Text;
Buttons[6].Text = temp;
}
break;
case 7:
if (Buttons[4].Text == "0")
{
String temp;
temp = Buttons[4].Text;
Buttons[4].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
if (Buttons[6].Text == "0")
{
String temp;
temp = Buttons[6].Text;
Buttons[6].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
if (Buttons[8].Text == "0")
{
String temp;
temp = Buttons[8].Text;
Buttons[8].Text = Buttons[7].Text;
Buttons[7].Text = temp;
}
break;
case 8:
if (Buttons[7].Text == "0")
{
String temp;
temp = Buttons[7].Text;
Buttons[7].Text = Buttons[8].Text;
Buttons[8].Text = temp;
} if (Buttons[5].Text == "0")
{
String temp;
temp = Buttons[5].Text;
Buttons[5].Text = Buttons[8].Text;
Buttons[8].Text = temp;
}
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
Buttons[i].Text = Convert.ToString(TextBoxs[i].Text);
}
}
}
}
訂閱:
文章 (Atom)