导航:首页 > 编程语言 > 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编程教程相关的资料

热点内容
gif有损压缩 浏览:929
windows下安装linux命令操作 浏览:840
米家app怎么设置进门亮灯 浏览:650
任我行服务器为什么会影响截图 浏览:294
安卓留言板怎么删除 浏览:16
做大厂程序员有什么感受 浏览:239
php文件只读 浏览:773
红色警戒3命令修改器112 浏览:431
安卓税收和苹果税是什么意思 浏览:444
快速排序算法的时间复杂度分析 浏览:110
大龄程序员困境 浏览:268
手机号忘了怎么登录农行app 浏览:571
商品信息管理系统php 浏览:8
效果器app怎么无线连接 浏览:404
clinux线程锁 浏览:851
怎么看新手机安卓充电器是不是原装 浏览:294
32单片机f4点灯源码 浏览:223
车载安卓导航开发者选项怎么开启 浏览:694
学生程序员兼职 浏览:360
androidswitch事件 浏览:998