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

 
Chạy Drupal 7 trên HipHop Virtual Machine tuyệt vời

Chạy Drupal 7 trên HipHop Virtual Machine tuyệt vời

Official HHVM packages are only distributed for Ubuntu and Debian, but thankfully some enterprising people have packaged them up for CentOS

HTC ChaCha Review

HTC ChaCha Review

The HTC ChaCha is definitely a new generation smart phone that is facebook focused, probably as a result of the fact that mobile phone manufacturers have realized that social networking is a great selling point.

Nhóm phát triển kính Project Glass trình diễn cách tìm kiếm trên kính Glass

Nhóm phát triển kính Project Glass trình diễn cách tìm kiếm trên kính Glass

Nhóm phát triển kính Project Glass đăng một video cho thấy chiếc kính thông minh của Google hoạt động ấn tượng với khả năng nhận diện giọng nói và tương tác thực ảo (AR - augmented reality).

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

 

Diet con trung