一个“歼灭敌机”的小游戏,DEVc++通过编译:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define zlx 10 //增量坐标(x)让游戏框不靠边
#define zly 3 //增量坐标(y)让誉清游戏框不靠边
#define W 26 //游戏框的宽度
#define H 24 //游戏框的高度
int jiem[22][22]={0}, wj=10 //界面数组, 我机位置(初值为10)
int speed=4,density=30, score=0,death=0//敌机速度, 敌机密度, 玩家成绩,死亡次数
int m=0,n=0 // m,n是控制敌机的变量
void gtxy (int x, int y) //控制光标位置的函数
{ COORD pos
pos.X = x pos.Y = y
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos )
}
void Color(int a) //设定颜色的函数(a应为1-15)
{ SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), a )}
void yinc(int x=1,int y=0) //隐藏光标的函数
{ CONSOLE_CURSOR_INFO gb={x,y} //y设为0即隐藏
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb)
}
void csh( ) //初始化函数
{ int i
Color(7)
gtxy(zlx,zly)printf("╔") gtxy(zlx+W-2,zly)printf("╗") //左上角和右上角的框角
gtxy(zlx,zly+H-1)printf("╚")gtxy(zlx+W-2,zly+H-1)printf("╝")//下边两框角
for(i=2i<W-2i+=2) {gtxy(zlx+i,zly) printf("═樱虚搭")} //打印上横框
for(i=2i<W-2i+=2) {gtxy(zlx+i,zly+H-1)printf("═")} //打印下横框
for(i=1i<H-1i++) { gtxy(zlx,zly+i) printf("║")} //打印左竖框
for(i=1i<H-1i++) {gtxy(zlx+W-2,zly+i)printf("║")} //打印右竖框
Color(14)gtxy(19,2)printf("歼灭敌机")Color(10)
gtxy(37,5)printf("设置:Esc ")
gtxy(37,7)printf("发射:↑ ")
gtxy(37,9)printf("控制:← → ")
gtxy(37,11)printf("得分:%d"脊拿,score)
gtxy(37,13)printf("死亡:%d",death)
yinc(1,0)
}
void qcjm( ) //清除界面函数
{int i,j
for(i=0i<H-2i++)
for(j=0j<W-4j++){gtxy(zlx+2+j,zly+1+i)printf(" ")}
}
void feiji( ) //飞机移动函数
{int i,j
for(i=21i>=0i--) //从底行往上是为了避免敌机直接冲出数组
for(j=0j<22j++)
{if(i==21&&jiem[i][j]==3) jiem[i][j]=0 //底行赋值0 以免越界
if(jiem[i][j]==3) jiem[i][j]=0, jiem[i+1][j]=3
}
if(jiem[20][wj]==3&&jiem[21][wj]==1) death++
}
void zidan( ) //子d移动函数
{ int i,j
for(i=0i<22i++)
for(j=0j<22j++)
{if(i==0&&jiem[i][j]==2) jiem[i][j]=0
if(jiem[i][j]==2) { if(jiem[i-1][j]==3) score+=100,printf("\7")
jiem[i][j]=0,jiem[i-1][j]=2}
}
}
void print( ) //输出界面函数
{int i,j
qcjm( )
for(i=0i<22i++)
for(j=0j<22j++)
{ gtxy(12+j,4+i)
if(jiem[i][j]==3) {Color(13)printf("□")}
if(jiem[i][j]==2) {Color(10)printf(".")}
if(jiem[i][j]==1) {Color(10)printf("■")}
}
gtxy(37,11)Color(10)printf("得分:%d",score)
gtxy(37,13)printf("死亡:%d",death)
}
void setting( ) //游戏设置函数
{ qcjm( )
gtxy(12,4)printf("选择敌机速度:")
gtxy(12,5)printf(" 1.快 2.中 3.慢>>")
switch(getche( ))
{case '1': speed=2break
case '2': speed=4break
case '3': speed=5break
default: gtxy(12,6)printf(" 错误!默认值")
}
gtxy(12,7)printf("选择敌机密度:")
gtxy(12,8)printf(" 1.大 2.中 3.小>>")
switch(getche( ))
{case '1': density=20break
case '2': density=30 break
case '3': density=40break
default: gtxy(12,9)printf(" 错误!默认值")
}
for(int i=0i<22i++)
for(int j=0j<22j++) jiem[i][j]=0
jiem[21][wj=10]=1jiem[0][5]=3
gtxy(12,10)printf(" 按任意键保存...")
getch( )
qcjm( )
}
void run( ) //游戏运行函数
{ jiem[21][wj]=1 //值为1代表我机(2则为子d)
jiem[0][5]=3 //值为3代表敌机
SetConsoleTitle("歼灭敌机") //设置窗口标题
while(1)
{ if (kbhit( )) //如有键按下,控制我机左右移动、发射或进行设定
{int key
if((key=getch( ))==224) key=getch( )
switch(key)
{case 75: if(wj>0) jiem[21][wj]=0,jiem[21][--wj]=1break
case 77: if(wj<20) jiem[21][wj]=0,jiem[21][++wj]=1 break
case 72: jiem[20][wj]=2break
case 27: setting( )
}
}
if(++n%density==0) //控制产生敌机的速度
{ n=0srand((unsigned)time(NULL))
jiem[0][rand( )%20+1]=3
}
if(++m%speed==0) { feiji( )m=0} //控制敌机移动速度(相对子d而言)
zidan( ) //子d移动
print( ) //输出界面
Sleep(120) //延时120毫秒
}
}
int main( )
{csh( )
run( )
return 0
}
int main(){
int a=3,b=2,c=2.5
float y=0
y=(float)(a+b)/锋芹团3+(int)c
printf("首判%f"银橘,y)
return 0
}
下面是我初学时的一个列子,是计算时间的。。可以进行时间相加减```
由于我是在linux下吵宴运行的,再转到window上所以格式会有点乱```谅解!!!
代码也没有进行优化颤碰团处理的```
#include <stdio.h>
#include <stdlib.h>
typedef struct time
{
int year
int month
int day
int hour
int minute
int sec
} ST_TIME
ST_TIME date
void add_sec(int secs)
void add_minute(int minutes)
void add_hour(int hours)
void add_day(int days)
int maxday(int leap, int month)
void add_month(int months)
void add_year(int years)
void sub_sec(int secs)
void sub_minute(int minutes)
void sub_hour(int hours)
void sub_day(int days)
void sub_month(int months)
void sub_year(int years)
void sum(void)
void sub(void)
void init_system(void)
int add_number
int main(int argc, char *argv[])
{
char array[20]
char emblem
init_system()
printf("Press the '+' or '-' to control.\n")
scanf("%c",&emblem)
switch (emblem)
{
case '+':
{
sum()
break
}
case '-':
{
sub()
break
}
}
sprintf(array,"%d-%02d-%02d %02d:%02d:%02d",date.year,date.month,
date.day,date.hour,date.minute,date.sec)
printf("%s\n",array)
return 0
}
void init_system(void)
{
char str[20]
char *p
p = str
printf("Enter the date:\n")
printf("Be sure your inputs like that:2008 12 12 12 12 12\n")
gets(str)
date.year = atoi(p)
date.month = atoi(p+5)
date.day = atoi(p+8)
date.hour = atoi(p+11)
date.minute = atoi(p+14)
date.sec = atoi(p+17)
}
void sum(void)
{
int lab
printf("please choose the option:\n")
printf("1: add the secs\茄橘n")
printf("2: add the minutes\n")
printf("3: add the hours\n")
printf("4: add the days\n")
printf("5: add the months\n")
printf("6: add the years\n")
while (getchar() != '\n')
scanf("%d",&lab)
switch (lab)
{
case 1 :
{
printf("enter the increased secs:")
scanf("%d",&add_number)
add_sec(add_number)
break
}
case 2:
{
printf("enter the increased minutes:")
scanf("%d",&add_number)
add_minute(add_number)
break
}
case 3:
{
printf("enter the increased hours:")
scanf("%d",&add_number)
add_hour(add_number)
break
}
case 4:
{
printf("enter the increased days:")
scanf("%d",&add_number)
add_day(add_number)
break
}
case 5:
{
printf("enter the increased months:")
scanf("%d",&add_number)
add_month(add_number)
break
}
case 6:
{
printf("enter the increased years:")
scanf("%d",&add_number)
add_year(add_number)
break
}
}
}
void sub(void)
{
int lab
printf("please choose the option:\n")
printf("1: reduce the secs\n")
printf("2: reduce the minutes\n")
printf("3: reduce the hours\n")
printf("4: reduce the days\n")
printf("5: reduce the months\n")
printf("6: reduce the years\n")
scanf("%d",&lab)
switch (lab)
{
case 1 :
{
printf("enter the reduced secs:")
scanf("%d",&add_number)
sub_sec(add_number)
break
}
case 2:
{
printf("enter the reduced minutes:")
scanf("%d",&add_number)
sub_minute(add_number)
break
}
case 3:
{
printf("enter the reduced hours:")
scanf("%d",&add_number)
sub_hour(add_number)
break
}
case 4:
{
printf("enter the reduced days:")
scanf("%d",&add_number)
sub_day(add_number)
break
}
case 5:
{
printf("enter the reduced months:")
scanf("%d",&add_number)
sub_month(add_number)
break
}
case 6:
{
printf("enter the increased years:")
scanf("%d",&add_number)
sub_year(add_number)
break
}
}
}
void add_sec(int secs)
{
date.sec = date.sec + secs
while (date.sec >= 60)
{
add_minute(1)
date.sec = date.sec - 60
}
}
void add_minute(int minutes)
{
date.minute = date.minute + minutes
while (date.minute >= 60)
{
add_hour(1)
date.minute = date.minute - 60
}
}
void add_hour(int hours)
{
date.hour = date.hour + hours
while (date.hour >= 24)
{
add_day(1)
date.hour = date.hour - 24
}
}
void add_day(int days)
{
int leap
date.day = days + date.day
/****************************************
* 功能:判断是否为闰年 *
****************************************/
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
while (date.day >maxday(leap,date.month))
{
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
date.day = date.day - maxday(leap,date.month)
add_month(1)
}
}
void add_month(int months)
{
int leap
date.month = date.month + months
while (date.month >12)
{
date.month = date.month - 12
add_year(1)
}
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
if (leap==1 &&date.month==2 &&date.day>=29)
{
date.day = 29
}
else if (leap==0 &&date.month==2 &&date.day>=28)
{
date.day = 28
}
}
void add_year(int years)
{
int leap
date.year = date.year + years
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
if (leap==1 &&date.month==2 &&date.day>=29)
{
date.month = 2
date.day = 29
}
else if (leap==0 &&date.month==2 &&date.day>=28)
{
date.month = 2
date.day = 28
}
}
void sub_sec(int secs)
{
date.sec = date.sec - secs
while (0 >date.sec)
{
sub_minute(1)
date.sec = date.sec + 60
}
}
void sub_minute(int minutes)
{
date.minute = date.minute - minutes
while (0 >date.minute)
{
sub_hour(1)
date.minute = date.minute + 60
}
}
void sub_hour(int hours)
{
date.hour = date.hour - hours
while (0 >date.hour)
{
sub_day(1)
date.hour = date.hour + 24
}
}
void sub_day(int days)
{
int leap
date.day = date.day - days
/****************************************
* 功能:判断是否为闰年 *
****************************************/
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
while (0 >= date.day)
{
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
date.day = date.day + maxday(leap,date.month-1)
sub_month(1)
}
}
void sub_month(int months)
{
int leap
date.month = date.month - months
while (0 >= date.month)
{
date.month = date.month + 12
sub_year(1)
}
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
if (leap==1 &&date.month==2 &&date.day>=29)
{
date.day = 29
}
else if (leap==0 &&date.month==2 &&date.day>=28)
{
date.day = 28
}
}
void sub_year(int years)
{
int leap
date.year = date.year - years
if ((date.year%4 == 0 &&date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1/*是闰年则leap的值为1*/
}
else
{
leap = 0/*不是则leap的值为0 */
}
if (leap==1 &&date.month==2 &&date.day>=29)
{
date.month = 2
date.day = 29
}
else if (leap==0 &&date.month==2 &&date.day>=28)
{
date.month = 2
date.day = 28
}
}
int maxday(int leap, int month)
{
int max_day/*用于返回此月份的最大值*/
switch (month)
{
case 4:
{
max_day = 30
break
}
case 6:
{
max_day = 30
break
}
case 9:
{
max_day = 30
break
}
case 11:
{
max_day = 30
break
}
case 2:
{
if (leap == 1)
{
max_day = 29
break
}
else
{
max_day = 28
break
}
}
default: max_day = 31
}
return max_day
我这里还有很多的,如果要的话,就联系我121779988````
只不过我也是个菜鸟级的``
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)