gpt4 book ai didi

C 编程 - 火柴人

转载 作者:行者123 更新时间:2023-11-30 17:30:52 25 4
gpt4 key购买 nike

我只是在进行一些简单的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;
}

有人可以帮助我吗?预先感谢您。

最佳答案

假设您的 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这样您就知道从未选择过颜色。

关于C 编程 - 火柴人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24899045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com