当前位置:首页 » 经典版本 » 赛车游戏源代码c语言
扩展阅读
天成解说一个人的游戏 2021-03-16 21:51:02
打游戏什么牌子显卡好 2021-03-16 21:51:00

赛车游戏源代码c语言

发布时间: 2021-02-21 09:55:10

1. 求c语言SDL的一些小游戏的源代码,随便什么游戏都可以

最基础的贪吃蛇的代码

#include<stdio.h>
#include<windows.h>//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。
#include<conio.h> //用户通过按键盘产生的对应操作 (控制台)
#include<stdlib.h>
#include<time.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]!=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;//处理蛇头
}

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;
}

2. 跪求c语言游戏源码(完全版)

大一编的扫雷(完整),一百多行,Turbo C下编译成功 #include<stdio.h> #include<conio.h> #include<time.h> void adjust(int*,int*); int a[23][23],c[22][22],mm,nn; int roundmine(int,int); void spread(); main() { int i,j,b[22][22],minenum,mine; int x,y,yy,s; char g,h,z; int*x1; int*y1; time_t t; again:srand((unsigned) time(&t)); textcolor(LIGHTGREEN); for(i=2;i<=21;i++) for(j=2;j<=21;j++) { gotoxy(i,j); putch(219); b[i][j]=1; c[i][j]=1; } minenum=0; for(i=1;i<=22;i++) for(j=1;j<=22;j++) { a[i][j]=0; if(i>1&&i<22&j>1&&j<22) {if((s=rand()%10)>0) a[i][j]=0; else if(s==0) a[i][j]=1; } if(a[i][j]) minenum++; } gotoxy(50,2); puts("dirction"); gotoxy(50,3); puts("SPACE:mark"); gotoxy(50,4); puts("ENTER:dig"); gotoxy(50,8); printf("mine num:%4d",minenum); gotoxy(50,5); printf("ESC:exit"); gotoxy(30,15); puts(" "); gotoxy(30,20); puts(" "); mine=minenum; gotoxy(2,2); x=2;y=2; while(mine) { h=getch(); if(h=='H') gotoxy(x,--y); else if(h=='P') gotoxy(x,++y); else if(h=='K') gotoxy(--x,y); else if(h=='M') gotoxy(++x,y); x1=&x;y1=&y; adjust(x1,y1); if(h==' ') { if(c[wherex()][wherey()]) if(b[wherex()][wherey()]) { textcolor(LIGHTRED); putch(16); mine--; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=0; } else { textcolor(LIGHTGREEN); putch(219); mine++; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=1; } } if(h=='\015') { mm=wherex();nn=wherey(); c[wherex()][wherey()]=0; if(a[wherex()][wherey()]) { for(i=2;i<=21;i++) for(j=2;j<=21;j++) {gotoxy(i,j); if(a[i][j]) {textcolor(LIGHTRED); putch(017); } } break; } else {spread();gotoxy(mm,nn);} } if(h=='\033') exit(0); } yy=1; for(i=2;i<=21;i++) for(j=2;j<=21;j++) if (a[i][j]==1&&b[i][j]==1) {gotoxy(30,15); cprintf("You lose!!"); yy=0; } if(yy) {gotoxy(30,15); cprintf("You win!!"); } gotoxy(30,20); cprintf("Once again(y/n)?"); z=getch(); if(z=='y') goto again; } void adjust(int* x,int* y) { if(*x<2) gotoxy(*x+=20,*y); else if(*x>21) gotoxy(*x-=20,*y); else if(*y<2) gotoxy(*x,*y+=20); else if(*y>21) gotoxy(*x,*y-=20); } int roundmine(int x,int y) { int i,j,r=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(a[i][j]) r++; return r; } void spread() { int r,i,j,x,y; x=wherex(); y=wherey(); r=roundmine(x,y); if(r) {printf("%d",r);c[x][y]=0;} else if(r==0&&x>1&&x<22&&y>1&&y<22) { putchar(' ');c[x][y]=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(x>1&&x<22&&y>1&&y<22&&c[i][j]) {gotoxy(i,j); spread(); } } }

3. 求C语言小游戏源程序

新手要方便写代码,可以收藏下面几个自编函数:

  1. gtxy (6, 3) //光标定位于窗口的第6列,第3行处(准备输出,行与列都是从0算起)

  2. Color (4, 0) //设置为红字配黑底 如 Color (10, 0)则是淡绿字配黑底

  3. yinc (1,0) //隐藏光标(第二个参数设为0就隐藏,没有光标闪烁,yinc代表隐藏)

  4. kou(80,25) //设定窗口缓冲区大小为80列,25行

    下面几个是库函数,不需自己编写,只要用#include包含就可以使用。

  5. SetConsoleTitle("俄罗斯方块"); //设置窗口左上角标题栏处出现"俄罗斯方块"5个字

  6. srand( (unsigned) time(NULL) ); //初始化随机数发生器

  7. n= rand( ) % 20; //产生随机数0-19中的一个. 如 rand( )%5 就产生0-4中的一个数

    SetConsoleTitle( )函数在<windows.h>里,srand( )函数与rand( )函数要配合用,

    就是同时要用,在<stdlib.h>里。如果 rand( )%10+1 就产生1-10之中的一个数。

  8. Sleep(300); //延时300毫秒(就是程序暂停300毫秒后继续运行)

  9. 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 (short ForeColor= 7, short BackGroundColor= 0) //设定颜色的函数

{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( hl, ForeColor + BackGroundColor * 0x10 );

}

声明时原型可写 void Color (short x, short y);

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 ”才有效。

4. 求一个用C语言编写的小游戏代码

我也是一个新手。
下面有一个游戏
名字叫:
“坑人的无限”(一):
#include
#include
#include
#include
#include
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循环输出各个数字,不能对循环输出再进行循环(如果对循环输出0123456789再进行循环,则move1就变成一个无限循环的函数,则下面的screen循环就进行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循环外面的话,time(0)的值就一直不变,放在循环里面,一秒钟进行一次判断,一秒钟进行一次循环
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"欢迎来到“无限 ”游戏"<<char(1)<<endl;
cout<<"下面会输出无限个笑脸"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下来会更让你丧心病狂的!"<<char(1)<<endl;
cout<<"但是坚持过后必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然坚持下来了!"<<char(1)<<" "<<char(2)<<endl<<"不可思议呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬请期待!等待无限游戏(二)!";
return 0;
}

5. C语言写的赛车游戏,赛道、车都画好了,不知道怎么实现小车的移动

每隔一定的时间间隔就画出赛道与车,而且赛道有不同的标志,就能显示出车在移动了,如果赛道始终是一样的,即使是不断的画出了赛道和车,也不能看出车在移动。

6. c语言赛车游戏

这个需要高手来帮你`
呵呵`

7. 赛车游戏的迈(速度),C语言怎么实现

另外定义一个加速度,在循环里面加速度增加,在把加速度加到速度上 例如:内 int spead=0;//速度 int v=1;//加速度int n=0;whlie(spead<368){ spead=spead+v; n++; if(n%3==0) { v=v+1; n=0; }}//这里它的加速度就是每容循环三次就加一 这样他的速度就增加的更快 这是它的编程思想 具体的数据根据你的实际情况需要进行调节就可以了

8. 赛车游戏编程流程

这个范围太大了
你要知道游戏不是那么容易就做出来的,就算你有现成的美工,音乐,没有引擎的话编码够你搞的了。就算你有专业引擎,你没有一定的编程功底也是很难的。现在市面上没有免费的赛车游戏引擎,有的只有底层图像和物理引擎,你要自己设计架构,程序逻辑。

真的要推荐的话, 图像引擎可以用ogre,公认的最好开源图像引擎,有中文教程。不过入门的话irlicht好些,还有c#版本,不过没有中文的教程。物理引擎我就用过Newton的。

微软的xna里面有一些现成的模板,我记得有一个就是赛车游戏的,也有源码。不过xna速度巨慢,而且很多地方不成熟,官方版本连骨骼动画都不支持,微软好像也没有再继续发展,没什么前途。