View Full Version : Building a slot machine
villagecirc
09-19-2003, 10:36 AM
I enjoy building a slot machine in each new platform that I program in. Anyways I am trying to change the imageurl in the code behind file using C#. I know the code for a regular windows aplications is in this format ImageName.Image = Image.FromFile("FileName"); so I figured ImageUrlName.ImageUrl = ImageUrl.FromFile("FileName"); but that is not it. I know I can change in the script tag but that kind of defeats of the purpose of a code behind file. Anyways in anyone happens to know the correct syntax for changing an imageurl with code through the code behind file let me know. [8]
Matthew H. Paulson
smpaulson@msn.com
http://webpages.charter.net/bctrotzer/
villagecirc
09-19-2003, 10:50 AM
Please Diseregard My last post as I was able to answer my own question it was a very simple layout that simply involved this format
PicturBoxName.ImageUrl = "path and file name";
Now why would I think it could be that simple$
Matthew H. Paulson
smpaulson@msn.com
http://webpages.charter.net/bctrotzer/
villagecirc
11-15-2003, 11:05 AM
I am posting my entire class for building a simple slot machine it was a fun project it anyone else is interested in building one I have pasted the entire code below...
public class FruitMedley : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel pnlSlot1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Image imgSlot2;
protected System.Web.UI.WebControls.Image imgSlot3;
protected System.Web.UI.WebControls.Button btnSpin;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label lblCredits;
protected System.Web.UI.WebControls.Image imgSlot1;
public static int iCredits = 500;//int value of credits
public string sCredits;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblLoser;
protected System.Web.UI.WebControls.Button btnReset;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label lblCreditsWon;
protected System.Web.UI.WebControls.Label Label8;//Name of Credits used as session variable name
public string vCredits;//Value of Credits used as session value
private void Page_Load(object sender, System.EventArgs e)
{
// // Put user code to initialize the page here
// this.imgSlot1.ImageUrl =" /MyWeb/slotpics/pic1.jpg";//test to fill picture box
// //form variables
// const string PicPath = "/MyWeb/slotpics/pic";
// int Pic1 = 9;//represent picture Number for fist picture box
// int Pic2 = 8;//2nd pic value
// int Pic3 = 7;//3rd pic value
// this.imgSlot1.ImageUrl = PicPath + Pic1 + ".jpg";//test variable substitutes for pathing
// //int Credits = 500;
// //int CreditsPlayed = 25;
if ( IsPostBack )
{
}
else
{
iCredits = 500;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSpin.Click += new System.EventHandler(this.btnSpin_Click);
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSpin_Click(object sender, System.EventArgs e)
{
RefreshSlot();
}
private void RefreshSlot()
{
const string PicPath = "/MyWeb/slotpics/pic";
int Pic1 = 9;//represent picture Number for fist picture box
int Pic2 = 8;//2nd pic value
int Pic3 = 7;//3rd pic value
int iCreditsWon = 0;// represents credits won if their is a match
Random temp = new Random();
Pic1 = temp.Next(9);
Pic2 = temp.Next(9);
Pic3 = temp.Next(9);
this.imgSlot1.ImageUrl = PicPath + Pic1 + ".jpg";
this.imgSlot2.ImageUrl = PicPath + Pic2 + ".jpg";
this.imgSlot3.ImageUrl = PicPath + Pic3 + ".jpg";
if (Pic1 == Pic2)//ask if pic1 and pic2 match
{
iCreditsWon = 125;//we have a winner for matching first two pics
if (Pic1 == Pic3)//ask if all pics match
{
iCreditsWon = 525;//we have a winner for matching all 3 pics
}
}
RefreshCredits(iCreditsWon);
}//end refreshslot
private void RefreshCredits(int CreditsWon)//pass it a value to add to credits if all pictures match
{
if (CreditsWon <= 0)
{
iCredits -= 25;
this.lblCredits.Text = String.Format("{0:D}",iCredits);
this.lblCreditsWon.Text = String.Format("{0:D}",CreditsWon);
}
else
{
iCredits += CreditsWon;
this.lblCredits.Text = String.Format("{0:D}",iCredits);
this.lblCreditsWon.Text = String.Format("{0:D}",CreditsWon);
}
if (iCredits < 0)
{
iCredits = 0;//never allow credits to drop below zero
this.btnSpin.Enabled = false;
this.lblLoser.Visible = true;
}
}//end refreshcredits
private void btnReset_Click(object sender, System.EventArgs e)
{
this.btnSpin.Enabled = true;
this.lblLoser.Visible = false;
iCredits = 500;
this.lblCredits.Text = String.Format("{0:D}",iCredits);
}
}
aplication is viewable at http://www.villagecircle.net/MyWeb/FruitMedley.aspx
Matthew H. Paulson
matt@villagecircle.net
http://www.villagecircle.net
villagecirc
11-15-2003, 11:09 AM
please note that I forgot to delete the denoted // text in my load methdod and all those variables can and should be disregarded....
// // Put user code to initialize the page here
// this.imgSlot1.ImageUrl =" /MyWeb/slotpics/pic1.jpg";//test to fill picture box
// //form variables
// const string PicPath = "/MyWeb/slotpics/pic";
// int Pic1 = 9;//represent picture Number for fist picture box
// int Pic2 = 8;//2nd pic value
// int Pic3 = 7;//3rd pic value
// this.imgSlot1.ImageUrl = PicPath + Pic1 + ".jpg";//test variable substitutes for pathing
// //int Credits = 500;
// //int CreditsPlayed = 25;
all that is not necesary....
Matthew H. Paulson
matt@villagecircle.net
http://www.villagecircle.net
vBulletin® ©Jelsoft Enterprises Ltd.