Skip to content

Commit

Permalink
Prima versiune de Pong gata
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmarius committed Dec 29, 2020
1 parent 532e9f5 commit 3116ebe
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions wip/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.gif filter=lfs diff=lfs merge=lfs -text
3 changes: 3 additions & 0 deletions wip/img/pong_gif2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions wip/libs.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,13 @@ eliberează resurse;
Puteți vedea codul după reorganizarea de care am spus [aici](https://github.com/mcmarius/prog-calc/blob/v0.0.2-pong/wip/pong/main.c) (ping me dacă stric link-ul între timp).

Acum vom adăuga următoarea funcționalitate: jucătorul din stânga se poate muta sus/jos cu <kbd>W</kbd> și <kbd>S</kbd>, iar jucătorul din dreapta va putea face același lucru cu <kbd>I</kbd> și <kbd>K</kbd>.

Trebuie să verificăm dacă noua poziție rămâne în interiorul ecranului.

Aceleași verificări le facem și pentru minge. Suplimentar, mingea va trebui să se întoarcă înapoi dacă se ciocnește de una dintre palete sau de pereții de sus și de jos.

În acest moment, jocul arată în felul următor:

![](img/pong_gif2.gif)

Codul poate fi accesat [aici](https://github.com/mcmarius/prog-calc/blob/v0.0.3rc1-pong/wip/pong/main.c). Urmează să împart codul în mai multe funcții înainte de a mai face modificări. Totuși, în acest moment avem un joc Pong complet funcțional (dacă facem abstracție de bug-uri 😄).
120 changes: 118 additions & 2 deletions wip/pong/main.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <SFML/Graphics.h>

#define EPS_X 30
#define EPS_Y 60

const int WIDTH = 1280;
const int HEIGHT = 720;
const int P1_X = 30;
Expand Down Expand Up @@ -30,11 +35,19 @@ int main()
sfRectangleShape_setSize(player1, size_player);
sfRectangleShape_setSize(player2, size_player);

int player_speed = 30;

sfCircleShape *ball = sfCircleShape_create();
const int ball_radius = 25;
sfVector2f pos_ball = {WIDTH / 2 - ball_radius, HEIGHT / 2 - ball_radius};
sfVector2f pos_ball;
sfVector2f pos_ball_start = {WIDTH / 2 - ball_radius, HEIGHT / 2 - ball_radius};
pos_ball = pos_ball_start;
sfVector2f dir_ball;
sfVector2f dir_ball_start = {-10, 0};
dir_ball = dir_ball_start;
sfCircleShape_setRadius(ball, ball_radius);
sfCircleShape_setFillColor(ball, sfBlue);
sfCircleShape_setFillColor(ball, sfYellow);

////////////////////////////////////////////////////////////////
/// initializations for middle bar, walls, score and fonts
/// fileu
Expand Down Expand Up @@ -90,6 +103,15 @@ int main()
const int score_buf_size = 20;
char player1_score_buf[score_buf_size];
char player2_score_buf[score_buf_size];

bool game_end = false;
bool help = false;
unsigned int target_score = 5;
bool pause = false;
char main_text_buf[300];
sfText *main_text = sfText_create();
sfText_setFont(main_text, score_font);
sfText_setCharacterSize(main_text, score_size);
/// end initializations for middle bar, walls, score and fonts
////////////////////////////////////////////////////////////////

Expand All @@ -107,12 +129,35 @@ int main()
sfRenderWindow_close(window);
break;
case sfKeyW:
if(pos_player1.y > player_speed) /// check bounds
pos_player1.y -= player_speed;
break;
case sfKeyS:
if(pos_player1.y < HEIGHT - size_player.y - player_speed)
pos_player1.y += player_speed;
break;
case sfKeyI:
if(pos_player2.y > player_speed)
pos_player2.y -= player_speed;
break;
case sfKeyK:
if(pos_player2.y < HEIGHT - size_player.y - player_speed)
pos_player2.y += player_speed;
break;
case sfKeyP:
pause = !pause;
break;
case sfKeyR:
if(event.key.control) {
game_end = help = pause = false;
player1_score = player2_score = 0;
pos_ball = pos_ball_start;
dir_ball = dir_ball_start;
}
break;
case sfKeyH:
pause = true;
help = !help;
break;
default:
break;
Expand All @@ -121,6 +166,76 @@ int main()
}
}

if(pause) {
strcpy(main_text_buf, "Game paused");
if(help)
strcat(main_text_buf,
"\n\nW - Left player up"
"\n\nS - Left player down"
"\n\nI - Right player up"
"\n\nK - Right player down"
"\n\nP - Toggle pause"
"\n\nCtrl+R - Reset game"
"\n\nH - Toggle help and pause"
"\n\nEsc - Quit"
);
}
else if(player1_score >= target_score) {
strcpy(main_text_buf, "Left player won!");
game_end = true;
}
else if(player2_score >= target_score) {
strcpy(main_text_buf, "Right player won!");
game_end = true;
}
if(pause || game_end) {
sfRenderWindow_clear(window, sfColor_fromRGB(160, 160, 160));

int pos_text_width = (WIDTH - 30*strlen(main_text_buf)) / 2;
int pos_text_height = HEIGHT / 2 - 100;
if(help) {
pos_text_width = 100;
pos_text_height = 20;
}
sfVector2f pos_main_text = {pos_text_width, pos_text_height};
sfText_setPosition(main_text, pos_main_text);
sfText_setString(main_text, main_text_buf);
sfRenderWindow_drawText(window, main_text, NULL);
sfRenderWindow_display(window);
continue;
}

if(fabs(pos_ball.x - pos_player1.x) < EPS_X && fabs(pos_ball.y - pos_player1.y) < EPS_Y) {
dir_ball.x = -dir_ball.x; /// left player hits the ball
if(pos_ball.x < pos_player1.x)
dir_ball.y--; ///
else
dir_ball.y++;
}
else if(fabs(pos_ball.x - pos_player2.x) < EPS_X && fabs(pos_ball.y - pos_player2.y) < EPS_Y) {
dir_ball.x = -dir_ball.x;
if(pos_ball.x < pos_player2.x)
dir_ball.y--;
else
dir_ball.y++;
}

if(pos_ball.x > 0 && pos_ball.x < WIDTH - 50)
pos_ball.x += dir_ball.x;
else {
if(pos_ball.x < WIDTH / 2)
player2_score++;
else
player1_score++;

pos_ball = pos_ball_start;
dir_ball.y = 0;
}

pos_ball.y += dir_ball.y;
if(pos_ball.y < 20 || pos_ball.y > HEIGHT - 50)
dir_ball.y = -dir_ball.y; /// collision with top and bottom

/// update positions
sfRectangleShape_setPosition(player1, pos_player1);
sfRectangleShape_setPosition(player2, pos_player2);
Expand Down Expand Up @@ -162,6 +277,7 @@ int main()
sfRectangleShape_destroy(walls);
sfText_destroy(score_text1);
sfText_destroy(score_text2);
sfText_destroy(main_text);
sfFont_destroy(score_font);

sfRenderWindow_destroy(window);
Expand Down

0 comments on commit 3116ebe

Please sign in to comment.