Visual improvements.
This commit is contained in:
42
pingpong.c
42
pingpong.c
@@ -134,26 +134,18 @@ void handle_ball(game_state *state)
|
|||||||
|
|
||||||
if(state->ball.point.x <= 0) {
|
if(state->ball.point.x <= 0) {
|
||||||
state->ball.point.x = WINDOW_WIDTH / 2;
|
state->ball.point.x = WINDOW_WIDTH / 2;
|
||||||
state->ball.point.y = 0;
|
state->ball.point.y = 1;
|
||||||
state->player_two.score += 1;
|
state->player_two.score += 1;
|
||||||
if(state->player_two.score > 5) {
|
|
||||||
state->player_two.score = 1;
|
|
||||||
state->player_one.score = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(state->ball.point.x >= WINDOW_WIDTH - state->ball.dimension.x) {
|
if(state->ball.point.x >= WINDOW_WIDTH - state->ball.dimension.x) {
|
||||||
state->ball.point.x = WINDOW_WIDTH / 2;
|
state->ball.point.x = WINDOW_WIDTH / 2;
|
||||||
state->ball.point.y = 0;
|
state->ball.point.y = 1;
|
||||||
state->player_one.score += 1;
|
state->player_one.score += 1;
|
||||||
if(state->player_one.score > 5) {
|
|
||||||
state->player_one.score = 1;
|
|
||||||
state->player_two.score = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(state->ball.point.y < 0) {
|
if(state->ball.point.y < 0) {
|
||||||
state->ball.velocity.y *= -1;
|
state->ball.velocity.y *= -1;
|
||||||
state->ball.point.y = 0;
|
state->ball.point.y = 1;
|
||||||
|
|
||||||
color.r = 198.0f;
|
color.r = 198.0f;
|
||||||
color.g = 204.0f;
|
color.g = 204.0f;
|
||||||
@@ -262,15 +254,19 @@ void render_score(game_state *state)
|
|||||||
/* Build score string */
|
/* Build score string */
|
||||||
char *score;
|
char *score;
|
||||||
score = malloc(10*sizeof(char));
|
score = malloc(10*sizeof(char));
|
||||||
sprintf(score, "%d:%d",
|
|
||||||
state->player_one.score, state->player_two.score);
|
|
||||||
|
|
||||||
|
/* Render player one score */
|
||||||
/* Render */
|
sprintf(score, "%d", state->player_one.score);
|
||||||
vector2 pos = { (float)(WINDOW_WIDTH / 2), 10.0f };
|
vector2 pos = { (float)(WINDOW_WIDTH / 4), 10.0f };
|
||||||
vector4 color = { 198.0f, 204.0f, 215.0f, 255.0f };
|
vector4 color = { 198.0f, 204.0f, 215.0f, 255.0f };
|
||||||
render_text_centered(state, score, 30, pos, color);
|
render_text_centered(state, score, 30, pos, color);
|
||||||
|
|
||||||
|
/* Render player two score */
|
||||||
|
sprintf(score, "%d", state->player_two.score);
|
||||||
|
pos.x = WINDOW_WIDTH - pos.x;
|
||||||
|
render_text_centered(state, score, 30, pos, color);
|
||||||
|
sprintf(score, "%d", state->player_one.score);
|
||||||
|
|
||||||
/* free */
|
/* free */
|
||||||
free(score);
|
free(score);
|
||||||
}
|
}
|
||||||
@@ -340,6 +336,18 @@ void render_particles(game_state *state)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* TODO: ??? */
|
||||||
|
void render_divider(game_state *state)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(state->renderer, 13, 17, 23, 255);
|
||||||
|
SDL_RenderDrawLine(state->renderer, (int)(WINDOW_WIDTH / 2), 0,
|
||||||
|
(int)(WINDOW_WIDTH / 2), WINDOW_HEIGHT);
|
||||||
|
SDL_RenderDrawLine(state->renderer, (int)(WINDOW_WIDTH / 2) - 1, 0,
|
||||||
|
(int)(WINDOW_WIDTH / 2) - 1, WINDOW_HEIGHT);
|
||||||
|
SDL_RenderDrawLine(state->renderer, (int)(WINDOW_WIDTH / 2) + 1, 0,
|
||||||
|
(int)(WINDOW_WIDTH / 2) + 1, WINDOW_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
void setup(game_state *state)
|
void setup(game_state *state)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
@@ -445,6 +453,8 @@ void render(game_state *state)
|
|||||||
SDL_SetRenderDrawColor(state->renderer, 24, 26, 31, 255);
|
SDL_SetRenderDrawColor(state->renderer, 24, 26, 31, 255);
|
||||||
SDL_RenderClear(state->renderer);
|
SDL_RenderClear(state->renderer);
|
||||||
|
|
||||||
|
render_divider(state);
|
||||||
|
|
||||||
render_paddles(state);
|
render_paddles(state);
|
||||||
render_ball(state);
|
render_ball(state);
|
||||||
render_particles(state);
|
render_particles(state);
|
||||||
|
|||||||
4
types.h
4
types.h
@@ -63,13 +63,13 @@ typedef struct {
|
|||||||
int last_frame_time;
|
int last_frame_time;
|
||||||
float delta_time;
|
float delta_time;
|
||||||
|
|
||||||
struct ball {
|
struct {
|
||||||
vector2 point;
|
vector2 point;
|
||||||
vector2 velocity;
|
vector2 velocity;
|
||||||
vector2 dimension;
|
vector2 dimension;
|
||||||
} ball;
|
} ball;
|
||||||
|
|
||||||
struct player {
|
struct {
|
||||||
vector2 point;
|
vector2 point;
|
||||||
vector2 dimension;
|
vector2 dimension;
|
||||||
float vy;
|
float vy;
|
||||||
|
|||||||
Reference in New Issue
Block a user