一个打飞机的游戏,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 zb //zb代指坐标
zb.X = x zb.Y = y
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), zb )
}
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} //gb代指光标, 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]=1 jiem[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
}
学习“推箱子”C语言编码:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0 //m代表第几关
struct maps{short a[9][11]}
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
衡蚂巧 1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
物野 1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
咐键 1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 }
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0} //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info)
printf("\n\n \t\t\b推箱子")
printf("\n \t")
for (int i = 0i <9i++)
{for (int j = 0j <11j++)
{switch (map[m].a[i][j])
{case 0: printf(" ")break
case 1: printf("■")break
case 3: printf("◎")break
case 4: printf("□")break
case 5: printf("♀")break //5是人
case 7: printf("□")break //4 + 3箱子在目的地中
case 8: printf("♀")break // 5 + 3人在目的地中
}
}
printf("\n\t")
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord
coord.X = x
coord.Y = y
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)
}
void start( ) //开始游戏
{ int r, c //r,c用于记录人的下标
for (int i = 0i <9i++)
{ for (int j = 0j <11j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i c = j} } //i j 人的下标
}
char key
key = getch( )
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3)printf("♀") // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3)printf("□")gtxy(2*c+8,r-1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 2][c] += 4 map[m]. a [r - 1][c] += 1
map[m]. a [r][c] -= 5}
} break
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3)printf("□")gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 2][c] += 4map[m]. a [r + 1][c] += 1
map[m]. a [r][c] -= 5}
}break
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 1] += 5map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3)printf("□")gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 2] += 4map[m]. a [r ][c - 1] += 1
map[m]. a [r][c] -= 5}
}break
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 1] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3)printf("□")gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 2] += 4map[m]. a [r][c + 1] += 1
map[m]. a [r][c] -= 5}
}break
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&&map[m].a[5][3]==7&&
map[m].a[6][2]==7&&map[m].a[6][3]==7) return 1}
if(m==1){if(map[m].a[5][2]==7&&map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1}
if(m==2){if(map[m].a[7][1]==7&&map[m].a[7][2]==7&&map[m].a[7][3]==7&&
map[m].a[7][4]==7&&map[m].a[7][5]==7) return 1}
if(m==3){if(map[m].a[4][4]==7&&map[m].a[4][5]==7&&map[m].a[4][6]==7&&
map[m].a[5][4]==7&&map[m].a[5][5]==7&&map[m].a[5][6]==7) return 1}
if(m==4){if(map[m].a[3][2]==7&&map[m].a[4][1]==7&&map[m].a[4][2]==7&&
map[m].a[5][1]==7&&map[m].a[5][2]==7) return 1}
return 0
}
int main( ) //主函数
{ while (1)
{ system("cls")
DrMap( )
while (1)
{ start( )
if(ifwan()){printf(" }7")break} //完成后响铃
m+=1
}
return 0
}
public class Demo{铅纤 public static 陆雹void 早激帆main(String args[]){
System.out.println("有趣的java小程序")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)