1、前言
本文实现了“打砖块”游戏的基本逻辑,算是一个小demo。开发环境配置可参考我的“SDL基础”系列文章,游戏截图如下:
2、代码
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#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;
SDL_Rect SourceRect;
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;
while(quit ==