本游戏使用了SDL2图形库来实现游戏界面和音效。
如果没有下载SDL2库请先下载SDL2库在运行游戏。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
using namespace std;
// 游戏窗口大小
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
// 跳跃高度和速度
const int JUMP_HEIGHT = 200;
const int JUMP_SPEED = 5;
// 障碍物宽度和速度
const int OBSTACLE_WIDTH = 50;
const int OBSTACLE_SPEED = 5;
// 奖励宽度和速度
const int REWARD_WIDTH = 50;
const int REWARD_SPEED = 5;
// 游戏状态
enum GameState {
STATE_READY,
STATE_PLAYING,
STATE_GAMEOVER
};
// 图片和字体
SDL_Texture* backgroundTexture;
SDL_Texture* playerTexture;
SDL_Texture* obstacleTexture;
SDL_Texture* rewardTexture;
TTF_Font* font;
// 游戏对象
class GameObject {
public:
SDL_Rect rect;
SDL_Texture* texture;
int speed;
bool active;
GameObject() {
rect.x = 0;
rect.y = 0;
rect.w = 0;
rect.h = 0;
texture = NULL;
speed = 0;
active = false;
}
void move() {
rect.x -= speed;
if (rect.x + rect.w < 0) {
active = false;
}
}
void draw(SDL_Renderer* renderer) {
if (active && texture != NULL) {
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
}
};
// 玩家对象
class Player : public GameObject {
public:
int jumpCount;
bool jumping;
int jumpSpeed;
Player() {
jumpCount = 0;
jumping = false;
jumpSpeed = 0;
}
void jump() {
if (!jumping) {
jumping = true;
jumpCount = 0;
jumpSpeed = JUMP_SPEED;
}
}
void update() {
if (jumping) {
rect.y -= jumpSpeed;
jumpCount += jumpSpeed;
if (jumpCount >= JUMP_HEIGHT) {
jumpSpeed = -JUMP_SPEED;
}
if (jumpCount <= 0) {
jumping = false;
}
}
}
};
// 游戏对象列表
const int MAX_GAMEOBJECTS = 100;
GameObject gameObjects[MAX_GAMEOBJECTS];
int numGameObjects;
// 初始化SDL和游戏对象
bool init() {
// 初始化SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
return false;
}
// 创建窗口和渲染器
SDL_Window* window = SDL_CreateWindow("Running Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
cout << "Window could not be created! SDL_Error: " << SDL_GetError() << endl;
return false;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
cout << "Renderer could not be created! SDL_Error: " << SDL_GetError() << endl;
return false;
}
// 初始化SDL_image
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << endl;
return false;
}
// 初始化SDL_ttf
if (TTF_Init() == -1) {
cout << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << endl;
return false;
}
// 加载字体
font = TTF_OpenFont("arial.ttf", 28);
if (font == NULL) {
cout << "Failed to load font! SDL_ttf Error: " << TTF_GetError() << endl;
return false;
}
// 加载背景图片
SDL_Surface* backgroundSurface = IMG_Load("background.png");
if (backgroundSurface == NULL) {
cout << "Failed to load background image! SDL_Error: " << SDL_GetError() << endl;
return false;
}
backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_FreeSurface(backgroundSurface);
// 加载玩家图片
SDL_Surface* playerSurface = IMG_Load("player.png");
if (playerSurface == NULL) {
cout << "Failed to load player image! SDL_Error: " << SDL_GetError() << endl;
return false;
}
playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
SDL_FreeSurface(playerSurface);
// 加载障碍物图片
SDL_Surface* obstacleSurface = IMG_Load("obstacle.png");
if (obstacleSurface == NULL) {
cout << "Failed to load obstacle image! SDL_Error: " << SDL_GetError() << endl;
return false;
}
obstacleTexture = SDL_CreateTextureFromSurface(renderer, obstacleSurface);
SDL_FreeSurface(obstacleSurface);
// 加载奖励图片
SDL_Surface* rewardSurface = IMG_Load("reward.png");
if (rewardSurface == NULL) {
cout << "Failed to load reward image! SDL_Error: " << SDL_GetError() << endl;
return false;
}
rewardTexture = SDL_CreateTextureFromSurface(renderer, rewardSurface);
SDL_FreeSurface(rewardSurface);
// 初始化游戏对象列表
numGameObjects = 0;
return true;
}
// 退出SDL和游戏对象
void quit() {
// 销毁字体
TTF_CloseFont(font);
font = NULL;
// 销毁纹理
SDL_DestroyTexture(backgroundTexture);
SDL_DestroyTexture(playerTexture);
SDL_DestroyTexture(obstacleTexture);
SDL_DestroyTexture(rewardTexture);
// 销毁窗口和渲染器
SDL_DestroyRenderer(SDL_GetRenderer(SDL_GetWindowFromID(1)));
SDL_DestroyWindow(SDL_GetWindowFromID(1));
// 关闭SDL_ttf
TTF_Quit();
// 关闭SDL_image
IMG_Quit();
// 关闭SDL
SDL_Quit();
}
// 创建游戏对象
GameObject* createGameObject(int x, int y, int w, int h, SDL_Texture* texture, int speed) {
if (numGameObjects >= MAX_GAMEOBJECTS) {
return NULL;
}
GameObject* gameObject = &gameObjects[numGameObjects++];
gameObject->rect.x = x;
gameObject->rect.y = y;
gameObject->rect.w = w;
gameObject->rect.h = h;
gameObject->texture = texture;
gameObject->speed = speed;
gameObject->active = true;
return gameObject;
}
// 更新游戏对象
void updateGameObjects() {
for (int i = 0; i < numGameObjects; i++) {
gameObjects[i].move();
}
}
// 绘制游戏对象
void drawGameObjects(SDL_Renderer* renderer) {
for (int i = 0; i < numGameObjects; i++) {
gameObjects[i].draw(renderer);
}
}
// 碰撞检测
bool collides(GameObject* object1, GameObject* object2) {
return SDL_HasIntersection(&object1->rect, &object2->rect);
}
// 检查玩家与障碍物的碰撞
bool checkObstacleCollisions(Player* player) {
for (int i = 0; i < numGameObjects; i++) {
if (gameObjects[i].texture == obstacleTexture && collides(player, &gameObjects[i])) {
return true;
}
}
return false;
}
// 检查玩家与奖励的碰撞
void checkRewardCollisions(Player* player) {
for (int i = 0; i < numGameObjects; i++) {
if (gameObjects[i].texture == rewardTexture && collides(player, &gameObjects[i])) {
gameObjects[i].active = false;
}
}
}
// 显示游戏状态
void showGameState(SDL_Renderer* renderer, GameState state, int score) {
SDL_Color textColor = {255, 255, 255};
SDL_Surface* textSurface = NULL;
switch (state) {
case STATE_READY:
textSurface = TTF_RenderText_Solid(font, "Press SPACE to start", textColor);
break;
case STATE_PLAYING:
textSurface = TTF_RenderText_Solid(font, to_string(score).c_str(), textColor);
break;
case STATE_GAMEOVER:
textSurface = TTF_RenderText_Solid(font, "GAME OVER", textColor);
break;
}
if (textSurface != NULL) {
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect rect;
rect.x = SCREEN_WIDTH / 2 - textSurface->w / 2;
rect.y = SCREEN_HEIGHT / 2 - textSurface->h / 2;
rect.w = textSurface->w;
rect.h = textSurface->h;
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(texture);
}
}
int main(int argc, char* argv[]) {
// 初始化SDL和游戏对象
if (!init()) {
return 1;
}
// 创建玩家对象
Player player;
player.rect.x = 100;
player.rect.y = SCREEN_HEIGHT - 100;
player.rect.w = 50;
player.rect.h = 50;
player.texture = playerTexture;
player.speed = 0;
player.active = true;
// 游戏状态
GameState state = STATE_READY;
// 得分
int score = 0;
// 游戏循环标志
bool quitFlag = false;
// 随机数种子
srand(time(NULL));
// 游戏循环
while (!quitFlag) {
// 处理事件
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quitFlag = true;
} else if (e.type == SDL_KEYDOWN) {
if (state == STATE_READY && e.key.keysym.sym == SDLK_SPACE) {
state = STATE_PLAYING;
} else if (state == STATE_PLAYING && e.key.keysym.sym == SDLK_SPACE) {
player.jump();
} else if (state == STATE_GAMEOVER && e.key.keysym.sym == SDLK_SPACE) {
state = STATE_READY;
score = 0;
numGameObjects = 0;
player.active = true;
player.rect.x = 100;
player.rect.y = SCREEN_HEIGHT - 100;
player.speed = 0;
}
}
}
// 更新游戏对象
updateGameObjects();
player.update();
// 检查玩家与障碍物的碰撞
if (checkObstacleCollisions(&player)) {
state = STATE_GAMEOVER;
}
// 检查玩家与奖励的碰撞
checkRewardCollisions(&player);
// 添加障碍物和奖励
if (state == STATE_PLAYING && rand() % 100 < 5) {
int type = rand() % 2;
int y = rand() % (SCREEN_HEIGHT - OBSTACLE_WIDTH - REWARD_WIDTH) + OBSTACLE_WIDTH;
if (type == 0) {
createGameObject(SCREEN_WIDTH, y, OBSTACLE_WIDTH, OBSTACLE_WIDTH, obstacleTexture, OBSTACLE_SPEED);
} else {
createGameObject(SCREEN_WIDTH, y, REWARD_WIDTH, REWARD_WIDTH, rewardTexture, REWARD_SPEED);
}
}
// 计算得分
score++;
// 清空屏幕
SDL_RenderClear(SDL_GetRenderer(SDL_GetWindowFromID(1)));
// 绘制背景和游戏对象
SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindowFromID(1)), backgroundTexture, NULL, NULL);
drawGameObjects(SDL_GetRenderer(SDL_GetWindowFromID(1)));
player.draw(SDL_GetRenderer(SDL_GetWindowFromID(1)));
// 显示游戏状态和得分
showGameState(SDL_GetRenderer(SDL_GetWindowFromID(1)), state, score);
// 更新屏幕
SDL_RenderPresent(SDL_GetRenderer(SDL_GetWindowFromID(1)));
}
// 退出SDL和游戏对象
quit();
return 0;
}