使用数组用随机变量替换旧变量? [英] Replace an old variable with a random variable using arrays?

查看:84
本文介绍了使用数组用随机变量替换旧变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我一直在尝试输入这个代码,用新值替换旧变量的值。 **我使用带有此代码的图形**请注意,该值也是一个变量。所以简而言之,它是变量中的一个变量。

**(我知道这个问题很长,但请跟我一起承担)**


这些是我声明的变量,这些变量对我游戏中的这部分代码至关重要

**我试图制作的游戏叫做Piano Tiles(这是我指定的游戏)重新制作我的学校项目,在这里简要介绍一个解释游戏的视频(很快就到了): https://www.youtube.com/watch?v=4uZPiynHRsw [ ^ ]

  int  x ,y; 
// x = ClientRectangle.Width;
// y = ClientRectangle.Height;

刷bsh_black,bsh_grey,bsh_white,bsh_red;

刷bc1,bc2,bc3,bc4;

int [] pat_select;

随机pat_ch = Random();

int pat_assign;





  3  
InitializeComponent();
yf1 = 0 ;

bsh_black = new SolidBrush(Color.Black);
bsh_grey = new SolidBrush(Color.Gray);
bsh_white = new SolidBrush(Color.White);
bsh_red = new SolidBrush(Color.Red);

bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_white;



我在页面顶部声明了这些变量。 bc变量,我用作换笔器的缩写。因此刷子会根据if条件改变(将在下面显示)



为了我的游戏的测试目的,我暂时使用yf1作为使用**计时器持续使行停止的变量**我将其编码为。 **当计时器滴答时,我输入yf1 ++;。



  if (dummy_state ==  0 
{
game.FillRectangle(bc1,x1,yf1,x2,y2);
game.FillRectangle(bc2,x2,yf1,x2,y2);
game.FillRectangle(bc3,x3,yf1,x2,y2);
game.FillRectangle(bc4,x4,yf1,x2,y2);
}





  private   void  Block_Timer_Tick( object  sender,EventArgs e)
{
yf1 ++;
Invalidate();
}





所以我打算在这里做的是,当yf1变量的值大于表格的垂直值时长度和pat_assign变量等于,然后画笔将变为某种颜色。对于pat_select变量(显示在此问题的顶部),如果已经选择了某个模式(例如pat4变量),则将重新排列数组列表并排除当前选定的模式。

  if (yf1 >  y)
{
// 原始数组
// pat_select = new int [4] {pat1,pat2,pat3,pat4};
if (pat_assign == pat4)
{
pat_select = new int [ 3 ] {pat1,pat2,pat3};
}

如果(pat_assign == pat3)
{
pat_select = new int [ 3 ] {pat1,pat2,pat4};
}

如果(pat_assign == pat2)
{
pat_select = new int [ 3 ] {pat1,pat3,pat4};
}

如果(pat_assign == pat1)
{
pat_select = new int [ 3 ] {pat2,pat3,pat4};
}
当pat_assign等于pat1
时的#region
如果(pat_assign == pat1)
{
bc1 = bsh_black;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign等于pat2
如果(pat_assign == pat2)
{
bc1 = bsh_white;
bc2 = bsh_black;
bc3 = bsh_white;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign等于pat3
如果(pat_assign == pat3)
{
bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_black;
bc4 = bsh_white;
Invalidate();
}
#endregion
#region when pat_assign等于pat4
如果(pat_assign == pat4)
{
bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_black;
Invalidate();
}
#endregion
yf1 = 0 - y2;
}



不幸的是,当图形返回到表单的顶部时,它根本没有改变模式,但保留了当前的模式了。有什么问题呢?我什么都没看到......



更新:

这是PainEventHandler:

< pre lang =c#> private void Form1_Paint( object sender,PaintEventArgs e)
{
Graphics game = e.Graphics;

yf2 = yf1 - (y / 4 );

if (dummy_state == 0
{
game.FillRectangle(bchanger1,x1,yf1,x2,y2);
game.FillRectangle(bchanger2,x2,yf1,x2,y2);
game.FillRectangle(bchanger3,x3,yf1,x2,y2);
game.FillRectangle(bchanger4,x4,yf1,x2,y2);
#region当varyf1越过表格底部
if (yf1 > y)
{
yf1 = 0 - y2;
if (pat_assign == pat2)
{
bchanger1 = bsh_white;
bchanger2 = bsh_black;
bchanger3 = bsh_white;
bchanger4 = bsh_white;
}

if (pat_assign == pat1)
{
bchanger1 = bsh_black;
bchanger2 = bsh_white;
bchanger3 = bsh_white;
bchanger4 = bsh_white;
pat_select = new int [ 3 ] {pat2,pat3,pat4};
}

如果(pat_assign == pat3)
{
bchanger1 = bsh_white;
bchanger2 = bsh_white;
bchanger3 = bsh_black;
bchanger4 = bsh_white;
pat_select = new int [ 3 ] {pat1,pat2,pat4};
}

如果(pat_assign == pat4)
{
bchanger1 = bsh_white;
bchanger2 = bsh_white;
bchanger3 = bsh_white;
bchanger4 = bsh_black;
pat_select = new int [ 3 ] {pat1,pat2,pat3};
}
pat_changer.Next( 0 ,pat_select.Length);
}
#endregion
}
}





这里是Timer Tick事件处理程序

  private   void  tile_timer_Tick( object  sender,EventArgs e)
{
yf1 ++;
yf1 ++;
yf1 ++;
Invalidate();

// 如果任何黑色瓷砖在游戏画面的底部偏离
if (yf1 > y)
{
if (bchanger1 == bsh_black || bchanger2 == bsh_black || bchanger3 == bsh_black || bchanger4 == bsh_black)
{
tile_timer.Stop( );
MessageBox.Show( Game Over);
}
}
}

解决方案

经过与OP的大量讨论后,他解决了他的问题。


Hey guys, I've been trying to type this code that will replace an old variable's value with a new value. **Im using graphics with this code** Note that the value is also a variable. So in short, it's a variable in a variable.
**(I know the question is long, but please, just bear with me)**

These are the variables that I declared that were key to this part of code in my game
**Game I'm trying to make is called Piano Tiles (it's a my assigned game to remake for my school project, to make things brief here's a video that explains the game (it's quick to the point): https://www.youtube.com/watch?v=4uZPiynHRsw[^]

int x, y;
//x = ClientRectangle.Width;
//y = ClientRectangle.Height;

Brush bsh_black, bsh_grey, bsh_white, bsh_red;

Brush bc1, bc2, bc3, bc4;

int[] pat_select;

Random pat_ch = new Random();

int pat_assign;



3
InitializeComponent();
yf1 = 0;

bsh_black = new SolidBrush(Color.Black);
bsh_grey = new SolidBrush(Color.Gray);
bsh_white = new SolidBrush(Color.White);
bsh_red = new SolidBrush(Color.Red);

bc1 = bsh_white;
bc2 = bsh_white;
bc3 = bsh_white;
bc4 = bsh_white;


I declared these variables at the top of the page. The "bc" variables, I used as short for "brush changer." So that the brush will change according to the if conditions (will be displayed below this)

For "testing" purposes of my game, temporarily, I used yf1 as the variable that continually makes the row go down with the **timer** I coded it to. **As the timer ticks, I typed in "yf1++;".

if (dummy_state == 0)
{
    game.FillRectangle(bc1, x1, yf1, x2, y2);
    game.FillRectangle(bc2, x2, yf1, x2, y2);
    game.FillRectangle(bc3, x3, yf1, x2, y2);
    game.FillRectangle(bc4, x4, yf1, x2, y2);
}



private void Block_Timer_Tick(object sender, EventArgs e)
{
    yf1++;
    Invalidate();
}



So what I intended to do here was, when the "yf1" variable's value is greater than the form's vertical length AND whatever the "pat_assign" variable equals then the brushes will change to a certain color. And for the "pat_select" variable (shown from the top of this question), if a certain pattern (for example "pat4" variable) is already chosen, then the arrays list would be rearranged and exclude the currently selected pattern.

if (yf1 > y)
{
    //the original array is 
    //pat_select = new int[4] {pat1, pat2, pat3, pat4};
    if (pat_assign == pat4)
    {
        pat_select = new int[3] { pat1, pat2, pat3 };
    }

    if (pat_assign == pat3)
    {
        pat_select = new int[3] { pat1, pat2, pat4 };
    }

    if (pat_assign == pat2)
    {
        pat_select = new int[3] { pat1, pat3, pat4 };
    }

    if (pat_assign == pat1)
    {
        pat_select = new int[3] { pat2, pat3, pat4 };
    }
    #region when pat_assign equals pat1
    if (pat_assign == pat1)
    {       
        bc1 = bsh_black;
        bc2 = bsh_white;
        bc3 = bsh_white;
        bc4 = bsh_white;
        Invalidate();
    }
    #endregion
    #region when pat_assign equals pat2
    if (pat_assign == pat2)
    {
        bc1 = bsh_white;
        bc2 = bsh_black;
        bc3 = bsh_white;
        bc4 = bsh_white;
        Invalidate();
    }
    #endregion
    #region when pat_assign equals pat3
    if (pat_assign == pat3)
    {
        bc1 = bsh_white;
        bc2 = bsh_white;
        bc3 = bsh_black;
        bc4 = bsh_white;
        Invalidate();
    }
    #endregion
    #region when pat_assign equals pat4
    if (pat_assign == pat4)
    {
        bc1 = bsh_white;
        bc2 = bsh_white;
        bc3 = bsh_white;
        bc4 = bsh_black;
        Invalidate();
    }
    #endregion
    yf1 = 0 - y2;
}


Unfortunately when the graphic went back to the top of the form, it didn't change the pattern at all, but stayed with the current pattern it had. What's the problem with it? I don't really see anything...

UPDATE:
Here is the PainEventHandler:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics game = e.Graphics;

            yf2 = yf1 - (y / 4);

            if (dummy_state == 0)
            {
                game.FillRectangle(bchanger1, x1, yf1, x2, y2);
                game.FillRectangle(bchanger2, x2, yf1, x2, y2);
                game.FillRectangle(bchanger3, x3, yf1, x2, y2);
                game.FillRectangle(bchanger4, x4, yf1, x2, y2);
                #region When var "yf1" goes past bottom of the form
                if (yf1 > y)
                {
                    yf1 = 0 - y2;
                    if (pat_assign == pat2)
                    {
                        bchanger1 = bsh_white;
                        bchanger2 = bsh_black;
                        bchanger3 = bsh_white;
                        bchanger4 = bsh_white;
                    }

                    if (pat_assign == pat1)
                    {
                        bchanger1 = bsh_black;
                        bchanger2 = bsh_white;
                        bchanger3 = bsh_white;
                        bchanger4 = bsh_white;
                        pat_select = new int[3] { pat2, pat3, pat4 };
                    }

                    if (pat_assign == pat3)
                    {
                        bchanger1 = bsh_white;
                        bchanger2 = bsh_white;
                        bchanger3 = bsh_black;
                        bchanger4 = bsh_white;
                        pat_select = new int[3] { pat1, pat2, pat4 };
                    }

                    if (pat_assign == pat4)
                    {
                        bchanger1 = bsh_white;
                        bchanger2 = bsh_white;
                        bchanger3 = bsh_white;
                        bchanger4 = bsh_black;
                        pat_select = new int[3] { pat1, pat2, pat3 };
                    }
                    pat_changer.Next(0, pat_select.Length);
                }
                #endregion
            }
        }



And here is the Timer Tick Event Handler

private void tile_timer_Tick(object sender, EventArgs e)
{
    yf1++;
    yf1++;
    yf1++;
    Invalidate();

    //If any black tile goes beyong the bottom off the game screen
    if (yf1 > y)
    {
        if (bchanger1 == bsh_black || bchanger2 == bsh_black || bchanger3 == bsh_black || bchanger4 == bsh_black)
        {
            tile_timer.Stop();
            MessageBox.Show("Game Over");
        }
    }
}

解决方案

After considerable discussions with OP, he solved his problem.


这篇关于使用数组用随机变量替换旧变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆