0

使用 C# 完成程序的主要功能后,我可以轻松地编写代码,但是遇到了障碍,并且已经搜索了几个小时该做什么。

我的程序是在部分类中编写的。我想打乱一个列表,一个图像列表,但我似乎无法从我的部分类中调用静态方法?我从Randomize a List<T>中挖掘了该方法,这正是我想要做的。您可能会注意到我有一个 shuffle 方法,但该方法会随机播放一个数组,而不是一个列表。我已将所有代码添加为折叠状态,仅扩展了感兴趣的方法。我尝试使用内部的 shuffle 方法创建一个新类,但在我的部分类中调用 if 时运气不佳:我希望这不是太多不必要的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace MemoryMortalKombatStyle
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //generate random number
        Random r = new Random();
        //int for matching panels loop in matching method
        int v = 0;
        //Counts how many times the music stop/start buttin has been clicked
        int musicCount = 0;
        //timer counter for starting and stopping timer
        int time = 1;
        //First picture to be compared against next clicked
        int myPBox1;
        //Second picture to be compared against first
        int myPBox2;
        //counts the number of clicks events occured during play
        int mouseClicks = 0;
        //create image list for easy
        List<Image> imagesEasy;
        //create image list for hard1
        List<Image> imagesHard1;
        //create image list for hard2
        List<Image> imagesHard2;
        //List for final images picked to be used in the pictureBoxes on easy mode
        List<Image> imagesFinalEasy;
        //List for final images picked to be used in the pictureBoxes hard mode
        List<Image> imagesFinalHard;
        //create array to pick numbers 1-54 for the images to be chosen for the game
        int[] picSlotArray = new int[8];
        //Array to hold what pictures the user has mached
        int[] picsMatched = new int[16];
        //an array for the eight pictures already picked to be stored but now shuffeled in an array
        int[] randArrayEightEasy = new int[8];
        //an array for the eight pictures already picked to be stored but now shuffeled in an array
        int[] randArrayEightHard = new int[8];
        //array containing all panels that match
        int[] matchedPanels = new int[16];
        //the pics picked for easy doubled up twice into one array
        int[] randArraySixteenEasy = new int[16];
        //the pics picked for hard doubles up twice into one array
        int[] randArraySixteenHard = new int[16];
        //random array of 16 from 0-15 including 0 & 15
        int[] randArray1616 = new int[16];
        //Array for pictureBoxes
        PictureBox[] myPics = new PictureBox[16];
        //Array for panels
        Panel[] myPanels = new Panel[16];

        private void Form2_Load(object sender, EventArgs e)...

        private void InitialiseMyPics()...

        private void InitialisePanels()...

        private void Theme()...

        private void ImageListEasy()...

        private void ImageListHard1()...

        private void ImageListHard2()...

        private void ImageListEasyFinal()...

        private void ImageListHardFinal()
        {
            imagesFinalHard = new List<Image>();
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[0])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[1])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[2])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[3])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[4])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[5])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[6])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[7])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[0])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[1])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[2])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[3])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[4])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[5])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[6])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[7])]);
        }

        private void Randoms()...

        private void ShuffleEasy()...

        //need to some how shuffle the hardFinalList
        private void ShuffleHard()...

        private void SetImagesEasy()...

        private void SetImagesHard()...

        private void ArrayEightTwiceEasy()...

        private void ArrayEightTwiceHard()...

        private void Tags()...

        private void TagsHard1()...

        private void TagsHard2()...

        private void Names1()...

        private void Names2()...

        private void Compare()...

        private void MatchedPics()...

        private void CheckForFinished()...

        private void HidePanels()...

        private void ShowPanels()...

        private void DisableAllPanles()...

        private void EnablePanels()...
        {
        private void HidePictureBoxes()...

        private void RevealPictureBoxes()...

        private void ShowPics()...

        private void panel2_Click_1(object sender, EventArgs e)...

        private void button6_Click(object sender, EventArgs e)...

        private void button7_Click(object sender, EventArgs e)...

        private void New_Game_Click(object sender, EventArgs e)...

        private void timer1_Tick_1(object sender, EventArgs e)...

        private void Music_Stop_Click(object sender, EventArgs e)...

        private void button1_Click(object sender, EventArgs e)
        {
            ImageListHard1();
            ImageListHard2();
            Randoms();
            ArrayEightTwiceHard();
            ShuffleHard();
            TagsHard1();
            ImageListHardFinal();
            //Shuffle Needs to be called here when button is clicked
            SetImagesHard();
            HidePictureBoxes();

        }
    }
}
4

1 回答 1

0

你没有理由不能从你的类中调用一个static方法。partial以下是一些需要检查的事项:

  • 您的类中是否声明了静态方法?如果不是,则必须指定它所在的类的名称。例如,Shuffler.Shuffle()
  • 如果该方法在另一个类中,请确保该类和方法是可访问的——public如果不是,则将该类和方法设置为。
  • 确保您以正确的顺序传递正确的参数类型。
于 2012-09-01T11:49:17.543 回答