#include<iostream.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
void gotoxy(int x,int y)//位置函数{
COORD pos
pos.X=2*x
pos.Y=y
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos)
}
void color(int a)//颜色函数{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a)
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j//初始化围墙
int wall[N+2][N+2]={{0}}
for(i=1i<=Ni++)
{
for(j=1j<=Nj++)
wall[i][j]=1
}
color(11)
for(i=0i<N+2i++)
{
for(j=0j<N+2j++)
{
if(wall[i][j])
cout<<"■返卜纯"
else cout<<"□"
}
cout<<endl
}
gotoxy(N+3,1)//显示信息
color(20)
cout<<"按 W S A D 移动方向"<<endl
gotoxy(N+3,2)
color(20)
cout<<"按任意键暂停"<<endl
gotoxy(N+3,3)
color(20)
cout<<"得分:"<<endl
apple[0]=rand()%N+1//苹果
apple[1]=rand()%N+1
gotoxy(apple[0],apple[1])
color(12)
cout<<"●"<<endl
}
int main()
{
int i,j
int** snake=NULL
int apple[2]
int score=0
int tail[2]
int len=3
char ch='p'
srand((unsigned)time(NULL))
init(apple)
snake=(int**)realloc(snake,sizeof(int*)*len)
for(i=0i<leni++)
snake[i]=(int*)malloc(sizeof(int)*2)
for(i=0i<leni++)
{
snake[i][0]=N/2
snake[i][1]=N/2+i
gotoxy(snake[i][0],snake[i][1])
color(14)
cout<<"★漏咐"<<endl
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0]
tail[1]=snake[len-1][1]
gotoxy(tail[0],tail[1])
color(11)
cout<<"■"<<endl
for(i=len-1i>0i--)
{
snake[i][0]=snake[i-1][0]
snake[i][1]=snake[i-1][1]
gotoxy(snake[i][0],snake[i][1])
color(14)
cout<<"★"<<endl
}
if(kbhit())
{
gotoxy(0,N+2)
ch=getche()
}
switch(ch)
{
case 'w':snake[0][1]--break
case 's':snake[0][1]++break
case 'a':snake[0][0]--break
case 'd':snake[0][0]++break
default: break
}
gotoxy(snake[0][0],snake[0][1])
color(14)
cout<<"★"弊乎<<endl
Sleep(abs(200-0.5*score))
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++
len++
snake=(int**)realloc(snake,sizeof(int*)*len)
snake[len-1]=(int*)malloc(sizeof(int)*2)
apple[0]=rand()%N+1
apple[1]=rand()%N+1
gotoxy(apple[0],apple[1])
color(12)
cout<<"●"<<endl
gotoxy(N+5,3)
color(20)
cout<<score<<endl
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2)
color(30)
cout<<"失败!!!"<<endl
for(i=0i<leni++)
free(snake[i])
Sleep(INFINITE)
exit(0)
}
}
return 0
}
/*一个火柴人游戏,亲自验证,可运行*//*在编译时添加如下命令:-std=c++11,否则会编译错误*/
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <此帆thread>
#include <conio.h>
using namespace std
const unsigned char CTRL_KEY = 0XE0
const unsigned char LEFT = 0X4B
const unsigned char RIGHT = 0X4D
const unsigned char DOWN = 0X50
const unsigned char UP = 0X48
int men2[2] = {0,0}
int women2[2]={10,10}
int Game()
void gotoxy( int x, int y ) //光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE)
COORD pos
pos.X = x
pos.Y = y
SetConsoleCursorPosition(handle,pos)
}
int clean( int mm, int nn )
{
gotoxy ( mm, nn )
printf ( " " )
gotoxy ( mm,nn+1)
printf ( "森穗雹 " )
gotoxy ( mm,nn+2)
printf (" ")
}
int men( int x, int y )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN)
gotoxy( x, y )
printf(" O")
gotoxy( x, y+1 )
printf("族亮<H>")
gotoxy( x, y+2 )
printf("I I")
}
int women( int i, int j )
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED)
gotoxy( i+1,j )
printf(" O")
gotoxy( i+1,j+1 )
printf("<H>")
gotoxy( i,j+2 )
printf("/I I\\")
}
int m=10, n=10
int x=0int y=0
int TorF()
{
if ( x == m &&y == n ) return 1
else return 0
}
int womenmove()
{
int turn
int YNbreak=0
while( YNbreak == 0 )
{
YNbreaak = TorF()
turn=rand()%3
clean( m, n )
if( m <x ) m++
else m--
if( m == x )
{
if( n <y ) n++
else n--
}
if ( m <0 ) m = 0
if ( m >= 75 ) m = 75
if ( n <0 ) n = 0
if ( n >= 22 ) n = 22
women( m,n )
women2[0]=m
women2[1]=n
Sleep(100)
}
system ( "cls" )
gotoxy ( 28, 10 )
printf ( "You died!!!\n" )
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE)
system ( "pause" )
exit(0)
return 0
}
int menmove()
{
system( "cls" )
while (1)
{
switch( getch())
{
case UP:y--break
case DOWN:y++break
case LEFT:x--break
case RIGHT:x++break
}
system( "cls" )
if ( x <0 ) x = 0
if ( x >77 ) x = 77
if ( y <0 ) y = 0
if ( y >22 ) y = 22
men( x, y )
men2[0] = x
men2[1] = y
}
}
int Game()
{
women( 10, 10 )
men( 0, 0 )
int t = 0
thread qq( womenmove )
menmove()
qq.join()
return 0
}
int main()
{
system( "mode con cols=80 lines=25" )
printf ( "游戏开始后,随机按下一个键,唤醒你的蓝色小人.如果你被红色的老女人碰到了,那么你就死了\n" )
printf ( "方向键 *** 控小人\n" )
system ( "pause" )
system ( "cls" )
Game()
return 0
}
/*留下您的赞再拿走,谢谢!*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)