一个“歼灭敌机”的小游戏,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( ) //子弹移动函数
{ 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则为子弹)
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} //控制敌机移动速度(相对子弹而言)
zidan( )
print( )
Sleep(120) //延时120毫秒
}
}
int main( )
{csh( )
run( )
return 0
}
新手要方便写代码,可以收藏下面几个自编函数:
SetConsoleTitle("俄罗斯方块") //设置窗口左上角标题栏处出现"俄罗斯方块"5个字
srand( (unsigned) time(NULL) ) //初始化随机数发生器
n= rand( ) % 20 //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数
SetConsoleTitle( )函数在<windows.h>里, srand( )函数与rand( )函数要配合用,
就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。
Sleep(300) //延时300毫秒(就是程序暂停300毫秒后继续运行)
system("cls") //清屏(把窗口里的内容全部清除,光标定于(0,0)位置处)
这两个函数都在<windows.h>里。开头4个自编函数 编写如下:
void gtxy (int x, int y) //控制光标位置的函数
{ COORD pos
pos.X = x
pos.Y = y
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos )
}
void Color (int a) //设定颜色的函数
{ SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ),a )}
void yinc (int x,int y) //隐藏光标的函数
{ CONSOLE_CURSOR_INFO gb={ x , y } //gb代表光标
SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb )
}
void kou(int w,int h) //设置窗口大小的函数
{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE )
COORD size={ w , h }
SetConsoleScreenBufferSize( hl , size )
SMALL_RECT rc={ 0, 0, w, h }
SetConsoleWindowInfo( hl, 1, &rc )
}
最后这个函数,参数w是宽h是高。里边5行中第一行定义了句柄型变量hl,并给它赋值。
第二行定义了坐标型结构体变量size,它的取值决定了缓冲区的大小。第三行就是使用
size的值设置好缓冲区大小。第四行定义了变量rc,它的值决定当前窗口显示的位置与
大小(不得超过缓冲区的大小)。前两个0,0是从缓冲区左上角0列0行位置处开始,后两
个参数可以小于w和h.比如 rc={0,0,w-10,h-5}最后一行使用rc的值设置好窗口,中间
那个参数要为" 1 "或写“ true ”才有效。
1.代码如下#include <stdio.h>
int main()
{
char c
int letter=0,space=0,digit=0,others=0
printf("please input some characters\n")
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++
else if(c==' ')
space++
else if(c>='0'&&c<='9')
digit++
else
others++
}
printf("all in all:letter=%d space=%d digit=%d others=%d\n",letter,space,digit,others)
getch()
}
2.代码如下
//求a和b最大公约数程序1:
int yue(int a,int b)
{
int k=1
int t=a>b?b:a//a大取b,否则取a
for(int i=1i<=ti++)
{
if((a%i==0)&&(b%i==0)) k=i
else continue
}
return k//返回最大公约数
}
//求a和b的最小公倍数,参数c传递的是a和b的最大公约数
int bei(int a,int b,int c)
{
return (a*b)/c
}
void main()
{
int a,b
cout<<"请按从大到小的顺序输入2个要求值的数"<<endl
cin>>a>>b
cout<<"两个数的最大公约数是"<<yue(a,b)<<endl
cout<<"两个数的最小公倍数是"<<bei(a,b,yue(a,b))<<endl
}
//求最大公约数程序2
#include <stdio.h>
int main()
{
int p,r,n,m,temp
printf("please enter two positive integer numbers n,m:")
scanf("%d%d",&n,&m)
if(n<m) //大数放在n中,小数放在m中;
{
temp=n
n=m
m=temp
}
p=n*m//先将n和m的乘积保存在P中,以便求最小公倍数用
while(m!=0)
{
r=n%m//求n和m的最大公约数
n=m
m=r
}
printf("最小公倍数为:%d\n",n)
printf("最大公约数为:%d\n",p/n)
return 0
}
3.代码如下
#include <iostream>
using namespace std
int main()
{
int i,j
int a[3][3]
for(i=0i<3i++)
{
printf("input the %d line' element:",i)
for(j=0j<3j++)
{
scanf("%d",&a[i][j])
}
}
for(i=0i<3i++)
{
for(j=0j<3j++)
{
printf("%2d",a[i][j])
}
cout<<endl
}
int sum=a[0][0]+a[1][1]+a[2][2]+a[0][2]+a[1][1]+a[2][0]
printf("该矩阵对角线元素之和为:%d\n",sum)
return 0
}
4.代码如下
#include <iostream>
#include <string>
using std::cin
using std::cout
using std::endl
using std::string
int main()
{
string s1,s2
cin>>s1>>s2
cout<<s1<<s2<<endl
return 0
}
5.第五个就是文件重定向的问题,和第一个差不多了,只要将输入定向到文件,将输出定向到标准输出即可
第1个,<剪刀石头布>,111行#include<iostream>
#include<string>
#include<ctime>
using namespace std
int main()
{
string playStr //玩家输入的选择
int playWin=0 //玩家赢
int cptWin=0//电脑赢
int noWin=0//平局
int cpt //电脑出什么
int sum=0 //玩了几局
float win=0//胜效率
begin:
sum=playWin+cptWin+noWin
if(sum==0)
{
sum=1
}
if(sum-noWin!=0)
{
win=(float)playWin/(float)(sum-noWin)*100
}
else
{
win=0
}
cout<<"游戏状态:"<<endl<<endl<<" 玩家赢:"<<playWin
<<" 电脑赢:"<<cptWin
<<" 平局:"<<noWin<<" 总局数:"<<sum<<" 胜率:"<<win
<<"%"<<endl<<endl
cout<<"请出拳(1->剪刀 2->石头 3->布 Q->退出)"<<endl
cin>>playStr
srand(time(0))
cpt=rand()%3+1
if(cpt==1) //电脑出 剪刀
{
cout<<"电脑出剪刀"<<endl
if(playStr[0]=='1')
{
cout<<"玩家出剪刀,平局."<<endl
noWin++
}
else if(playStr[0]=='2')
{
cout<<"玩家出石头,玩家赢."<<endl
playWin++
}
else
{
cout<<"玩家出布,玩家输."<<endl
cptWin++
}
}
else if(cpt==2)
{
cout<<"电脑出石头"<<endl
if(playStr[0]=='1')
{
cout<<"玩家出剪刀,玩家输."<<endl
cptWin++
}
else if(playStr[0]=='2')
{
cout<<"玩家出石头,平局."<<endl
noWin++
}
else
{
cout<<"玩家出剪布,玩家赢."<<endl
playWin++
}
}
else
{
cout<<"电脑出布"<<endl
if(playStr[0]=='1')
{
cout<<"玩家出剪刀,玩家赢."<<endl
playWin++
}
else if(playStr[0]=='2')
{
cout<<"玩家出石头,玩家输."<<endl
cptWin++
}
else
{
cout<<"玩家出布,平局."<<endl
noWin++
}
}
if(playStr[0]=='q'||playStr[0]=='Q')
{
return 0
}
else
{
getchar()
getchar()
system("cls")
goto begin
}
}
第2个:还是111行
#include<iostream>
using namespace std
bool Is(long num)
void Print(long i, long num1, long num2)
int main()
{
long m,m1,m2
long a,sum
long i1=0
begin:
cout<<"请输入要分解的数字:"<<endl
cin>>m
if(m<1)
{
goto begin
}
m2=m
cout<<endl
cout<<"----------------------------------------"<<endl
m1=m
cout<<" "<<m<<endl
if(Is(m))
{
cout<<" / \\"<<endl
cout<<" "<<"1"<<" "<<m<<endl
}
else
{
for(long i=2i<m)
{
if( (i%2!=0) || (i==2 ) &&Is(i))
{
a=m%i
if(a==0)
{
m/=i
sum*=i
Print(i1,i,m)
i1++
if(sum==m1 || (Is(m)) )
{
i1=0
break
}
}
else
{
i++
}
}
else
{
i++
}
}
}
cout<<endl<<"----------------------------------------"<<endl<<endl
return
}
bool Is(long num)
{
long m=0
bool is=false
if(num==2)
{
return true
}
if(num%2!=0)
{
m=num+1
m/=2
}
else
{
return false
}
long i
for( i=2i<=mi++)
{
if(num%i!=0)
{
is=true
}
else
{
is=false
break
}
}
return is
}
void Print(long i, long num1, long num2)
{
for(long j=0j<=i+2j++)
{
cout<<" "
}
cout<<" / \\"<<endl
for( j=0j<=i+2j++)
{
cout<<" "
}
cout<<" "<<num1<<" "<<num2<<endl
}
第3个,统计字符串的个数,这个200多行
#include <iostream>
#include <string>
#include <iomanip>
using namespace std
struct WordNode
{
int num
char word[3]
WordNode* pNext
}
WordNode *pHead
void Fun(char* str)
{
WordNode* pNew= new WordNode
pNew->pNext = NULL
char ch[3]
begin:
for(int i=0i<strlen(str)i++)
{
memset( ch, 0, 3)
if( str[i] >= 0 &&str[i] <=127 )
{
ch[0] = str[i]
ch[1] = ' '
}
else
{
ch[0] = str[i]
ch[1] = str[++i]
}
WordNode* head = pHead
bool flag = false
if ( !pHead)
{
pNew ->num = 0
strcpy( pNew ->word, ch)
pHead = pNew
goto begin
}
while( head )
{
if ( !strcmp( ch, head->word))
{
flag = true
break
}
head = head ->pNext
}
if ( flag == true )
{
head ->num ++
}
else
{
pNew = new WordNode
pNew ->num = 1
strcpy( pNew ->word, ch)
pNew ->pNext = NULL
for( WordNode* loop = pHeadloop->pNextloop= loop->pNext)
{
}
loop ->pNext = pNew
}
}
}
void Print(WordNode* pFirst)
{
cout <<endl <<"统计结果:" <<endl <<endl
int maxNum = -1
int wordNum = 0
WordNode* head = pFirst
while ( head )
{
if( maxNum <head->num )
{
maxNum = head->num
}
wordNum ++
head = head->pNext
}
head = pFirst
int num = 0
cout<<"-----------------------------------------------------------------" <<endl
for( int i=0i<wordNumi++)
{
num = head->num
cout <<head->word <<" : "
for(int j=0j<maxNumj++)
{
if( head ->num >0 )
{
cout<<"*"
}
else
{
cout<<" "
}
head->num --
}
cout<<setw( maxNum + 2 ) <<num
head->num = num
head = head->pNext
cout<<endl
}
cout<<"-----------------------------------------------------------------" <<endl
head = pFirst
int layer = maxNum
while (head)
{
cout <<head->num <<setw( 2 )
head = head->pNext
}
cout<<endl
head = pFirst
for(int j=0j<maxNumj++)
{
WordNode* node = head
for( int i=0i<wordNumi++)
{
if( layer == node->num )
{
cout<<"* "
node->num --
}
else
{
cout<<" "
}
node = node->pNext
}
layer --
cout<<endl
}
head = pFirst
while( head )
{
cout<<head->word
head = head ->pNext
}
cout<<endl<<"-----------------------------------------------------------------" <<endl
}
void DeleteWordNode(WordNode* pFirst)
{
WordNode* cp, *np
cp = pFirst
while ( cp)
{
np = cp->pNext
delete cp
cp = np
}
pFirst = NULL
}
int main()
{
char *str = new char[10000]
try
{
if ( str == NULL)
{
throw(str)
}
cout <<"输入要统计的字符串: " <<endl
cin.getline( str, 10000 )
Fun(str)
Print(pHead)
}
catch( char* )
{
cout <<"动态申请空间发生错误,程序自动退出." <<endl
exit( 1 )
}
DeleteWordNode(pHead)
delete []str
getchar()
return 0
}
这些都自己写的小程序,你可以看下