小瑪莉程式
利用c#實作一個介面的小遊戲,同時也運用了物件導向的概念,算是有了解到OO的類別實作,從C語言跳到C#的實作方式,有蠻大的不同,然而要設計一個好類別更是需要不少的經驗,學習寫介面算是不錯的興趣,比較有個實體的感覺。也想問問有人會寫讓圖片振動的效果嗎??
執行畫面
開始後圖片會以輪流消失的方式來增加動態的感覺,也可以學習一些C# IDE上的工具來設計介面
程式碼
Form1.cs
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 Little_Mary
{
public partial
class Form1
: Form
{
private Player
player;
private Game[]
game;
private int
betmoney;
public int
number = 1;
public bool
gamestart = true;
public Form1()
{
InitializeComponent();
Random
myRandomizer = new Random();
game = new
Game[7]{
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox1, MyTextBox = odds1, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox2, MyTextBox = odds2, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox3, MyTextBox = odds3, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox4, MyTextBox = odds4, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox5, MyTextBox = odds5, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox6, MyTextBox = odds6, Odd = 0},
new
Game() {MyRandom = myRandomizer,
MyPictureBox = pictureBox7, MyTextBox = odds7, Odd = 0},
};
player = new
Player() { Cash = 300 };
OddProduct();
int
Playmoney = 0;
playmoney.Text = "$" + Playmoney;
cash.Text = "$" + player.Cash;
}
private void
numericUpDown1_ValueChanged(object sender, EventArgs e)
/*賭注單位*/
{
betmoney = (int)numericUpDown1.Value;
}
private void
bet_Click(object sender, EventArgs e)/*下注資訊*/
{
betmoney = (int)numericUpDown1.Value;
gamestart = false;
UpdateGame(gamestart);
if
(player.Cash >= betmoney * 10)
{
playmoney.Text = "$" + betmoney * 10;
player.Cash -= betmoney * 10;
cash.Text = "$" + player.Cash;
}
else
{
MessageBox.Show("You have not enough money!!!");
start.Enabled = false;
}
}
private void
rb_CheckedChanged(object sender, EventArgs e)/*判斷玩家選幾號*/
{
for
(int i = 1; i < 7; i++)
{
if
(radioButton1.Checked == true)
{
number = 1;
}
else
if (radioButton2.Checked == true)
{
number = 2;
}
else
if (radioButton3.Checked == true)
{
number = 3;
}
else
if (radioButton4.Checked == true)
{
number = 4;
}
else
if (radioButton5.Checked == true)
{
number = 5;
}
else
if (radioButton6.Checked == true)
{
number = 6;
}
else
if (radioButton7.Checked == true)
{
number = 7;
}
}
}
private void
start_Click(object sender, EventArgs e)/*開始*/
{
int
count = 2;
while
(count != 0)
{
for
(int i = 0; i < 7; i++)
{
game[i].MyPictureBox.Visible
= false;
System.Threading.Thread.Sleep(125);
Application.DoEvents();
}
for
(int i = 0; i < 7; i++)
{
game[i].MyPictureBox.Visible = true;
System.Threading.Thread.Sleep(125);
Application.DoEvents();
}
count--;
}
int
winner = player.MyGame.Run();
MessageBox.Show("The winner number is " + winner);
if
(winner == number)
{
player.Cash += betmoney * 10 *
game[number - 1].Odd;
MessageBox.Show("You win " + betmoney * 10 * game[number
- 1].Odd + " dollars");
}
else
{
MessageBox.Show("You are lost");
}
gamestart = true;
UpdateGame(gamestart);
OddProduct();
cash.Text = "$" + player.Cash;
playmoney.Text = "$";
}
private void
UpdateGame(bool gamestart)
{
bet.Enabled = gamestart;
radioButton1.Enabled = gamestart;
radioButton2.Enabled = gamestart;
radioButton3.Enabled = gamestart;
radioButton4.Enabled = gamestart;
radioButton5.Enabled = gamestart;
radioButton6.Enabled = gamestart;
radioButton7.Enabled =
gamestart;
}
private void
OddProduct()
{
for
(int i = 0; i < 7; i++)
{
game[i].Odd =
player.MyGame.Odds();
game[i].MyTextBox.Text =
game[i].Odd.ToString();
}
}
}
}
Game.cs
namespace Little_Mary
{
public class
Game
{
public Random
MyRandom;
public PictureBox
MyPictureBox = null;
public TextBox
MyTextBox = null;
public int
Odd;
public Game()
{
MyRandom = new
Random();
MyPictureBox = new PictureBox();
MyTextBox = new TextBox();
}
public int
Odds()
{
int
odds = MyRandom.Next(2, 6);
MyTextBox.Text = odds.ToString();
return
odds;
}
public int
Run()
{
int
winNumber = MyRandom.Next(1, 8);
return
winNumber;
}
}
}
namespace Little_Mary
{
public class
Player
{
public int
Cash;
public Game
MyGame;
public Random
MyRandom;
public Player()
{
MyGame = new
Game();
MyRandom = new
Random();
}
}
}
沒有留言:
張貼留言