Hướng dẫn làm trò chơi Quay số

Hướng dẫn làm trò chơi Quay số

Đầu tiên, thiết kế Form như hình dưới đây:

Hướng dẫn làm trò chơi Quay số

Ok…Sau khi đã thiết kế  Form xong…chúng ta bắt đầu code nhé ^^

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PrjQuaySo

{

public partial class frmQuaySo : Form

{

Random rnd = new Random();

public frmQuaySo()

{

InitializeComponent();

}

private void btnThoat_Click(object sender, EventArgs e)

{

Application.Exit();

}

// Chi cho phep nhap vao la so

private void txtDat1_KeyPress(object sender, KeyPressEventArgs e)

{

if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))

{

e.Handled = true;

}

}

// Chi cho phep nhap vao la so

private void txtDat2_KeyPress(object sender, KeyPressEventArgs e)

{

if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))

{

e.Handled = true;

}

}

// Chi cho phep nhap vao la so

private void txtDat3_KeyPress(object sender, KeyPressEventArgs e)

{

if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))

{

e.Handled = true;

}

}

// Quay so ngau nhien tu 0 den 9

private void dongho1_Tick(object sender, EventArgs e)

{

int soPhatSinh;

// Phat sinh 1 so ngau nhien tu 0 den 9

soPhatSinh = rnd.Next(0, 10);

lbQuay1.Text = soPhatSinh.ToString();

soPhatSinh = rnd.Next(0, 10);

lbQuay2.Text = soPhatSinh.ToString();

soPhatSinh = rnd.Next(0, 10);

lbQuay3.Text = soPhatSinh.ToString();

}

// Thoi gian quay so ngau nhien

private void dongho2_Tick(object sender, EventArgs e)

{

dongho1.Stop();

dongho2.Stop();

int soLanTrung = 0;

long MucThuong = 100000;

long TienThuong;

if (lbQuay1.Text == txtDat1.Text) soLanTrung++;

if (lbQuay2.Text == txtDat2.Text) soLanTrung++;

if (lbQuay3.Text == txtDat3.Text) soLanTrung++;

if (soLanTrung == 3)

{

TienThuong = MucThuong * soLanTrung * 3;

lbKetQua.Text = "Xin chúc mừng!!! Bạn đã trúng giải đặc biệt!!!";

lbTrungThuong.Text = TienThuong.ToString("#,##0 VND"); // Quy dinh chuoi xuat ra

}

else

{

if (soLanTrung == 0)

{

lbKetQua.Text = "Chúc bạn may mắn lần sau!!!";

lbTrungThuong.Text = "";

}

else

{

lbKetQua.Text = "Chúc mừng bạn!!!";

TienThuong = MucThuong * soLanTrung;

lbTrungThuong.Text = TienThuong.ToString("#,# USD");

}

}

if (soLanTrung > 0) picTien.Visible = true;

}

private void btnQuay_Click(object sender, EventArgs e)

{

lbTrungThuong.Text = "";

picTien.Visible = false;

dongho1.Start();

dongho2.Start();

}

private void txtDat1_TextChanged(object sender, EventArgs e)

{

txtDat2.Focus();

}

private void txtDat2_TextChanged(object sender, EventArgs e)

{

txtDat3.Focus();

}

private void txtDat3_TextChanged(object sender, EventArgs e)

{

btnQuay.Focus();

}

}

} 

Các bạn lưu ý là interval của dongho1 là thời gian quay số ngẫu nhiên từ 0 đến 9, ở đây mình chọn là 5. Còn interval của dongho2 là thời gian quay số ngẫu nhiên, mình chọn là 1500 (khi chạy chương trình bạn sẽ rõ hơn)^^

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Facebook sẽ thâu tóm 72% doanh thu quảng cáo từ mạng xã hội vào năm 2012

Facebook sẽ thâu tóm 72% doanh thu quảng cáo từ mạng xã hội vào năm 2012

Năm tới, Facebook dự kiến ​​sẽ thâu tóm 72% doanh thu quảng cáo mạng xã hội. Điều này cũng có nghĩa là họ sẽ có trong tay 6,1% doanh thu của quảng cáo trực tuyến toàn cầu.

Apple “hạ mình” tham dự hội nghị bảo mật dành cho hacker

Apple “hạ mình” tham dự hội nghị bảo mật dành cho hacker

Sau khi hàng loạt mã độc bị phát hiện trên Mac OS X, Apple sẽ lần đầu tiên góp mặt tại hội nghị bảo mật Black Hat, một động thái được cho là Apple đã bắt đầu quan tâm hơn đến vấn đề bảo mật trên các sản phẩm của mình.

How To Create an Ajax Autocomplete Textfield In Drupal 7

Cách tạo 1 Textfield Ajax Autocomplete trong Drupal 7

People are lazy by nature and love when something is done for them automatically.

Công ty diệt chuột T&C

 

Diet con trung