導航:首頁 > 編程語言 > c語言graphics編程教程

c語言graphics編程教程

發布時間:2022-08-29 07:31:33

⑴ 經典C語言編程30例(二)

【程序31】
題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續
判斷第二個字母。
1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。
2.程序源代碼:
#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*當所按字母為Y時才結束*/
{ switch (letter)
{case 'S':printf("please input second letter\n");
if((letter=getch())=='a')
printf("saturday\n");
else if ((letter=getch())=='u')
printf("sunday\n");
else printf("data error\n");
break;
case 'F':printf("friday\n");break;
case 'M':printf("monday\n");break;
case 'T':printf("please input second letter\n");
if((letter=getch())=='u')
printf("tuesday\n");
else if ((letter=getch())=='h')
printf("thursday\n");
else printf("data error\n");
break;
case 'W':printf("wednesday\n");break;
default: printf("data error\n");
}
}
}
==============================================================
【程序32】
題目:Press any key to change color, do you want to try it. Please hurry up!
1.程序分析:
2.程序源代碼:
#include
void main(void)
{
int color;
for (color = 0; color < 8; color++)
{
textbackground(color);/*設置文本的背景顏色*/
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch();/*輸入字元看不見*/
}
}
==============================================================
【程序33】
題目:學習gotoxy()與clrscr()函數
1.程序分析:
2.程序源代碼:
#include
void main(void)
{
clrscr();/*清屏函數*/
textbackground(2);
gotoxy(1, 5);/*定位函數*/
cprintf("Output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20\n");
}
==============================================================
【程序34】
題目:練習函數調用
1. 程序分析:
2.程序源代碼:
#include
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();/*調用此函數*/
}
void main(void)
{
three_hellos();/*調用此函數*/
}
==============================================================
【程序35】
題目:文本顏色設置
1.程序分析:
2.程序源代碼:
#include
void main(void)
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color);/*設置文本顏色*/
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
}
==============================================================
【程序36】
題目:求100之內的素數
1.程序分析:
2.程序源代碼:
#include
#include "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;ifor(i=2;i for(j=i+1;j {
if(a[i]!=0&&a[j]!=0)
if(a[j]%a[i]==0)
a[j]=0;}
printf("\n");
for(i=2,line=0;i{
if(a[i]!=0)
{printf("]",a[i]);
line++;}
if(line==10)
{printf("\n");
line=0;}
}
}
==============================================================
【程序37】
題目:對10個數進行排序
1.程序分析:可以利用選擇法,即從後9個比較過程中,選擇一個最小的與第一個元素交換,
下次類推,即用第二個元素與後8個進行比較,並進行交換。
2.程序源代碼:
#define N 10
main()
{int i,j,min,tem,a[N];
/*input data*/
printf("please input ten num:\n");
for(i=0;i{
printf("a[%d]=",i);
scanf("%d",&a[i]);}
printf("\n");
for(i=0;iprintf("]",a[i]);
printf("\n");
/*sort ten num*/
for(i=0;i{min=i;
for(j=i+1;jif(a[min]>a[j]) min=j;
tem=a[i];
a[i]=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted \n");
for(i=0;iprintf("]",a[i]);
}
==============================================================
【程序38】
題目:求一個3*3矩陣對角線元素之和
1.程序分析:利用雙重for循環控制輸入二維數組,再將a[i][i]累加後輸出。
2.程序源代碼:
main()
{
float a[3][3],sum=0;
int i,j;
printf("please input rectangle element:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
sum=sum+a[i][i];
printf("ijiaoxian he is %6.2f",sum);
}
==============================================================
【程序39】
題目:有一個已經排好序的數組。現輸入一個數,要求按原來的規律將它插入數組中。
1. 程序分析:首先判斷此數是否大於最後一個數,然後再考慮插入中間的數的情況,插入後
此元素之後的數,依次後移一個位置。
2.程序源代碼:
main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("original array is:\n");
for(i=0;i<10;i++)
printf("]",a[i]);
printf("\n");
printf("insert a new number:");
scanf("%d",&number);
end=a[9];
if(number>end)
a[10]=number;
else
{for(i=0;i<10;i++)
{ if(a[i]>number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j<11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
printf("m",a[i]);
}
==============================================================
【程序40】
題目:將一個數組逆序輸出。
1.程序分析:用第一個與最後一個交換。
2.程序源代碼:
#define N 5
main()
{ int a[N]={9,6,5,4,1},i,temp;
printf("\n original array:\n");
for(i=0;i printf("M",a[i]);
for(i=0;i {temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf("\n sorted array:\n");
for(i=0;i printf("M",a[i]);
}
【程序41】
題目:學習static定義靜態變數的用法
1.程序分析:
2.程序源代碼:
#include "stdio.h"
varfunc()
{
int var=0;
static int static_var=0;
printf("\40:var equal %d \n",var);
printf("\40:static var equal %d \n",static_var);
printf("\n");
var++;
static_var++;
}
void main()
{int i;
for(i=0;i<3;i++)
varfunc();
}
==============================================================
【程序42】
題目:學習使用auto定義變數的用法
1.程序分析:
2.程序源代碼:
#include "stdio.h"
main()
{int i,num;
num=2;
for (i=0;i<3;i++)
{ printf("\40: The num equal %d \n",num);
num++;
{
auto int num=1;
printf("\40: The internal block num equal %d \n",num);
num++;
}
}
}
==============================================================
【程序43】
題目:學習使用static的另一用法。
1.程序分析:
2.程序源代碼:
#include "stdio.h"
main()
{
int i,num;
num=2;
for(i=0;i<3;i++)
{
printf("\40: The num equal %d \n",num);
num++;
{
static int num=1;
printf("\40:The internal block num equal %d\n",num);
num++;
}
}
}
==============================================================
【程序44】
題目:學習使用external的用法。
1.程序分析:
2.程序源代碼:
#include "stdio.h"
int a,b,c;
void add()
{ int a;
a=3;
c=a+b;
}
void main()
{ a=b=4;
add();
printf("The value of c is equal to %d\n",c);
}
==============================================================
【程序45】
題目:學習使用register定義變數的方法。
1.程序分析:
2.程序源代碼:
void main()
{
register int i;
int tmp=0;
for(i=1;i<=100;i++)
tmp+=i;
printf("The sum is %d\n",tmp);
}
==============================================================
【程序46】
題目:宏#define命令練習(1)
1.程序分析:
2.程序源代碼:
#include "stdio.h"
#define TRUE 1
#define FALSE 0
#define SQ(x) (x)*(x)
void main()
{
int num;
int again=1;
printf("\40: Program will stop if input value less than 50.\n");
while(again)
{
printf("\40:Please input number==>");
scanf("%d",&num);
printf("\40:The square for this number is %d \n",SQ(num));
if(num>=50)
again=TRUE;
else
again=FALSE;
}
}
==============================================================
【程序47】
題目:宏#define命令練習(2)
1.程序分析:
2.程序源代碼:
#include "stdio.h"
#define exchange(a,b) { \ /*宏定義中允許包含兩道衣裳命令的情形,此時必須在最右邊加上"\"*/
int t;\
t=a;\
a=b;\
b=t;\
}
void main(void)
{
int x=10;
int y=20;
printf("x=%d; y=%d\n",x,y);
exchange(x,y);
printf("x=%d; y=%d\n",x,y);
}
==============================================================
【程序48】
題目:宏#define命令練習(3)
1.程序分析:
2.程序源代碼:
#define LAG >
#define SMA <
#define EQ ==
#include "stdio.h"
void main()
{ int i=10;
int j=20;
if(i LAG j)
printf("\40: %d larger than %d \n",i,j);
else if(i EQ j)
printf("\40: %d equal to %d \n",i,j);
else if(i SMA j)
printf("\40:%d smaller than %d \n",i,j);
else
printf("\40: No such value.\n");
}
==============================================================
【程序49】
題目:#if #ifdef和#ifndef的綜合應用。
1. 程序分析:
2.程序源代碼:
#include "stdio.h"
#define MAX
#define MAXIMUM(x,y) (x>y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
void main()
{ int a=10,b=20;
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
}
==============================================================
【程序50】
題目:#include 的應用練習
1.程序分析:
2.程序源代碼:
test.h 文件如下:
#define LAG >
#define SMA <
#define EQ ==
#include "test.h" /*一個新文件50.c,包含test.h*/
#include "stdio.h"
void main()
{ int i=10;
int j=20;
if(i LAG j)
printf("\40: %d larger than %d \n",i,j);
else if(i EQ j)
printf("\40: %d equal to %d \n",i,j);
else if(i SMA j)
printf("\40:%d smaller than %d \n",i,j);
else
printf("\40: No such value.\n");
}
【程序51】
題目:學習使用按位與 & 。
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
2.程序源代碼:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a&3;
printf("\40: The a & b(decimal) is %d \n",b);
b&=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
==============================================================
【程序52】
題目:學習使用按位或 | 。
1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
2.程序源代碼:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a|3;
printf("\40: The a & b(decimal) is %d \n",b);
b|=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
==============================================================
【程序53】
題目:學習使用按位異或 ^ 。
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
2.程序源代碼:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a^3;
printf("\40: The a & b(decimal) is %d \n",b);
b^=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
==============================================================
【程序54】
題目:取一個整數a從右端開始的4~7位。
程序分析:可以這樣考慮:
(1)先使a右移4位。
(2)設置一個低4位全為1,其餘全為0的數。可用~(~0<<4)
(3)將上面二者進行&運算。
2.程序源代碼:
main()
{
unsigned a,b,c,d;
scanf("%o",&a);
b=a>>4;
c=~(~0<<4);
d=b&c;
printf("%o\n%o\n",a,d);
}
==============================================================
【程序55】
題目:學習使用按位取反~。
1.程序分析:~0=1; ~1=0;
2.程序源代碼:
#include "stdio.h"
main()
{
int a,b;
a=234;
b=~a;
printf("\40: The a's 1 complement(decimal) is %d \n",b);
a=~a;
printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
}
==============================================================
【程序56】
題目:畫圖,學用circle畫圓形。
1.程序分析:
2.程序源代碼:
/*circle*/
#include "graphics.h"
main()
{int driver,mode,i;
float j=1,k=1;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
for(i=0;i<=25;i++)
{
setcolor(8);
circle(310,250,k);
k=k+j;
j=j+0.3;
}
}
==============================================================
【程序57】
題目:畫圖,學用line畫直線。
1.程序分析:
2.程序源代碼:
#include "graphics.h"
main()
{int driver,mode,i;
float x0,y0,y1,x1;
float j=12,k;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(GREEN);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
j=j+10;
}
x0=263;y1=275;y0=263;
for(i=0;i<=20;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0+5;
y0=y0+5;
y1=y1-5;
}
}
==============================================================
【程序58】
題目:畫圖,學用rectangle畫方形。
1.程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。
2.程序源代碼:
#include "graphics.h"
main()
{int x0,y0,y1,x1,driver,mode,i;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(1);
rectangle(x0,y0,x1,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(150,40,"How beautiful it is!");
line(130,60,480,60);
setcolor(2);
circle(269,269,137);
}
==============================================================
【程序59】
題目:畫圖,綜合例子。
1.程序分析:
2.程序源代碼:
# define PAI 3.1415926
# define B 0.809
# include "graphics.h"
#include "math.h"
main()
{
int i,j,k,x0,y0,x,y,driver,mode;
float a;
driver=CGA;mode=CGAC0;
initgraph(&driver,&mode,"");
setcolor(3);
setbkcolor(GREEN);
x0=150;y0=100;
circle(x0,y0,10);
circle(x0,y0,20);
circle(x0,y0,50);
for(i=0;i<16;i++)
{
a=(2*PAI/16)*i;
x=ceil(x0+48*cos(a));
y=ceil(y0+48*sin(a)*B);
setcolor(2); line(x0,y0,x,y);}
setcolor(3);circle(x0,y0,60);
/* Make 0 time normal size letters */
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
outtextxy(10,170,"press a key");
getch();
setfillstyle(HATCH_FILL,YELLOW);
floodfill(202,100,WHITE);
getch();
for(k=0;k<=500;k++)
{
setcolor(3);
for(i=0;i<=16;i++)
{
a=(2*PAI/16)*i+(2*PAI/180)*k;
x=ceil(x0+48*cos(a));
y=ceil(y0+48+sin(a)*B);
setcolor(2); line(x0,y0,x,y);
}
for(j=1;j<=50;j++)
{
a=(2*PAI/16)*i+(2*PAI/180)*k-1;
x=ceil(x0+48*cos(a));
y=ceil(y0+48*sin(a)*B);
line(x0,y0,x,y);
}
}
restorecrtmode();
}
==============================================================
【程序60】
題目:畫圖,綜合例子。
1.程序分析:
2.程序源代碼:
#include "graphics.h"
#define LEFT 0
#define TOP 0
#define RIGHT 639
#define BOTTOM 479
#define LINES 400
#define MAXCOLOR 15
main()
{
int driver,mode,error;
int x1,y1;
int x2,y2;
int dx1,dy1,dx2,dy2,i=1;
int count=0;
int color=0;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode,"");
x1=x2=y1=y2=10;
dx1=dy1=2;
dx2=dy2=3;
while(!kbhit())
{
line(x1,y1,x2,y2);
x1+=dx1;y1+=dy1;
x2+=dx2;y2+dy2;
if(x1<=LEFT||x1>=RIGHT)
dx1=-dx1;
if(y1<=TOP||y1>=BOTTOM)
dy1=-dy1;
if(x2<=LEFT||x2>=RIGHT)
dx2=-dx2;
if(y2<=TOP||y2>=BOTTOM)
dy2=-dy2;
if(++count>LINES)
{
setcolor(color);
color=(color>=MAXCOLOR)?0:++color;
}
}
closegraph();
}

⑵ 如何用visual c 編程c語言graphics

(1)文件->新建->項目->visual
c++->win32控制台應用程序
名稱中輸入項目的名稱
確定->下一步->空項目->完成
(2)解決方案資源管理器

源文件
右擊
添加->新建項->代碼->c++文件(.cpp)
名稱中輸入
某某文件名.c
不要.cpp
添加
(3)項目->項目名
屬性->c/c++->高級->編譯
選擇
編譯為c代碼(/tc)
確定
其實(3)這一步也可以不用做
因為c++兼容c的
不過(3)做了
那就是使用真正的c標准進行編譯了

⑶ C語言 GRAPHICS庫

#include <iostream>
#include <cstdlib>
#include <graphics.h>
#include <conio.h>
using namespace std;
IMAGE *MG=NULL;
void draw()
{
int n,i;
initgraph(1366, 768);
setbkcolor(WHITE);
cleardevice();
IMAGE MG;
loadimage(&MG,_T( "A.JPG"));
for(i=100;i<800;i+=27)
for(n=100;n<800;n+=27)
putimage(i, n, &MG);
}
void mouse()
{
MOUSEMSG W;
IMAGE MG;
loadimage(&MG,_T( "b.JPG"));

int i=100;

while(!kbhit()){ //當敲擊回車退出 這里加了個循環
W=GetMouseMsg();
if(W.uMsg==WM_LBUTTONDOWN)
{
putimage(i,i , &MG);
i+=27;
}
}
}
int main()
{
draw();
mouse();
}


我這里測試通過

⑷ C語言如何畫圖

framebuffer(幀緩沖)。
幀的最低數量為24(人肉眼可見)(低於24則感覺到畫面不流暢)。
顯卡與幀的關系:由cpu調節其數據傳輸速率來輸出其三基色的配比。
三基色:RGB(紅綠藍)。

在沒有桌面和圖形文件的系統界面,可以通過C語言的編程來實現在黑色背景上畫圖!

用下面的代碼,在需要的地方(有注釋)適當修改,就能畫出自己喜歡的圖形!

PS:同樣要編譯運行後才能出效果。

printf("xres: %d ", info.xres);

printf("yres: %d ", info.yres);

printf("bits_per_pixel: %d ", info.bits_per_pixel);

size_t len = info.xres*info.yres*info.bits_per_pixel >> 3;

unsigned long* addr = NULL;

addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);

if(addr == (void*)-1){

perror("mmap err. ");

⑸ 求解此C語言編程題 要詳細過程 格式工整 稍帶講解再加150分 謝謝大家

返回子字元串個數,列印del子字元串後的字元串。

#include&lt;stdio.h&gt;
#include&lt;string.h&gt;
int comp(char *p1,char *p2);
char *del(char *);
char str[1024],substr[1024];
int main()
{
printf("Please input a string: ");
gets(str);
printf("Please input a substring: ");
gets(substr);
printf("%d ",comp(str,substr));
printf("%s ",str);
}
int comp(char *p1,char *p2)
{
unsigned int count=0,temp=0;
char *iTemp1,*iTemp2;
unsigned int p2Temp = strlen(p2),i=0;
while(*p1!='')
{
while(*p1 == *p2) //
{
if(temp == 0)
iTemp1 = p1;
p1++;
p2++;
temp++;
if(*p1 == '') // 已經比較完
break;
}
if(temp == strlen(substr)) //相等的個數是否和子字元串相等,如果相等,則子字元串+1
{
iTemp2 = p1; // 保存P1結束位置
p1 = iTemp1;
while(*p1 != '')
{
iTemp1 = p1 ;// 保存p1 位置
*p1 = *(p1 + p2Temp); // 字元串向前移動,即刪除子字元串
p1 = iTemp1;
p1++;
}
p1 = iTemp2-p2Temp; //
i = 0;
temp=0;
p2=substr; //p2重新賦初值
count++;
p1--; // p1z指向上一個字元的末尾
}
temp=0;
p2=substr;
p1++; // p1指向新的字元,即將新的比較
}
return count;
}

⑹ C語言graphics.h 畫圖

H是一個頭文件,一般只寫介面,聲明的一部分; C是實現文件,寫具體實施的一部分。通常具有相同名稱的H和C文件,並且在C文件的#include文件為H。

⑺ C語言:畫組合圖形問題

#include "stdio.h"
#include "graphics.h"
#include "conio.h"
main()
{
int x=260,y=160,driver=VGA,mode=VGAHI;
int num=20,i;
int top,bottom;
initgraph(&driver,&mode,"");
top=y-30;
bottom=y-30;
for(i=0;i<num;i++)
{
ellipse(x,250,0,360,top,bottom);
top-=5;
bottom+=5;
}
for(i=0;i<15;i++)
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
getch();
}

⑻ 使用C語言編程畫圖

換賬號再來回答,veket的小號。。。

.......用了ege圖形庫.........純體力活.........

源代碼....demo.cpp.....

/////////////////////////////////////////

// ege0.3.8

// MinGW g++3.4.2

// veket的小號

/////////////////////////////////////////

#include"graphics.h"

#include<stdio.h>

constintYLEN=50;

constintXLEN=40;

constintWIDTH=10*XLEN;

constintHEIGHT=2*YLEN;

constintXWIDTH=640;

constintYHEIGHT=480;

intmain()

{

intx1,y1,x2,y2,x,y;

charstrbuf[100];

inti;

setinitmode(0);

initgraph(XWIDTH,YHEIGHT);

setbkcolor(RGB(0xcc,0xcc,0xcc));

setfillstyle(RGB(0xff,0xff,0xff));

x2=x1=(getmaxx()-WIDTH)/2;

y1=(getmaxy()-2*HEIGHT-YLEN)/2;

y2=y1+HEIGHT+YLEN;

bar(x1,y1,x1+WIDTH,y1+2*YLEN);

setcolor(RGB(0x04,0x04,0x04));

rectangle(x1,y1,x1+WIDTH,y1+2*YLEN);

outtextxy(x1-20,y1-10,"1");

outtextxy(x1-20,y1-10+YLEN,"0");

outtextxy(x1-20,y1-10+2*YLEN,"-1");

for(x=x1,y=y1+2*YLEN+10,i=0;x<=x1+WIDTH;x+=2*XLEN,i+=2)

{

sprintf(strbuf,"%d",i);

outtextxy(x,y,strbuf);

}

for(x=x1;x<x1+(int)(3*PI*XLEN);x++)

{

y=y1+YLEN-(int)(YLEN*sin(1.0*(x-x1)/XLEN)+0.5);

putpixel(x,y,RGB(0x3e,0x3e,0xff));

}

bar(x2,y2,x2+WIDTH,y2+2*YLEN);

rectangle(x2,y2,x2+WIDTH,y2+2*YLEN);

outtextxy(x2-20,y2-10,"1");

outtextxy(x2-20,y2-10+YLEN,"0.5");

outtextxy(x2-20,y2-10+2*YLEN,"0");

for(x=x2,y=y2+2*YLEN+10,i=0;x<=x1+WIDTH;x+=2*XLEN,i+=2)

{

sprintf(strbuf,"%d",i);

outtextxy(x,y,strbuf);

}

for(x=x2;x<x2+(int)(PI*XLEN);x++)

{

y=y2+2*YLEN-(int)(2*YLEN*sin(1.0*(x-x1)/XLEN)+0.5);

putpixel(x,y,RGB(0x3e,0x3e,0xff));

}

for(x=x2+(int)(2*PI*XLEN);x<x2+(int)(3*PI*XLEN);x++)

{

y=y2+2*YLEN-(int)(2*YLEN*sin(1.0*(x-x1)/XLEN)+0.5);

putpixel(x,y,RGB(0x3e,0x3e,0xff));

}

getch();

closegraph();

return0;

}

⑼ 我要用C語言寫貪食蛇啊·圖形界面怎麼寫,graphics.h函數怎麼用 只要告訴我哪裡有講解圖形函數我自己去看

給個案例你……慢慢研究
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 21
int apple[3];
char score[3];
char tail[3];
void gotoxy(int x, int y) //輸出坐標
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int b) //顏色函數
{
HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
SetConsoleTextAttribute(hConsole,b) ;
}
int Block(char head[2]) //判斷出界
{
if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))
return 1;
return 0;
}
int Eat(char snake[2]) //吃了蘋果
{
if ((snake[0] == apple[0]) && (snake[1] == apple[1]))
{
apple[0] = apple[1] = apple[2] = 0;
gotoxy(N+44,10);
color(13);
printf("%d",score[0]*10);
color(11);
return 1;
}
return 0;
}
void Draw(char **snake, int len) //蛇移動
{
if (apple[2]) {
gotoxy(apple[1] * 2, apple[0]);
color(12);
printf("●");
color(11);
}
gotoxy(tail[1] * 2, tail[0]);
if (tail[2])
{ color(14);
printf("★");
color(11);
}
else
printf("■");
gotoxy(snake[0][1] * 2, snake[0][0]);
color(14);
printf("★");
color(11);
putchar('\n');
}
char** Move(char **snake, char dirx, int *len) //控制方向
{
int i, full = Eat(snake[0]);
memcpy(tail, snake[(*len)-1], 2);
for (i = (*len) - 1; i > 0; --i)
memcpy(snake[i], snake[i-1], 2);
switch (dirx)
{
case 'w': case 'W': --snake[0][0]; break;
case 's': case 'S': ++snake[0][0]; break;
case 'a': case 'A': --snake[0][1]; break;
case 'd': case 'D': ++snake[0][1]; break;
default: ;
}
if (full)
{
snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));
snake[(*len)] = (char *)malloc(sizeof(char) * 2);
memcpy(snake[(*len)], tail, 2);
++(*len);
++score[0];
if(score[3] < 16)
++score[3];
tail[2] = 1;
}
else
tail[2] = 0;
return snake;
}
void init(char plate[N+2][N+2], char ***snake_x, int *len) //初始化
{
int i, j;
char **snake = NULL;
*len = 3;
score[0] = score[3] =3;
snake = (char **)realloc(snake, sizeof(char *) * (*len));
for (i = 0; i < *len; ++i)
snake[i] = (char *)malloc(sizeof(char) * 2);

for (i = 0; i < 3; ++i)
{
snake[i][0] = N/2 + 1;
snake[i][1] = N/2 + 1 + i;
}

for (i = 1; i <= N; ++i)
for (j = 1; j <= N; ++j)
plate[i][j] = 1;

apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;
apple[2] = 1;
for (i = 0; i < N + 2; ++i)
{
gotoxy(0, i);
for (j = 0; j < N + 2; ++j)
{
switch (plate[i][j])
{
case 0:
color(12);printf("□");color(11); continue;
case 1: printf("■"); continue;
default: ;
}
}
putchar('\n');
}
for (i = 0; i < (*len); ++i)
{
gotoxy(snake[i][1] * 2, snake[i][0]);
printf("★");
}
putchar('\n');
*snake_x = snake;
}
void Manual()
{
gotoxy(N+30,2);
color(10);
printf("按 W S A D 移動方向");
gotoxy(N+30,4);
printf("按 space 鍵暫停");
gotoxy(N+30,8);
color(11);
printf("歷史最高分為: ");
color(12);
gotoxy(N+44,8);
printf("%d",score[1]*10);
color(11);
gotoxy(N+30,12);
printf("你現在得分為: 0");
}
int File_in() //取記錄的分數
{
FILE *fp;
if((fp = fopen("C:\\tcs.txt","a+")) == NULL)
{
gotoxy(N+18, N+2);
printf("文件不能打開\n");
exit(0);
}
if((score[1] = fgetc(fp)) != EOF);
else
score[1] = 0;
return 0;
}
int File_out() //存數據
{

FILE *fp;
if(score[1] > score[0])
{gotoxy(10,10);
color(12);
puts("闖關失敗 加油耶");
gotoxy(0,N+2);
return 0;
}
if((fp = fopen("C:\\tcs.txt","w+")) == NULL)
{
printf("文件不能打開\n");
exit(0);
}
if(fputc(--score[0],fp)==EOF)
printf("輸出失敗\n");
gotoxy(10,10);
color(12);
puts("恭喜您打破記錄");
gotoxy(0,N+2);
return 0;
}

void Free(char **snake, int len) //釋放空間
{
int i;
for (i = 0; i < len; ++i)
free(snake[i]);
free(snake);
}
int main(void)
{
int len;
char ch = 'g';
char a[N+2][N+2] = {{0}};
char **snake;
srand((unsigned)time(NULL));
color(11);
File_in();
init(a, &snake, &len);
Manual();
// while (ch != 0x1B) // 按 ESC 結束
while(1)
{
Draw(snake, len);
if (!apple[2]) {
apple[0] = rand()%N + 1;
apple[1] = rand()%N + 1;
apple[2] = 1;
}
Sleep(200-score[3]*10);
setbuf(stdin, NULL);
if (kbhit())
{
gotoxy(0, N+2);
ch = getche();
}
snake = Move(snake, ch, &len);
/*if (Block(snake[0])==1)
{
gotoxy(N+2, N+2);
puts("你輸了");
File_out();
Free(snake, len);
getche();
exit(0);
} */
}
Free(snake, len);
exit(0);
}

⑽ 如何用C語言畫基本圖形

下面舉一個用drawpoly()函數畫箭頭的例子。#include
#include
int main()
{
int gdriver, gmode, i;
int arw[16]={200,102,300,102,300,107,330,<br/>100,300,93,300,98,200,98,200,102};
gdriver=DETECT;
initgraph(&gdriver, &gmode, "c:\\caic\\bgi");
setbkcolor(BLUE);
cleardevice();
setcolor(12); /*設置作圖顏色*/
drawpoly(8, arw); /*畫一箭頭*/
getch();
closegraph();
return 0;
}
設定線型函數
在沒有對線的特性進行設定之前,TURBO C 用其默認值,即一點寬的實線,但TURBO C 也提供了可以改變線型的函數。線型包括:寬度和形狀。其中寬度只有兩種選擇:一點寬和三點寬。而線的形狀則有五種。下面介紹有關線型的設置函數。
void far setlinestyle(intlinestyle,unsigned upattern,int thickness); 該函數用來設置線的有關信息,其中linestyle是線形狀的規定,
見下表:
有關線的形狀(linestyle)
━━━━━━━━━━━━━━━━━━━━━━━━━
符號常數 數值 含義
─────────────────────────
SOLID_LINE 0 實線
DOTTED_LINE 1 點線
CENTER_LINE 2 中心線
DASHED_LINE 3 點畫線
USERBIT_LINE 4 用戶定義線
━━━━━━━━━━━━━━━━━━━━━━━━━
有關線寬(thickness)
thickness是線的寬度,見下表。
━━━━━━━━━━━━━━━━━━━━━━━━━
符號常數 數值 含義
─────────────────────────
NORM_WIDTH 1 一點寬
THIC_WIDTH 3 三點寬
━━━━━━━━━━━━━━━━━━━━━━━━━
對於upattern,只有linestyle選USERBIT_LINE 時才有意義 (選其它線型,uppattern取0即可)。此進uppattern的16位二進制數的每一位代表一個象元,如果那位為1,則該象元打開,否則該象元關閉。 void far getlinesettings(struct linesettingstypefar *lineinfo);該函數將有關線的信息存放到由lineinfo 指向的結構中,表中linesettingstype的結構如下:
struct linesettingstype
{
int linestyle;
unsigned upattern;
int thickness;
}

閱讀全文

與c語言graphics編程教程相關的資料

熱點內容
卸載聯想app哪個好 瀏覽:719
php文字轉圖片 瀏覽:328
豆客後台怎麼加密碼 瀏覽:574
jpg轉換pdf破解版 瀏覽:978
php基礎書籍推薦 瀏覽:775
伺服器與外網不通如何驗證 瀏覽:351
電子版是不是就是文件夾 瀏覽:50
游戲屬性文件加密 瀏覽:462
如何讓安卓手機桌面圖標下移 瀏覽:528
ubuntuphp5環境搭建 瀏覽:99
賭癮解壓視頻 瀏覽:917
晉城移動dns伺服器地址 瀏覽:294
php開源文庫系統 瀏覽:134
android記事本源碼 瀏覽:407
安卓11小游戲怎麼玩法 瀏覽:190
gif有損壓縮 瀏覽:937
windows下安裝linux命令操作 瀏覽:844
米家app怎麼設置進門亮燈 瀏覽:652
任我行伺服器為什麼會影響截圖 瀏覽:296
安卓留言板怎麼刪除 瀏覽:18