SDL实例-1、打砖块

1、前言

本文实现了“打砖块”游戏的基本逻辑,算是一个小demo。开发环境配置可参考我的“SDL基础”系列文章,游戏截图如下:

这里写图片描述

2、代码

#include <stdbool.h>//使用bool类型
#include <stdio.h>
#include <stdlib.h>
#include <math.h>//数学库,用于计算两点间距离,判断是否发生碰撞
#include <SDL2/SDL.h>//引用SDL库
#include <SDL2/SDL_image.h>//引用SDL_image库
//屏幕属性
#define WINDOW_WIDTH  320//屏幕宽度
#define WINDOW_HEIGHT  600//屏幕长度


//位置
SDL_Rect BlueRect ; //砖块
SDL_Rect BallRect ; //球
SDL_Rect ReboundRodsRect ; //反弹棒

//事件
SDL_Event event;

//指针
SDL_Window* window = NULL;

SDL_Renderer* renderer = NULL;

SDL_Surface* BallSueface = NULL;
SDL_Surface* ReboundRodsSurface = NULL;
SDL_Surface* BlueBrick = NULL;//砖块
SDL_Surface* breakBlue = NULL;//打碎的砖块


SDL_Texture* BlueBrickTT;
SDL_Texture* breakBlueTT;
SDL_Texture* BallTT = NULL;
SDL_Texture* ReboundRodsTT = NULL;
//结构体

typedef struct SSurface//砖块结构体
{
    SDL_Texture* Texture;//文理
    SDL_Rect TargetRect;//目标rect
    SDL_Rect SourceRect;//源图片的rect
    int index;//显示那张图片
    bool state;//是否有效
}SSurface;
SSurface BlickManager[10][5];//结构体数组,存放方砖结构体

//全局参数
const int BrickW = 64;//砖块的宽度
const int BrickH = 32;//砖块的高度
//球移动速度
int ReboundRodsSpeed = 3;//反弹棒移动速度
int BallSpeed = 2;//小球移动速度

//控制反弹棒的移动方向
bool moveReboundRodsLeft = false;//反弹棒左移标志位
bool moveReboundRodsRight = false;//反弹棒右移标志位
//控制球的移动方向
bool moveRight = true;
bool moveDown = true;

//子函数
void init(void);//初始化函数
void UpdateLogic(void);//更新逻辑
void draw(void);//画图
int check_collision();//检查球与砖块的碰撞,还有瑕疵待修正
bool checkCollision(SDL_Rect A,SDL_Rect B);//球与反弹棒相撞检测,暂不去除
double GetDistance(int x1,int y1,int x2,int y2);//返回两点距离

int main(int argc,char* argv[])
{
    init();//初始化
    bool quit = false;//跳出循环后执行quit(),确保资源的释放
    while(quit == 
#include #include //用键盘控制精灵移动 int main(int argc, char ** argv) { SDL_Surface * screen; //主表面 SDL_Surface * image; //用来放MM-----的图片信息(像素) SDL_Surface * PlayerImage; //用来测试的图片 SDL_Event event; Uint32 BeginTicks, EndTicks; SDL_Rect PRect, BRect; //PRect对应精灵的移动的小图位置(实现动画),BRect对应精灵在屏幕的位置。 unsigned char PlayerStarts = 0; unsigned char PlayerIndex = 0; bool bAppRun = true; //初始化SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { fprintf(stderr, "SDL_Init %s\n", SDL_GetError()); return -1; } //初始化成功设置退出要调用的函数SDL_Quit atexit(SDL_Quit); //创建一个640X480 16-bit 模式的主表面 16位可以让MM的效果好看一点 screen = SDL_SetVideoMode(230, 230, 16, SDL_SWSURFACE); if (screen == NULL) { fprintf(stderr, "Couldn't set 640x480x8 video mode %s\n", SDL_GetError()); return -1; } //读取MM图片信息,并创建一个表面,并把数据填入该表面中。 image = SDL_LoadBMP("./mm.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取player.bmp PlayerImage = SDL_LoadBMP("./player.bmp"); //请在终端里运行该程序 if (image == NULL) { fprintf(stderr, "Couldn't load MM, %s\n", SDL_GetError()); //遗憾你不能显示MM了,不过你可以用图片浏览程序看。 return -1; } //读取第一个像素 Uint8 key = *((Uint8 *)PlayerImage->pixels); //设置色键 SDL_SetColorKey(PlayerImage, SDL_SRCCOLORKEY, key); //有了MM的表面了,就可以显示了。 //将MM的表面画在我们的主表面上,用MM来作为背景 if (SDL_BlitSurface(image, NULL, screen, NULL) < 0) { //解释一下NULL,第一个是按照image的尺寸显示,第二个是默认显示。你也可以指定大小,不过要用到SDL_Rect你可以看一看帮助。 fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError()); //看看提示吧 return -1; } PRect.x = 0; //初始化动画显示的图片。 PRect.y = 0; PRect.w = 32; PRect.h = 48; BRect.x = 0; //初始化精灵的位置。 BRect.y = 0; BRect.w = 32; BRect.h = 48; //贴上测试用的表面 if (SDL_BlitSurface(PlayerImage, &PRect, screen, &BRect) w, image->h); BeginT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值