#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#include<string.h>
// 使用的头文件
int W // 窗口宽
int H // 窗口高
int PH // 游戏区的高
int i, df = 0 // 定义的变量
char sorry[] = "游戏不适合你的手机", win[9] = "You win!", lose[10] =
"You lose", str[19] = "得分:"
Uint32 began = 0
float time = 0.0 // 定义的计时变量
bool quit = false // 退出标志
float X = 0, Y = 0 // 用于获取触控位置的坐标
SDL_Event event
// 定义一个事件
SDL_Window *w = NULL // 创建一个窗口指针
SDL_Renderer *ren = NULL // 创建一个渲染器指针
SDL_Surface *sur = NULL // 表面
SDL_Surface *sur1 = NULL // 得分表面
SDL_Surface *text = NULL // 标题文本区表面
SDL_Texture *tex = NULL // 画刷
SDL_Texture *tex1 = NULL // 画刷
SDL_Texture *tex2 = NULL // 画刷
TTF_Font *font = NULL // 定义一个字体
SDL_Color color, fontcolor // 定义字体颜色
SDL_Rect rect[4] // 屏幕出现的四个黑块
SDL_Rect title // 标题区
SDL_Rect pp, over
void change() // 使方块动起来
void init() // 启动sdl及相关
void draw() // 画图
void start() // 初始化相关数据
void end() // 游戏结尾
int main(int argc, char **argv)
{ // 主函数
init()
start()
began = SDL_GetTicks() // 得到开始时间
while (quit == false)
{
draw() // 画图
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_FINGERDOWN:
X = event.tfinger.x * W
Y = event.tfinger.y * H
// 获取x,y的坐标
if (Y >rect[0].y)
if ((X >rect[0].x) &&(X <(W / 4 + rect[0].x))) // 判断坐标
{
change()
df = df + 1
}
else // 如果触屏位置不对
{
quit = true
if (df <3)
sur1 = TTF_RenderUTF8_Blended(font, sorry, color)
else
sur1 = TTF_RenderUTF8_Blended(font, lose, color)
}
break
case SDL_FINGERUP:
break
default:
}
if (df >100) // 如果在30秒内得分超过100
{
quit = true
sur1 = TTF_RenderUTF8_Blended(font, win, color)
}
}
time = 30 - ((SDL_GetTicks() - began) / 1000) // 计算剩余时间
if (time == 0.0) // 当时间结束
{
quit = true
sprintf(str, "时间到,得分:%d", df)
sur1 = TTF_RenderUTF8_Blended(font, str, color)
}
}
tex1 = SDL_CreateTextureFromSurface(ren, sur1) // 把表面转换成画刷
SDL_RenderCopy(ren, tex1, NULL, &over)
SDL_RenderPresent(ren)
SDL_Delay(3000) // 暂停3秒
end()
return 0
}
void change()
{
for (int i = 0i <3i++)
rect[i].x = rect[i + 1].x
rect[3].x = (random() % 4) * W / 4 // 最上层产生随机的黑块
}
void init()
{
SDL_Init(SDL_INIT_EVERYTHING)
w = SDL_CreateWindow("别踩白块儿", 0, 0, W, H, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS) // 配置窗口参数并创建
//
//
ren = SDL_CreateRenderer(w, -1, SDL_RENDERER_PRESENTVSYNC) // 为创建的窗口配置渲染器
TTF_Init() // 启动font
font = TTF_OpenFont("/system/fonts/DroidSansFallback.ttf", 60) // 打开系统自带字体并设置大小为60
}
void draw()
{
sur = SDL_CreateRGBSurface(0, W, H, 32, 0, 0, 0, 0) // 先把表面刷白
SDL_FillRect(sur, NULL, 0xffffff)
for (int i = 0i <4i++)
SDL_FillRect(sur, &rect[i], 0x00000) // 再把黑块逐个贴上表面
tex = SDL_CreateTextureFromSurface(ren, sur) // 把表面转换成画刷*/
sprintf(str, "得分:%d", df)
sur1 = TTF_RenderUTF8_Blended(font, str, color)
tex1 = SDL_CreateTextureFromSurface(ren, sur1) // 把表面转换成画刷
sprintf(str, "时间:%.2f\"", time)
text = TTF_RenderUTF8_Blended(font, str, fontcolor)
tex2 = SDL_CreateTextureFromSurface(ren, text) // 把表面转换成画刷
SDL_RenderCopy(ren, tex, NULL, NULL)
SDL_RenderCopy(ren, tex2, NULL, &title)
SDL_RenderCopy(ren, tex1, NULL, &pp)
SDL_RenderPresent(ren)
SDL_FreeSurface(sur)
SDL_DestroyTexture(tex)
SDL_FreeSurface(sur1)
SDL_FreeSurface(text)
SDL_DestroyTexture(tex1)
SDL_DestroyTexture(tex2)
}
void start()
{
SDL_GetWindowSize(w, &W, &H) // 得到屏幕分辨率
PH = H * 7 / 8
color =
{
255, 215, 0}
fontcolor =
{
0, 0, 0} // 字体颜色赋值
float a = PH * 3 / 4 + H / 8
float wight = W / 4
float hight = PH / 4
rect[0].x = 0
rect[0].y = a
rect[1].x = wight
rect[1].y = a - hight
rect[2].x = wight * 2
rect[2].y = a - 2 * hight
rect[3].x = 3 * wight
rect[3].y = a - 3 * hight
for (int i = 0i <4i++)
{ // 为黑块的高和宽赋值
rect[i].h = hight
rect[i].w = wight
}
title =
{
1.10, 0, W / 2, W / 8}
pp =
{
W / 2, 0, W / 2, W / 8}
over =
{
0, H / 4, W, H / 4}
}
void end()
{
TTF_CloseFont(font)
TTF_Quit()
亡灵杀手 灵魂救星2 逃离神殿2 激流快艇 侠盗猎车 乐动达人 音乐节拍 滑雪大冒险手机游戏是指运行于手机上的游戏软件。目前用来编写手机最多的程序是Java语言,见J2ME。其次是C语言。随着科技的发展,现在手机的功能也越来越多,越来越强大。而手机游戏也远远不是我们印象中的什么“俄罗斯方块”“贪吃蛇”之类画面简陋,规则简单的游戏,进而发展到了可以和掌上游戏机媲美,具有很强的娱乐性和交互性的复杂形态了。于是,抛弃你的随身听和Gameboy,买一个好手机吧,你会发现,一个手机已经足够满足你所有路途中的大部分娱乐需要了。