=0;)a[y--][x++]=k++;}if(n%2" />
文档库 最新最全的文档下载
当前位置:文档库 › c语言编写n阶蛇形方阵

c语言编写n阶蛇形方阵

c语言编写n阶蛇形方阵
c语言编写n阶蛇形方阵

/*

打印如下方阵

蛇形方阵:

1 2 6 7

3 5 8 13

4 9 12 14

10 11 15 16

*/

#include

#include

int main()

{

int i,j,k,m,n,x,h,y;

int a[15][15]={0};

printf("输入蛇形方阵的阶数:");

scanf("%d",&i);

k=1;

for(n=1;n<=i;n++) //上三角的循环

{

if(n%2==1)

{

y=n-1;

x=0;

for(;y>=0;)

a[y--][x++]=k++;

}

if(n%2==0)

{

x=n-1;

y=0;

for(;x>=0;)

a[y++][x--]=k++;

}

}

k=(n-1)*(n-1);

for(n=2*i-1;n>=i;n--) //下三角的循环

{

if(n%2==0)

{

x=i-1;

y=i-(2*i-1-n);

for(;y<=i-1;)

a[y++][x--]=k--;

}

if(n%2==1)

{

y=i-1;

x=i-(2*i-1-n);

for(;x<=i-1;)

a[y--][x++]=k--;

}

}

for (h=0;h

{

for (j=0;j

printf("%5d",a[h][j]);

printf("\n");

}

system("pause");

return 0;

}

电子琴C程序代码,四乘四矩阵键盘输入

电子琴C程序代码,四乘四矩阵键盘输入#include #define uchar unsigned char #define uint unsigned int sbit duan=P 2八6; sbit wei=P 2八7; sbit bee=P 2八3; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; uchar code tablewe[]={ 0x7f,0xbf,0xdf,0xef, 0xf7,0xfb,0xfd,0xfe}; uchar disp[16]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; // 在里面输入按下键值为0~15 对应要显示的第一位码值uchar disp1[16]={0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0x3f}; // 在里面输入按下键值为0~15 对应要显示的第二位码值unsigned char temp; unsigned char key; unsigned char i,j;

unsigned char STH0; unsigned char STL0; unsigned int code tab[]={ //63625, 63833, 64019, 64104, 64260, 64400, 64524 ,// 低音区:1 2 3 4 64580, 64685, 64778, 64820, 64898, 64968, 65030 ,// 中音区:1 2 3 4 5 65058, 65110, 65157, 65178, 65217, 65252, 65283 ,// 高音区:1 2 3 4 5 65297 ,// 超高音:1 }; // 音调数据表可改 void delay(uchar x) uchar y,z; for(y=x;y>0;y--) for(z=0;z<110;z++); void init() TMOD=0x01; ET0=1; EA=1; void display() { for(i=0;i<2;i++)

c语言小游戏编程

程序设计实践 大作业 学号:20100302XXXX 姓名:XXX 班级:信息10-3班 实验题目:射击类飞机游戏 页脚内容1

成绩: 一.实验题目: 射击类飞机游戏 二.实验目的: 通过c语言编写一个射击类的打飞机小游戏,可以通过键盘来进行游戏,操作方法是“a”“d”“w”或者“←”“↑”“→”来控制,击中敌机可获得积分,被敌机撞中死亡一次,每次游戏有3次生还机会,游戏结束后可选择是否重新开始游戏…… 三.对游戏的改进: 这个游戏是我对一个小游戏进行的改造升级版,添加了颜色函数、终止函数,选择类函数,使游戏实现了可以终止,不再是分数、死亡数一直增加但是没有结束的情况;增加了颜色函数,使得游戏看起来更加的舒适;增加了终止函数,使游戏在死亡三次后自动结束游戏,并且可以选择是否重新开始游戏;另外增添了设置函数,使得可以对游戏进行设置,改变游戏大小,调整飞机运行速度等等,是游戏更加的人性化…… 四.实验内容 页脚内容2

编写出c语言代码,运行程序,并调试程序,最终实现游戏的功能。本程序主要包含游戏控制函数、设置是否继续游戏函数、输出函数、子弹移动函数、敌机移动函数、设置函数、菜单函数等7个主要函数,包含了不同的功能,对原来的程序作出了很大的改进,用到的主要语句有getche语句、for语句、while语句、printf语句、switch语句等等,添加了颜色函数,实现了诸多功能。可以在页面上显示制作人的主要信息等等…… 【流程图见打印版】 五.源程序: #include 页脚内容3

#include #include #include #define N 35 #define up 72 #define left 75 #define right 77 void run();//游戏控制函数 void yn();//设置是否继续游戏函数 void print(int [][N]);//输出函数 void movebul(int [][N]);//子弹移动函数 void movepla(int [][N]);//敌机移动函数 void setting(void);//设置函数 void menu(void);//菜单函数 int scr[22][N]={0},pl=9,width=24,speed=3,density=30,score=0,death=0;//全局变量:界面、我机初始位、界面宽度、敌机速度、敌机密度、得分、死亡 页脚内容4

MSP430单片机的4X4矩阵键盘C语言程序

MSP430单片机的4X4矩阵键盘C语言程序 #include #define uchar unsigned char#define uint unsigned int uchar table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; void delay(unsigned int i) //延时子程序{while(i--);} uchar keyvalue(){ uchar key; uchar np10,np11,np12,np13; P1DIR=0x0f;//第一排P1OUT=~BIT3; delay(10); np10=P1IN&BIT4; if(np10==0) { key=0; } np11=P1IN&BIT5; if(np11==0) { key=1; } np12=P1IN&BIT6; if(np12==0) { key=2; } np13=P1IN&BIT7; if(np13==0) { key=3; } //第二行P1OUT=~BIT2; delay(10); np10=P1IN&BIT4; if(np10==0) { key=4; } np11=P1IN&BIT5; if(np11==0) { key=5; } np12=P1IN&BIT6; if(np12==0) { key=6; } np13=P1IN&BIT7; if(np13==0) { key=7; } //第三行P1OUT=~BIT1; delay(10); np10=P1IN&BIT4; if(np10==0) { key=8; } np11=P1IN&BIT5; if(np11==0) { key=9; } np12=P1IN&BIT6; if(np12==0) { key=10; } np13=P1IN&BIT7; if(np13==0) { key=11; } //第四行P1OUT=~BIT0; delay(10); np10=P1IN&BIT4; if(np10==0) { key=12; } np11=P1IN&BIT5; if(np11==0) { key=13; } np12=P1IN&BIT6; if(np12==0) { key=14; } np13=P1IN&BIT7; if(np13==0) { key=15; } P1OUT=0X00; return key; while(1) { if((P1IN&0X0F)==0x0f) break; }} void main(){ uchar key_value; WDTCTL=WDTPW+WDTHOLD; P1DIR=0X0F; P2DIR=0XFF; P2OUT=0XFF; while(1) { if((P1IN&0XF0)!=0XF0) { delay(100); if((P1IN&0XF0)!=0XF0) { delay(100); if((P1IN&0XF0)!=0XF0) { key_value=keyvalue(); } } } P2OUT=~key_value; }} tips:感谢大家的阅读,本文由我司收集整编。仅供参阅!

C语言编程游戏代码

#include #include #include #include #include #define L 1 #define LX 15 #define L Y 4 static struct BLOCK { int x0,y0,x1,y1,x2,y2,x3,y3; int color,next; intb[]={{0,1,1,1,2,1,3,1,4,1},{1,0,1,3,1,2,1,1,4,0},{1,1,2,2,1,2,2,1,1,2},{0,1,1,1,1,0,2,0,2,4}, {0,0,0,1,1,2,1,1,2,3},{0,0,1,0,1,1,2,1,3,8},{1,0,1,1,2,2,2,1,2,5},{0,2,1,2,1,1,2,1,2,6},{0,1,0,2,1,1,1 ,0,3,9},{0,1,1,1,1,2,2,2,3,10},{1,1,1,2,2,1,2,0,3,7},{ 1,0,1,1,1,2,2,2,7,12},{0,1,1,1,2,1,2,0,7,13},{0 ,0,1,2,1,1,1,0,7,14},{0,1,0,2,1,1,2,1,7,11},{0,2,1,2,1,1,1,0,5,16},{0,1,1,1,2,2,2,1,5,17},{1,0,1,1,1, 2,2,0,5,18},{0,0,0,1,1,11,2,1,5,15},{0,1,1,1,1,0,2,1,6,2,0},{0,1,1,2,1,1,1,0,6,21},{0,1,1,2,1,1,2,1,6 ,22},{1,0,1,1,1,2,2,1,6,19}}; static int d[10]={33000,3000,1600,1200,900,800,600,400,300,200}; int Llevel,Lcurrent,Lnext,Lable,lx,ly,Lsum; unsigned Lpoint; int La[19][10],FLAG,sum; unsigned ldelay; void scrint(),datainit(),dispb(),eraseeb(); void throw(),judge(),delayp(),move(0,note(0,show(); int Ldrop(),Ljudge(),nextb(),routejudge(); } main() { char c; datainit(); Label=nextb(); Label=Ldrop(); while(1){ delayp(); if(Label!=0) { Ljudge(); Lable=nextb(); } ldelay--; if(ldelay==0)

51单片机矩阵键盘的C语言程序与分析

51单片机矩阵键盘的C语言程序与分析 2009-10-17 19:25 学习51单片机矩阵键盘时,我有点迷乱了,不知道是怎样处理的,经过仔细分析电路,然后终于明白其中的原理,这样的话,再看程序,就是那样的简单了。。 首先看一下电路图是怎样连接的,我买的开发板上是AT89S52单片机,矩阵键盘在P3口。接法如下图: 当然上面的图的意思是P3.1~P3.3 跟P3.4~P3.7不一样的,他们是相互连接(当按下键时),组成4*4=16个键的。

如果给P3一个扫描初值的话:如0x0F ,则没有键按下时为: P3.1~P3.3为1,P3.4~P3.7为0。 如果有键按下,则情况发生变化:高电平接入低电平:如P3.3与P3.7连接的键按下,则P3.3与P3.7为0,即接地了。 则P3此时为:0000 0111,这时如果用P3&0x0F,则高四位为0,低四位保留,可以得到低四位的内容了。 通过去抖操作,即一个delay,可以得到低四位内容。这里设为:h=P3&0x0F; 如果再得到高四位内容,则可以组成一个数,来定位哪个键了。 用P3=h|0xF0;这会出现什么情况呢?1|0=1 1| 1 =1,这里难道高四位全置1 吗?不是的,当赋值后,如果有键按下的话,P3高四位不会全为1111,被拉到0了。如P3.3与P3.7连接的键按下,则P3.3与P3.7为0,即接地了。即:0111 0111,&F0之后,得到0111 0000,这样的话,我们得到高四位的值了, 用高四位+低四位,就可以得到一个数值,确定一个键。 下面看看人家编写的程序,相信不是太难了吧。 //keyboard.c 这里的行与列的扫描,也就是把字节的8位,高四位与低四位分开来,从而确定坐标。 //行列扫描程序,可以自己定义端口和扫描方式,这里做简单介绍 #include //包含头文件 #define uchar unsigned char #define uint unsigned int unsigned char const dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F,数码管来显示按下键的值。 uchar keyscan(void); //主要的矩阵键盘扫描函数。 void delay(uint i); void main() { uchar key; P2=0x00;//1数码管亮按相应的按键,会显示按键上的字符 while(1) { key=keyscan();//调用键盘扫描,

矩阵键盘程序c程序,51单片机.

/*编译环境:Keil 7.50A c51 */ /*******************************************************/ /*********************************包含头文件********************************/ #include /*********************************数码管表格********************************/ unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x 8E}; /**************************************************************************** 函数功能:延时子程序 入口参数: 出口参数: ****************************************************************************/ void delay(void) { unsigned char i,j; for(i=0;i<20;i++) for(j=0;j<250;j++); } /**************************************************************************** 函数功能:LED显示子程序 入口参数:i 出口参数: ****************************************************************************/ void display(unsigned char i) { P2=0xfe; P0=table[i]; } /**************************************************************************** 函数功能:键盘扫描子程序 入口参数: 出口参数: ****************************************************************************/ void keyscan(void) { unsigned char n; //扫描第一行 P1=0xfe;

C编写的小游戏

C编写的小游戏 集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-

#i n c l u d e usingnamespacestd; constcharMOUSE='*'; constcharWAY=''; constcharWALL='@'; constcharPASS='.'; constcharIMPASS='X'; classGame{ public: Game(size_tuWidth,size_tuHeight):m_uWidth(uWidth),m_uHeigh t(uHeight),m_pcMaze(newchar[uWidth*uHeight]),m_mouse(0,1){ srand(time(NULL)); char(*pcMaze)[m_uWidth]=reinterpret_cast(m_pcMaze); for(size_ti=0;im_uWidth-5)) pcMaze[i][j]=WAY;

elseif(i==0||i==m_uHeight- 1||j==0||j==m_uWidth-1) pcMaze[i][j]=WALL; else pcMaze[i][j]=rand()%4WAY:WALL; } ~Game(void){ if(m_pcMaze){ delete[]m_pcMaze; m_pcMaze=NULL; } } voidRun(void){ for(Show();!Quit()&&Step();); } private: classMouse{ public: Mouse(size_tx,size_ty):m_x(x),m_y(y){} size_tGetX(void){returnm_x;} size_tGetY(void){returnm_y;} voidStepRight(void){

单片机矩阵键盘检测程序并用数码管显示c语言程序

#include #define uint16 unsigned int #define uint8 unsigned char //控制数码管段选锁存口 sbit P3_7=P3^7; //共阴数码管显示 uint8 code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0}; uint8 temp; uint16 num; //延时子函数 void delay(uint16 z) { uint16 x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } //子函数声明 uint8 keyscan(); void display(uint8);

void main() { num=17; while(1) { display(keyscan()); } } void display(uint8 num1) { P2=0xf8; P3_7=1; P0=table[num1-1]; P3_7=0; } uint8 keyscan() { P1=0xfe; temp = P1;

temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xee:num=1;break; case 0xde:num=2;break; case 0xbe:num=3;break; case 0x7e:num=4;break; default:break; } while(temp!=0xf0)//检测按键是否放开 { temp=P1; temp=temp&0xf0; }

C语言小游戏程序设计

程序设计实验 大作业 学院: 班级: 学号: 指导老师: 姓名: 实验项目名称:猜数字游戏成绩:

一、实验要求 根据所学知识编写一个小游戏--猜数字游戏 二、实验目的 为了加深对编程课程的基本知识的理解和掌握,并且掌握编程和程序调试的技巧通过程序的设计训练和提高学生的基本技能,掌握字符串的表示方法和字符串函数的功能、编程软件操作的基本知识、键盘上特殊键的获取以及图形方式下光标的显示,提高编程兴趣与编程水平,学会如何正确的书写程序设计说明文档,提高解决实际问题的能力,巩固语法规则的理解和掌握,学会通过源程序写出流程图,提高自学以及查阅资料的能力。 三、功能模块分析 1.系统能够随机产生一个1到1000的数字 2.比较输入数字与随机产生数字的大小 3.选择是否继续游戏 四、修改处 1.将输出内容由英文变为汉字 2.改变背景及输出字体的颜色 3将随机产生的数值范围由1-100变为1-1000 四、设计思路 # include # include # include main() { void textbackground(int color); system("color fc"); int a,b; char c; srand(time(NULL)); a=1+(rand()%1000); printf("这有一个数字从1到1000.\n你能猜出它吗?\n输入你猜的数字.\n"); scanf("%d",&b); while(b!=-1) { if(b==a) { printf("恭喜你猜对了!\n你喜欢再玩一遍吗?(y or n)?");

c语言游戏讲解

1: C语言游戏设计 1.1:课程设计目标 C语言编写一款打飞机游戏,掌握函数的使用和编程的整体思路。 1.2:整体设计思路 总体设想:运行游戏时显示游戏的界面,允许用户选择个性化设置,A、D、W分别控制飞机的左右移动和发射子弹,不同的敌机飞行和攻击,并不时伴有奖励。游行运行时播放音效,同时记录飞机的生命和得分,达到一定的分数进入下一关。 需要构建的函数:位图调用模块、界面输出函数、主菜单函数、设置函数、速度选择函数、游戏控制函数、敌机移动函数、子弹移动函数、boss 函数、计时函数。 1.3: 需调用的头文件:include //常规库头文件 include //控制台输入输出流头文件 include //系统函数头文件 include //时间函数头文件 include //windows函数库 include //导入声音头文件 pragma comment(lib,"winmm.lib ")//导入声音头文件库 1.4: 游戏过程模拟:

1.5:显示效果如下图所示:

初始设置界面:游戏界面 2 算法实现 2.1需要了解的知识点: Main函数和menu函数编写比较顺利,在bmp函数中遇到如何显示窗口和调用图片的问题,看了一些例子和网上的资料,逐渐掌握了一下知识点: ○1设备上下文:设备,显示器,打印机,数码相机,扫描仪这些类似的设备, 上下文,就是上面设备的相关信息,比如屏幕分辨率等 ○2 HANDLE 通用句柄,实际上是一个指向指针的指针,在Windows操作系统中, 内存操作器经常移动对象,来满足各种程序的内存需要,为了解决寻找对象问题,内存管理器开辟出一块专门的内存储存空间,把移动后的地址存储在该空间,这样就可以通过该地址间接寻找到对象。 ○3 HDC设备描述句柄,实现对窗口操作区域的GDI(图形设备接口, 负责系统 与绘图程序之间的信息交换,处理所有Windows程序的图形输出)操作,如画线、写文本、绘画制图等。 ○4 BITMAP 用于处理有像素数据定义的图像的对象。

基于C51单片机矩阵键盘控制蜂鸣器的应用

学校代码 10126 学号科研创新训练论文 题目基于C51单片机的蜂鸣器和流水灯的 应用 院系内蒙古大学鄂尔多斯学院 专业名称自动化 年级 2013 级 学生姓名高乐 指导教师高乐奇 2015年06月20日

基于C51单片机的蜂鸣器和流水灯的应用 摘要 当今时代是一个新技术层出不穷的时代,在电子领域尤其是自动化智能控制领域,传统的分立元件或数字逻辑电路构成的控制系统,正以前所未见的速度被单片机智能控制系统所取代。单片机具有体积小、功能强、成本低、应用面广等优点,可以说,智能控制与自动控制的核心就是单片机。本文介绍了单片机的发展及应用,和基于单片机的蜂鸣器和流水灯的知识及应用,还介绍了此次我所设计的课题。 关键词:C-51单片机,控制系统,流水灯,蜂鸣器,程序设计

The application of buzzer and flowing water light based on C51 MCU Author:GaoLe Tutor:GaoLeQi Abstract This age is a new technology emerge in endlessly era, in the electronic field especially automation intelligent control field, the traditional schism components or digital logic circuit, is composed of control system with unprecedented speed was replaced by micro-controller intelligent control system. SCM has small, strong function, low cost, etc, it can be said that wide application, intelligent control and automatic control core is the micro-controller.This article introduces the MCU development and application,the knowledge and application of buzzer and flowing water light based on MCU,then introduces the task I have designed this time. Keyword:C51 micro-controller,control system,flowing water light,buzzer ,programming

C编写的小游戏

C编写的小游戏集团档案编码:[YTTR-YTPT28-YTNTL98-UYTYNN08]

#i n c l u d e usingnamespacestd; constcharMOUSE='*'; constcharWAY=''; constcharWALL='@'; constcharPASS='.'; constcharIMPASS='X'; classGame{ public: Game(size_tuWidth,size_tuHeight):m_uWidth(uWidth),m_uHeight(uHe ight),m_pcMaze(newchar[uWidth*uHeight]),m_mouse(0,1){ srand(time(NULL)); char(*pcMaze)[m_uWidth]=reinterpret_cast(m_p cMaze); for(size_ti=0;im_uWidth-5)) pcMaze[i][j]=WAY; elseif(i==0||i==m_uHeight- 1||j==0||j==m_uWidth-1) pcMaze[i][j]=WALL; else pcMaze[i][j]=rand()%4?WAY:WALL; } ~Game(void){ if(m_pcMaze){ delete[]m_pcMaze; m_pcMaze=NULL; } } voidRun(void){ for(Show();!Quit()&&Step();); } private: classMouse{ public: Mouse(size_tx,size_ty):m_x(x),m_y(y){} size_tGetX(void){returnm_x;} size_tGetY(void){returnm_y;} voidStepRight(void){

纯C语言写的一个小型游戏-源代码

/* A simple game*/ /*CopyRight: Guanlin*/ #include #include #include #include #include #include struct object_fix { char name[20]; char id[5]; char desc[500]; char action[30]; char im[5]; }; struct object_move { char name[20]; char id[5]; char desc[500]; int loc; int pwr; int strg; char im[5]; }; struct rover { char name[20]; char id[5]; char desc[500]; int pwr; int strg; int location[2]; char im[5]; }; struct map /* this is the map structure*/ { char data[20]; char add_data[20]; int amount; int x; /* this were the successor keeps it's x & y values*/ int y; }; struct location /*this structure is for the successor lister*/ { float height; char obj;

STM32-矩阵键盘程序4×4

/*--------------------------------------------------------------------------------------* 矩阵键盘驱动 * 文件: keyboard.c * 编写人:LiuHui * 描述:扫描4x4 矩阵键盘输入,并返回键值 * 适用范围:驱动采用ST3.5 库编写,适用于STM32F10x 系列单片机 * 所用引脚:PA0-PA7 * 编写时间:2014 年5 月20 日 --------------------------------------------------------------------------------------*/ #include "stm32f10x.h" #include "keyboard.h" #include "dealy.h" /*--------------------------------矩阵键盘初始化----------------------------------------* 功能:初始化stm32 单片机GPIO //PA0-PA7 * 参数传递: * 输入:无 * 返回值:无 --------------------------------------------------------------------------------------*/ void KeyBoard_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3); GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); } /*------------------------------矩阵键盘扫描--------------------------------------------* 功能:扫描矩阵键盘,并返回键值 * 参数: * 输入:无 * 返回:有键按下返回该键值 * 无键按下时则返回0 --------------------------------------------------------------------------------------*/ u8 Read_KeyValue(void) { u8 KeyValue=0; if((GPIO_ReadInputData(GPIOA)&0xff)!=0x0f) {

矩阵键盘单个数码管显示C语言程序

#include #define uchar unsigned char #define uint unsigned int uchar code_h,code_l; //定义行扫描码,列检测数据uchar tmp,keyvalue; //定义接收键值 /*函数说明*/ void delay(void); uchar keyscan(); /*主函数*/ void main () //键值处理 { while(1) { tmp=keyscan();//调用键盘扫描程序 switch(tmp) { case 0x11: P0=0x3f; break; //0 case 0x12: P0=0x06; break; //1 case 0x14: P0=0x5b; break; //2 case 0x18: P0=0x4f; break; //3 case 0x21: P0=0x66; break; //4 case 0x22: P0=0x6d; break; //5 case 0x24: P0=0x7d; break; //6 case 0x28: P0=0x07; break; //7 case 0x41: P0=0x7f; break; //8 case 0x42: P0=0x67; break; //9 case 0x44: P0=0x77; break; //a case 0x48: P0=0x7c; break; //b case 0x81: P0=0x39; break; //c case 0x82: P0=0x5c; break; //d case 0x84: P0=0x79; break; //e case 0x88: P0=0x71; break; //f case 0x00: ; break; default:P0=0x00; } delay(); } } /*延时函数*/ void delay(void) {uchar i; for(i=0;i<200;i++){} } /*键盘扫描函数*/ uchar keyscan(void)

使用C语言编写简单小游戏

纯真童趣的《泡泡堂》,还有武林情仇,笑傲江湖的《剑侠情缘on line》.它是e 时代常谈的话题,是交互式娱乐的主力军,是一种高层次的综合艺术,更是一个民族的文化,世界观的全新传播方式 .作为游戏玩家的我们,是不是想设计一个属于自己的游戏呢? 爱玩是人的天性,而C语言是我们计算机专业都要学习的一门基础学科.一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣. 1,总是从Hello,world开始学习编程的第一个程序,一般就是打印一个亲切的词语——"Hell o,world!".让我们来看看这个最简单的C程序: #incolude <> /*把输入输出函数的头文件包含进来*/ int main() { printf("Hello, world!");/*在屏幕上输出字符串"Hello,world!"*/ return 0;/*退出main函数,并返回0*/ } 下面我们发现几个值得改进的地方,1,程序的运行结果一闪而过 .2,每执行这个程序一次都能看见上次运行留下的字符.3,我们还希望屏幕输出一个笑脸来欢迎我们. 让我们来改进一下这个程序吧! 1,在return语句的前面加一句:getch ();,表示按任意键结束.2,在printf语句前用clrscr函数清屏,要使用这个函数和getch函数,需要在程序开头再包含头文件码也有许多非常好玩的字符,比如ASCII码值为2的就是一个笑脸,我们可以用printf("%c", 2)来输出一个笑脸. 现在我们把Hello,world程序改成一个更好看的Hello,world了.下面让我们开始做游戏吧! 2,心动的开始,一个运动中的笑脸大家小时侯喜欢看动画片吗?哈哈,我猜你们都喜欢吧!下面就让我们来做一个小动画吧.在屏幕上显示一个运动的小笑脸,而且当它到达屏幕的边缘时会自动弹回来.先在程序定义一个在屏幕中运动的点的结构: struct move_point { int x, y;/*该点的位置,包括x坐标和y坐标*/ int xv, yv;/*该点在x轴,y轴的速度*/ }; 运动的原理是,先擦去物体先前的轨迹,让物体按其速度移动一段距离,再画出该物体.让我们看到以下代码: gotoxy, ;/*把光标移到指定的坐标*/ printf(" ");/*输出一个空格,把先前的字符擦去*/ 然后我们让物体按其速度运动: += ;/*水平方向按x轴的速度运动*/ += ;/*垂直方向按y轴的速度运动*/ 运动后还要判断物体是否出界,如果出了界,就令物体反弹,即让它下一刻的速度等于现在的速度的相反数.最后打印出这个笑脸: gotoxy, ; printf("%c\b", 2); /*输出ASCII码值为2的"笑脸"字符*/ 怎么样?是不是很有趣呢?不过这个笑脸一直是自己运动,能不能让我们来控制它运动呢?答案是肯定的,让我们继续往下学吧! 3,交互的实现——让我们来控制笑脸运动 这个程序的主要功能是接受按键,如果接收的是方向键,就让笑脸顺着方向移动,如果接收的是ESC键就退出程序,其他按键则忽略处理.接受按键我们用以下两条语句: while (bioskey(1) == 0);/*等待按键*/ key = bioskey(0);/*把接收的按键的键盘码赋给变量key*/ 然后用switch语句来判断按键以及执行相关操作,如下: switch (key) /*对变量key的值进行判断*/ { case UP: /*如果按的是向上键*/ … break; /*让物体向上运动,并退出switch*/

c语言编写的小游戏代码

用vc6.0新建一个Win32 Application工程,把附件代码拷贝进去即可。上下左右键控制蛇的方向,空格键用于启动或者停止游戏。 上下左右空格键 #include #include #include #define C_W 516 #define C_H 548 //#define C_W 1024 //#define C_H 1024 #define GO_RIGHT 0x01 #define GO_DOWN 0x02 #define GO_LEFT 0x03 #define GO_UP 0x04 #define SNAKE_NUMBER 30 typedef struct node_struct { unsigned char direction; unsigned char cnt; }s_node,*s_node_handle; s_node s_count[SNAKE_NUMBER ]; typedef struct SNAKE { unsigned char Head_X; unsigned char Head_Y; unsigned char Tail_X; unsigned char Tail_Y; unsigned char h_index; unsigned char t_index;

unsigned char food_state; unsigned char score; unsigned char snake_state; } Snake_Data,*Snake_Data_handle; Snake_Data snk_1; #define MAP_X 64 #define MAP_Y 64 unsigned char game_map[MAP_Y][MAP_X]; LRESULT CALLBACK Win_tetris_Proc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { snk_1.Head_X = 0x01;//head x snk_1.Head_Y = 0x00;//head y snk_1.Tail_X = 0x00;//tail x snk_1.Tail_Y = 0x00;//tail y snk_1.h_index=0; snk_1.t_index=0; snk_1.food_state=0; snk_1.score=0; snk_1.snake_state = 1; s_count[snk_1.h_index].cnt=2; s_count[snk_1.h_index].direction=GO_RIGHT; s_count[snk_1.t_index].cnt=2; s_count[snk_1.t_index].direction=GO_RIGHT; WNDCLASS wndcls; wndcls.cbClsExtra=0;

相关文档 最新文档