各位老鐵們好,相信很多人對c語言游戲最簡單代碼都不是特別的了解,因此呢,今天就來為大家分享下關于c語言游戲最簡單代碼以及c++語言基礎知識入門的問題知識,還望可以幫助大家,解決大家的一些困惑,下面一起來看看吧!
貪吃蛇c語言代碼最短
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
usingnamespacestd;
voidgotoxy(intx,inty){COORDpos={x,y};SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//光標定位
classFood{//食物類
private:intm_x;intm_y;
public:
voidrandfood(){//隨機產生一個食物
srand((int)time(NULL));//利用時間添加隨機數種子,需要ctime頭文件
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2)gotoL1;//如果食物的x坐標不是偶數則重新確定食物的坐標
gotoxy(m_x,m_y);//在確認好的位置輸出食物
cout<<"★";}}
intgetFoodm_x(){returnm_x;}//返回食物的x坐標
intgetFoodm_y(){returnm_y;}};//返回食物的y坐標
classSnake{
private:
structSnakecoor{intx;inty;};//定義一個蛇的坐標機構
vector<Snakecoor>snakecoor;//將坐標存入vector容器中
//判斷并改變前進方向的函數
voiddegdir(Snakecoor&nexthead){//定義新的蛇頭變量
staticcharkey='d';//靜態變量防止改變移動方向后重新改回來
if(_kbhit()){
chartemp=_getch();//定義一個臨時變量儲存鍵盤輸入的值
switch(temp){//如果臨時變量的值為wasd中的一個,則賦值給key
default:break;//default是缺省情況,只有任何條件都不匹配的情況下才會執行必須寫在前面!不然蛇無法轉向
case'w':case'a':case's':case'd':
//如果temp的方向和key的方向不相反則賦值因為兩次移動方向不能相反將蛇設置為初始向右走
if(key=='w'&&temp!='s'||key=='s'&&temp!='w'||key=='a'&&temp!='d'||key=='d'&&temp!='a')key=temp;}}
switch(key){//根據key的值來確定蛇的移動方向
case'd':nexthead.x=snakecoor.front().x+2;nexthead.y=snakecoor.front().y;break;
//新的蛇頭的頭部等于容器內第一個數據(舊蛇頭)x坐標+2因為蛇頭占兩個坐標,移動一次加2
case'a':nexthead.x=snakecoor.front().x-2;nexthead.y=snakecoor.front().y;break;
case'w':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y-1;break;
//因為控制臺的x長度是y的一半,所以用兩個x做蛇頭,需要的坐標是二倍
case's':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y+1;}}
//游戲結束時設計一個界面輸出“游戲結束”以及分數
voidfinmatt(constintscore){
system("cls");gotoxy(40,14);//清屏然后輸出
cout<<"游戲結束";gotoxy(40,16);
cout<<"得分:"<<score;gotoxy(0,26);
exit(0);}//exit為C++的退出函數exit(0)表示程序正常退出,非0表示非正常退出
voidfinishgame(constintscore){//游戲結束
if(snakecoor[0].x>=88||snakecoor[0].x<0||snakecoor[0].y>=28||snakecoor[0].y<0)finmatt(score);//撞墻
for(inti=1;i<snakecoor.size();i++)if(snakecoor[0].x==snakecoor[i].x&&snakecoor[0].y==snakecoor[i].y)finmatt(score
c語言跪求,最簡單的,求和程序,代碼
c語言最簡單的求和程序代碼如下。
#include<stdio.h>
voidmain(){
inta,b;
printf("pleaseinputtownumber:");
scanf("%d%d",&a,&b);
printf("a+b=%d",a+b);
}
我調試過了是正確無誤的!
c語言制作游戲需要什么
1.基本上,大型游戲引擎都不是C語言開發的。如果小游戲的功能代碼的話,c沒問題,如果要好的界面,C要用gtk+,這個比較難;熟悉c++的話,用wxWidget和Qt寫界面會好用點。大型游戲的話,還是要用專門的游戲引擎設計,大型游戲不是一個人能完成的。
2.游戲開發主要涉及到ui和場景、對象管理的需求,c++相比c是更普遍的選擇;使用c++的游戲框架、引擎也更多,移動端常見的cocos2d-x,面向3a開發的udk等都是最典型的例子。
c語言貪吃蛇代碼及解析
以下是一個使用C語言編寫的簡單貪吃蛇游戲,包括初始化游戲界面、繪制蛇和食物、移動蛇和檢測碰撞等功能。
```c
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//定義常量
constintwidth=20;
constintheight=20;
constintmax_length=5;
constintblock_size=20;
constchardirection[]="RDLU";
constintfood_x=10;
constintfood_y=10;
constintsnake_speed=100;
//定義結構體,存儲蛇的身體坐標和方向
structSnake{
intx,y;
intlength;
chardirection;
};
//定義結構體,存儲食物的位置和狀態(是否被吃掉)
structFood{
intx,y;
};
//定義全局變量,存儲蛇和食物的信息
structSnakesnake;
structFoodfood;
intscore;
//初始化游戲界面和蛇的狀態(位置和長度)
voidinit(){
//初始化窗口大小和標題欄
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),width*block_size,height*block_size);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&buffer_info);
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE),TRUE,NULL,NULL,buffer_info.dwMaximumWindowSize);
printf("SnakeGame!
");
fflush(stdout);
//初始化蛇的位置和長度為3個方塊,方向為左移符('L')
snake.x=height/2;
snake.y=height/2;
snake.length=3;
snake.direction='L';
//隨機生成一個食物的位置和狀態(是否被吃掉)
srand((unsigned)time(NULL));
food.x=(rand()%(width*block_size))+food_x;
food.y=(rand()%(height*block_size))+food_y;
}
//在屏幕上繪制蛇和食物的圖像
voiddraw(){
RECTrect;
inti;
//根據蛇的位置和方向計算出每個方塊的坐標和顏色值(RGB)
i=snake.length;
intcolorR=(snake.direction&'R')=='R'?155:155-(snake.length-i)*20;
intcolorG=(snake.direction&'G')=='G'?180:180-(snake.length-i)*20;
intcolorB=(snake.direction&'B')=='B'?25:25-(snake.length-i)*20;
intcolorD=(snake.direction&'D')=='D'?0:0-(snake.length-i)*20;
intcolorE=(snake.direction&'E')=='E'?7:7-(snake.length-i)*20;
intcolorF=(snake.direction&'F')=='F'?145:145-(snake.length-i)*20;
intcolorY=(snake.direction&'Y')=='Y'?11:11-(snake.length-i)*20;
intcolorX=(snake.direction&'X')=='X'?191:191-(snake.length-i)*20;
intcolorN=(snake.direction&'N')=='N'?165:165-(snake.length-i)*20;
intcolorM=(snake.direction&'M')=='M'?135:135-(snake.length-i)*20;
用C語言編寫的小游戲代碼是什么
最基礎的貪吃蛇的代碼
#include<stdio.h>
#include<windows.h>//基本型態定義。支援型態定義函數。使用者界面函數圖形裝置界面函數。
#include<conio.h>
//用戶通過按鍵盤產生的對應操作(控制臺)
#include<stdlib.h>
#include<time.h>//日期和時間頭文件
#defineLEN30
#defineWID25
intSnake[LEN][WID]={0};
//數組的元素代表蛇的各個部位
charSna_Hea_Dir='a';//記錄蛇頭的移動方向
intSna_Hea_X,Sna_Hea_Y;//記錄蛇頭的位置
intSnake_Len=3;//記錄蛇的長度
clock_tNow_Time;//記錄當前時間,以便自動移動
intWait_Time;//記錄自動移動的時間間隔
intEat_Apple=1;//吃到蘋果表示為1
intLevel;
intAll_Score=-1;
intApple_Num=-1;
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
//獲取標準輸出的句柄<windows.h>
//句柄:標志應用程序中的不同對象和同類對象中的不同的實例方便操控,
voidgotoxy(intx,inty)//設置光標位置
{
COORDpos={x,y};
//定義一個字符在控制臺屏幕上的坐標POS
SetConsoleCursorPosition(hConsole,pos);
//定位光標位置的函數<windows.h>
}
voidHide_Cursor()//隱藏光標固定函數
{
CONSOLE_CURSOR_INFOcursor_info={1,0};
SetConsoleCursorInfo(hConsole,&cursor_info);
}
voidSetColor(intcolor)//設置顏色
{
SetConsoleTextAttribute(hConsole,color);
//是API設置字體顏色和背景色的函數格式:SetConsoleTextAttribute(句柄,顏色);
}
voidPrint_Snake()//打印蛇頭和蛇的脖子和蛇尾
{
intiy,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("≈");
}
}
}
voidClear_Snake()//擦除貪吃蛇
{
intiy,ix;
for(iy=0;iy<WID;++iy)
for(ix=0;ix<LEN;++ix)
{
gotoxy(ix*2,iy);
if(Snake[ix][iy]==Snake_Len)
printf("");
}
}
voidRand_Apple()//隨機產生蘋果
{
intix,iy;
do
{
ix=rand()%LEN;
iy=rand()%WID;
}while(Snake[ix][iy]);
Snake[ix][iy]=-1;
gotoxy(ix*2,iy);
printf("⊙");
Eat_Apple=0;
}
voidGame_Over()//蛇死掉了
{
gotoxy(30,10);
printf("GameOver");
Sleep(3000);
system("pause>nul");
exit(0);
}
voidMove_Snake()//讓蛇動起來
{
intix,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]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[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;//處理蛇頭
}
voidGet_Input()//控制蛇的移動方向
{
if(kbhit())
{
switch(getch())
{
case87:
Sna_Hea_Dir='w';
break;
case83:
Sna_Hea_Dir='s';
break;
case65:
Sna_Hea_Dir='a';
break;
case68:
Sna_Hea_Dir='d';
break;
default:
break;
}
}
if(clock()-Now_Time>=Wait_Time)//蛇到時間自動行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time=clock();
}
}
voidInit()//初始化
{
system("title貪吃毛毛蛇");
system("modecon:cols=80lines=25");
Hide_Cursor();
gotoxy(61,4);
printf("YouScore:");
gotoxy(61,6);
printf("YouLevel:");
gotoxy(61,8);
printf("TheLenght:");
gotoxy(61,10);
printf("TheSpeed:");
gotoxy(61,12);
printf("AppleNum:");
inti;
for(i=0;i<Snake_Len;++i)//生成蛇
Snake[10+i][15]=i+1;
intiy,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("■");
}
}
}
voidPri_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);
}
voidLev_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;
}
}
intmain(void)
{
Init();
srand((unsigned)time(NULL));//設置隨機數的種子
Now_Time=clock();
intspeed1=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);
}
return0;
}
關于c語言游戲最簡單代碼的內容到此結束,希望對大家有所幫助。