• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++版本简易Flappy bird

C++版本简易Flappy bird

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了flappy bird,flappy bird作者自杀,flappy bird电脑版,flappy bird源代码,flappy bird最高分等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

大一,上学期学完了C,写了几个控制台游戏
这学期自学C++,由于学校课程第七周才有C++

边学边写了这个小游戏,SDL 图形库完成的图形绘画
时间匆忙,BUG也有,代码效率比较低
和原作品还是很大的差别, 源代码在附件游戏文件夹中

演示图

#include <stdlib.h> 
#include<windows.h>   
#include <time.h>
#include<conio.h>
#include <iostream> 
#include <mmsystem.h> 
#include "SDL\SDL_image.h"
#include "SDL\SDL_ttf.h"
#include "SDL\SDL.h"
using namespace std;
#pragma comment(lib, "winmm.lib") 
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"") 
 int iii=0;
 int score =0;
 int y=0;
 int posi[3][2]= {{640,-300},{940,-300},{1240,-300}};
int i=0,n=3;  //n为管道对数
SDL_Color textColor_1 = { 37, 134, 108 };//绿色
//SDL_Color textColor_2 = { 112, 112, 35 };
SDL_Color textColor_2 = { 255, 255, 255 }; //白色
 
SDL_Color textColor_3 = { 0, 88, 132 }; //白色
 
class Event
{
public:
  SDL_Event event;
  Uint8 *keystates;
  Event()
  {
    keystates=SDL_GetKeyState( NULL );
  }
 
 
};
Event thing;
class Data
{
public:
  int bird_y;
  int score;
  int posi[20][2];
  SDL_Rect bird_1;
  SDL_Rect bird_2;
  SDL_Rect bg;
  SDL_Rect welcome;
  SDL_Rect pipe_up;
  SDL_Rect pipe_down;
  SDL_Rect bird_cls;
  SDL_Rect foot;
 
  SDL_Surface* load_imag(char *s)
  {
    SDL_Surface* temp;
    temp=IMG_Load(s);
 
    bg.x=0;
    bg.y=0;
    bg.w=640;
    bg.h=480;
 
    welcome.x=681;
    welcome.y=0;
    welcome.w=640;
    welcome.h=480;
 
    bird_1.x=17;
    bird_1.y=508;
    bird_1.w=52;
    bird_1.h=34;
 
    bird_2.x=461;
    bird_2.y=531;
    bird_2.w=52;
    bird_2.h=33;
 
    bird_cls.w=52;
    bird_cls.h=33;
    bird_cls.x=0;
    bird_cls.y=0;
     
    pipe_up.x=1361;
    pipe_up.y=11;
    pipe_up.w=103;
    pipe_up.h=410;
 
    pipe_down.x=1487;
    pipe_down.y=14;
    pipe_down.w=103;
    pipe_down.h=443;
 
    foot.x=0;
    foot.y=404;
    foot.w=640;
    foot.h=80;
 
 
   
    return temp;//SDL_DisplayFormat( temp );
  }
 
  TTF_Font* load_font()
  {
    SDL_Init( SDL_INIT_EVERYTHING );
    TTF_Init();
    return TTF_OpenFont( "main_1.dat", 36 );
 
  }
};
 
Data data;
 
class Pri
{
 
public:
  SDL_Surface* out; //输出
  SDL_Surface* main ; //主窗口
  TTF_Font *font;
  SDL_Rect a; //设置初始位置
  SDL_Surface*bird;
  SDL_Rect xy;
  SDL_Surface*font_1;
  SDL_Surface*font_2;
  SDL_Surface*font_11;
  SDL_Surface*font_22;
 
 
  Pri()
  {
      TTF_Font *font = NULL;
      init();//初始化输出设备
     
  }
 
  int init()
  {
    SDL_Init( SDL_INIT_EVERYTHING );
    TTF_Init();
    main= SDL_SetVideoMode( 640, 478, 32, SDL_SWSURFACE );
    SDL_WM_SetCaption("Flappy bird",NULL);//标题
    font=data.load_font();
    out=data.load_imag("main_2.dat");
    y=150;
   
    posi[0][0]=640;
    posi[0][1]=-300;
    posi[1][0]=940;
    posi[1][1]=-300;
    posi[2][0]=1240;
    posi[2][1]=-300;
    return 1;
  }
 
  int pipe()
  {
 
  }
 
  int bir(int y)//x=150
  {int ii;
  i+=1;
  xy.x=150;
  xy.y=y;
  if(i<14)
   
    {
      xy.x=150;
      xy.y=y;
      SDL_BlitSurface(out,&data.bg,main,NULL);
 
       
      for(ii=0;ii<n;ii++)
      {
        xy.x=posi[ii][0];
        xy.y=posi[ii][1];
        SDL_BlitSurface(out,&data.pipe_down,main,&xy);
        xy.x=posi[ii][0];
        xy.y=posi[ii][1]+600;
        SDL_BlitSurface(out,&data.pipe_up,main,&xy);
 
      }
 
 
 
 
 
      xy.x=150;
      xy.y=y;
      SDL_BlitSurface(out,&data.bird_1,main,&xy);
    xy.x=0;
      xy.y=404;
      SDL_BlitSurface(out,&data.foot,main,&xy);
   
    }
   
     
  else
    {
      xy.x=150;
      xy.y=y;
      SDL_BlitSurface(out,&data.bg,main,NULL);
      for(ii=0;ii<n;ii++)
      {
        xy.x=posi[ii][0];
        xy.y=posi[ii][1];
        SDL_BlitSurface(out,&data.pipe_down,main,&xy);
 
        xy.x=posi[ii][0];
        xy.y=posi[ii][1]+600;
        SDL_BlitSurface(out,&data.pipe_up,main,&xy);
 
      }
 
      xy.x=150;
      xy.y=y;
      SDL_BlitSurface(out,&data.bird_2,main,&xy);
      xy.x=0;
      xy.y=404;
      SDL_BlitSurface(out,&data.foot,main,&xy);
      if(i>28)i=0;
    }
    return 0;
     
 
  }
 
  int bg()
  {
      SDL_BlitSurface(out,&data.bg,main,&xy);
       
      return 0;
  }
 
  int over()
  {
    int x=0,y=0,i=0;
 
    xy.x=250;
    xy.y=150;
    font_1=TTF_RenderText_Solid( font, "Game over !", textColor_3);
    SDL_BlitSurface( font_1 , NULL, main, &xy);
 
    SDL_Flip(main);
 
    xy.x=350;
    xy.y=200;
    font_1=TTF_RenderText_Solid( font, "Retry game", textColor_1 );
    font_11=TTF_RenderText_Solid( font, "Retry game", textColor_2 );
    SDL_BlitSurface( font_1 , NULL, main, &xy); 
     
 
    xy.x=350;
    xy.y=280;
 
    font_2=TTF_RenderText_Solid( font, " Exit game", textColor_1 );
    font_22=TTF_RenderText_Solid( font, " Exit game", textColor_2 );
    SDL_BlitSurface( font_2 , NULL, main, &xy); 
/*
    xy.x=10;
    xy.y=440;
    //野指针
    SDL_BlitSurface( TTF_RenderText_Solid( font, "Esc:exit game  Space:to jump hekun ", textColor_1 ) , NULL, main, &xy); 
    SDL_Flip( main ); //更新窗口
    */
 
    while(SDL_PollEvent( &thing.event ), 1)
    {
      i+=2;
      Sleep(1);
      if( thing.keystates[ SDLK_ESCAPE] )exit(0);
      if( thing.event.type == SDL_QUIT )// 点击了SDL关闭按钮
      {
        exit(0);
      } 
 
 
      if((x>350&&x<540)&&(y<240)&&(y>200))
      {
        if(thing. event.type == SDL_MOUSEBUTTONDOWN)
          if(thing.event.button.button== SDL_BUTTON_LEFT )
          {
            SDL_FreeSurface(font_1);
            SDL_FreeSurface(font_11);
            SDL_FreeSurface(font_22);
            SDL_FreeSurface(font_2);
          WinExec("Flappy bird.exe",NULL);
          exit(0);
            return 11;
          }
 
 
      }
      else
      {
 
 
      }
 
 
 
      if((x>350&&x<540)&&(y<320)&&(y>280))  //结束游戏
      {
        if(thing. event.type == SDL_MOUSEBUTTONDOWN)
          if(thing.event.button.button== SDL_BUTTON_LEFT )exit(0);
 
      }
 
 
      if(thing. event.type == SDL_MOUSEMOTION )
      {
        x=thing.event.motion.x;
        y=thing.event.motion.y;
 
        //获得鼠标坐标
        if((thing.event.motion.x>350&&thing.event .motion .x<540)&&(thing.event.motion.y<240)&&(thing.event.motion.y>200))
        {
          xy.x=350;
          xy.y=200;
          SDL_BlitSurface( font_11, NULL, main, &xy); 
          SDL_Flip(main);
        }
        else
        {
          xy.x=350;
          xy.y=200;
          SDL_BlitSurface( font_1 , NULL, main, &xy); 
          SDL_Flip(main);
        }
 
 
 
        if((thing.event.motion.x>350&&thing.event .motion .x<540)&&(thing.event.motion.y<320)&&(thing.event.motion.y>280))
        {
          xy.x=350;
          xy.y=280;
          SDL_BlitSurface( font_22 , NULL, main, &xy); 
          SDL_Flip(main);
 
 
        }
        else
        {
          xy.x=350;
          xy.y=280;
          SDL_BlitSurface( font_2 , NULL, main, &xy); 
          SDL_Flip(main);
        }
      }
 
 
 
    }
    return 0;
  }
 
  int welcome()
  {    
 
    int x=0,y=0,i=0;
 
 
    SDL_BlitSurface(out,&data.welcome,main,NULL);
    bird=out;
    xy.x=350;
    xy.y=200;
    font_1=TTF_RenderText_Solid( font, "Start game", textColor_1 );
    font_11=TTF_RenderText_Solid( font, "Start game", textColor_2 );
    SDL_BlitSurface( font_1 , NULL, main, &xy); 
 
    xy.x=350;
    xy.y=280;
 
    font_2=TTF_



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++版本简易Flappy bird

相关文章

  • 2017-05-28C语言 if else 语句详细讲解
  • 2017-05-28C++的头文件和实现文件详解
  • 2017-05-28C++中的哈希容器unordered_map使用示例
  • 2017-05-28VC编程控件类HTControl之CHTGDIManager GDI资源管理类用法解析
  • 2017-05-28ipv6实现tcp编程示例
  • 2017-05-28c语言实现的hashtable分享
  • 2017-05-28C语言实现去除字符串中空格的简单实例
  • 2022-04-30assert断言函数
  • 2017-05-28解析C语言中结构体struct的对齐问题
  • 2017-05-28C++静态成员变量和静态成员函数的使用方法总结

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C语言设计一个闪闪的圣诞树
    • 详解C++中const_cast与reinterpret_cast运算符的用法
    • c++输出斐波那契数列示例分享
    • MFC实现全屏功能代码实例
    • 自己简单封装的一个CDialog类实例
    • C++基础入门教程(一):基础知识大杂烩
    • 解读C++编程中类模板的三种特化
    • C语言putenv()函数和getenv()函数的使用详解
    • 深入linux下遍历目录树的方法总结分析
    • C++中可以接受任意多个参数的函数定义方法(详解)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有