C动画轮盘

时间:2014-04-14 16:42:45

标签: c animation

对于课程,我的任务是设置必须的轮盘赌轮:

  1. 从标准输入读取一个有效的赌注。
  2. 随机选择中奖号码。
  3. 显示由至少2张图像组成的轮盘赌轮的动画。 4.在动画结束时显示用户下注,中奖号码和支付值(根据每个下注值为1计算)。
  4. 我使用for循环动画我的Wheel并且随机获胜我只需要用户输入的帮助。

    对于我的用户输入,我现在感谢所有评论和反馈:

           char userbet[3];  
           int result1;
           scanf( "%s" , &userbet);
           if(atoi(userbet) >=0 && atoi(userbet) <= 36){result1 = 1;
           printf("you bet on number %s", &userbet);} 
           else {
           printf("please enter a valid bet", &userbet);}
    

    这很有效,我测试了几个数字并得到了我想要的结果。现在任何人都可以帮助我使用它来触发我的动画吗?

    希望使用&#39;结果&#39;通过if语句触发动画,但程序跳过整个部分并无论如何都要播放动画。如果有人知道如何强制程序检查stdin控制台或可以看到一个方法,请帮助!

    我是初学者,所有评论都很有价值,感谢您的阅读,我希望我的问题不会违反任何规则。感谢您的所有反馈!

    如果有人对用户输入提出了更好的建议,我会全力以赴。

    源代码的其余部分:

        #include <stdio.h>
        #include <stdlib.h>
        #include <gd.h>
    
        #define USE_GLOBAL_COLORMAP  1
        #define USE_LOCAL_COLORMAP   0
    
        #define LOOP_ANIMATION_FOREVER  0
        #define DO_NOT_LOOP_ANIMATION  -1
        #define AUTO_FRAME_DISPOSAL     1
        #define ANIMATION_DELAY         1 /* in milliseconds */
    
        #define IMAGE_WIDTH        640
        #define IMAGE_HEIGHT       720
        #define CIRCLE_DIAMETER    30
        #define MAX_FRAMES_COUNT   70
        #define MOVEMENT_PER_FRAME 10
    
    
        int main( void )
        {
    
          FILE *animatedGIF;
          gdImagePtr frameImage;
          int yellow, black, red, green, brown, darkbrown, gold, silver, white, grey, frameCounter, drawingColor, number, WinningAngle;
          int sliceCounter = 0;
          int sliceStartingAngle = 0;
          int sliceEndingAngle = 0;
          int sliceAngleIncrement = 9;
          int centerPointX = 320;
          int centerPointY = 360;
          int wheelDiameter = 560;
          int insideCoverDiameter = 400;
          int textPositionAngle = 367;
          int textRotationAngle = 270;
          int BallPositionAngle = 0;
          int BallSpeed;
          int Ballroute = 180;
          double textPositionX, textPositionY, BallPositionX, BallPositionY;
          double degreesToRadiansMultiplier = 0.0174532925;
          char *pocketNumbers[] = { "32", "15", "19", "4",  "21", "2",  "25", "17", "34",
                             "6",  "27", "13", "36", "11", "30", "8",  "23", "10",
                             "5",  "24", "16", "33", "1",  "20", "14", "31", "9",
                             "22", "18", "29", "7",  "28", "12", "35", "3",  "26", "0" };                    
    
          srand( time( 0 ) );
          number = rand();
          number = number % 100;
          number++;
          BallSpeed = number;
    
    
    
    
          animatedGIF = fopen( "animation.gif", "wb" );
          frameImage = gdImageCreate( IMAGE_WIDTH, IMAGE_HEIGHT );
          gdImageColorAllocate( frameImage, 34, 139, 34 );
    
         gdImageGifAnimBegin( frameImage, animatedGIF, USE_GLOBAL_COLORMAP,         DO_NOT_LOOP_ANIMATION); 
    
          for ( frameCounter = 0; frameCounter <= MAX_FRAMES_COUNT; frameCounter++ )
          {
            gdImageDestroy( frameImage );
    
            frameImage = gdImageCreate( IMAGE_WIDTH, IMAGE_HEIGHT );
            gdImageColorAllocate( frameImage, 34, 139, 34 );
            yellow = gdImageColorAllocate( frameImage, 255, 255, 0 );
            green = gdImageColorAllocate( frameImage,  0, 190, 0  );
            red = gdImageColorAllocate( frameImage, 255, 0, 0 );
            black = gdImageColorAllocate( frameImage, 0, 0, 0 );
            brown = gdImageColorAllocate( frameImage, 222, 184, 135 );
            darkbrown = gdImageColorAllocate( frameImage, 139, 69, 19);
            gold = gdImageColorAllocate( frameImage, 255, 215, 0);
            silver = gdImageColorAllocate( frameImage, 191, 192, 192);
            white = gdImageColorAllocate( frameImage, 255, 255, 255 );
            grey = gdImageColorAllocate( frameImage, 105, 105, 105 );
            gdImageSetThickness( frameImage, 4 );
            gdImageFilledArc(frameImage, centerPointX, centerPointY, 596, 596, 0, 360, brown, gdArc);
            do {
    
            sliceStartingAngle = sliceEndingAngle;
            sliceEndingAngle = sliceStartingAngle + sliceAngleIncrement;
            textRotationAngle -= sliceAngleIncrement;
            textPositionAngle -= sliceAngleIncrement;
    
            if ( sliceCounter == 36)
            {
              drawingColor = green;
              sliceEndingAngle += 1;
              textPositionAngle -= 3;
    
            }
             else if ( (sliceCounter % 2) == 0 )
            {
              drawingColor = red;
              textRotationAngle += 2;
    
            }
            else
            {
              drawingColor = black;
              sliceEndingAngle += 1;
              textPositionAngle -= 1;
              textRotationAngle -= 1;
            }
            if ( (sliceCounter % 5) == 0 ) sliceEndingAngle += 1;
            if ( (sliceCounter % 7) == 6 ) { textPositionAngle -= 1; textRotationAngle -= 8; }
            if ( (sliceCounter % 13) == 12 ) textPositionAngle -= 1;
    
            textPositionX = (cos(textPositionAngle * degreesToRadiansMultiplier) * (wheelDiameter / 2.25)) + centerPointX;
            textPositionY = centerPointY - (sin(textPositionAngle * degreesToRadiansMultiplier) * (wheelDiameter / 2.25));
    
            gdImageFilledArc( frameImage, centerPointX, centerPointY, wheelDiameter, wheelDiameter, sliceStartingAngle, sliceEndingAngle, drawingColor, gdArc ); 
    
            gdImageStringFT( frameImage, NULL, white, "Vera.ttf", 14, textRotationAngle * degreesToRadiansMultiplier, textPositionX, textPositionY, pocketNumbers[ sliceCounter ] );
            }while (++sliceCounter < 37 );
    
            gdImageFilledArc(frameImage, centerPointX, centerPointY, 400, 400, 0, 360, grey, gdArc); 
            gdImageFilledArc(frameImage, centerPointX, centerPointY, 320, 320, 0, 360, black, gdArc); 
            gdImageArc(frameImage, centerPointX, centerPointY, 400, 400, 0, 360, gold);
            gdImageFilledArc(frameImage, centerPointX, centerPointY, 100, 100, 0, 360, black, gdArc);
            gdImageFilledArc(frameImage, centerPointX, centerPointY, 20, 20, 0, 360, silver, gdArc);
            gdImageFilledArc(frameImage, centerPointX, 345, 10, 20, 0, 360, silver, gdArc);
            gdImageFilledArc(frameImage, centerPointX, 375, 10, 20, 0, 360, silver, gdArc);
            gdImageFilledArc(frameImage, 335, centerPointY, 20, 10, 0, 360, silver, gdArc);
            gdImageFilledArc(frameImage, 305, centerPointY, 20, 10, 0, 360, silver, gdArc);
            gdImageArc(frameImage, centerPointX, centerPointY, 460, 460, 0, 360, gold);
            gdImageArc(frameImage, centerPointX, centerPointY, 600, 600, 0, 360, darkbrown);
    
            if ( frameCounter == 70 ) {
            BallPositionX = (cos( degreesToRadiansMultiplier * BallPositionAngle ) * 215) + centerPointX;
            BallPositionY = centerPointY - (sin( degreesToRadiansMultiplier * BallPositionAngle ) * 215);
            gdImageFilledEllipse( frameImage, BallPositionX, BallPositionY, CIRCLE_DIAMETER, CIRCLE_DIAMETER, silver );
            BallPositionAngle = WinningAngle; }
    
            else if(frameCounter >= 20) {
            BallPositionX = (cos( degreesToRadiansMultiplier * BallPositionAngle ) * Ballroute) + centerPointX;
            BallPositionY = centerPointY - (sin( degreesToRadiansMultiplier * BallPositionAngle ) * Ballroute);
            gdImageFilledEllipse( frameImage, BallPositionX, BallPositionY, CIRCLE_DIAMETER, CIRCLE_DIAMETER, silver ); }
    
            sliceCounter = 0;
            sliceStartingAngle = 0;
            sliceEndingAngle = 9;
            textPositionAngle = 367;
            textRotationAngle = 270;
            BallPositionAngle = 0;
            sliceStartingAngle += 9 * frameCounter;
            sliceEndingAngle += 9 * frameCounter;
            textPositionAngle -= 9 * (frameCounter + 1);
            textRotationAngle -= 9 * (frameCounter + 1);
            BallPositionAngle += BallSpeed * (frameCounter); 
    
    
            gdImageGifAnimAdd( frameImage, animatedGIF, USE_GLOBAL_COLORMAP, 0, 0, ANIMATION_DELAY, AUTO_FRAME_DISPOSAL, NULL ); } 
    
    
            //sliceCounter = (WinningAngle / 9);
            //gdImageStringFT( frameImage, NULL, white, "Vera.ttf", 14, 0, centerPointX, centerPointY, pocketNumbers[ sliceCounter ] ); }
    
          gdImageGifAnimEnd( animatedGIF );
          fclose( animatedGIF );
          gdImageDestroy( frameImage ); }
    

2 个答案:

答案 0 :(得分:1)

为什么在需要号码时会读取字符串?

更好的做法是scanf为int并检查数字是否符合您的要求。

Atoi可能无法转换字符串并返回值0或1 ...

尝试扫描%d ...

答案 1 :(得分:0)

您希望如何使用此代码处理大于10的输入?

char userbet[2];        // Only 2 characters!
scanf( "%s" , userbet); // %s means string, which means it ends with a \0 terminator.

10或更高的输入看起来像:

[1] [0] [\0-terminator].

这是3个字符!