diff options
| author | Ray <[email protected]> | 2017-05-15 22:48:13 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2017-05-15 22:48:13 +0200 |
| commit | c9d6b7e3567bdd02af014e8a7cd1956157307303 (patch) | |
| tree | ccdbd2308677f457e5f93353d20d18875deb0669 /games | |
| parent | aba25e9ba379cdf2e68b65f55dd08ab4e0d713d8 (diff) | |
| parent | b3b828c8bba76116f353a2010e56a74fd56b388c (diff) | |
| download | raylib-c9d6b7e3567bdd02af014e8a7cd1956157307303.tar.gz raylib-c9d6b7e3567bdd02af014e8a7cd1956157307303.zip | |
Merge branch 'develop' of https://github.com/raysan5/raylib into develop
Diffstat (limited to 'games')
| -rw-r--r-- | games/Makefile (renamed from games/makefile) | 0 | ||||
| -rw-r--r-- | games/arkanoid.c | 169 | ||||
| -rw-r--r-- | games/asteroids.c | 95 | ||||
| -rw-r--r-- | games/drturtle/Makefile (renamed from games/drturtle/makefile) | 0 | ||||
| -rw-r--r-- | games/just_do/Makefile (renamed from games/just_do/makefile) | 0 | ||||
| -rw-r--r-- | games/light_my_ritual/Makefile (renamed from games/light_my_ritual/makefile) | 0 | ||||
| -rw-r--r-- | games/skully_escape/Makefile (renamed from games/skully_escape/makefile) | 0 |
7 files changed, 123 insertions, 141 deletions
diff --git a/games/makefile b/games/Makefile index 60f38bff..60f38bff 100644 --- a/games/makefile +++ b/games/Makefile diff --git a/games/arkanoid.c b/games/arkanoid.c index f10f9383..6231fb8b 100644 --- a/games/arkanoid.c +++ b/games/arkanoid.c @@ -77,9 +77,6 @@ static void DrawGame(void); // Draw game (one frame) static void UnloadGame(void); // Unload game static void UpdateDrawFrame(void); // Update and Draw (one frame) -// Additional module functions -static void UpdateBall(void); - //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -165,13 +162,13 @@ void UpdateGame(void) if (!pause) { - // Player movement + // Player movement logic if (IsKeyDown(KEY_LEFT)) player.position.x -= 5; if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2; if (IsKeyDown(KEY_RIGHT)) player.position.x += 5; if ((player.position.x + player.size.x/2) >= screenWidth) player.position.x = screenWidth - player.size.x/2; - // Launch ball + // Ball launching logic if (!ball.active) { if (IsKeyPressed(KEY_SPACE)) @@ -181,7 +178,81 @@ void UpdateGame(void) } } - UpdateBall(); + // Ball movement logic + if (ball.active) + { + ball.position.x += ball.speed.x; + ball.position.y += ball.speed.y; + } + else + { + ball.position = (Vector2){ player.position.x, screenHeight*7/8 - 30 }; + } + + // Collision logic: ball vs walls + if (((ball.position.x + ball.radius) >= screenWidth) || ((ball.position.x - ball.radius) <= 0)) ball.speed.x *= -1; + if ((ball.position.y - ball.radius) <= 0) ball.speed.y *= -1; + if ((ball.position.y + ball.radius) >= screenHeight) + { + ball.speed = (Vector2){ 0, 0 }; + ball.active = false; + + player.life--; + } + + // Collision logic: ball vs player + if (CheckCollisionCircleRec(ball.position, ball.radius, + (Rectangle){ player.position.x - player.size.x/2, player.position.y - player.size.y/2, player.size.x, player.size.y})) + { + if (ball.speed.y > 0) + { + ball.speed.y *= -1; + ball.speed.x = (ball.position.x - player.position.x)/(player.size.x/2)*5; + } + } + + // Collision logic: ball vs bricks + for (int i = 0; i < LINES_OF_BRICKS; i++) + { + for (int j = 0; j < BRICKS_PER_LINE; j++) + { + if (brick[i][j].active) + { + // Hit below + if (((ball.position.y - ball.radius) <= (brick[i][j].position.y + brickSize.y/2)) && + ((ball.position.y - ball.radius) > (brick[i][j].position.y + brickSize.y/2 + ball.speed.y)) && + ((fabs(ball.position.x - brick[i][j].position.x)) < (brickSize.x/2 + ball.radius*2/3)) && (ball.speed.y < 0)) + { + brick[i][j].active = false; + ball.speed.y *= -1; + } + // Hit above + else if (((ball.position.y + ball.radius) >= (brick[i][j].position.y - brickSize.y/2)) && + ((ball.position.y + ball.radius) < (brick[i][j].position.y - brickSize.y/2 + ball.speed.y)) && + ((fabs(ball.position.x - brick[i][j].position.x)) < (brickSize.x/2 + ball.radius*2/3)) && (ball.speed.y > 0)) + { + brick[i][j].active = false; + ball.speed.y *= -1; + } + // Hit left + else if (((ball.position.x + ball.radius) >= (brick[i][j].position.x - brickSize.x/2)) && + ((ball.position.x + ball.radius) < (brick[i][j].position.x - brickSize.x/2 + ball.speed.x)) && + ((fabs(ball.position.y - brick[i][j].position.y)) < (brickSize.y/2 + ball.radius*2/3)) && (ball.speed.x > 0)) + { + brick[i][j].active = false; + ball.speed.x *= -1; + } + // Hit right + else if (((ball.position.x - ball.radius) <= (brick[i][j].position.x + brickSize.x/2)) && + ((ball.position.x - ball.radius) > (brick[i][j].position.x + brickSize.x/2 + ball.speed.x)) && + ((fabs(ball.position.y - brick[i][j].position.y)) < (brickSize.y/2 + ball.radius*2/3)) && (ball.speed.x < 0)) + { + brick[i][j].active = false; + ball.speed.x *= -1; + } + } + } + } // Game over logic if (player.life <= 0) gameOver = true; @@ -260,90 +331,4 @@ void UpdateDrawFrame(void) { UpdateGame(); DrawGame(); -} - -//-------------------------------------------------------------------------------------- -// Additional module functions -//-------------------------------------------------------------------------------------- -static void UpdateBall() -{ - // Update position - if (ball.active) - { - ball.position.x += ball.speed.x; - ball.position.y += ball.speed.y; - } - else - { - ball.position = (Vector2){ player.position.x, screenHeight*7/8 - 30 }; - } - - // Bounce in x - if (((ball.position.x + ball.radius) >= screenWidth) || ((ball.position.x - ball.radius) <= 0)) ball.speed.x *= -1; - - // Bounce in y - if ((ball.position.y - ball.radius) <= 0) ball.speed.y *= -1; - - // Ball reaches bottom of the screen - if ((ball.position.y + ball.radius) >= screenHeight) - { - ball.speed = (Vector2){ 0, 0 }; - ball.active = false; - - player.life--; - } - - // Collision logic: ball vs player - if (CheckCollisionCircleRec(ball.position, ball.radius, - (Rectangle){ player.position.x - player.size.x/2, player.position.y - player.size.y/2, player.size.x, player.size.y})) - { - if (ball.speed.y > 0) - { - ball.speed.y *= -1; - ball.speed.x = (ball.position.x - player.position.x)/(player.size.x/2)*5; - } - } - - // Collision logic: ball vs bricks - for (int i = 0; i < LINES_OF_BRICKS; i++) - { - for (int j = 0; j < BRICKS_PER_LINE; j++) - { - if (brick[i][j].active) - { - // Hit below - if (((ball.position.y - ball.radius) <= (brick[i][j].position.y + brickSize.y/2)) && - ((ball.position.y - ball.radius) > (brick[i][j].position.y + brickSize.y/2 + ball.speed.y)) && - ((fabs(ball.position.x - brick[i][j].position.x)) < (brickSize.x/2 + ball.radius*2/3)) && (ball.speed.y < 0)) - { - brick[i][j].active = false; - ball.speed.y *= -1; - } - // Hit above - else if (((ball.position.y + ball.radius) >= (brick[i][j].position.y - brickSize.y/2)) && - ((ball.position.y + ball.radius) < (brick[i][j].position.y - brickSize.y/2 + ball.speed.y)) && - ((fabs(ball.position.x - brick[i][j].position.x)) < (brickSize.x/2 + ball.radius*2/3)) && (ball.speed.y > 0)) - { - brick[i][j].active = false; - ball.speed.y *= -1; - } - // Hit left - else if (((ball.position.x + ball.radius) >= (brick[i][j].position.x - brickSize.x/2)) && - ((ball.position.x + ball.radius) < (brick[i][j].position.x - brickSize.x/2 + ball.speed.x)) && - ((fabs(ball.position.y - brick[i][j].position.y)) < (brickSize.y/2 + ball.radius*2/3)) && (ball.speed.x > 0)) - { - brick[i][j].active = false; - ball.speed.x *= -1; - } - // Hit right - else if (((ball.position.x - ball.radius) <= (brick[i][j].position.x + brickSize.x/2)) && - ((ball.position.x - ball.radius) > (brick[i][j].position.x + brickSize.x/2 + ball.speed.x)) && - ((fabs(ball.position.y - brick[i][j].position.y)) < (brickSize.y/2 + ball.radius*2/3)) && (ball.speed.x < 0)) - { - brick[i][j].active = false; - ball.speed.x *= -1; - } - } - } - } }
\ No newline at end of file diff --git a/games/asteroids.c b/games/asteroids.c index 53ebbd8e..11063889 100644 --- a/games/asteroids.c +++ b/games/asteroids.c @@ -82,9 +82,9 @@ static Meteor bigMeteor[MAX_BIG_METEORS]; static Meteor mediumMeteor[MAX_MEDIUM_METEORS]; static Meteor smallMeteor[MAX_SMALL_METEORS]; -static int countMediumMeteors; -static int countSmallMeteors; -static int meteorsDestroyed; +static int midMeteorsCount; +static int smallMeteorsCount; +static int destroyedMeteorsCount; //------------------------------------------------------------------------------------ // Module Functions Declaration (local) @@ -95,7 +95,6 @@ static void DrawGame(void); // Draw game (one frame) static void UnloadGame(void); // Unload game static void UpdateDrawFrame(void); // Update and Draw (one frame) -static void InitShoot(Shoot shoot); static void DrawSpaceship(Vector2 position, float rotation, Color color); //------------------------------------------------------------------------------------ @@ -164,7 +163,7 @@ void InitGame(void) player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12}; player.color = LIGHTGRAY; - meteorsDestroyed = 0; + destroyedMeteorsCount = 0; // Initialization shoot for (int i = 0; i < PLAYER_MAX_SHOOTS; i++) @@ -237,8 +236,8 @@ void InitGame(void) smallMeteor[i].color = BLUE; } - countMediumMeteors = 0; - countSmallMeteors = 0; + midMeteorsCount = 0; + smallMeteorsCount = 0; } // Update game (one frame) @@ -250,17 +249,15 @@ void UpdateGame(void) if (!pause) { - // Player logic - - // Rotation + // Player logic: rotation if (IsKeyDown(KEY_LEFT)) player.rotation -= 5; if (IsKeyDown(KEY_RIGHT)) player.rotation += 5; - // Speed + // Player logic: speed player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED; player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED; - // Controller + // Player logic: acceleration if (IsKeyDown(KEY_UP)) { if (player.acceleration < 1) player.acceleration += 0.04f; @@ -276,17 +273,17 @@ void UpdateGame(void) else if (player.acceleration < 0) player.acceleration = 0; } - // Movement + // Player logic: movement player.position.x += (player.speed.x*player.acceleration); player.position.y -= (player.speed.y*player.acceleration); - // Wall behaviour for player + // Collision logic: player vs walls if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight); else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight; if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight); else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight; - // Activation of shoot + // Player shoot logic if (IsKeyPressed(KEY_SPACE)) { for (int i = 0; i < PLAYER_MAX_SHOOTS; i++) @@ -318,7 +315,7 @@ void UpdateGame(void) shoot[i].position.x += shoot[i].speed.x; shoot[i].position.y -= shoot[i].speed.y; - // Wall behaviour for shoot + // Collision logic: shoot vs walls if (shoot[i].position.x > screenWidth + shoot[i].radius) { shoot[i].active = false; @@ -351,7 +348,7 @@ void UpdateGame(void) } } - // Collision Player to meteors + // Collision logic: player vs meteors player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12}; for (int a = 0; a < MAX_BIG_METEORS; a++) @@ -369,16 +366,16 @@ void UpdateGame(void) if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true; } - // Meteor logic + // Meteors logic: big meteors for (int i = 0; i < MAX_BIG_METEORS; i++) { if (bigMeteor[i].active) { - // movement + // Movement bigMeteor[i].position.x += bigMeteor[i].speed.x; bigMeteor[i].position.y += bigMeteor[i].speed.y; - // wall behaviour + // Collision logic: meteor vs wall if (bigMeteor[i].position.x > screenWidth + bigMeteor[i].radius) bigMeteor[i].position.x = -(bigMeteor[i].radius); else if (bigMeteor[i].position.x < 0 - bigMeteor[i].radius) bigMeteor[i].position.x = screenWidth + bigMeteor[i].radius; if (bigMeteor[i].position.y > screenHeight + bigMeteor[i].radius) bigMeteor[i].position.y = -(bigMeteor[i].radius); @@ -386,15 +383,16 @@ void UpdateGame(void) } } + // Meteors logic: medium meteors for (int i = 0; i < MAX_MEDIUM_METEORS; i++) { if (mediumMeteor[i].active) { - // movement + // Movement mediumMeteor[i].position.x += mediumMeteor[i].speed.x; mediumMeteor[i].position.y += mediumMeteor[i].speed.y; - // wall behaviour + // Collision logic: meteor vs wall if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius); else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius; if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius); @@ -402,15 +400,16 @@ void UpdateGame(void) } } + // Meteors logic: small meteors for (int i = 0; i < MAX_SMALL_METEORS; i++) { if (smallMeteor[i].active) { - // movement + // Movement smallMeteor[i].position.x += smallMeteor[i].speed.x; smallMeteor[i].position.y += smallMeteor[i].speed.y; - // wall behaviour + // Collision logic: meteor vs wall if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius); else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius; if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius); @@ -418,7 +417,7 @@ void UpdateGame(void) } } - // Collision behaviour + // Collision logic: player-shoots vs meteors for (int i = 0; i < PLAYER_MAX_SHOOTS; i++) { if ((shoot[i].active)) @@ -430,31 +429,30 @@ void UpdateGame(void) shoot[i].active = false; shoot[i].lifeSpawn = 0; bigMeteor[a].active = false; - meteorsDestroyed++; + destroyedMeteorsCount++; + for (int j = 0; j < 2; j ++) { - if (countMediumMeteors%2 == 0) + if (midMeteorsCount%2 == 0) { - mediumMeteor[countMediumMeteors].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y}; - mediumMeteor[countMediumMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1}; + mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y}; + mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1}; } else { - mediumMeteor[countMediumMeteors].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y}; - mediumMeteor[countMediumMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED}; + mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y}; + mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED}; } - mediumMeteor[countMediumMeteors].active = true; - countMediumMeteors ++; + mediumMeteor[midMeteorsCount].active = true; + midMeteorsCount ++; } //bigMeteor[a].position = (Vector2){-100, -100}; bigMeteor[a].color = RED; a = MAX_BIG_METEORS; } } - } - if ((shoot[i].active)) - { + for (int b = 0; b < MAX_MEDIUM_METEORS; b++) { if (mediumMeteor[b].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, mediumMeteor[b].position, mediumMeteor[b].radius)) @@ -462,31 +460,30 @@ void UpdateGame(void) shoot[i].active = false; shoot[i].lifeSpawn = 0; mediumMeteor[b].active = false; - meteorsDestroyed++; + destroyedMeteorsCount++; + for (int j = 0; j < 2; j ++) { - if (countSmallMeteors%2 == 0) + if (smallMeteorsCount%2 == 0) { - smallMeteor[countSmallMeteors].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y}; - smallMeteor[countSmallMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1}; + smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y}; + smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1}; } else { - smallMeteor[countSmallMeteors].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y}; - smallMeteor[countSmallMeteors].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED}; + smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y}; + smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED}; } - smallMeteor[countSmallMeteors].active = true; - countSmallMeteors ++; + smallMeteor[smallMeteorsCount].active = true; + smallMeteorsCount ++; } //mediumMeteor[b].position = (Vector2){-100, -100}; mediumMeteor[b].color = GREEN; b = MAX_MEDIUM_METEORS; } } - } - if ((shoot[i].active)) - { + for (int c = 0; c < MAX_SMALL_METEORS; c++) { if (smallMeteor[c].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, smallMeteor[c].position, smallMeteor[c].radius)) @@ -494,7 +491,7 @@ void UpdateGame(void) shoot[i].active = false; shoot[i].lifeSpawn = 0; smallMeteor[c].active = false; - meteorsDestroyed++; + destroyedMeteorsCount++; smallMeteor[c].color = YELLOW; // smallMeteor[c].position = (Vector2){-100, -100}; c = MAX_SMALL_METEORS; @@ -504,7 +501,7 @@ void UpdateGame(void) } } - if (meteorsDestroyed == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true; + if (destroyedMeteorsCount == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true; } else { diff --git a/games/drturtle/makefile b/games/drturtle/Makefile index 89821929..89821929 100644 --- a/games/drturtle/makefile +++ b/games/drturtle/Makefile diff --git a/games/just_do/makefile b/games/just_do/Makefile index 6dc096fb..6dc096fb 100644 --- a/games/just_do/makefile +++ b/games/just_do/Makefile diff --git a/games/light_my_ritual/makefile b/games/light_my_ritual/Makefile index 7bede523..7bede523 100644 --- a/games/light_my_ritual/makefile +++ b/games/light_my_ritual/Makefile diff --git a/games/skully_escape/makefile b/games/skully_escape/Makefile index 70f7f5a3..70f7f5a3 100644 --- a/games/skully_escape/makefile +++ b/games/skully_escape/Makefile |
