Skip to content

Commit

Permalink
add more monsters;add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dimweak authored and Dimweaker committed Apr 18, 2023
1 parent e8ecdc0 commit 50dc1dc
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Rect_Queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "SDL2/SDL.h"
#include "parameter.h"

#ifndef PARKOUR_RECT_QUEUE_H
#define PARKOUR_RECT_QUEUE_H
/* 怪物的节点,用于存储怪物的信息 */
typedef struct RectNode {
SDL_Rect rect; /* 怪物的位置 */
int rect_index; /* 怪物的索引 */
int rect_type; /* 怪物的种类,分空中和地面 */
int monster; /* 怪物的类型,在空中或地面的基础上细分 */
int is_passed; /* 是否已经被通过 */
struct RectNode* next; /* 指向下一个节点 */
} RectNode;

/* 怪物的队列,用于存储怪物的节点,以链式列表进行存储 */
typedef struct RectQueue {
RectNode* front;
RectNode* rear;
int size;
} RectQueue;

RectQueue* InitRectQueue();
void EnRectQueue(RectQueue* queue, SDL_Rect rect, int rect_type, int monster);
RectNode* DeRectQueue(RectQueue* queue);
void DestroyRectQueue(RectQueue* queue);
void CreateMonsterRectQueue(RectQueue* queue);
void FillMonsterRectQueue(RectQueue* queue);
int random_monster(int monster_type);
#endif
Binary file added images/monster/air0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added images/monster/ground0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added images/monster/ground2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions manage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "manage.h"

/**
* 加载人物的不同动作的图像资源,对PlayerAction进行初始化
* @param renderer 窗口的渲染器
* @param Karyl_actions PlayerAction结构体数组
*/
void Load_Karyl_Actions(SDL_Renderer* renderer, PlayerAction* Karyl_actions[]) {
int i;

/* 初始化每种动作的图片和当前帧数 */
for(i = 0;i < NUMS_OF_STATE; i++){
Karyl_actions[i] = Create_Player_Action();
Karyl_actions[i]->current_frame = 0;
}

/* 初始化每种动作的总帧数 */
Karyl_actions[STATE_RUN]->nums_of_frames = FRAMES_RUN;
Karyl_actions[STATE_JUMP]->nums_of_frames = FRAMES_JUMP;
Karyl_actions[STATE_DIE]->nums_of_frames = FRAMES_DIE;

/* 初始化每种动作的位置 */
Karyl_actions[STATE_RUN]->rect = KARYL_RUN_RECT;
Karyl_actions[STATE_JUMP]->rect = KARYL_JUMP_RECT;
Karyl_actions[STATE_DIE]->rect = KARYL_DIE_RECT;

/* 初始化每种动作的surface */
Set_Player_Action_Surface(Karyl_actions[STATE_RUN], KARYL_RUN_PATH);
Set_Player_Action_Surface(Karyl_actions[STATE_JUMP], KARYL_JUMP_PATH);
Set_Player_Action_Surface(Karyl_actions[STATE_DIE], KARYL_DIE_PATH);

/* 初始化每种动作的texture */
Set_Player_Action_Texture(Karyl_actions[STATE_RUN], renderer);
Set_Player_Action_Texture(Karyl_actions[STATE_JUMP], renderer);
Set_Player_Action_Texture(Karyl_actions[STATE_DIE], renderer);
}

/**
* 重置人物的动作数组
* @param Karyl_actions PlayerAction结构体数组
*/
void Reset_Karyl_Actions(PlayerAction* Karyl_actions[]) {
int i;

/* 重置每种动作类型的当前帧数 */
for(i = 0; i < NUMS_OF_STATE; i++) {
Karyl_actions[i]->current_frame = 0;
}

/* 重置人物的位置 */
Karyl_actions[STATE_RUN]->rect = KARYL_RUN_RECT;
Karyl_actions[STATE_JUMP]->rect = KARYL_JUMP_RECT;
Karyl_actions[STATE_DIE]->rect = KARYL_DIE_RECT;

}

/**
* 释放PlayerAction数组
* @param Karyl_actions PlayerAction结构体数组
*/
void Destroy_Karyl_Actions(PlayerAction* Karyl_actions[]) {
int i;

for(i = 0; i < NUMS_OF_STATE; i++) {
Destroy_Player_Action(Karyl_actions[i]);
}
}

/**
* 加载不同怪物类型的图像资源,对MonsterModel数组进行初始化
* @param renderer 窗口的渲染器
* @param monster_actions MonsterModel结构体数组
*/
void Load_Monster_Model(SDL_Renderer* renderer, MonsterModel* monster_models[]) {
int i, j, nums_of_type;
char path[30];

for(i = 0; i < NUMS_OF_MONSTER; i++) {
monster_models[i] = (MonsterModel*)malloc(sizeof(MonsterModel));
nums_of_type = i == MONSTER_GROUND ? GROUND_TYPE : AIR_TYPE;

for(j = 0; j < nums_of_type; j++){
sprintf(path, i==MONSTER_GROUND ? MONSTER_GROUND_PATH : MONSTER_AIR_PATH, j); /* 根据怪物类型和怪物编号格式化路径 */
monster_models[i]->surface[j] = IMG_Load(path); /* 加载怪物的surface */
monster_models[i]->texture[j] = SDL_CreateTextureFromSurface(renderer, monster_models[i]->surface[j]); /* 加载怪物的texture */
}
}

}

/**
* 释放MonsterModel数组
* @param monster_model MonsterModel结构体数组
*/
void Destroy_Monster_Models(MonsterModel* monster_model[]) {
Destroy_Monster_Model(monster_model[MONSTER_GROUND], GROUND_TYPE);
Destroy_Monster_Model(monster_model[MONSTER_AIR], AIR_TYPE);
}


18 changes: 18 additions & 0 deletions manage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include "Player_Action.h"
#include "Monster_Model.h"
#include "parameter.h"

#ifndef PARKOUR_LOAD_RESOURCE_H
#define PARKOUR_LOAD_RESOURCE_H

void Load_Karyl_Actions(SDL_Renderer* renderer, PlayerAction* Karyl_actions[]);
void Load_Monster_Model(SDL_Renderer* renderer, MonsterModel* monster_models[]);

void Reset_Karyl_Actions(PlayerAction* Karyl_actions[]);

void Destroy_Karyl_Actions(PlayerAction* Karyl_actions[]);
void Destroy_Monster_Models(MonsterModel* monster_model[]);

#endif //PARKOUR_LOAD_RESOURCE_H
41 changes: 41 additions & 0 deletions score.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "score.h"

/**
* 初始化分数
* @return Score结构体指针
*/
Score* InitScore(){
Score* score = (Score*)malloc(sizeof(Score));
score->score = 0;
score->surface = NULL;
score->texture = NULL;
return score;
}

/**
* 销毁分Score结构体
* @param score Score结构体指针
*/
void DestroyScore(Score* score){
SDL_FreeSurface(score->surface);
SDL_DestroyTexture(score->texture);
free(score);
}

/**
* 绘制分数
* @param renderer 窗口的渲染器
* @param score Score结构体指针
* @param font 字体
* @param color 颜色
*/
void DrawScore(SDL_Renderer* renderer, Score* score, TTF_Font* font, SDL_Color color){
char score_str[100];
sprintf(score_str, "Score: %d", score->score);
SDL_FreeSurface(score->surface);
SDL_DestroyTexture(score->texture);
score->surface = TTF_RenderText_Solid(font, score_str, color);
score->texture = SDL_CreateTextureFromSurface(renderer, score->surface);
SDL_Rect rect = {SCORE_RECT.x, SCORE_RECT.y, score->surface->w/2, score->surface->h/2};
SDL_RenderCopy(renderer, score->texture, NULL, &rect);
}
16 changes: 16 additions & 0 deletions score.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "parameter.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#include <stdio.h>
#ifndef PARKOUR_SCORE_H
#define PARKOUR_SCORE_H
/* 分数,用于存储分数的值、texture和surface */
typedef struct Score {
int score;
SDL_Texture *texture;
SDL_Surface *surface;
}Score;
Score* InitScore();
void DestroyScore(Score* score);
void DrawScore(SDL_Renderer* renderer, Score* score, TTF_Font* font, SDL_Color color);
#endif

0 comments on commit 50dc1dc

Please sign in to comment.