반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- PostgreSQL
- Google Excel
- Tkinter
- numpy
- google apps script
- 파이썬
- array
- GIT
- django
- Mac
- PANDAS
- Python
- hive
- Google Spreadsheet
- Excel
- Github
- string
- Kotlin
- dataframe
- math
- matplotlib
- Apache
- Java
- Redshift
- gas
- SQL
- PySpark
- list
- c#
Archives
- Today
- Total
달나라 노트
C# Sample : 주사위 번호 맞추기 본문
728x90
반응형
주사위 번호 맞추기 화면
image source
using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
class DiceCatch
{
public static void Main()
{
// Constants //
Constants cts = new Constants();
Form fm = new Form();
FlowLayoutPanel flp = new FlowLayoutPanel();
PictureBox pb_dice_main = new PictureBox();
Button btn_roll = new Button();
Button btn_confirm = new Button();
Label lbl_result = new Label();
PictureBox[] list_pb_user_choice = new PictureBox[6];
int target_dice_idx = -1;
int target_dice_number = target_dice_idx + 1;
int user_select_dice_idx = -1;
int user_select_dice_number = user_select_dice_idx + 1;
// Settings //
fm.Text = cts.form_title;
fm.Width = cts.form_width;
fm.Height = cts.form_height;
flp.Parent = fm;
flp.AutoSize = true;
flp.Dock = DockStyle.Bottom;
pb_dice_main.Parent = fm;
pb_dice_main.Image = Image.FromFile(cts.dir_img_dice_0);
pb_dice_main.SizeMode = PictureBoxSizeMode.StretchImage;
pb_dice_main.Width = cts.pb_main_width;
pb_dice_main.Height = cts.pb_main_height;
pb_dice_main.Left = fm.ClientSize.Width / 2 - pb_dice_main.Width / 2;
btn_roll.Parent = fm;
btn_roll.Text = "Roll";
btn_roll.Width = cts.btn_width;
btn_roll.Height = cts.btn_height;
btn_roll.Top = pb_dice_main.Bottom + 20;
btn_roll.Left = 10;
btn_roll.Font = new Font("Arial", 13, FontStyle.Bold);
btn_confirm.Parent = fm;
btn_confirm.Text = "Confirm";
btn_confirm.Width = cts.btn_width;
btn_confirm.Height = cts.btn_height;
btn_confirm.Top = pb_dice_main.Bottom + 20;
btn_confirm.Left = btn_roll.Right + 10;
btn_confirm.Font = new Font("Arial", 13, FontStyle.Bold);
lbl_result.Parent = fm;
lbl_result.Width = cts.label_width;
lbl_result.Height = cts.label_height;
lbl_result.Top = pb_dice_main.Bottom + 20;
lbl_result.Left = btn_confirm.Right + 10;
lbl_result.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
lbl_result.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
lbl_result.Font = new Font("Arial", 11, FontStyle.Bold);
for (int i = 0; i < cts.list_dir_img_dice.Length; i++)
{
list_pb_user_choice[i] = new PictureBox();
list_pb_user_choice[i].Parent = flp;
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice[i]);
list_pb_user_choice[i].SizeMode = PictureBoxSizeMode.StretchImage;
list_pb_user_choice[i].Width = cts.pb_dice_user_choice_width;
list_pb_user_choice[i].Height = cts.pb_dice_user_choice_height;
}
// Event //
void btn_roll_click(object sender, EventArgs e)
{
Random rand = new Random();
target_dice_idx = rand.Next(6);
target_dice_number = target_dice_idx + 1;
lbl_result.Text = "Target is set.\nChoose dice and click confirm button.";
pb_dice_main.Image = Image.FromFile(cts.dir_img_dice_0);
for (int i = 0; i < list_pb_user_choice.Length; i++)
{
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice[i]);
}
user_select_dice_idx = -1;
user_select_dice_number = user_select_dice_number + 1;
}
btn_roll.Click += new EventHandler(btn_roll_click);
void select_dice(object sender, EventArgs e)
{
PictureBox pb_user_select = (PictureBox)sender;
for (int i = 0; i < list_pb_user_choice.Length; i++)
{
if (pb_user_select == list_pb_user_choice[i])
{
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice_selected[i]);
user_select_dice_idx = i;
user_select_dice_number = user_select_dice_idx + 1;
}
else
{
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice[i]);
}
}
}
for (int i = 0; i < list_pb_user_choice.Length; i++)
{
list_pb_user_choice[i].Click += new EventHandler(select_dice);
}
void btn_confirm_click(object sender, EventArgs e)
{
if (target_dice_idx < 0 | 5 < target_dice_idx)
{
lbl_result.Text = "You should click Roll button first.";
pb_dice_main.Image = Image.FromFile(cts.dir_img_dice_0);
for (int i = 0; i < list_pb_user_choice.Length; i++)
{
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice[i]);
}
}
else if (user_select_dice_idx < 0 | 5 < user_select_dice_idx)
{
lbl_result.Text = "You should select any dice between 1 ~ 6.";
pb_dice_main.Image = Image.FromFile(cts.dir_img_dice_0);
for (int i = 0; i < list_pb_user_choice.Length; i++)
{
list_pb_user_choice[i].Image = Image.FromFile(cts.list_dir_img_dice[i]);
}
}
else
{
if (target_dice_idx == user_select_dice_idx)
{
lbl_result.Text = $"Target = {target_dice_number}\nYou selected = {user_select_dice_number}\nGreat!";
}
else
{
lbl_result.Text = $"Target = {target_dice_number}\nYou selected = {user_select_dice_number}\nWrong";
}
pb_dice_main.Image = Image.FromFile(cts.list_dir_img_dice_selected[target_dice_idx]);
target_dice_idx = -1;
target_dice_number = target_dice_number + 1;
user_select_dice_idx = -1;
user_select_dice_number = user_select_dice_number + 1;
}
}
btn_confirm.Click += new EventHandler(btn_confirm_click);
// Main //
Application.Run(fm);
}
}
class Constants
{
// Constants //
public string form_title = "Dice Catch";
public int form_width = 600;
public int form_height = 400;
public int pb_main_width = 160;
public int pb_main_height = 160;
public int btn_width = 100;
public int btn_height = 60;
public int label_width = 300;
public int label_height = 60;
public int pb_dice_user_choice_width = 80;
public int pb_dice_user_choice_height = 80;
public string dir_source = "C:\\Users\\Public\\mysource\\";
public string dir_img_dice_0;
public string[] list_dir_img_dice = new string[6];
public string[] list_dir_img_dice_selected = new string[6];
// Constructor //
public Constants()
{
string[] list_dir_img_source = Directory.GetFiles(this.dir_source, "dice_main_*.png");
for (int i = 0; i < list_dir_img_source.Length; i++)
{
if (i == 0)
{
dir_img_dice_0 = list_dir_img_source[i];
}
else
{
list_dir_img_dice[i - 1] = list_dir_img_source[i];
}
}
list_dir_img_dice_selected = Directory.GetFiles(this.dir_source, "dice_select_*.png");
}
}
728x90
반응형
'C# > C# Samples' 카테고리의 다른 글
C# Samples : 포물선 운동 (Parabolic Motion) (0) | 2022.05.28 |
---|---|
C# Samples : Graphic Timer 3개 (0) | 2022.05.24 |
C# Sample : 타이머 30분, 20분, 15분 (0) | 2022.04.21 |
Comments