Loading

C#_code_review

seonkyoung

This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.

{c#_매칭게임 코딩}

컴퓨터 공학과

202216023 장선경

1

게임 소개

2

window form

디자인 요소 소개

3

코드 리뷰

#1 게임소개
#2 Window 웹 폼 구조

Label

name:score_la,life

TableLayoutPanel

name:tableLayoutPanel1

Label

font: Weding

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace final_c4
{
    public partial class Form1 : Form

    {
        private SoundPlayer player1, player2, player3,player4,player5;
      
        Random random = new Random();
      
        List<String> icons = new List<String>()
        {
            "!","!","N","N",",",",","k","k"
            ,"b","b","v","v","w","w","z","z"
        };
        int life_count = 5;
        int score_count = 0;
        Label firstClicked = null;//처음클릭한 라벨을 가리키는 변수
        Label secondClicked = null;//두번째 클릭한 라벨을 가리키는 변수

        private void AssiginIconsToSquares()
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    int randomNumber = random.Next(icons.Count);//0-icons.Count-1까지의 난수 생성
                    iconLabel.Text = icons[randomNumber];
                    //iconLabel.ForeColor = iconLabel.BackColor;
                    //icons.RemoveAt(randomNumber); 그림이 2-3개 중복될것을 방지하기위해
                    icons.RemoveAt(randomNumber);
                }
            }
            timer2.Start();

        }
        public Form1()
        {
            InitializeComponent();
            AssiginIconsToSquares();
            player1 = new SoundPlayer("match.wav");
            player2 = new SoundPlayer("wrong.wav");
            player3 = new SoundPlayer("cardSlide.wav");
            player4 = new SoundPlayer("bravo.wav");
            player5 = new SoundPlayer("tryagain.wav");



        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
            
            if (timer1.Enabled == true)
                return;
           
            Label clickedLabel = sender as Label;
         
            if (clickedLabel != null)
            {

                if (clickedLabel.ForeColor == Color.Black)
                    return;//라벨색이 검정색이면 이미 눌렸다는 의미이므로 클릭무시
                if (firstClicked == null)
                {

                    firstClicked = clickedLabel;
                    firstClicked.ForeColor = Color.Black;
                    return;
                }

                secondClicked = clickedLabel;
                secondClicked.ForeColor = Color.Black;


                if (firstClicked.Text == secondClicked.Text)
                {
                    player1.Play();
                    life.Text = "목숨: " + life_count;
                    score_la.Text = "점수: " + score_count;
                    score_count += 20;
                    life.Text = "목숨: " + life_count;
                    score_la.Text = "점수: " + score_count;
                    CheckForWinner();
                    firstClicked = null;
                    secondClicked = null;
                    return;
                }
                if (firstClicked.Text != secondClicked.Text)
                {
                    player2.Play();
                    life.Text = "목숨: " + life_count;
                    score_la.Text = "점수: " + score_count;
                    life_count -= 1;
                    life.Text = "목숨: " + life_count;
                    score_la.Text = "점수: " + score_count;
                    CheckForWinner();
                    timer1.Start();
                    //timer1: 그림이 일하지 않을때 발생하는 틱
                    //return;
                }



                //타이머가 750밀리초 동안 기다렸다가 단일 틱을 발생시킴
            }

        }
      
        private void CheckForWinner()
        {
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    if (iconLabel.ForeColor == iconLabel.BackColor)
                        return;
                }
            }
            player4.Play();
            MessageBox.Show("You match all the icons! \n score: 160 ", "congratulations!");
            Close();
        }
      
        private void label17_Click(object sender, EventArgs e)
     
        private void life_TextChanged(object sender, EventArgs e)
       
        {
            if (life_count == 0)
            {
                player5.Play();
                MessageBox.Show("Game over try agin! \n score: " + score_count, "life over");
                Close();
            }
            else
            {
                life.Text = "목숨: " + life_count;


            }

        }


        private void timer1_Tick_1(object sender, EventArgs e)
        {

            timer1.Stop();
            player3.Play();
            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;
            firstClicked = null;
            secondClicked = null;
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            
            timer2.Stop();//이게 있어야 또 안사라짐.
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {

                    iconLabel.ForeColor = iconLabel.BackColor;
                    //icons.RemoveAt(randomNumber); 그림이 2-3개 중복될것을 방지하기위해

                }
            }

        }

     
    }
}
#3 코드 리뷰
private SoundPlayer player1, player2, player3,player4,player5;

Random random = new Random();

List<String> icons = new List<String>()
{
  "!","!","N","N",",",",","k","k"
  ,"b","b","v","v","w","w","z","z"
};
int life_count = 5;
int score_count = 0;
Label firstClicked = null;
Label secondClicked = null;
#3 코드 리뷰

세부 분석1_초기 설정

 private void AssiginIconsToSquares(){
    foreach(Control control in tableLayoutPanel1.Controls){   
        Label iconLabel = control as Label;
        if (iconLabel != null){
             int randomNumber=random.Next(icons.Count);
             iconLabel.Text = icons[randomNumber];
             icons.RemoveAt(randomNumber);
 		}
 	}
 	timer2.Start();           
}
#3 코드 리뷰

세부 분석2_그림을 랜덤으로 배치하는 함수

private void timer2_Tick(object sender, EventArgs e){
	
    timer2.Stop();
    foreach (Control control in tableLayoutPanel1.Controls){
        Label iconLabel = control as Label;
        if (iconLabel != null){
          iconLabel.ForeColor = iconLabel.BackColor;
        }
    }
 }
#3 코드 리뷰

세부 분석2_그림을 랜덤으로 배치하는 함수

#3 코드 리뷰

세부분석3_라벨을 클릭했을때

private void label1_Click(object sender, EventArgs e)
{
 
  if (timer1.Enabled == true)
        return;
 
  Label clickedLabel = sender as Label;
  
    if (clickedLabel != null){

        if (clickedLabel.ForeColor == Color.Black)
            return;
        if (firstClicked == null){

          firstClicked = clickedLabel;
          firstClicked.ForeColor = Color.Black;
          return;
     }

    secondClicked = clickedLabel;
    secondClicked.ForeColor = Color.Black;


    if (firstClicked.Text == secondClicked.Text){
          player1.Play();
          life.Text = "목숨: " + life_count;
          score_la.Text = "점수: " + score_count;
          score_count += 20;
          life.Text = "목숨: " + life_count;
          score_la.Text = "점수: " + score_count;
          CheckForWinner();
          firstClicked = null;
          secondClicked = null;
          return;
    }
  if (firstClicked.Text != secondClicked.Text){
      player2.Play();
      life.Text = "목숨: " + life_count;
      score_la.Text = "점수: " + score_count;
      life_count -= 1;
      life.Text = "목숨: " + life_count;
      score_la.Text = "점수: " + score_count;
      CheckForWinner();
      timer1.Start();
     
      }

    }

}
#3 코드 리뷰

세부분석3_라벨을 클릭했을때

private void CheckForWinner(){
	foreach(Control control in tableLayoutPanel1.Controls){
		Label iconLabel=control as Label;
        if (iconLabel != null){
            if (iconLabel.ForeColor == iconLabel.BackColor)
                return; 
                }
            }
    player4.Play();
    MessageBox.Show("You match all the icons! \n score: 160 ", "congratulations!");
    Close();
}
#3 코드 리뷰

세부분석3_라벨을 클릭했을때

 private void timer1_Tick(object sender, EventArgs e){
 //타이머의 틱 이벤트 처리기는 두 아이콘을 숨기고 firstClicked 및 secondClicked 참조 변수를 다시 설정합니다.
 //플레이어가 다른 아이콘 쌍을 선택할 수 있도록 폼이 준비됩니다.
     timer1.Stop();
     player3.Play();
     firstClicked.ForeColor=firstClicked.BackColor;
     secondClicked.ForeColor=secondClicked.BackColor;
     firstClicked = null;
     secondClicked = null;
 }
#3 코드 리뷰

세부분석4_목숨이 0 이 될때

 private void life_TextChanged(object sender, EventArgs e){
     if (life_count == 0){
         player5.Play();
         MessageBox.Show("Game over try agin! \n score: "
         + score_count, "life over");
         Close();
    }
}
 public Form1(){
     InitializeComponent();
     AssiginIconsToSquares();
     player1 = new SoundPlayer("match.wav");
     player2 = new SoundPlayer("Fail.wav");
     player3 = new SoundPlayer("cardSlide.wav");
     player4 = new SoundPlayer("bravo.wav");
     player5 = new SoundPlayer("tryagain.wav");
 }
#3 코드 리뷰

세부분석5_Form1 생성자

감사합니다

Made with Slides.com