使用手機(jī)C4droid編譯器的SDL2庫(kù)寫(xiě)的打飛機(jī)
(水平很有限,僅僅是基本實(shí)現(xiàn)能玩)
希望可以和大家一起學(xué)習(xí)進(jìn)步


敵機(jī)

本機(jī)

子彈
這段代碼復(fù)制下來(lái)就能用,圖片素材都在上面,字體順便網(wǎng)上下載
#include
#include
#include
#include
#include
#include
#define WIDTH 1000
#define HEIGHT 2000
#define ERROR_FONT_SIZOF 80
//字體大小
#define FRAMERATE 60
//幀率
#define BULLET_SPEED 5
//子彈速度
#define ENEMY_SPEED 3
//敵機(jī)速度
#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"
//上面的路徑自己修改自己手機(jī)圖片的路徑就好
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 //飛機(jī)類
{
?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 //敵機(jī)類
{
?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(); ?//初始化函數(shù)
plane plane1; //飛機(jī)對(duì)象
//這里用指針圓形鏈表創(chuàng)建子彈和敵機(jī)
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; ??//幀數(shù)
int grade = 0; //分?jǐn)?shù)
int tt = 0;
while (1)
{
long begin = SDL_GetTicks(); //測(cè)時(shí)間開(kāi)點(diǎn)
SDL_SetRenderDrawColor(renderer, 55, 55, 55, 255);
SDL_RenderClear(renderer); //以上面的顏色清屏,分別是R.G.B.α透明
SDL_Rect bullet_rect;
bullet_rect = plane1.draw();
//創(chuàng)建子彈鏈
if (i % 40 == 1) //每到這里往鏈表里面塞一個(gè)對(duì)象
{
butemp = butip;
butip = new bullet;
butip->next = butemp->next;
butip->last = butemp;
butemp->next->last = butip;
butemp->next = butip;
butip->move(bullet_rect); //子彈的初始位置等于當(dāng)前本機(jī)的位置
}
butip2 = butip;
do
{
butip->draw(5);
if (butip->srcr.y < -400) //判斷是否到達(dá)邊界,這里設(shè)置的比較遠(yuǎn)是因?yàn)椴荒茏屚瑫r(shí)只能存在一個(gè)子彈
{
butemp = butip;
butip->last->next = butip->next;
butip->next->last = butip->last;
butip = butip->last;
butemp->~bullet();
free(butemp);
butemp = NULL;
}
butip = butip->next;
} while (butip != butip2);
//
//創(chuàng)建敵機(jī)鏈
if (i % (60 - (i / 100)) == 1)//敵機(jī)的創(chuàng)建速度跟隨幀率
{
entip = entip2->next;
entemp = new enemy;
entip2->next = entemp;
entip->last = entemp;
entemp->next = entip;
entemp->last = entip2;
entip = entemp;
}
entip2 = entip;
do
{
entip->draw(ENEMY_SPEED + i / 800);//幀數(shù)每增加800個(gè),速度加1
if (entip->srcr.y > HEIGHT) //判斷是否到達(dá)邊界
{
entemp = entip;
entip->last->next = entip->next;
entip->next->last = entip->last;
entip = entip->last;
entemp->~enemy();
free(entemp);
entemp = NULL;
}
entip = entip->next;
} while (entip != entip2);
///////*
butip2 = butip;
entip2 = entip;
do
{
do
{
//判斷是否接觸
if (SDL_HasIntersection(entip->returect(), butip->returect()))
{
//showvalue("jz", entip->srcr.y, 0, 300);
butip->show = false; //這里不刪除,發(fā)現(xiàn)總會(huì)刪除標(biāo)記指針,索性直接這個(gè)鏈元素直接隱藏,
entip->show = false; //并且上面的判斷函數(shù)里面的返回該鏈對(duì)象Rect的函數(shù)都返回一個(gè)固定框,確保不會(huì)相交
grade++;
}
entip = entip->next;
} while (entip != entip2);
butip = butip->next;
} while (butip != butip2);
showvalue("grade:", grade, 0, 100);
i++;
showvalue("zhen", i, 0, 500);
int bb = 0;
do //循環(huán)判斷敵機(jī)與本機(jī)是否接觸,但這里使用圖片的大小方塊,判定距離有點(diǎn)大
{
if (SDL_HasIntersection(&(plane1.srcr), entip->returect()))
{
showvalue("jz", entip->srcr.y, 0, 300);
bb = 1;
break;
}
entip = entip->next;
} while (entip != entip2);
if (bb)
{
break;
}
SDL_RenderPresent(renderer);
//////////
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEMOTION:;
case SDL_MOUSEBUTTONDOWN:;
case SDL_MOUSEBUTTONUP:
plane1.mouseplane(&event);
break;
case SDL_QUIT:
return 0;
}
}
//////*/
long current = SDL_GetTicks(); //測(cè)時(shí)間結(jié)束點(diǎn)
long cost = current - begin;
long frame = 1000 / FRAMERATE;
long delay = frame - cost;
if (delay > 0) //通過(guò)上面的來(lái)計(jì)算一幀的時(shí)間。
{
SDL_Delay(delay);
}
}
showvalue("beybey", 0, WIDTH / 3, HEIGHT / 2);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
close();
return 0;
}
bool init()
{
// 初始化SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "SDL初始化失敗。SDL錯(cuò)誤:" << SDL_GetError() << std::endl;
return false;
}
// 創(chuàng)建窗口
window = SDL_CreateWindow("SDL Font Rendering", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
std::cout << "窗口創(chuàng)建失敗。SDL錯(cuò)誤:" << SDL_GetError() << std::endl;
return false;
}
// 創(chuàng)建渲染器
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL)
{
std::cout << "渲染器創(chuàng)建失敗。SDL錯(cuò)誤:" << SDL_GetError() << std::endl;
return false;
}
// 設(shè)置渲染器繪制顏色為白色
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// 初始化SDL_ttf
if (TTF_Init() == -1)
{
std::cout << "SDL_ttf初始化失敗。SDL_ttf錯(cuò)誤:" << TTF_GetError() << std::endl;
return false;
}
font = TTF_OpenFont(FONT_NANE, ERROR_FONT_SIZOF);
if (font == NULL)
{
std::cout << "字體加載失敗。TTF錯(cuò)誤:" << TTF_GetError() << std::endl;
return false;
}
return true;
}
void showvalue(string a, int g, int x = 100, int y = 100) //這是一個(gè)顯示字符的函數(shù)
{
char yy[16];
sprintf(yy, "%s:%d", a.c_str(), g);
SDL_Color color = {255, 0, 0, 0};
SDL_Surface *fontsurface = TTF_RenderText_Solid(font, yy, color);
SDL_Texture *fontTexture = SDL_CreateTextureFromSurface(renderer, fontsurface);
SDL_Rect textRect;
textRect.x = x;
textRect.y = y;
textRect.w = fontsurface->w;
textRect.h = fontsurface->h;
SDL_FreeSurface(fontsurface);
SDL_RenderCopy(renderer, fontTexture, NULL, &textRect);
SDL_DestroyTexture(fontTexture);
}
void close()
{
// 釋放字體資源
TTF_CloseFont(font);
font = NULL;
// 銷毀渲染器和窗口
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
renderer = NULL;
window = NULL;
// 退出SDL_ttf
TTF_Quit();
// 退出SDL
SDL_Quit();
}
plane::plane() //飛機(jī)類的構(gòu)造函數(shù)
{
this->img = IMG_LoadTexture(renderer, PLANE_FILE);
if (this->img == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
}
this->simg = IMG_Load(PLANE_FILE);
if (this->simg == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
};
this->planew = WIDTH / 5; //是圖像大小為屏幕寬度的1/5
this->planeh = WIDTH / 5;
//(int)( (float)(planew) /(float)(this->simg->w) * (float)(this->simg->h));
this->imgr = {0, 0, (this->simg->w), (this->simg->h)};
this->srcr = {WIDTH / 2 - planew / 2, HEIGHT - planeh * 2, planew, planeh};
}
int plane::mouseplane(SDL_Event *event) ////把鼠標(biāo)的位置傳給圖像方塊
{
SDL_Point mousep = {event->button.x,
event->button.y};
switch (event->type)
{
case SDL_MOUSEMOTION:
{
if (move_no == SDL_TRUE) //按下且移動(dòng)鼠標(biāo)時(shí)才能移動(dòng)
{
showvalue("1", mousep.x, 0, 0);
this->srcr.x = event->button.x - this->planew / 2;
this->srcr.y = event->button.y - this->planeh / 2;
move_no = SDL_TRUE;
return 0;
}
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if (SDL_PointInRect(&mousep, &(this->srcr))) //判斷鼠標(biāo)按下的位置是不是在圖像的上
{
showvalue("down", 1, 0, 100);
move_no = SDL_TRUE; //在圖像上按下了鼠標(biāo)
return 0;
}
break;
}
case SDL_MOUSEBUTTONUP:
{
move_no = SDL_FALSE; //松開(kāi)鼠標(biāo)停止移動(dòng)。
return 0;
break;
}
default:
break;
}
return 0;
}
SDL_Rect &plane::draw()
{
SDL_RenderCopy(renderer, this->img, &(this->imgr), &(this->srcr));
return this->srcr;
}
plane::~plane()
{
SDL_DestroyTexture(this->img);
SDL_FreeSurface(this->simg);
};
bullet::bullet()
{
this->img = IMG_LoadTexture(renderer, BULLET_FILE);
if (this->img == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
}
this->simg = IMG_Load(BULLET_FILE);
if (this->simg == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
};
this->bulletw = WIDTH / 30; //根據(jù)屏幕寬度設(shè)置子彈的大小
this->bulleth = WIDTH / 18;
this->show = true; //默認(rèn)顯示子彈
this->imgr = {0, 0, (this->simg->w), (this->simg->h)};
this->srcr = {WIDTH / 2, HEIGHT - 100, bulletw, bulleth};
}
void bullet::move(SDL_Rect rect) //鼠標(biāo)位置轉(zhuǎn)換成子彈的位置。
{
this->srcr.x = rect.x + rect.w / 2 - this->srcr.w / 2;
this->srcr.y = rect.y;
}
void bullet::draw(int y) //顯示子彈,并按照傳入的數(shù)值每幀移動(dòng)。
{
this->srcr.y = this->srcr.y - y;
if (this->show)
{
SDL_RenderCopy(renderer, this->img, &(this->imgr), &(this->srcr));
}
return;
}
SDL_Rect *bullet::returect()
{
if (this->show)
{
return &(this->srcr); //如果依舊沒(méi)有結(jié)束,始終是判斷為顯示,則輸出該方塊的位置
}
else
{
SDL_Rect temp = {0, 3, 0, 0}; //如果接觸了,位置始終是在這個(gè)點(diǎn)。
return &temp;
}
}
bullet::~bullet() //子彈類的析構(gòu)函數(shù)
{
SDL_DestroyTexture(this->img);
SDL_FreeSurface(this->simg);
};
enemy::enemy() //敵機(jī)類的構(gòu)造函數(shù)。
{
this->img = IMG_LoadTexture(renderer, ENEMY_FILE);
if (this->img == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
}
this->simg = IMG_Load(ENEMY_FILE);
if (this->simg == NULL)
{
SDL_Log("圖片加載失敗:%s", SDL_GetError());
return;
};
this->enemyw = WIDTH / 6; //按屏幕寬度的比值顯示敵機(jī)
this->enemyh = WIDTH / 6;
this->show = true;
//srand(time(0));
//(int)( (float)(planew) /(float)(this->simg->w) * (float)(this->simg->h));
this->imgr = {0, 0, (this->simg->w), (this->simg->h)};
this->srcr = {(rand() % (WIDTH / this->enemyw)) * this->enemyw, -(this->enemyh), this->enemyw, this->enemyh};
}
SDL_Rect &enemy::draw(int y)
{
this->srcr.y = this->srcr.y + y;
if (this->show)
{
SDL_RenderCopy(renderer, this->img, &(this->imgr), &(this->srcr));
}
return this->srcr;
}
SDL_Rect *enemy::returect()
{
if (this->show) //如果你是沒(méi)有接觸顯示,則輸出敵機(jī)的位置。
{
return &(this->srcr);
}
else
{
SDL_Rect temp = {0, 0, 0, 0}; //如果接觸了判斷不顯示,則始終輸出該位置。
return &temp;
}
}
enemy::~enemy()
{
SDL_DestroyTexture(this->img);
SDL_FreeSurface(this->simg);
};
//程序?qū)懙煤荃磕_,我也僅僅是游戲愛(ài)好者,沒(méi)有電腦,不足之處請(qǐng)懇請(qǐng)大家積極指導(dǎo)。
標(biāo)簽: