使用手机C4droid编译器的SDL2库写的打飞机
(水平很有限,仅仅是基本实现能玩)
希望可以和大家一起学习进步
这段代码复制下来就能用,图片素材都在上面,字体顺便网上下载
#include <iostream>
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <time.h>
#include <SDL2/SDL_ttf.h>
#define WIDTH 1000
#define HEIGHT 2000
#define ERROR_FONT_SIZOF 80
//字体大小
#define FRAMERATE 60
//帧率
#define BULLET_SPEED 5
//子弹速度
#define ENEMY_SPEED 3
//敌机速度
#define FONT_NANE "/storage/emulated/0/1/resource//ttf/15.ttf"
#define PLANE_FILE "/storage/emulated/0/1/resource/aircraft.png"
#define BULLET_FILE "/storage/emulated/0/1/resource/bullet.png"
#define ENEMY_FILE "/storage/emulated/0/1/resource/enemy.png"
//上面的路径自己修改自己手机图片的路径就好
using namespace std;
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font = NULL;
SDL_bool move_no = SDL_FALSE;
bool init();
void showvalue(string a, int g, int x, int y);
void close();
class plane //飞机类
{
public:
plane();
SDL_Texture *img;
SDL_Surface *simg;
int planew;
int planeh;
SDL_Rect imgr;
SDL_Rect srcr;
int mouseplane(SDL_Event *event);
SDL_Rect &draw();
~plane();
};
class bullet //子弹类
{
public:
bullet();
SDL_Texture *img;
SDL_Surface *simg;
SDL_Rect imgr;
SDL_Rect srcr;
bullet *last;
bullet *next;
bool show;
void move(SDL_Rect rect);
int bulletw;
int bulleth;
SDL_Rect *returect();
void draw(int y);
~bullet();
};
class enemy //敌机类
{
public:
enemy();
SDL_Texture *img;
SDL_Surface *simg;
SDL_Rect imgr;
SDL_Rect srcr;
enemy *last;
enemy *next;
bool show;
SDL_Rect *returect();
int enemyw;
int enemyh;
SDL_Rect &draw(int y);
~enemy();
};
int main(int argc, char *argv[])
{
init(); //初始化函数
plane plane1; //飞机对象
//这里用指针圆形链表创建子弹和敌机
bullet *butemp = new bullet;
bullet *butip2, *butip;
butip = butemp;
butemp->srcr.y = HEIGHT - 50;
butemp->next = butemp;
butemp->last = butemp;
//
enemy *entemp = new enemy;
enemy *entip2 = new enemy;
entip2->srcr.y = 50;
enemy *entip = entemp;
entip2->next = entemp;
entip2->last = entemp;
entemp->next = entip2;
entemp->last = entip2;
//
int i = 0; //帧数