本文目录一览
- 1、求助用C或者C++语言实现一个迷宫小游戏代码
- 2、找个大神帮我用C++写一个猜数游戏的代码
- 3、求经典小游戏(五子棋 贪吃蛇 俄罗斯方块等)c++ 源代码。最好能有软件设计过程 我想学思路
- 4、用C++做23根火柴游戏
- 5、求一个能在vs上运行的C++编写的贪食蛇游戏
求助用C或者C++语言实现一个迷宫小游戏代码
#includebits/stdc++.h
#includewindows.h
#includeconio.h
using namespace std;
int main()
{
int i123;
cout"? ?."endl;
cout"? . ."endl;
cout" . . ."endl;
cout"? . ."endl;
cout"? ?."endl;
cout"? T M";
for(i123=500;i123=1000;i123+=100){
Beep(i123,1000);
}
system("cls");
char a[50][50]={"##############################",
? ? ? ? "#o? ? ? ? ?#? ?##? ?# ### ####",
? ? ? ? "# ###### # # #? ? # # ### ####",
? ? ? ? "# #? ?## #? ?# #### # ###? ?##",
? ? ? ? "#? ?# ##? ###? ? #? #? ## ####",
? ? ? ? "##### #? ? ?# ##### ##? ? ####",
? ? ? ? "#? ?# ##### #? ?#? ?# # #? ? #",
? ? ? ? "# # #? ? ## # #### ## # # ####",
? ? ? ? "# # # ##? ? ? ##? ? ? # # ####",
? ? ? ? "# # # ####### ## ###### #? ?##",
? ? ? ? "# #? ?##? ?# ## ###### ###? ?#",
? ? ? ? "# ###### # #####? #? ? ? ? # #",
? ? ? ? "#? ? ? ? # #? ? ?##### ### #? ",
? ? ? ? "# ######## ##### # ### ### # #",
? ? ? ? "#? ? ?# ## ##### ###? ? ? ?###",
? ? ? ? "##### # ## #? ? ? ######## # #",
? ? ? ? "#? ? ?# ## ## ###? ? ? ? #? ?#",
? ? ? ? "# # ###? ? ? ?###### ####### #",
? ? ? ? "# #? ? ### ##? ? ? #? ? ? ? ?#",
? ? ? ? "##############################"
? ? ? ? };
int i,i1,x=1,y=1,n=12,m=29;
char ch;
for(i=0;i=19;i++){
puts(a[i]);
}
while(x!=n||y!=m){
? ch=getch();
if(ch==115||ch==25){
if(a[x+1][y]!=35){
a[x][y]=32;
x++;
a[x][y]=111;
}
}
if(ch==119||ch==24){
if(a[x-1][y]!=35){
a[x][y]=32;
x--;
a[x][y]=111;
}
}
if(ch==97||ch==27){
if(a[x][y-1]!=35){
a[x][y]=32;
y--;
a[x][y]=111;
}
}
if(ch==100||ch==26){
if(a[x][y+1]!=35){
a[x][y]=32;
y++;
a[x][y]=111;
}
}
system("cls");
for(i=0;i=19;i++){
puts(a[i]);
}
}
for(i1=500;i1=1000;i1=i1+100){
Beep(i1,1000);
}
coutendl"你赢了!!!祝贺你!!!";
system("pause");
}
找个大神帮我用C++写一个猜数游戏的代码
刚写的,新鲜出炉,基本满足需要,要求的头文件之类的,把相关的函数放进去就行,产生随机数的函数封装一下也可以,很简单,自己再改改吧,
有问题追问,或百度HI联系
?全部代码:
#include?iostream
#include?stdio.h
#include?stdlib.h
#include?string
#include?time.h
using?namespace?std;
?
//猜测最大次数
const?int?MAX_GUESS_TIME=?8;
void?ShowMenu();//显示菜单
void?ShowGameInfo();//显示游戏简介
string?GetNum();//产生一个不包含重复数字的四位数
string?CalcResult(string?strResult,?string?strInput);//比较两个数,输出?A?B结果
bool?CheckInput(string?strInput);//检查输入有效性,简单处理,可增加其它判断
?
int?main()
{
?string?strResult;
?string?strInput;
?int?iGuessTime=1;
?cout"==***欢迎使用猜数字游戏***=="endl;
?ShowMenu();
?cinstrInput;
?while?(1)
?{?
? if?(strInput?==?"1")
? {
? ?break;
? }
? else?if?(strInput?==?"2")
? {
? ?ShowGameInfo();
? }
? else?if?(strInput?==?"0")
? {
? ?cout"谢谢使用,再见!\n";
? ?return?0;
? }
? else
? {
? ?cout"对不起,请输入正确的数字!\n";
?
? }
? ShowMenu();
? cinstrInput;
?}
?
?
?srand(time(NULL));
?strResult?=?GetNum();
?cout"请输入四位数(输入exit退出):"endl;
?cinstrInput;
?while?(strInput?!=?"exit")
?{
? if?(!CheckInput(strInput))
? {
? ?cout"您的输入不正确,请输入4位数字,不能有重复\n";
? ?cinstrInput;
? ?continue;
? }
?
? if?(strResult?==?strInput)
? {
? ?cout"哈!猜对了,你好棒!数字是:"strResultendl;
? ?cout"继续?(y/n)";
? ?cinstrInput;
? ?if?(!(strInput[0]=='y'?||?strInput[0]=='Y'))
? ?{
? ? break;
? ?}
? ?strResult?=?GetNum();
? ?iGuessTime=1;
? }
? else
? {
? ?if?(MAX_GUESS_TIME?? iGuessTime)
? ?{
? ? cout"第?"iGuessTime"次猜测,输入"strInput",结果:"CalcResult(strResult,?strInput)endl;
? ? ++iGuessTime;? ?
? ?}
? ?else
? ?{
? ? cout"对不起,您猜得不对,数字是:"strResultendl;
? ? cout"继续?(y/n)";
? ? cinstrInput;
? ? if?(!(strInput[0]=='y'?||?strInput[0]=='Y'))
? ? {
? ? ?break;
? ? }
?
? ? strResult?=?GetNum();
? ? iGuessTime=1;
? ?}
? }
?
?
? cout"请输入四位数(输入exit退出):"endl;
? cinstrInput;?
?}
?
?cout"谢谢使用,再见.\n";
?return?0;
}
string?GetNum()
{
?char?cTmp[2]?=?{0};
?string?strTmp;
?int?iTmp=0;?
?
?while?(strTmp.length()?!=?4)
?{
? iTmp?=?rand()?%?10;
? itoa(iTmp,?cTmp,?10);
? int?iIndex?=?strTmp.find(cTmp);
? if?(?iIndex??0)
? {
? ?strTmp?+=?cTmp;
? }
?}
?
?return?strTmp;
}
?
string?CalcResult(string?strResult,?string?strInput)
{
?int?iA=0,iB=0;
?int?i=0,j=0;
?char?cResult[5]={0};
?for?(;i??strResult.length();++i)
?{
? for?(j=0;j??strInput.length();++j)
? {
? ?char?strA=strResult[i];
? ?char?strB=strInput[j];
? ?if?(strA?==?strB)
? ?{
? ? if?(i?==?j)
? ? {
? ? ?++iA;
? ? }
? ? else
? ? {
? ? ?++iB;
? ? }
? ?}
?
? }?
}
?sprintf(cResult,"%dA%dB",?iA,?iB);
?return?cResult;
}
bool?CheckInput(string?strInput)
{
?int?i=0;
?if?(strInput.length()?!=?4)
?{
? return?false;
?}
?
?for?(;i??4;?i++)
?{
? char?cTmp?=?strInput[i];
? if?(?cTmp??'9'?||?cTmp??'0')
? {
? ?return?false;
? }
?}
?
?return?true;
}
void?ShowMenu()
{
?cout"请输入对应数字进行选择"endl;
?cout"1--玩游戏"endl;
?cout"2--游戏介绍"endlendl;
?cout"0--退出游戏"endl;
}
?
void?ShowGameInfo()
{
?cout"这里是游戏介绍,输入你自己的游戏介绍\n";
?system("pause");
}
运行截图:
求经典小游戏(五子棋 贪吃蛇 俄罗斯方块等)c++ 源代码。最好能有软件设计过程 我想学思路
这有一个最简单的贪吃蛇的控制过程。一般对于此类的游戏,都分为控制算法,显示算法,判定算法等几个大部分。
供参考:
#include stdio.h
#include windows.h
#include stdlib.h
#include string.h
#include conio.h
#include time.h //使用当前时间做种子;
enum dir{up,down,left,right}; //枚举类型enum dir;
//围墙;
void InitFence();
void OutputF();
char game[20][20];
//画框框;
void InitFence(){
int i,j;
for(i=0; i20; i++)
for(j=0; j20; j++){
if(i==0||i==19||j==0||j==19)
game[i][j]= '*';
else game[i][j]= ' ';
}
}
//显示框框;
void OutputF(){
int i,j;
for(i=0; i20; i++){
for(j=0; j20; j++)
printf("%c ",game[i][j]);
printf("\n");
}
}
//蛇结点;
struct SnakeNode{
int x,y;
struct SnakeNode *prior,*next;
}*head=NULL, *tail =NULL;
void add_head(int x,int y);
int get_x(struct SnakeNode *p);
int get_y(struct SnakeNode *p);
void delete_tail();
//插入头结点;
void add_head(int x,int y){
struct SnakeNode *q= (struct SnakeNode *)malloc(sizeof(struct SnakeNode));
q-x =x; q-y =y;
q-next =head;
q-prior =NULL;
if(head) head-prior =q;
head =q;
if(!tail) tail =head;
game[x][y]= '*'; //f对象可以在定义Fence类时定义; 且Fence类在SnakeNode类前定义;
}
int get_x(struct SnakeNode *p){
return p-x;
}
int get_y(struct SnakeNode *p){
return p-y;
}
//删除尾结点;
void delete_tail(){
struct SnakeNode *p =tail;
game[get_x(tail)][get_y(tail)]= ' ';//把尾结点的坐标表示的'*'置为空格;
if(tail==head)
tail= head= NULL;
else{
tail= tail-prior;
tail-next= NULL;
}
free(p);
}
//move移动;
struct move{
enum dir point; //枚举变量point: 控制方向;
int food_x;
int food_y;
};
void moving(struct move *m);
void change_point(char,struct move *m); //改变方向;
void get_food(struct move *m);
void get_food(struct move *m){
srand((unsigned int) time(NULL)); //做种子(程序运行时间);
m-food_x= rand()%18+1;
m-food_y= rand()%18+1;
game[m-food_x][m-food_y]= '*';
}
void moving(struct move *m){
int a,b;
a= get_x(head); //取得头结点横坐标
b= get_y(head); //头结点纵坐标
switch(m-point){
case up: --a; break;
case down: ++a; break;
case left: --b; break;
case right: ++b; break;
}
if(a==19||b==19||a==0||b==0){ //判断是否撞墙;
printf("game over!!!\n");
exit(0);
}
if(a==m-food_x b==m-food_y){ //吃food;
add_head(a,b);
get_food(m);
}
else{
add_head(a,b); //插入头结点;
delete_tail(); //删除尾结点;
}
}
void change_point(char keydown,struct move *m){
switch(keydown){
// case 'w': m-point= up; break;
// case 's': m-point= down; break;
// case 'a': m-point= left; break;
// case 'd': m-point= right; break;
case 72: m-point= up; break;
case 80: m-point= down; break;
case 75: m-point= left; break;
case 77: m-point= right; break;
}
}
//main();
int main(){
struct move m;
printf("Using 'w,s,a,d'to control direction!!!\n\n\n");
InitFence();
add_head(4,3);
add_head(4,4);
add_head(4,5);
get_food(m);
OutputF();
while (1){
char keydown= getch(); //getch()返回键盘上读取的字符;包含头文件conio.h
change_point(keydown,m);
while(!kbhit()){ //判断有没有按键落下;
system("cls"); //清屏函数;
moving(m);
OutputF();
Sleep(200);
}
}
return 0;
}
用C++做23根火柴游戏
终于改完啦!嗯,完整版,包括函数,注释!#includeiostream
#includestring
#define NUM 20
using namespace std;
void main()
{
//规则的输出
coutendl;
cout"***************************************************"endl;
cout"游戏规则:共有23根火柴,2个人依次拿取,每个人每次只"endl;
cout"能拿1跟或者2跟或者3跟火柴。拿到最后一跟火柴的人算输"endl;
cout"****************************************************"endl;
coutendl;
//变量的定义
int match_num=23; //火柴数目
int *p_match_num; //指向火柴数目的指针
int player = 0; //玩家变量,偶数代表玩家1,奇数代表玩家2
char player_first[NUM]; //玩家1的名字
char player_second[NUM]; //玩家2的名字
char judge; //判断变量,值为'y'时,表示玩家1先开局
int put_num=0; //拿走火柴的数目
char temp[NUM];
char *p_player_now;
int *p_put_num; //指向拿走火柴数目的指针
int *p_player;
//指针赋值
p_match_num=match_num;
p_put_num=put_num;
p_player=player;
p_player_now=player_first;
//函数的定义
void match(int* p_player, int* p_match_num,int* p_put_num,char* p_player_now); //火柴拿走数目函数
//游戏开始
cout"请输入玩家1的名字:";
gets(player_first);
cout"请输入玩家2的名字:";
gets(player_second);
//玩家开局顺序的选择
cout"玩家“"player_first"”选择拿火柴顺序(“F(First)/S(Second)”)";
cinjudge;
if(judge=='s')
{
strcpy(temp,player_first);
strcpy(player_first,player_second);
strcpy(player_second,temp);
}
cout"玩家“"player_first"”先开始"endl;
coutendl;
//火柴的拿取
while(1)
{
if(player%2==0)
p_player_now=player_first;
else
p_player_now=player_second;
match(p_player,p_match_num,p_put_num,p_player_now);
match_num=match_num-put_num;
if(match_num==0)
{
cout"玩家“"p_player_now"”您输了!"endl;
break;
}
}
}
void match(int* p_player,int* p_match_num,int* p_put_num,char* p_player_now)
{
cout"请“"p_player_now"”输入准备拿走火柴的数目:";
cin*p_put_num;
while(*p_put_num!=1 *p_put_num!=2 *p_put_num!=3 || *p_match_num-*p_put_num0)
{
if(*p_put_num!=1 *p_put_num!=2 *p_put_num!=3)
{
cout"您输入的数值不合法,拿走的火柴数目只能是 1 或 2 或 3,请重新输入:";
cin*p_put_num;
}
if(*p_match_num-*p_put_num0)
{
cout"您输入的数值大于剩余火柴数目,请重新输入:";
cin*p_put_num;
}
}
cout"您拿走的火柴数目是: "*p_put_numendl;
cout"剩余火柴数目是: "*p_match_num-*p_put_numendl;
coutendl;
(*p_player)++;
}
求一个能在vs上运行的C++编写的贪食蛇游戏
代码如下:#includeiostream
#includewindows.h
#includetime.h
#includeconio.h
using namespace std;
// 刷新当前屏幕
inline void Refresh(char q[][22], int grade, int gamespeed){
system("cls"); // 清屏
int i,j;
cout endl;
for(i=0;i22;i++){
cout "\t";
for(j=0;j22;j++)
coutq[i][j]' '; // 输出贪吃蛇棋盘
if(i==0) cout "\t等级为:" grade;
if(i==4) cout "\t自动前进时间";
if(i==6) cout "\t间隔为:" gamespeed "ms";
coutendl;
}
}
int main(){
char tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
int i,j;
for(i=1;i=20;i++)
for(j=1;j=20;j++)
tcsQipan[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
for(i=0;i=21;i++)
tcsQipan[0][i] = tcsQipan[21][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for(i=1;i=20;i++)
tcsQipan[i][0] = tcsQipan[i][21] = '|'; //初始化贪吃蛇棋盘左右墙壁
int tcsZuobiao[2][100]; //蛇的坐标数组
for(i=0; i4; i++){
tcsZuobiao[0][i] = 1;
tcsZuobiao[1][i] = i + 1;
}
int head = 3,tail = 0;
for(i=1;i=3;i++)
tcsQipan[1][i]='*'; //蛇身
tcsQipan[1][4]='#'; //蛇头
int x1, y1; // 随机出米
srand(time(0));
do{
x1=rand()%20+1;
y1=rand()%20+1;
}while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='*';
cout"\n\n\t\t贪吃蛇游戏即将开始 !"endl;//准备开始
long start;
int grade = 1, length = 4;
int gamespeed = 500; //前进时间间隔
for(i=3;i=0;i--){
start=clock();
while(clock()-start=1000);
system("cls");
if(i0)
cout "\n\n\t\t进入倒计时:" i endl;
else
Refresh(tcsQipan,grade,gamespeed);
}
int timeover;
char direction = 77; // 初始情况下,向右运动
int x,y;
while(1){
timeover = 1;
start = clock();
while((timeover=(clock()-start=gamespeed))!kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if(timeover){
getch();direction = getch();
}
switch(direction){
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上
case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)){ // 按键非方向键
cout "\tGame over!" endl; system("pause");return 0;
}
if(x==0 || x==21 ||y==0 || y==21){ // 碰到墙壁
cout "\tGame over!" endl; system("pause");return 0;
}
if(tcsQipan[x][y]!=' '!(x==x1y==y1)){ // 蛇头碰到蛇身
cout "\tGame over!" endl; system("pause");return 0;
}
if(x==x1 y==y1){ // 吃米,长度加1
length ++;
if(length=8){
length -= 8;
grade ++;
if(gamespeed=200)
gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
}
tcsQipan[x][y]= '#';
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';
head = (head+1)%100;
tcsZuobiao[0][head] = x;
tcsZuobiao[1][head] = y;
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}while(tcsQipan[x1][y1]!=' ');
tcsQipan[x1][y1]='*';
Refresh(tcsQipan,grade,gamespeed);
}
else{ // 不吃米
tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';
tail=(tail+1)%100;
tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';
head=(head+1)%100;
tcsZuobiao[0][head]=x;
tcsZuobiao[1][head]=y;
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';
Refresh(tcsQipan,grade,gamespeed);
}
}
system("pause");
return 0;
}
- 随机文章
标签 求经典小游戏五子棋 贪吃蛇 俄罗斯方块等c 源代码最好能有软件设计过程 我想学思路
c小游戏代码复制