本文目錄一覽:
- 1、用C語言,同時接受多個按鍵,要怎麼寫代碼,例如我做坦克大戰,雙人遊戲時要控制坦克和發射子彈。
- 2、猜數字遊戲中的雙人遊戲模塊的c語言代碼。
- 3、用C語言編寫的小遊戲代碼是什麼?
- 4、c語言小遊戲代碼
- 5、求幾C語言個小遊戲代碼,簡單的,要注釋、、謝謝了、
用C語言,同時接受多個按鍵,要怎麼寫代碼,例如我做坦克大戰,雙人遊戲時要控制坦克和發射子彈。
windows下是用消息隊列來捕捉按鍵事件的,建議看下MFC,至於怎麼控制坦克,用BFS和堆棧
猜數字遊戲中的雙人遊戲模塊的c語言代碼。
首先我們先點開C-Free5.0然後新建一個你猜.c的空白頁面,輸入基本語句。然後保存。
然後我們要解決的問題是如何生成一個隨即數值。
#includestdio.h
#includestdlib.h //為了獲得隨機數
#includetime.h //獲取時間
main()
{
int a,b;
srand(time(NULL));
a=1+(rand()%100);//獲得一個100以內的隨機數
printf(“%d”,a);
}
rand()是「偽隨機數」產生函數,注意,是「偽隨機數」而不是「隨機數」,如果每次srand()給的參數值相同,那麼rand()產生的序列就是相同的。time(NULL)返回的是從某年的1月1日0時0分0秒開始到系統當前時間所經過的秒數,所以如果不是同一秒鐘以內多次運行程序的話,time(NULL)的返回值一定是不同的,用這樣的方式來產生相對比較隨機的序列。
第三步自然是,開始猜的過程,我們這裡用的是while 循環 然後用if語句判斷是否正確,如果正確就用break;跳出循環。當然為了嚴重能不能退出,我們還要作弊下,先看到a的值,我們需要輸出下a的值在while循環之前。
while(a)
{
printf(“請輸入你心中想的那個數: “);
scanf(“%d”,b);
if(a==b)
break; }
printf(“恭喜你猜對了!\n”);
好,上面的程序基本功能能用,但是程序整體不是很美觀,缺少提示語句,所以我們加上提示語句。
if(ba)
printf(“你輸入的數值太大。\n”);
if(ba)
printf(“你輸入的數值太小。\n”);
這部分代碼用if語句來實現。
好了我們現在將完整的代碼寫一邊然後進行調試下代碼是否如我們所想的那樣。
#includestdio.h
#includestdlib.h //為了獲得隨機數
#includetime.h //獲取時間
main()
{
int a,b;
srand(time(NULL));
a=1+(rand()%100);//獲得一個100以內的隨機數
while(a)
{
printf(“請輸入你心中想的那個數: “);
scanf(“%d”,b);
if(a==b)
{
printf(“恭喜你猜對了!\n”);
break; }
if(ba)
printf(“你輸入的數值太大。\n”);
if(ba)
printf(“你輸入的數值太小。\n”);
}
getch();
}
6
好的如上圖我們可以看到我們的程序很完美,然後編譯後會出現一個exe的程序,這個程序就是我們的小猜數字遊戲,但是我們發現每次等我們猜對後後,就會閃退,我們在程序裡面寫一個 getch();放在最後沒就好了,再重新編譯下。
用C語言編寫的小遊戲代碼是什麼?
「猜數字小遊戲」,每個數字後按空格,最後按回車確認
#includestdio.h
#includestdlib.h
#includetime.h
int a[4],b[4];
int count=0; //計算猜測次數
void csh( ); //初始化
void start( ); //開始遊戲
int main( )
{ csh( );
start( );
}
void csh( ) //初始化
{ printf(“\n\n 猜 數 字 小 游 戲\n\n”);
printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B.\n」);
}
void start( ) //開始遊戲
{int m,n; //m是完全猜對的個數,n是順序不對的個數
while(1)
{srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
while(1) { for(int i=0;i4;i++) a[i]=rand( )%10; //rand( )函數每次隨機產生一個0-9的數
if( (a[3]!=a[2]a[3]!=a[1]a[3]!=a[0])
(a[2]!=a[1]a[2]!=a[0])a[1]!=a[0] ) break; } //4個隨機數各自不相等
printf(” 請依次輸入4個一位整數:\n\n “);
while(1)
{for(int i=0;i4;i++) scanf(「%d」,b[i]);
printf(” 你輸入的是:%d %d %d %d “,b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i4;i++)
{for(int j=0;j4;j++)
{ if(b[i]==a[j]i==j)m=m+1; if(b[i]==a[j]i!=j)n=n+1; }
}
count=count+1;
printf(” %dA %dB 你試了%d次\n “,m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
}
printf(“\n”);
if(m==4)printf(” 你猜對了(^-^)! 就是:%d %d %d %d\n”,a[0],a[1],a[2],a[3]);
else printf(” 你輸了(T-T)!哈哈!應該是:%d %d %d %d\n”,a[0],a[1],a[2],a[3]);
int z;
printf(” (要繼續嗎?1或0)\n “);
scanf(「%d」,z);
if(z==0) break;
}
}
c語言小遊戲代碼
最基礎的貪吃蛇的代碼
#includestdio.h
#includewindows.h//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。
#includeconio.h //用戶通過按鍵盤產生的對應操作 (控制台)
#includestdlib.h
#includetime.h //日期和時間頭文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //數組的元素代表蛇的各個部位
char Sna_Hea_Dir = ‘a’;//記錄蛇頭的移動方向
int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置
int Snake_Len = 3;//記錄蛇的長度
clock_t Now_Time;//記錄當前時間,以便自動移動
int Wait_Time ;//記錄自動移動的時間間隔
int Eat_Apple = 1;//吃到蘋果表示為1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標準輸出的句柄 windows.h
//句柄 :標誌應用程序中的不同對象和同類對象中的不同的實例 方便操控,
void gotoxy(int x, int y)//設置游標位置
{
COORD pos = {x,y}; //定義一個字元在控制台屏幕上的坐標POS
SetConsoleCursorPosition(hConsole, pos); //定位游標位置的函數windows.h
}
void Hide_Cursor()//隱藏游標 固定函數
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, cursor_info);
}
void SetColor(int color)//設置顏色
{
SetConsoleTextAttribute(hConsole, color);
//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);
}
void Print_Snake()//列印蛇頭和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇頭
{
SetColor(0xf); //oxf代表分配的內存地址 setcolor:34行自定義設置顏色的函數
gotoxy(ix*2, iy);
printf(“※”);
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函數是產生隨機數的一個隨機函數。C語言里還有 srand()函數等。
//頭文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //變色
SetColor(color);
gotoxy(ix*2, iy);
printf(“■”);
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf(“≈”);
}
}
}
void Clear_Snake()//擦除貪吃蛇
{
int iy, ix;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(” “);
}
}
void Rand_Apple()//隨機產生蘋果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf(“⊙”);
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf(“Game Over”);
Sleep(3000);
system(“pause nul”);
exit(0);
}
void Move_Snake()//讓蛇動起來
{
int ix, iy;
for(ix = 0; ix LEN; ++ix)//先標記蛇頭
for(iy = 0; iy WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根據新的蛇頭方向標誌蛇頭
{
case ‘w’:
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy – 1;
Sna_Hea_X = ix;
break;
case ‘s’:
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case ‘a’:
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix – 1;
Sna_Hea_Y = iy;
break;
case ‘d’:
if(ix == (LEN – 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] 0)//吃到蘋果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix LEN; ++ix)//處理蛇尾
for(iy = 0; iy WID; ++iy)
{
if(Snake[ix][iy] 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//處理蛇頭
}
void Get_Input()//控制蛇的移動方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = ‘w’;
break;
case 83:
Sna_Hea_Dir = ‘s’;
break;
case 65:
Sna_Hea_Dir = ‘a’;
break;
case 68:
Sna_Hea_Dir = ‘d’;
break;
default:
break;
}
}
if(clock() – Now_Time = Wait_Time)//蛇到時間自動行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system(“title 貪吃毛毛蛇”);
system(“mode con: cols=80 lines=25”);
Hide_Cursor();
gotoxy(61, 4);
printf(“You Score:”);
gotoxy(61, 6);
printf(“You Level:”);
gotoxy(61, 8);
printf(“The Lenght:”);
gotoxy(61, 10);
printf(“The Speed:”);
gotoxy(61, 12);
printf(“Apple Num:”);
int i;
for(i = 0; i Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//列印蛇
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf(“■”);
}
}
}
void Pri_News()//列印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf(“%3d”, All_Score);
gotoxy(73, 6);
printf(“%3d”, Level);
gotoxy(73, 8);
printf(“%3d”,Snake_Len);
gotoxy(73, 10);
printf(“0.%3ds”, Wait_Time/10);
gotoxy(73, 12);
printf(“%d”, Apple_Num);
}
void Lev_Sys()//等級系統
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time 50)
Wait_Time -= 50;
else
if(Wait_Time 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//設置隨機數的種子
Now_Time = clock();
int speed1=1000,speed2,a;
printf(“\n”);
printf(“請輸入你想要的速度\n”);
scanf(“%d”,speed2);
Level=1;
Wait_Time=speed1-speed2;
printf(“請輸入你想要的蘋果數\n”);
scanf(“%d”,a);
while(a–)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
求幾C語言個小遊戲代碼,簡單的,要注釋、、謝謝了、
// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24點遊戲
*/
#include “conio.h”
#include “stdlib.h”
#include “time.h”
#include “math.h”
#include “string.h”/*
從一副撲克牌中,任取4張。
2-10 按其點數計算(為了表示方便10用T表示),J,Q,K,A 統一按 1 計算
要求通過加減乘除四則運算得到數字 24。
本程序可以隨機抽取紙牌,並用試探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {‘A’,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’T’,’J’,’Q’,’K’}; for(int i=0; i4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c==’T’) return 10;
if(c=’0′ c=’9′) return c – ‘0’;
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return ‘+’;
case 1:
return ‘-‘;
case 2:
return ‘*’;
case 3:
return ‘/’;
} return ‘ ‘;
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 – op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i4; i++)
{
if( question[i] == ‘T’ )
strcpy(p[i], “10”);
else
sprintf(p[i], “%c”, question[i]);
}
switch(type)
{
case 0:
sprintf(answer, “%s %c (%s %c (%s %c %s))”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, “%s %c ((%s %c %s) %c %s)”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, “(%s %c %s) %c (%s %c %s)”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, “((%s %c %s) %c %s) %c %s”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, “(%s %c (%s %c %s)) %c %s”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//計算類型
double p[4];
double sum=0;
//
for(int i=0; i4; i++) //循環取得點數
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i5;i++)
{
MakeAnswer(answer,type[i],question,oper); //獲取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
採用隨機試探法:就是通過隨機數字產生 加減乘除的 組合,通過大量的測試來命中的解法
提示:
1. 需要考慮用括弧控制計算次序的問題 比如:( 10 – 4 ) * ( 3 + A ), 實際上計算次序的數目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考慮計算結果為分數的情況:( 3 + (3 / 7) ) * 7
3. 題目中牌的位置可以任意交換
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存儲運算符,0:加法 1:減法 2:乘法 3:除法
for(int i=0; i1000 * 1000; i++)
{
// 打亂紙牌順序
shuffle(question);
// 隨機產生運算符
for(int j=0; j3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化隨機種子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 題目
char buf2[30]; // 解答
printf(“***************************\n”);
printf(“計算24\n”);
printf(“A J Q K 均按1計算,其它按牌點計算\n”);
printf(“目標是:通過四則運算組合出結果:24\n”);
printf(“***************************\n\n”);
for(;;)
{
GivePuzzle(buf1); // 出題
printf(“題目:”);
for(int j=0; j4; j++){
if( buf1[j] == ‘T’ )
printf(“10 “);
else
printf(“%c “, buf1[j]);
} printf(“\n按任意鍵參考答案…\n”);
getch(); if( TryResolve(buf1, buf2) ) // 解題
printf(“參考:%s\n”, buf2);
else
printf(“可能是無解…\n”); printf(“按任意鍵出下一題目,x 鍵退出…\n”);
if( getch() == ‘x’ ) break;
} return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/300845.html