C编程 - 坚持人

时间:2014-07-22 22:37:10

标签: c

我正在进行一些简单的 C 编程练习,这是为了设计一个棒人并改变程序的一部分(例如它的颜色等......)。基本的东西,我猜,但我遇到了麻烦。

到目前为止,我已经设计了一个棒人,使用getch()函数进行了一些用户交互,我正试图让棒人(使用filled_circle函数)的填充头部成为与用户选择的颜色相同。
我通过将key_entered声明为int作为it = getch()来尝试此操作,我认为通过输入'key_entered'获取filled_circle函数的颜色,这可以解决问题,但它没有和我'我不太清楚为什么?

我也尝试使用 ascii颜色编码,而不是只输入单词,但没有真正有任何区别。

到目前为止,这是我的代码:

    #include <graphics_lib.h>
    #include <conio.h>
    #include <stdio.h>

int main(void)
{
    /* Declare two variables for the x and y positions */
    int x_position, y_position;
    int number_entered;
    int number_entered2;

    /* Open a graphics window */
    /* Make it 640 pixels wide by 480 pixels high */
    initwindow(640, 480);

    /* Set up some coordinates */
    x_position = 320;
    y_position = 140;

    printf("Enter a number between 100 and 320 for the horizontal position of the stick man\n");
    scanf("%d", &number_entered);
    printf("Enter a number between 90 and 140 for the vertical position of the stick man\n");
    scanf("%d", &number_entered2);
    printf("You have entered a horizontal position of %d\n", number_entered);
    printf("and a vertical position of %d\n", number_entered2);

    if ((number_entered < 100) || (number_entered2 < 90))
    printf("The number or numbers you have entered are invalid, you cannot go beyond this point\n");

    else if ((number_entered > 320) || (number_entered2 > 140))
    printf("The number or numbers you have entered are invalid, you cannot go beyond this point\n");

    else if ((number_entered >= 100 <= 320) && (number_entered2 >=90 <=140))
{
    /*This list allows the user to choose the colour*/

    printf("Now select the colour of the stick man, choose from the following colours by entering the letter beside each option\n");
    printf("BLUE - B\n");
    printf("GREEN - G\n");
    printf("RED - R\n");
    printf("CYAN - C\n");
    printf("LIGHTMAGNETA - L\n");
    printf("YELLOW - Y\n");
    printf("WHITE - W\n");

    /* choose red pen colour */
    /*Now using switch statements the user should now choose a colour of which the cases are insensitive*/
    int key_entered;
    {
    key_entered = getch();
    switch (key_entered)
    {
    case 'B':
        printf("You have Selected the BLUE colour\n");
        setcolor(1);
        break;

    case 'b':
        printf("You have Selected the BLUE colour\n");
        setcolor(1);
        break;


    case 'G':
        printf("You have Selected the GREEN colour\n");
        setcolor(2);
        break;

    case 'g':
        printf("You have Selected the GREEN colour\n");
        setcolor(2);
        break;


    case 'R':
        printf("You have Selected the RED colour\n");
        setcolor(4);
        break;

    case 'r':
        printf("You have Selected the RED colour\n");
        setcolor(4);
        break;


    case 'C':
        printf("You have Selected the CYAN colour\n");
        setcolor(3);
        break;

    case 'c':
        printf("You have Selected the CYAN colour\n");
        setcolor(3);
        break;

    case 'L':
        printf("You have Selected the LIGHTMAGNETA colour\n");
        setcolor(13);
        break;

    case 'l':
        printf("You have Selected the LIGHTMAGNETA colour\n");
        setcolor(13);
        break;


    case 'Y':
        printf("You have Selected the YELLOW colour\n");
        setcolor(14);
        break;

    case 'y':
        printf("You have Selected the YELLOW colour\n");
        setcolor(14);
        break;

    case 'W':
        printf("You have Selected the WHITE colour\n");
        setcolor(15);
        break;

    case 'w':
        printf("You have Selected the WHITE colour\n");
        setcolor(15);
        break;

    default:
        printf("The letter you selected is invalid\n");

    }}


    /* draw a circle on the screen buffer
     at x_position, y_position
     with radius 10 and fill colour using the filled_circle function */
    filled_circle(number_entered, number_entered2, 40, getch());

    /*the co_ordinates for the body*/
    line(number_entered, number_entered2 + 40, number_entered, number_entered2 + 180, 5);

    /*the co_ordinates for the arms*/
    line(number_entered - 50, number_entered2 + 110, number_entered + 50, number_entered2 + 110, 5);

    /*the co_ordinates for the left leg*/
    line(number_entered, number_entered2 + 180, number_entered - 50, number_entered2 + 240, 5);

    /*the co_ordinates for the right leg*/
    line(number_entered, number_entered2 + 180, number_entered + 50, number_entered2 + 240, 5);
    /* move the contents of the screen buffer to the display */
    update_display();

}

    /* Wait for a key press */
    getch();

    /* remove the display */
    closegraph();

    return 0;
}

有人能帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

假设您的filled_circle函数的最后一个参数是一种颜色,并且它是int,那么您的代码应该是以下内容(从switch语句的正上方到你的filled_circle电话:

int selected_color = 0; // or whatever type this is if not an 'int'
switch (getch())
{
    case 'b': case 'B':
        printf("You have Selected the BLUE colour\n");
        selected_color = 1;
        break;

    case 'g': case 'G':
        printf("You have Selected the GREEN colour\n");
        selected_color = 2;
        break;

    case 'r': case 'R':
        printf("You have Selected the RED colour\n");
        selected_color = 4;
        break;

    case 'c': case 'C':
        printf("You have Selected the CYAN colour\n");
        selected_color = 3;
        break;
    case 'l': case 'L':
        printf("You have Selected the LIGHTMAGNETA colour\n");
        selected_color = 13;
        break;

    case 'y': case 'Y':
        printf("You have Selected the YELLOW colour\n");
        selected_color = 14;
        break;

    case 'w': case 'W':
        printf("You have Selected the WHITE colour\n");
        selected_color = 15;
        break;

    default:
        printf("The letter you selected is invalid\n");
        break;
}


/* draw a circle on the screen buffer
 at x_position, y_position
 with radius 10 and fill colour using the filled_circle function */
filled_circle(number_entered, number_entered2, 40, selected_color);

请注意,您可能还想检查是否selected_color == 0,以便您知道从未选择过某种颜色。