带有图形的 c/c++ 赛车游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19330495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:43:36  来源:igfitidea点击:

Racing game in c/c++ with graphics

c++

提问by aditya mithal

I am trying to make a racing game in c/c++ using graphics

我正在尝试使用图形在 c/c++ 中制作赛车游戏

my problem as of now is that i cant get color in the enemy car when i add color it flickers

我现在的问题是,当我添加颜色时,我无法在敌车中获得颜色,它会闪烁

How do i remove the flickering

怎么去除闪烁

Here is the game which i have made till now.

这是我迄今为止制作的游戏。

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#include<stdlib.h>

void enemycar(int x, int y)
 {
  setcolor(15);
  rectangle(x+1,y,x+49,y+100);
  rectangle(x+1,y+25,x+49,y+75);
  setfillstyle( SOLID_FILL,YELLOW);
  floodfill((x+x+50)/2,(y+y+100)/2,15);
  setfillstyle(1,YELLOW);
  floodfill((x+x+50)/2,(y+y+40)/2,15);
  floodfill((x+x+50)/2,(y+y+160)/2,15);

  }

void mycar(int x, int y)
 {
  setcolor(15);
  rectangle(x+1,y,x+49,y+100);
  rectangle(x+1,y+25,x+49,y+75);
  setfillstyle(5,RED);
  floodfill((x+x+50)/2,(y+y+100)/2,15);
  setfillstyle(2,BLUE);
  floodfill((x+x+50)/2,(y+y+40)/2,15);
  floodfill((x+x+50)/2,(y+y+160)/2,15);
  }



void myclear(int x,int y)
 {
 setcolor(8);
 rectangle(x+1,y,x+49,y+100);
 rectangle(x+1,y+25,x+49,y+75);
 setfillstyle(SOLID_FILL,8);
 floodfill((x+x+50)/2,(y+y+100)/2,8);
 floodfill((x+x+50)/2,(y+y+40)/2,8);
 floodfill((x+x+50)/2,(y+y+160)/2,8);
 }


 void enemyclear(int x,int y)
 {
 setcolor(8);
 rectangle(x+1,y,x+49,y+100);
 rectangle(x+1,y+25,x+49,y+75);
// line(x+1,y,x+1,y+100);
// delay(500);
 }

void main()
 {
 int gdriver = DETECT,gmode;
 initgraph(&gdriver,&gmode,"C:\Turboc3\BGI");

  int x=300,y=350,ch,life=3,score=0;
  char choice;
  cout<<"\n\n\n\n\n\t\t\t* THUNDER RACER *";
  cout<<"\n\t\t\t ---------------";
  cout<<"\n\n\tLong long ago the state of Valencia was attacked by the";
  cout<<"\n\n\tenemies and were defeated. Every soldier and citizen was";
  cout<<"\n\n\tkilled except the beautiful princess Cindrella  and you";
  cout<<"\n\n\twho survived. Enemies want to kill her and she is in your";
  cout<<"\n\n\tcar right now. If you have the guts save her from enemies'";
  cout<<"\n\n\tcars and marry her.";
  cout<<"\n\n\tSo all the best...";
  delay(200);
  for(int m=1;m<25;m++)
    for(int n=1;n<80;n++) {
        gotoxy(n,m);
        cout<<" ";
        }
  setcolor(RED);
  rectangle(20,60,200,120);
  rectangle(20,300,200,420);
  gotoxy(5,21);
  cout<<"Only Aditya & Anshul";
  gotoxy(5,22);
  cout<<"have saved her till";
  gotoxy(5,23);
  cout<<"now. So you can be";
  gotoxy(5,24);
  cout<<"the 3-rd";
  rectangle(249,0,401,getmaxy());
  setfillstyle(SOLID_FILL,8);
  floodfill(325,getmaxy()/2,RED);
  setcolor(RED);
  rectangle(20,200,200,250);
  gotoxy(5,15);
  cout<<"Press <Esc> to Exit";
  for(int level=1;(level<=5)&&(life>0);level++){
    if(level==1){
      gotoxy(5,5);
      cout<<"Your War Starts Now";
      gotoxy(5,7);
      cout<<"All the best";
      delay(500);
      gotoxy(5,5);
      cout<<"                    ";
      gotoxy(5,7);
      cout<<"            ";
      }
     else {
         gotoxy(5,5);
         cout<<"Next level.";
         delay(500);
         gotoxy(5,5);
         cout<<"           ";
         }
  for(int i=0;(i<15)&&(life>0);i++) {
    if((level==5)&&(i==14)){
       gotoxy(5,5);
       cout<<"You have won";
       gotoxy(5,6);
       cout<<"Wanna continue <y/n>";
       choice = getch();
       if ((choice=='y')||(choice=='Y'))
        main();
       else
        exit(0);
       }
    setcolor(RED);
    rectangle(420,125,600,175);
    gotoxy(55,10);
    cout<<"Level = "<<level;
    rectangle(420,250,600,300);
    gotoxy(55,18);
    cout<<"Lives = "<<life;
    rectangle(420,350,600,400);
    gotoxy(55,24);
    cout<<"Score = "<<score;
    int accident=0;
    int y1=1,x1=250+((rand()%3)*50);
    int y2=1,x2=250+((rand()%3)*50);
    score += 10;
    while(y1<getmaxy()-1)
    {

        enemyclear(x1,y1);
        enemyclear(x2,y2);
        y1++;
        y2++;
        if(accident == 1)
            break;
        enemycar(x1,y1);
        enemycar(x2,y2);
        mycar(x,y);
        delay(5-level);
        if(kbhit())
        {
            mycar(x,y);
            ch=getch();
            switch(ch) {
            case 27: exit(0);
            break;
            case 75:myclear(x,y);
                if(x>250)
                    x=x-50;
                    if((x==x1)||(x==x2))
                        if( ( ((y-y1)<100)&&((y-y1)>0) ) || ( ((y-y2)<100)&&((y-y2)>0) ) )
                        {
                            accident=1;
                            x=x+50;
                            mycar(x,y);
                            goto Next1;
                        }
                 mycar(x,y);
            break;
            case 77:myclear(x,y);
                if(x<350)
                    x=x+50;
                    if((x==x1)||(x==x2))
                        if( ( ((y-y1)<100)&&((y-y1)>0) ) || ( ((y-y2)<100)&&((y-y2)>0) ) )
                        {
                            accident=1;
                            x=x-50;
                            mycar(x,y);
                            goto Next1;
                        }
                mycar(x,y);
            break;
            case 72:myclear(x,y);
                if(y>0)
                      y=y-5;
                mycar(x,y);
             break;
             case 80:myclear(x,y);
                if(y<getmaxy()-105)
                    y=y+5;
                mycar(x,y);
             break;
            }
        }
        if((x==x1)||(x==x2))
            if( ( ((y-y1)<100)&&((y-y1)>0) ) || ( ((y-y2)<100)&&((y-y2)>0) ) )
                accident=1;



      Next1:
      if(accident==1){
        life =life-1;
        score -= 10;
        if(life==0) {
         gotoxy(5,5);
         cout<<"GAME OVER ";
         gotoxy(5,6);
         cout<<"You could not save";
         gotoxy(5,7);
         cout<<"her";
         break;
         }
        gotoxy(5,5);
        cout<<"You have lost 1 life";
        delay(500);
        gotoxy(5,5);
        cout<<"                    ";
        }
        }
  }
  }
 getch();
 }

回答by Jasper Blues

I can see that you've modularizedyour code by breaking up the calls to create characters into methods. This is good.

我可以看到您通过将创建字符的调用分解为方法来模块化代码。这很好。

However, since you're working in C++, which is an Object Orientedlanguage I recommend that you try to create some classesfor each of the character types.

但是,由于您使用的是 C++,这是一种面向对象的语言,我建议您尝试为每种字符类型创建一些

Flickering

闪烁

The flickering is probably because your drawing library is not designed for high performance. I recommend that you try using sprites. In fact you might wish to use an open-source 2D games/sprite library like Cocos2d-x.

闪烁可能是因为您的绘图库不是为高性能而设计的。我建议您尝试使用精灵。事实上,您可能希望使用像Cocos2d-x这样的开源 2D 游戏/精灵库。

What will a games engine get me?

游戏引擎会给我带来什么?

A games engine like the one I described above, uses a lot of performance techniques that have been developed over the years, such as texture packing, look-up tables, etc to provide real-time performance. . . its still necessary to be learning about all of these techniques, however you probably don't want to be re-implementing them from the ground up (unless you're developing your own games engine).

像我上面描述的那样的游戏引擎,使用了许多多年来开发的性能技术,例如纹理打包、查找表等,以提供实时性能。. . 仍然有必要学习所有这些技术,但是您可能不想从头开始重新实现它们(除非您正在开发自己的游戏引擎)。

回答by Yash Sharma

To remove flickering here you can use the concept of graphical pages by using functions: setactivepage( ); and setvisualpage( );

要在此处消除闪烁,您可以通过使用以下函数来使用图形页面的概念: setactivepage( ); 和 setvisualpage();