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

 
Sản phẩm dịch vụ công trực tuyến liên thông tiết kiệm thời gian

Sản phẩm dịch vụ công trực tuyến liên thông tiết kiệm thời gian

Dịch vụ công trực tuyến là hệ thống giao dịch điện tử các thủ tục hành chính giữa Cơ quan Nhà nước với Công dân và Doanh nghiệp được phát triển bởi IT Software với mục tiêu

7 đại gia công nghệ đồng loạt bị kiện

7 đại gia công nghệ đồng loạt bị kiện

Việc thỏa thuận không lôi kéo, tuyển dụng nhân sự của nhau đã khiến Google, Apple và 5 hãng khác bị kiện với cáo buộc vi phạm luật chống độc quyền.

Drupal 8: Writing a Hello World Module

Drupal 8: Thử viết Hello World Module

It's been a while since I've sat down and tried to write a module from scratch in Drupal 8. I've dabbled here and there in the various already existing modules but there's always something interesting about just trying to write that simple 

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

 

Diet con trung