commit
2d1b2efb92
35 changed files with 1268 additions and 162 deletions
39
CHANGELOG
39
CHANGELOG
|
|
@ -1,11 +1,48 @@
|
||||||
changelog
|
changelog
|
||||||
---------
|
---------
|
||||||
|
|
||||||
Current Release: raylib 1.3.0 (03 September 2015)
|
Current Release: raylib 1.4.0 (22 February 2016)
|
||||||
|
|
||||||
NOTE: Only versions marked as 'Release' are available in installer, updates are only available as source.
|
NOTE: Only versions marked as 'Release' are available in installer, updates are only available as source.
|
||||||
NOTE: Current Release includes all previous updates.
|
NOTE: Current Release includes all previous updates.
|
||||||
|
|
||||||
|
-----------------------------------------------
|
||||||
|
Release: raylib 1.4.0 (22 February 2016)
|
||||||
|
-----------------------------------------------
|
||||||
|
NOTE:
|
||||||
|
This version supposed another big improvement for raylib, inlcuding new modules and new features.
|
||||||
|
More than 30 new functions have been added to previous raylib version.
|
||||||
|
Around 8 new examples and +10 new game samples have been added.
|
||||||
|
|
||||||
|
BIG changes:
|
||||||
|
[textures] IMAGE MANIPULATION: Functions to crop, resize, colorize, flip, dither and even draw image-to-image or text-to-image.
|
||||||
|
[text] SPRITEFONT SUPPORT: Added support for AngelCode fonts (.fnt) and TrueType fonts (.ttf).
|
||||||
|
[gestures] REDESIGN: Gestures system simplified and prepared to process generic touch events, including mouse events (multiplatform).
|
||||||
|
[physac] NEW MODULE: Basic 2D physics support, use colliders and rigidbodies; apply forces to physic objects.
|
||||||
|
|
||||||
|
other changes:
|
||||||
|
[rlgl] Removed GLEW library dependency, now using GLAD
|
||||||
|
[rlgl] Implemented alternative to glGetTexImage() on OpenGL ES
|
||||||
|
[rlgl] Using depth data on batch drawing
|
||||||
|
[rlgl] Reviewed glReadPixels() function
|
||||||
|
[core][rlgl] Reviewed raycast system, now 3D picking works
|
||||||
|
[core] Android: Reviewed Android App cycle, paused if inactive
|
||||||
|
[shaders] Implemented Blinn-Phong lighting shading model
|
||||||
|
[textures] Implemented Floyd-Steinberg dithering - ImageDither()
|
||||||
|
[text] Added line-break support to DrawText()
|
||||||
|
[text] Added TrueType Fonts support (using stb_truetype)
|
||||||
|
[models] Implement function: CalculateBoundingBox(Mesh mesh)
|
||||||
|
[models] Added functions to check Ray collisions
|
||||||
|
[models] Improve map resolution control on LoadHeightmap()
|
||||||
|
[camera] Corrected small-glitch on zoom-in with mouse-wheel
|
||||||
|
[gestures] Implemented SetGesturesEnabled() to enable only some gestures
|
||||||
|
[gestures] Implemented GetElapsedTime() on Windows system
|
||||||
|
[gestures] Support mouse gestures for desktop platforms
|
||||||
|
[raymath] Complete review of the module and converted to header-only
|
||||||
|
[easings] Added new module for easing animations
|
||||||
|
[stb] Updated to latest headers versions
|
||||||
|
[*] Lots of tweaks around
|
||||||
|
|
||||||
-----------------------------------------------
|
-----------------------------------------------
|
||||||
Release: raylib 1.3.0 (01 September 2015)
|
Release: raylib 1.3.0 (01 September 2015)
|
||||||
-----------------------------------------------
|
-----------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,8 @@ contact
|
||||||
* Webpage: [http://www.raylib.com](http://www.raylib.com)
|
* Webpage: [http://www.raylib.com](http://www.raylib.com)
|
||||||
* Twitter: [http://www.twitter.com/raysan5](http://www.twitter.com/raysan5)
|
* Twitter: [http://www.twitter.com/raysan5](http://www.twitter.com/raysan5)
|
||||||
* Facebook: [http://www.facebook.com/raylibgames](http://www.facebook.com/raylibgames)
|
* Facebook: [http://www.facebook.com/raylibgames](http://www.facebook.com/raylibgames)
|
||||||
|
* Reddit: [https://www.reddit.com/r/raylib](https://www.reddit.com/r/raylib)
|
||||||
|
* Twitch: [http://www.twitch.tv/raysan5](http://www.twitch.tv/raysan5)
|
||||||
|
* Patreon: [https://www.patreon.com/raysan5](https://www.patreon.com/raysan5)
|
||||||
|
|
||||||
|
[raysan5]: mailto:raysan5@gmail.com "Ramon Santamaria - Ray San"
|
||||||
[raysan5]: mailto:raysan@raysanweb.com "Ramon Santamaria - Ray San"
|
|
||||||
|
|
|
||||||
31
README.md
31
README.md
|
|
@ -6,7 +6,6 @@ about
|
||||||
raylib is a simple and easy-to-use library to learn videogames programming.
|
raylib is a simple and easy-to-use library to learn videogames programming.
|
||||||
|
|
||||||
raylib is highly inspired by Borland BGI graphics lib and by XNA framework.
|
raylib is highly inspired by Borland BGI graphics lib and by XNA framework.
|
||||||
Allegro and SDL have also been analyzed for reference.
|
|
||||||
|
|
||||||
NOTE for ADVENTURERS: raylib is a programming library to learn videogames programming;
|
NOTE for ADVENTURERS: raylib is a programming library to learn videogames programming;
|
||||||
no fancy interface, no visual helpers, no auto-debugging... just coding in the most
|
no fancy interface, no visual helpers, no auto-debugging... just coding in the most
|
||||||
|
|
@ -15,7 +14,7 @@ pure spartan-programmers way. Are you ready to learn? Jump to [code examples!](h
|
||||||
history
|
history
|
||||||
-------
|
-------
|
||||||
|
|
||||||
I've developed videogames for some years and last year I had to taught videogames development
|
I've developed videogames for some years and 4 year ago I started teaching videogames development
|
||||||
to young people with artistic profile, most of them had never written a single line of code.
|
to young people with artistic profile, most of them had never written a single line of code.
|
||||||
|
|
||||||
I started with C language basis and, after searching for the most simple and easy-to-use library to teach
|
I started with C language basis and, after searching for the most simple and easy-to-use library to teach
|
||||||
|
|
@ -102,25 +101,28 @@ Lots of code changes and lot of testing have concluded in this amazing new rayli
|
||||||
notes on raylib 1.4
|
notes on raylib 1.4
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
On February 2016, after 4 months of raylib 1.3 release, it comes raylib 1.4.
|
On February 2016, after 4 months of raylib 1.3 release, it comes raylib 1.4. For this new version, lots of parts of the library have been reviewed to better accomodate the shaders system.
|
||||||
|
Lots of bugs have been solved and some missing features have been added to the library.
|
||||||
|
|
||||||
Lots of parts of the library have been reviewed to better accomodate to shaders systems and multiple new features have been added.
|
SpriteFonts system has been improved, adding support for AngelCode fonts (.fnt) and TTF fonts (using stb_truetype helper library).
|
||||||
|
|
||||||
SpriteFonts system has been improved, adding support for AngelCode fonts (.fnt) and TTF fonts (using stb_truetype).
|
A set of Image manipulation functions have been added to crop, resize, colorize, flip, dither and even draw image-to-image or text-to-image.
|
||||||
|
|
||||||
Finally, raycast system for 3D picking is working, including some ray collision-detection functions.
|
[raymath](https://github.com/raysan5/raylib/blob/develop/src/raymath.h) module has been reviewed and converted to a header-only file for easier portability, functions can also be used as inline.
|
||||||
|
|
||||||
A set of Image manipulation functions have been added to crop, resize, colorize, dither and even draw image-to-image or text-to-image.
|
Raycast system for 3D picking is now fully functional, including some ray collision-detection functions.
|
||||||
|
|
||||||
Two new functions added for persistent data storage.
|
[raymath](https://github.com/raysan5/raylib/blob/develop/src/gestures.c) module has redesigned and simplified, now it can process touch events from any source, including mouse. This way, gestures system can be used for any platform.
|
||||||
|
|
||||||
New [physac](https://github.com/raysan5/raylib/blob/develop/src/physac.h) physics module!
|
Raspberry Pi input system has been redesigned to better read raw inputs using generic Linux event handlers (keyboard:`stdin`, mouse:`/dev/input/mouse0`, gamepad:`/dev/input/js0`). Gamepad support has been added.
|
||||||
|
|
||||||
Complete LUA scripting support to allow raylib usage from LUA and LUA scripts support within raylib.
|
Two new functions added for persistent data storage. Now raylib user can save and load game data in a file.
|
||||||
|
|
||||||
Up to 8 new examples have been added to show the new raylib features.
|
[physac](https://github.com/raysan5/raylib/blob/develop/src/physac.h) physics module added. Basic 2D physics support.
|
||||||
|
|
||||||
Lots of code changes and lot of testing have concluded in this amazing new raylib 1.4.
|
Up to 8 new examples have been added to show the new raylib features... and +10 game samples to learn how to create some classic games like Arkanoid, Asteroids, Missile Commander, Snake or Tetris.
|
||||||
|
|
||||||
|
Lots of code changes and lots of hours of hard work have concluded in this amazing new raylib 1.4.
|
||||||
|
|
||||||
features
|
features
|
||||||
--------
|
--------
|
||||||
|
|
@ -130,7 +132,7 @@ features
|
||||||
* Hardware accelerated with OpenGL (1.1, 3.3 or ES2)
|
* Hardware accelerated with OpenGL (1.1, 3.3 or ES2)
|
||||||
* Unique OpenGL abstraction layer (usable as standalone module): [rlgl](https://github.com/raysan5/raylib/blob/master/src/rlgl.c)
|
* Unique OpenGL abstraction layer (usable as standalone module): [rlgl](https://github.com/raysan5/raylib/blob/master/src/rlgl.c)
|
||||||
* Powerful fonts module with multiple SpriteFonts formats support (XNA bitmap fonts, AngelCode fonts, TTF)
|
* Powerful fonts module with multiple SpriteFonts formats support (XNA bitmap fonts, AngelCode fonts, TTF)
|
||||||
* Outstanding texture formats support, including compressed formats
|
* Outstanding texture formats support, including compressed formats (DXT, ETC, PVRT, ASTC)
|
||||||
* Basic 3d support for Shapes, Models, Billboards, Heightmaps and Cubicmaps
|
* Basic 3d support for Shapes, Models, Billboards, Heightmaps and Cubicmaps
|
||||||
* Powerful math module for Vector and Matrix operations: [raymath](https://github.com/raysan5/raylib/blob/master/src/raymath.c)
|
* Powerful math module for Vector and Matrix operations: [raymath](https://github.com/raysan5/raylib/blob/master/src/raymath.c)
|
||||||
* Audio loading and playing with streaming support (WAV and OGG)
|
* Audio loading and playing with streaming support (WAV and OGG)
|
||||||
|
|
@ -181,6 +183,9 @@ contact
|
||||||
* Webpage: [http://www.raylib.com](http://www.raylib.com)
|
* Webpage: [http://www.raylib.com](http://www.raylib.com)
|
||||||
* Twitter: [http://www.twitter.com/raysan5](http://www.twitter.com/raysan5)
|
* Twitter: [http://www.twitter.com/raysan5](http://www.twitter.com/raysan5)
|
||||||
* Facebook: [http://www.facebook.com/raylibgames](http://www.facebook.com/raylibgames)
|
* Facebook: [http://www.facebook.com/raylibgames](http://www.facebook.com/raylibgames)
|
||||||
|
* Reddit: [https://www.reddit.com/r/raylib](https://www.reddit.com/r/raylib)
|
||||||
|
* Twitch: [http://www.twitch.tv/raysan5](http://www.twitch.tv/raysan5)
|
||||||
|
* Patreon: [https://www.patreon.com/raysan5](https://www.patreon.com/raysan5)
|
||||||
|
|
||||||
If you are using raylib and you enjoy it, please, [let me know][raysan5].
|
If you are using raylib and you enjoy it, please, [let me know][raysan5].
|
||||||
|
|
||||||
|
|
|
||||||
41
ROADMAP.md
41
ROADMAP.md
|
|
@ -1,28 +1,37 @@
|
||||||
roadmap
|
roadmap
|
||||||
-------
|
-------
|
||||||
|
|
||||||
Current version of raylib is quite complete and functional but there is still a lot of things I would like to improve.
|
Current version of raylib is quite complete and functional but there is still a lot of things to add and improve.
|
||||||
Here it is a list of features I would like to add and functions to improve.
|
Here it is a wish list of features I would like to add and functions to improve.
|
||||||
|
|
||||||
Around the source code there are multiple TODO points with pending revisions/bugs and here it is a list of desired features.
|
Note that around the raylib source code there are multiple TODO points with pending revisions/bugs. Check [GITHUB ISSUES][issues] for further details!
|
||||||
|
|
||||||
raylib v1.4
|
raylib 1.5
|
||||||
|
|
||||||
[DONE] TTF fonts support (using stb_truetype)
|
Redesign Shaders/Textures system, use Materials
|
||||||
[DONE] Raycast system for 3D picking (including collisions detection)
|
Redesign physics module (physac)
|
||||||
[DONE] Floyd-Steinberg dithering on 16bit image format conversion
|
Basic GPU stats sytem (memory, draws, time...)
|
||||||
[DONE] Basic image manipulation functions (crop, resize, draw...)
|
Procedural image generation functions (spot, gradient, noise...)
|
||||||
[DONE] Storage load/save data functionality
|
Procedural mesh generation functions (cube, cone, sphere...)
|
||||||
[DONE] Physics module
|
Touch-based camera controls for Android
|
||||||
[IN PROGRESS] LUA scripting support (wrapper to lua lib)
|
Skybox and Fog support
|
||||||
- Remove GLEW dependency (use another solution... glad?)
|
[IN PROGRESS] LUA scripting support (wrapper to lua lib)
|
||||||
- Basic image procedural generation (spot, gradient, noise...)
|
|
||||||
- Basic GPU stats sytem (memory, draws, time...)
|
|
||||||
|
|
||||||
|
raylib 1.4
|
||||||
|
|
||||||
Check [GITHUB ISSUES][issues] for further details on implementation status for this features!
|
[DONE] TTF fonts support (using stb_truetype)
|
||||||
|
[DONE] Raycast system for 3D picking (including collisions detection)
|
||||||
|
[DONE] Floyd-Steinberg dithering on 16bit image format conversion
|
||||||
|
[DONE] Basic image manipulation functions (crop, resize, draw...)
|
||||||
|
[DONE] Storage load/save data functionality
|
||||||
|
[DONE] Add Physics module (physac)
|
||||||
|
[DONE] Remove GLEW dependency -> Replaced by GLAD
|
||||||
|
[DONE] Redesign Raspberry PI inputs system
|
||||||
|
[DONE] Redesign gestures module to be multiplatform
|
||||||
|
[DONE] Module raymath as header-only and functions inline
|
||||||
|
[DONE] Add Easings module (easings.h)
|
||||||
|
|
||||||
Any feature missing? Do you have a request? [Let me know!][raysan5]
|
Any feature missing? Do you have a request? [Let me know!][raysan5]
|
||||||
|
|
||||||
[raysan5]: mailto:raysan@raysanweb.com "Ramon Santamaria - Ray San"
|
[raysan5]: mailto:raysan5@gmail.com "Ramon Santamaria - Ray San"
|
||||||
[isssues]: https://github.com/raysan5/raylib/issues
|
[isssues]: https://github.com/raysan5/raylib/issues
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 648 KiB After Width: | Height: | Size: 379 KiB |
|
|
@ -25,14 +25,14 @@ int main()
|
||||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||||
|
|
||||||
Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
|
Image cat = LoadImage("resources/cat.png"); // Load image in CPU memory (RAM)
|
||||||
ImageCrop(&cat, (Rectangle){ 170, 120, 280, 380 }); // Crop an image piece
|
ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 }); // Crop an image piece
|
||||||
ImageFlipHorizontal(&cat); // Flip cropped image horizontally
|
ImageFlipHorizontal(&cat); // Flip cropped image horizontally
|
||||||
ImageResize(&cat, 150, 200); // Resize flipped-cropped image
|
ImageResize(&cat, 150, 200); // Resize flipped-cropped image
|
||||||
|
|
||||||
Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
|
Image parrots = LoadImage("resources/parrots.png"); // Load image in CPU memory (RAM)
|
||||||
|
|
||||||
// Draw one image over the other with a scaling of 1.5f
|
// Draw one image over the other with a scaling of 1.5f
|
||||||
ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height}, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
|
ImageDraw(&parrots, cat, (Rectangle){ 0, 0, cat.width, cat.height }, (Rectangle){ 30, 40, cat.width*1.5f, cat.height*1.5f });
|
||||||
ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
|
ImageCrop(&parrots, (Rectangle){ 0, 50, parrots.width, parrots.height - 100 }); // Crop resulting image
|
||||||
|
|
||||||
UnloadImage(cat); // Unload image from RAM
|
UnloadImage(cat); // Unload image from RAM
|
||||||
|
|
|
||||||
349
games/arkanoid.c
Normal file
349
games/arkanoid.c
Normal file
|
|
@ -0,0 +1,349 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: arkanoid
|
||||||
|
*
|
||||||
|
* Sample game Marc Palau and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Some Defines
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define PLAYER_MAX_LIFE 5
|
||||||
|
#define LINES_OF_BRICKS 5
|
||||||
|
#define BRICKS_PER_LINE 20
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
typedef enum GameScreen { LOGO, TITLE, GAMEPLAY, ENDING } GameScreen;
|
||||||
|
|
||||||
|
typedef struct Player {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 size;
|
||||||
|
int life;
|
||||||
|
} Player;
|
||||||
|
|
||||||
|
typedef struct Ball {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
int radius;
|
||||||
|
bool active;
|
||||||
|
} Ball;
|
||||||
|
|
||||||
|
typedef struct Brick {
|
||||||
|
Vector2 position;
|
||||||
|
bool active;
|
||||||
|
} Brick;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static int framesCounter;
|
||||||
|
static bool gameOver;
|
||||||
|
static bool pause;
|
||||||
|
|
||||||
|
static Player player;
|
||||||
|
static Ball ball;
|
||||||
|
static Brick brick[LINES_OF_BRICKS][BRICKS_PER_LINE];
|
||||||
|
static Vector2 brickSize;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
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
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: arkanoid");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
brickSize = (Vector2){ GetScreenWidth()/BRICKS_PER_LINE, 40 };
|
||||||
|
|
||||||
|
// Initialize player
|
||||||
|
player.position = (Vector2){ screenWidth/2, screenHeight*7/8 };
|
||||||
|
player.size = (Vector2){ screenWidth/10, 20 };
|
||||||
|
player.life = PLAYER_MAX_LIFE;
|
||||||
|
|
||||||
|
// Initialize ball
|
||||||
|
ball.position = (Vector2){ screenWidth/2, screenHeight*7/8 - 30 };
|
||||||
|
ball.speed = (Vector2){ 0, 0 };
|
||||||
|
ball.radius = 7;
|
||||||
|
ball.active = false;
|
||||||
|
|
||||||
|
// Initialize bricks
|
||||||
|
int initialDownPosition = 50;
|
||||||
|
|
||||||
|
for (int i = 0; i < LINES_OF_BRICKS; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < BRICKS_PER_LINE; j++)
|
||||||
|
{
|
||||||
|
brick[i][j].position = (Vector2){ j*brickSize.x + brickSize.x/2, i*brickSize.y + initialDownPosition };
|
||||||
|
brick[i][j].active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
// Player movement
|
||||||
|
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
|
||||||
|
if (!ball.active)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_SPACE))
|
||||||
|
{
|
||||||
|
ball.active = true;
|
||||||
|
ball.speed = (Vector2){ 0, -5 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateBall();
|
||||||
|
|
||||||
|
// Game over logic
|
||||||
|
if (player.life <= 0) gameOver = true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gameOver = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < LINES_OF_BRICKS; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < BRICKS_PER_LINE; j++)
|
||||||
|
{
|
||||||
|
if (brick[i][j].active) gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
// Draw player bar
|
||||||
|
DrawRectangle(player.position.x - player.size.x/2, player.position.y - player.size.y/2, player.size.x, player.size.y, BLACK);
|
||||||
|
|
||||||
|
// Draw player lives
|
||||||
|
for (int i = 0; i < player.life; i++) DrawRectangle(20 + 40*i, screenHeight - 30, 35, 10, LIGHTGRAY);
|
||||||
|
|
||||||
|
// Draw ball
|
||||||
|
DrawCircleV(ball.position, ball.radius, MAROON);
|
||||||
|
|
||||||
|
// Draw bricks
|
||||||
|
for (int i = 0; i < LINES_OF_BRICKS; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < BRICKS_PER_LINE; j++)
|
||||||
|
{
|
||||||
|
if (brick[i][j].active)
|
||||||
|
{
|
||||||
|
if ((i + j) % 2 == 0) DrawRectangle(brick[i][j].position.x - brickSize.x/2, brick[i][j].position.y - brickSize.y/2, brickSize.x, brickSize.y, GRAY);
|
||||||
|
else DrawRectangle(brick[i][j].position.x - brickSize.x/2, brick[i][j].position.y - brickSize.y/2, brickSize.x, brickSize.y, DARKGRAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
579
games/asteroids.c
Normal file
579
games/asteroids.c
Normal file
|
|
@ -0,0 +1,579 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: asteroids
|
||||||
|
*
|
||||||
|
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Some Defines
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
#define PLAYER_BASE_SIZE 20.0f
|
||||||
|
#define PLAYER_SPEED 6.0f
|
||||||
|
#define PLAYER_MAX_SHOOTS 10
|
||||||
|
|
||||||
|
#define METEORS_SPEED 2
|
||||||
|
#define MAX_BIG_METEORS 4
|
||||||
|
#define MAX_MEDIUM_METEORS 8
|
||||||
|
#define MAX_SMALL_METEORS 16
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef struct Player {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
float acceleration;
|
||||||
|
float rotation;
|
||||||
|
Vector3 collider;
|
||||||
|
Color color;
|
||||||
|
} Player;
|
||||||
|
|
||||||
|
typedef struct Shoot {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
float radius;
|
||||||
|
float rotation;
|
||||||
|
int lifeSpawn;
|
||||||
|
bool active;
|
||||||
|
Color color;
|
||||||
|
} Shoot;
|
||||||
|
|
||||||
|
typedef struct Meteor {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
float radius;
|
||||||
|
bool active;
|
||||||
|
Color color;
|
||||||
|
} Meteor;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static int framesCounter;
|
||||||
|
static bool gameOver;
|
||||||
|
static bool pause;
|
||||||
|
static bool victory;
|
||||||
|
|
||||||
|
// NOTE: Defined triangle is isosceles with common angles of 70 degrees.
|
||||||
|
static float shipHeight;
|
||||||
|
|
||||||
|
static Player player;
|
||||||
|
static Shoot shoot[PLAYER_MAX_SHOOTS];
|
||||||
|
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;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
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);
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: asteroids");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
int posx, posy;
|
||||||
|
int velx, vely;
|
||||||
|
bool correctRange = false;
|
||||||
|
victory = false;
|
||||||
|
pause = false;
|
||||||
|
|
||||||
|
shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
|
||||||
|
|
||||||
|
// Initialization player
|
||||||
|
player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
|
||||||
|
player.speed = (Vector2){0, 0};
|
||||||
|
player.acceleration = 0;
|
||||||
|
player.rotation = 0;
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Initialization shoot
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
shoot[i].position = (Vector2){0, 0};
|
||||||
|
shoot[i].speed = (Vector2){0, 0};
|
||||||
|
shoot[i].radius = 2;
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
shoot[i].color = WHITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
||||||
|
{
|
||||||
|
posx = GetRandomValue(0, screenWidth);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
|
||||||
|
posy = GetRandomValue(0, screenHeight);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bigMeteor[i].position = (Vector2){posx, posy};
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (velx == 0 && vely == 0)
|
||||||
|
{
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
}
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bigMeteor[i].speed = (Vector2){velx, vely};
|
||||||
|
bigMeteor[i].radius = 40;
|
||||||
|
bigMeteor[i].active = true;
|
||||||
|
bigMeteor[i].color = BLUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
mediumMeteor[i].position = (Vector2){-100, -100};
|
||||||
|
mediumMeteor[i].speed = (Vector2){0,0};
|
||||||
|
mediumMeteor[i].radius = 20;
|
||||||
|
mediumMeteor[i].active = false;
|
||||||
|
mediumMeteor[i].color = BLUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
smallMeteor[i].position = (Vector2){-100, -100};
|
||||||
|
smallMeteor[i].speed = (Vector2){0,0};
|
||||||
|
smallMeteor[i].radius = 10;
|
||||||
|
smallMeteor[i].active = false;
|
||||||
|
smallMeteor[i].color = BLUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
countMediumMeteors = 0;
|
||||||
|
countSmallMeteors = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
// Player logic
|
||||||
|
|
||||||
|
// Rotation
|
||||||
|
if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
|
||||||
|
if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
|
||||||
|
|
||||||
|
// Speed
|
||||||
|
player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
|
||||||
|
// Controller
|
||||||
|
if (IsKeyDown(KEY_UP))
|
||||||
|
{
|
||||||
|
if (player.acceleration < 1) player.acceleration += 0.04f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (player.acceleration > 0) player.acceleration -= 0.02f;
|
||||||
|
else if (player.acceleration < 0) player.acceleration = 0;
|
||||||
|
}
|
||||||
|
if (IsKeyDown(KEY_DOWN))
|
||||||
|
{
|
||||||
|
if (player.acceleration > 0) player.acceleration -= 0.04f;
|
||||||
|
else if (player.acceleration < 0) player.acceleration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Movement
|
||||||
|
player.position.x += (player.speed.x*player.acceleration);
|
||||||
|
player.position.y -= (player.speed.y*player.acceleration);
|
||||||
|
|
||||||
|
// Wall behaviour for player
|
||||||
|
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
|
||||||
|
if (IsKeyPressed(KEY_SPACE))
|
||||||
|
{
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
if (!shoot[i].active)
|
||||||
|
{
|
||||||
|
shoot[i].position = (Vector2){ player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight) };
|
||||||
|
shoot[i].active = true;
|
||||||
|
shoot[i].speed.x = 1.5*sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
shoot[i].speed.y = 1.5*cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
shoot[i].rotation = player.rotation;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shoot life timer
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
if (shoot[i].active) shoot[i].lifeSpawn++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shot logic
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
if (shoot[i].active)
|
||||||
|
{
|
||||||
|
// Movement
|
||||||
|
shoot[i].position.x += shoot[i].speed.x;
|
||||||
|
shoot[i].position.y -= shoot[i].speed.y;
|
||||||
|
|
||||||
|
// Wall behaviour for shoot
|
||||||
|
if (shoot[i].position.x > screenWidth + shoot[i].radius)
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
}
|
||||||
|
else if (shoot[i].position.x < 0 - shoot[i].radius)
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
}
|
||||||
|
if (shoot[i].position.y > screenHeight + shoot[i].radius)
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
}
|
||||||
|
else if (shoot[i].position.y < 0 - shoot[i].radius)
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Life of shoot
|
||||||
|
if (shoot[i].lifeSpawn >= 60)
|
||||||
|
{
|
||||||
|
shoot[i].position = (Vector2){0, 0};
|
||||||
|
shoot[i].speed = (Vector2){0, 0};
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
shoot[i].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collision Player to 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++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, bigMeteor[a].position, bigMeteor[a].radius) && bigMeteor[a].active) gameOver = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int a = 0; a < MAX_SMALL_METEORS; a++)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (bigMeteor[i].active)
|
||||||
|
{
|
||||||
|
// movement
|
||||||
|
bigMeteor[i].position.x += bigMeteor[i].speed.x;
|
||||||
|
bigMeteor[i].position.y += bigMeteor[i].speed.y;
|
||||||
|
|
||||||
|
// wall behaviour
|
||||||
|
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);
|
||||||
|
else if (bigMeteor[i].position.y < 0 - bigMeteor[i].radius) bigMeteor[i].position.y = screenHeight + bigMeteor[i].radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (mediumMeteor[i].active)
|
||||||
|
{
|
||||||
|
// movement
|
||||||
|
mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
|
||||||
|
mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
|
||||||
|
|
||||||
|
// wall behaviour
|
||||||
|
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);
|
||||||
|
else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (smallMeteor[i].active)
|
||||||
|
{
|
||||||
|
// movement
|
||||||
|
smallMeteor[i].position.x += smallMeteor[i].speed.x;
|
||||||
|
smallMeteor[i].position.y += smallMeteor[i].speed.y;
|
||||||
|
|
||||||
|
// wall behaviour
|
||||||
|
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);
|
||||||
|
else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collision behaviour
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
if ((shoot[i].active))
|
||||||
|
{
|
||||||
|
for (int a = 0; a < MAX_BIG_METEORS; a++)
|
||||||
|
{
|
||||||
|
if (bigMeteor[a].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, bigMeteor[a].position, bigMeteor[a].radius))
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
bigMeteor[a].active = false;
|
||||||
|
meteorsDestroyed++;
|
||||||
|
for (int j = 0; j < 2; j ++)
|
||||||
|
{
|
||||||
|
if (countMediumMeteors%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};
|
||||||
|
}
|
||||||
|
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[countMediumMeteors].active = true;
|
||||||
|
countMediumMeteors ++;
|
||||||
|
}
|
||||||
|
//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))
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
mediumMeteor[b].active = false;
|
||||||
|
meteorsDestroyed++;
|
||||||
|
for (int j = 0; j < 2; j ++)
|
||||||
|
{
|
||||||
|
if (countSmallMeteors%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};
|
||||||
|
}
|
||||||
|
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[countSmallMeteors].active = true;
|
||||||
|
countSmallMeteors ++;
|
||||||
|
}
|
||||||
|
//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))
|
||||||
|
{
|
||||||
|
shoot[i].active = false;
|
||||||
|
shoot[i].lifeSpawn = 0;
|
||||||
|
smallMeteor[c].active = false;
|
||||||
|
meteorsDestroyed++;
|
||||||
|
smallMeteor[c].color = YELLOW;
|
||||||
|
// smallMeteor[c].position = (Vector2){-100, -100};
|
||||||
|
c = MAX_SMALL_METEORS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meteorsDestroyed == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
// Draw spaceship
|
||||||
|
Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
|
||||||
|
Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
||||||
|
Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
||||||
|
DrawTriangle(v1, v2, v3, MAROON);
|
||||||
|
|
||||||
|
// Draw meteors
|
||||||
|
for (int i = 0; i < MAX_BIG_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (bigMeteor[i].active) DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, DARKGRAY);
|
||||||
|
else DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
|
||||||
|
else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, GRAY);
|
||||||
|
else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw shoot
|
||||||
|
for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
|
||||||
|
{
|
||||||
|
if (shoot[i].active) DrawCircleV(shoot[i].position, shoot[i].radius, BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (victory) DrawText("VICTORY", screenWidth/2 - MeasureText("VICTORY", 20)/2, screenHeight/2, 20, LIGHTGRAY);
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
UpdateGame();
|
||||||
|
DrawGame();
|
||||||
|
}
|
||||||
383
games/asteroids_survival.c
Normal file
383
games/asteroids_survival.c
Normal file
|
|
@ -0,0 +1,383 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: asteroids survival
|
||||||
|
*
|
||||||
|
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Some Defines
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
#define PLAYER_BASE_SIZE 20.0f
|
||||||
|
#define PLAYER_SPEED 6.0f
|
||||||
|
#define PLAYER_MAX_SHOOTS 10
|
||||||
|
|
||||||
|
#define METEORS_SPEED 2
|
||||||
|
#define MAX_MEDIUM_METEORS 8
|
||||||
|
#define MAX_SMALL_METEORS 16
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef struct Player {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
float acceleration;
|
||||||
|
float rotation;
|
||||||
|
Vector3 collider;
|
||||||
|
Color color;
|
||||||
|
} Player;
|
||||||
|
|
||||||
|
typedef struct Meteor {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
float radius;
|
||||||
|
bool active;
|
||||||
|
Color color;
|
||||||
|
} Meteor;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static int framesCounter;
|
||||||
|
static bool gameOver;
|
||||||
|
static bool pause;
|
||||||
|
|
||||||
|
// NOTE: Defined triangle is isosceles with common angles of 70 degrees.
|
||||||
|
static float shipHeight;
|
||||||
|
|
||||||
|
static Player player;
|
||||||
|
static Meteor mediumMeteor[MAX_MEDIUM_METEORS];
|
||||||
|
static Meteor smallMeteor[MAX_SMALL_METEORS];
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
static void DrawGame(void); // Draw game (one frame)
|
||||||
|
static void UnloadGame(void); // Unload game
|
||||||
|
static void UpdateDrawFrame(void); // Update and Draw (one frame)
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: asteroids survival");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
int posx, posy;
|
||||||
|
int velx, vely;
|
||||||
|
bool correctRange = false;
|
||||||
|
|
||||||
|
pause = false;
|
||||||
|
|
||||||
|
framesCounter = 0;
|
||||||
|
|
||||||
|
shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
|
||||||
|
|
||||||
|
// Initialization player
|
||||||
|
player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
|
||||||
|
player.speed = (Vector2){0, 0};
|
||||||
|
player.acceleration = 0;
|
||||||
|
player.rotation = 0;
|
||||||
|
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;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
posx = GetRandomValue(0, screenWidth);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
|
||||||
|
posy = GetRandomValue(0, screenHeight);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (velx == 0 && vely == 0)
|
||||||
|
{
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
}
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
mediumMeteor[i].position = (Vector2){posx, posy};
|
||||||
|
mediumMeteor[i].speed = (Vector2){velx, vely};
|
||||||
|
mediumMeteor[i].radius = 20;
|
||||||
|
mediumMeteor[i].active = true;
|
||||||
|
mediumMeteor[i].color = GREEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
posx = GetRandomValue(0, screenWidth);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
|
||||||
|
posy = GetRandomValue(0, screenHeight);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
correctRange = false;
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
|
||||||
|
while(!correctRange)
|
||||||
|
{
|
||||||
|
if (velx == 0 && vely == 0)
|
||||||
|
{
|
||||||
|
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
|
||||||
|
}
|
||||||
|
else correctRange = true;
|
||||||
|
}
|
||||||
|
smallMeteor[i].position = (Vector2){posx, posy};
|
||||||
|
smallMeteor[i].speed = (Vector2){velx, vely};
|
||||||
|
smallMeteor[i].radius = 10;
|
||||||
|
smallMeteor[i].active = true;
|
||||||
|
smallMeteor[i].color = YELLOW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
framesCounter++;
|
||||||
|
|
||||||
|
// Player logic
|
||||||
|
|
||||||
|
// Rotation
|
||||||
|
if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
|
||||||
|
if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
|
||||||
|
|
||||||
|
// Speed
|
||||||
|
player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
|
||||||
|
|
||||||
|
// Controller
|
||||||
|
if (IsKeyDown(KEY_UP))
|
||||||
|
{
|
||||||
|
if (player.acceleration < 1) player.acceleration += 0.04f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (player.acceleration > 0) player.acceleration -= 0.02f;
|
||||||
|
else if (player.acceleration < 0) player.acceleration = 0;
|
||||||
|
}
|
||||||
|
if (IsKeyDown(KEY_DOWN))
|
||||||
|
{
|
||||||
|
if (player.acceleration > 0) player.acceleration -= 0.04f;
|
||||||
|
else if (player.acceleration < 0) player.acceleration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Movement
|
||||||
|
player.position.x += (player.speed.x*player.acceleration);
|
||||||
|
player.position.y -= (player.speed.y*player.acceleration);
|
||||||
|
|
||||||
|
// Wall behaviour for player
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Collision Player to 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_MEDIUM_METEORS; a++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int a = 0; a < MAX_SMALL_METEORS; a++)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (mediumMeteor[i].active)
|
||||||
|
{
|
||||||
|
// movement
|
||||||
|
mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
|
||||||
|
mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
|
||||||
|
|
||||||
|
// wall behaviour
|
||||||
|
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);
|
||||||
|
else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (smallMeteor[i].active)
|
||||||
|
{
|
||||||
|
// movement
|
||||||
|
smallMeteor[i].position.x += smallMeteor[i].speed.x;
|
||||||
|
smallMeteor[i].position.y += smallMeteor[i].speed.y;
|
||||||
|
|
||||||
|
// wall behaviour
|
||||||
|
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);
|
||||||
|
else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
// Draw spaceship
|
||||||
|
Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
|
||||||
|
Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
||||||
|
Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
|
||||||
|
DrawTriangle(v1, v2, v3, MAROON);
|
||||||
|
|
||||||
|
// Draw meteor
|
||||||
|
for (int i = 0;i < MAX_MEDIUM_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
|
||||||
|
else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0;i < MAX_SMALL_METEORS; i++)
|
||||||
|
{
|
||||||
|
if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, DARKGRAY);
|
||||||
|
else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawText(FormatText("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
UpdateGame();
|
||||||
|
DrawGame();
|
||||||
|
}
|
||||||
|
|
@ -338,7 +338,7 @@ int main()
|
||||||
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, GetFontBaseSize(font), 0, WHITE);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, font.size, 0, WHITE);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case GAMEPLAY:
|
case GAMEPLAY:
|
||||||
|
|
@ -388,8 +388,8 @@ int main()
|
||||||
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
||||||
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
if (gameraMode)
|
if (gameraMode)
|
||||||
{
|
{
|
||||||
|
|
@ -403,15 +403,15 @@ int main()
|
||||||
// Draw a transparent black rectangle that covers all screen
|
// Draw a transparent black rectangle that covers all screen
|
||||||
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
||||||
|
|
||||||
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, GetFontBaseSize(font)*3, -2, MAROON);
|
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, font.size*3, -2, MAROON);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, GetFontBaseSize(font), -2, LIGHTGRAY);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, font.size, -2, LIGHTGRAY);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
default: break;
|
default: break;
|
||||||
|
|
|
||||||
|
|
@ -351,7 +351,7 @@ int main()
|
||||||
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, GetFontBaseSize(font), 0, WHITE);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, font.size, 0, WHITE);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case GAMEPLAY:
|
case GAMEPLAY:
|
||||||
|
|
@ -401,8 +401,8 @@ int main()
|
||||||
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
||||||
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
if (gameraMode)
|
if (gameraMode)
|
||||||
{
|
{
|
||||||
|
|
@ -416,15 +416,15 @@ int main()
|
||||||
// Draw a transparent black rectangle that covers all screen
|
// Draw a transparent black rectangle that covers all screen
|
||||||
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
||||||
|
|
||||||
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, GetFontBaseSize(font)*3, -2, MAROON);
|
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, font.size*3, -2, MAROON);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, GetFontBaseSize(font), -2, LIGHTGRAY);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, font.size, -2, LIGHTGRAY);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
default: break;
|
default: break;
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#include <math.h> // Used for sinf()
|
||||||
|
|
||||||
#define MAX_ENEMIES 10
|
#define MAX_ENEMIES 10
|
||||||
|
|
||||||
typedef enum { TITLE, GAMEPLAY, ENDING } GameScreen;
|
typedef enum { TITLE, GAMEPLAY, ENDING } GameScreen;
|
||||||
|
|
@ -128,7 +130,7 @@ int main()
|
||||||
framesCounter++;
|
framesCounter++;
|
||||||
|
|
||||||
// Sea color tint effect
|
// Sea color tint effect
|
||||||
blue = 210 + 25 * sin(timeCounter);
|
blue = 210 + 25 * sinf(timeCounter);
|
||||||
timeCounter += 0.01;
|
timeCounter += 0.01;
|
||||||
|
|
||||||
// Game screens management
|
// Game screens management
|
||||||
|
|
@ -374,7 +376,7 @@ int main()
|
||||||
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, GetFontBaseSize(font), 0, WHITE);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, font.size, 0, WHITE);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case GAMEPLAY:
|
case GAMEPLAY:
|
||||||
|
|
@ -424,8 +426,8 @@ int main()
|
||||||
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
||||||
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
if (gameraMode)
|
if (gameraMode)
|
||||||
{
|
{
|
||||||
|
|
@ -439,15 +441,15 @@ int main()
|
||||||
// Draw a transparent black rectangle that covers all screen
|
// Draw a transparent black rectangle that covers all screen
|
||||||
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
||||||
|
|
||||||
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, GetFontBaseSize(font)*3, -2, MAROON);
|
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, font.size*3, -2, MAROON);
|
||||||
|
|
||||||
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, GetFontBaseSize(font), -2, GOLD);
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, font.size, -2, GOLD);
|
||||||
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, font.size, -2, ORANGE);
|
||||||
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, GetFontBaseSize(font), -2, ORANGE);
|
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
// Draw blinking text
|
// Draw blinking text
|
||||||
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, GetFontBaseSize(font), -2, LIGHTGRAY);
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, font.size, -2, LIGHTGRAY);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
default: break;
|
default: break;
|
||||||
|
|
|
||||||
541
games/drturtle/drturtle_final_web.c
Normal file
541
games/drturtle/drturtle_final_web.c
Normal file
|
|
@ -0,0 +1,541 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib game - Dr. Turtle & Mr. Gamera
|
||||||
|
*
|
||||||
|
* Welcome to raylib!
|
||||||
|
*
|
||||||
|
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||||
|
* Note that compiled executable is placed in the same folder as .c file
|
||||||
|
*
|
||||||
|
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||||
|
* raylib official webpage: www.raylib.com
|
||||||
|
*
|
||||||
|
* Enjoy using raylib. :)
|
||||||
|
*
|
||||||
|
* This game has been created using raylib 1.1 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
#include <math.h> // Used for sinf()
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_ENEMIES 10
|
||||||
|
|
||||||
|
typedef enum { TITLE = 0, GAMEPLAY, ENDING } GameScreen;
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Global Variables Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 1280;
|
||||||
|
const int screenHeight = 720;
|
||||||
|
|
||||||
|
Texture2D sky;
|
||||||
|
Texture2D mountains;
|
||||||
|
Texture2D sea;
|
||||||
|
Texture2D title;
|
||||||
|
Texture2D turtle;
|
||||||
|
Texture2D gamera;
|
||||||
|
Texture2D shark;
|
||||||
|
Texture2D orca;
|
||||||
|
Texture2D swhale;
|
||||||
|
Texture2D fish;
|
||||||
|
Texture2D gframe;
|
||||||
|
|
||||||
|
SpriteFont font;
|
||||||
|
|
||||||
|
Sound eat;
|
||||||
|
Sound die;
|
||||||
|
Sound growl;
|
||||||
|
|
||||||
|
// Define scrolling variables
|
||||||
|
int backScrolling = 0;
|
||||||
|
int seaScrolling = 0;
|
||||||
|
|
||||||
|
// Define current screen
|
||||||
|
GameScreen currentScreen = 0;
|
||||||
|
|
||||||
|
// Define player variables
|
||||||
|
int playerRail = 1;
|
||||||
|
Rectangle playerBounds;
|
||||||
|
bool gameraMode = false;
|
||||||
|
|
||||||
|
// Define enemies variables
|
||||||
|
Rectangle enemyBounds[MAX_ENEMIES];
|
||||||
|
int enemyRail[MAX_ENEMIES];
|
||||||
|
int enemyType[MAX_ENEMIES];
|
||||||
|
bool enemyActive[MAX_ENEMIES];
|
||||||
|
float enemySpeed = 10;
|
||||||
|
|
||||||
|
// Define additional game variables
|
||||||
|
int score = 0;
|
||||||
|
float distance = 0.0f;
|
||||||
|
int hiscore = 0;
|
||||||
|
float hidistance = 0.0f;
|
||||||
|
int foodBar = 0;
|
||||||
|
int framesCounter = 0;
|
||||||
|
|
||||||
|
unsigned char blue = 200;
|
||||||
|
float timeCounter = 0;
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
void UpdateDrawFrame(void); // Update and Draw one frame
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Main Enry Point
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Init window
|
||||||
|
InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA");
|
||||||
|
|
||||||
|
// Initialize audio device
|
||||||
|
InitAudioDevice();
|
||||||
|
|
||||||
|
// Load game resources: textures
|
||||||
|
sky = LoadTexture("resources/sky.png");
|
||||||
|
mountains = LoadTexture("resources/mountains.png");
|
||||||
|
sea = LoadTexture("resources/sea.png");
|
||||||
|
title = LoadTexture("resources/title.png");
|
||||||
|
turtle = LoadTexture("resources/turtle.png");
|
||||||
|
gamera = LoadTexture("resources/gamera.png");
|
||||||
|
shark = LoadTexture("resources/shark.png");
|
||||||
|
orca = LoadTexture("resources/orca.png");
|
||||||
|
swhale = LoadTexture("resources/swhale.png");
|
||||||
|
fish = LoadTexture("resources/fish.png");
|
||||||
|
gframe = LoadTexture("resources/gframe.png");
|
||||||
|
|
||||||
|
// Load game resources: fonts
|
||||||
|
font = LoadSpriteFont("resources/komika.png");
|
||||||
|
|
||||||
|
// Load game resources: sounds
|
||||||
|
eat = LoadSound("resources/eat.wav");
|
||||||
|
die = LoadSound("resources/die.wav");
|
||||||
|
growl = LoadSound("resources/gamera.wav");
|
||||||
|
|
||||||
|
// Start playing streaming music
|
||||||
|
PlayMusicStream("resources/speeding.ogg");
|
||||||
|
|
||||||
|
playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
|
||||||
|
|
||||||
|
// Init enemies variables
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
// Define enemy type (all same probability)
|
||||||
|
//enemyType[i] = GetRandomValue(0, 3);
|
||||||
|
|
||||||
|
// Probability system for enemies type
|
||||||
|
int enemyProb = GetRandomValue(0, 100);
|
||||||
|
|
||||||
|
if (enemyProb < 30) enemyType[i] = 0;
|
||||||
|
else if (enemyProb < 60) enemyType[i] = 1;
|
||||||
|
else if (enemyProb < 90) enemyType[i] = 2;
|
||||||
|
else enemyType[i] = 3;
|
||||||
|
|
||||||
|
// define enemy rail
|
||||||
|
enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
// Make sure not two consecutive enemies in the same row
|
||||||
|
if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
|
||||||
|
enemyActive[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
UpdateDrawFrame();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Unload textures
|
||||||
|
UnloadTexture(sky);
|
||||||
|
UnloadTexture(mountains);
|
||||||
|
UnloadTexture(sea);
|
||||||
|
UnloadTexture(gframe);
|
||||||
|
UnloadTexture(title);
|
||||||
|
UnloadTexture(turtle);
|
||||||
|
UnloadTexture(shark);
|
||||||
|
UnloadTexture(orca);
|
||||||
|
UnloadTexture(swhale);
|
||||||
|
UnloadTexture(fish);
|
||||||
|
UnloadTexture(gamera);
|
||||||
|
|
||||||
|
// Unload font texture
|
||||||
|
UnloadSpriteFont(font);
|
||||||
|
|
||||||
|
// Unload sounds
|
||||||
|
UnloadSound(eat);
|
||||||
|
UnloadSound(die);
|
||||||
|
UnloadSound(growl);
|
||||||
|
|
||||||
|
StopMusicStream(); // Stop music
|
||||||
|
CloseAudioDevice(); // Close audio device
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateMusicStream();
|
||||||
|
|
||||||
|
framesCounter++;
|
||||||
|
|
||||||
|
// Sea color tint effect
|
||||||
|
blue = 210 + 25 * sinf(timeCounter);
|
||||||
|
timeCounter += 0.01;
|
||||||
|
|
||||||
|
// Game screens management
|
||||||
|
switch (currentScreen)
|
||||||
|
{
|
||||||
|
case TITLE:
|
||||||
|
{
|
||||||
|
// Sea scrolling
|
||||||
|
seaScrolling -= 2;
|
||||||
|
if (seaScrolling <= -screenWidth) seaScrolling = 0;
|
||||||
|
|
||||||
|
// Press enter to change to gameplay screen
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
currentScreen = GAMEPLAY;
|
||||||
|
framesCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case GAMEPLAY:
|
||||||
|
{
|
||||||
|
// Background scrolling logic
|
||||||
|
backScrolling--;
|
||||||
|
if (backScrolling <= -screenWidth) backScrolling = 0;
|
||||||
|
|
||||||
|
// Sea scrolling logic
|
||||||
|
seaScrolling -= (enemySpeed - 2);
|
||||||
|
if (seaScrolling <= -screenWidth) seaScrolling = 0;
|
||||||
|
|
||||||
|
// Player movement logic
|
||||||
|
if (IsKeyPressed(KEY_DOWN)) playerRail++;
|
||||||
|
else if (IsKeyPressed(KEY_UP)) playerRail--;
|
||||||
|
|
||||||
|
// Check player not out of rails
|
||||||
|
if (playerRail > 4) playerRail = 4;
|
||||||
|
else if (playerRail < 0) playerRail = 0;
|
||||||
|
|
||||||
|
// Update player bounds
|
||||||
|
playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
|
||||||
|
|
||||||
|
// Enemies activation logic (every 40 frames)
|
||||||
|
if (framesCounter > 40)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
if (enemyActive[i] == false)
|
||||||
|
{
|
||||||
|
enemyActive[i] = true;
|
||||||
|
i = MAX_ENEMIES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
framesCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enemies logic
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
if (enemyActive[i])
|
||||||
|
{
|
||||||
|
enemyBounds[i].x -= enemySpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check enemies out of screen
|
||||||
|
if (enemyBounds[i].x <= 0 - 128)
|
||||||
|
{
|
||||||
|
enemyActive[i] = false;
|
||||||
|
enemyType[i] = GetRandomValue(0, 3);
|
||||||
|
enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
// Make sure not two consecutive enemies in the same row
|
||||||
|
if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gameraMode) enemySpeed += 0.005;
|
||||||
|
|
||||||
|
// Check collision player vs enemies
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
if (enemyActive[i])
|
||||||
|
{
|
||||||
|
if (CheckCollisionRecs(playerBounds, enemyBounds[i]))
|
||||||
|
{
|
||||||
|
if (enemyType[i] < 3) // Bad enemies
|
||||||
|
{
|
||||||
|
if (gameraMode)
|
||||||
|
{
|
||||||
|
if (enemyType[i] == 0) score += 50;
|
||||||
|
else if (enemyType[i] == 1) score += 150;
|
||||||
|
else if (enemyType[i] == 2) score += 300;
|
||||||
|
|
||||||
|
foodBar += 15;
|
||||||
|
|
||||||
|
enemyActive[i] = false;
|
||||||
|
|
||||||
|
// After enemy deactivation, reset enemy parameters to be reused
|
||||||
|
enemyType[i] = GetRandomValue(0, 3);
|
||||||
|
enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
// Make sure not two consecutive enemies in the same row
|
||||||
|
if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
|
||||||
|
|
||||||
|
PlaySound(eat);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Player die logic
|
||||||
|
PlaySound(die);
|
||||||
|
|
||||||
|
currentScreen = ENDING;
|
||||||
|
framesCounter = 0;
|
||||||
|
|
||||||
|
// Save hiscore and hidistance for next game
|
||||||
|
if (score > hiscore) hiscore = score;
|
||||||
|
if (distance > hidistance) hidistance = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Sweet fish
|
||||||
|
{
|
||||||
|
enemyActive[i] = false;
|
||||||
|
enemyType[i] = GetRandomValue(0, 3);
|
||||||
|
enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
// Make sure not two consecutive enemies in the same row
|
||||||
|
if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
|
||||||
|
|
||||||
|
if (!gameraMode) foodBar += 80;
|
||||||
|
else foodBar += 25;
|
||||||
|
|
||||||
|
score += 10;
|
||||||
|
|
||||||
|
if (foodBar == 400)
|
||||||
|
{
|
||||||
|
gameraMode = true;
|
||||||
|
|
||||||
|
PlaySound(growl);
|
||||||
|
}
|
||||||
|
|
||||||
|
PlaySound(eat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gamera mode logic
|
||||||
|
if (gameraMode)
|
||||||
|
{
|
||||||
|
foodBar--;
|
||||||
|
|
||||||
|
if (foodBar <= 0)
|
||||||
|
{
|
||||||
|
gameraMode = false;
|
||||||
|
enemySpeed -= 2;
|
||||||
|
if (enemySpeed < 10) enemySpeed = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update distance counter
|
||||||
|
distance += 0.5f;
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case ENDING:
|
||||||
|
{
|
||||||
|
// Press enter to play again
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
currentScreen = GAMEPLAY;
|
||||||
|
|
||||||
|
// Reset player
|
||||||
|
playerRail = 1;
|
||||||
|
playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
|
||||||
|
gameraMode = false;
|
||||||
|
|
||||||
|
// Reset enemies data
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
int enemyProb = GetRandomValue(0, 100);
|
||||||
|
|
||||||
|
if (enemyProb < 30) enemyType[i] = 0;
|
||||||
|
else if (enemyProb < 60) enemyType[i] = 1;
|
||||||
|
else if (enemyProb < 90) enemyType[i] = 2;
|
||||||
|
else enemyType[i] = 3;
|
||||||
|
|
||||||
|
//enemyType[i] = GetRandomValue(0, 3);
|
||||||
|
enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
// Make sure not two consecutive enemies in the same row
|
||||||
|
if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
|
||||||
|
|
||||||
|
enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
|
||||||
|
enemyActive[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
enemySpeed = 10;
|
||||||
|
|
||||||
|
// Reset game variables
|
||||||
|
score = 0;
|
||||||
|
distance = 0.0;
|
||||||
|
foodBar = 0;
|
||||||
|
framesCounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
// Draw background (common to all screens)
|
||||||
|
DrawTexture(sky, 0, 0, WHITE);
|
||||||
|
|
||||||
|
DrawTexture(mountains, backScrolling, 0, WHITE);
|
||||||
|
DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE);
|
||||||
|
|
||||||
|
if (!gameraMode)
|
||||||
|
{
|
||||||
|
DrawTexture(sea, seaScrolling, 0, (Color){ 16, 189, blue, 255});
|
||||||
|
DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 16, 189, blue, 255});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DrawTexture(sea, seaScrolling, 0, (Color){ 255, 113, 66, 255});
|
||||||
|
DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 255, 113, 66, 255});
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (currentScreen)
|
||||||
|
{
|
||||||
|
case TITLE:
|
||||||
|
{
|
||||||
|
// Draw title
|
||||||
|
DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
|
||||||
|
|
||||||
|
// Draw blinking text
|
||||||
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, font.size, 1, WHITE);
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case GAMEPLAY:
|
||||||
|
{
|
||||||
|
// Draw water lines
|
||||||
|
for (int i = 0; i < 5; i++) DrawRectangle(0, i*120 + 120, screenWidth, 110, Fade(SKYBLUE, 0.1f));
|
||||||
|
|
||||||
|
// Draw player
|
||||||
|
if (!gameraMode) DrawTexture(turtle, playerBounds.x - 14, playerBounds.y - 14, WHITE);
|
||||||
|
else DrawTexture(gamera, playerBounds.x - 64, playerBounds.y - 64, WHITE);
|
||||||
|
|
||||||
|
// Draw player bounding box
|
||||||
|
//if (!gameraMode) DrawRectangleRec(playerBounds, Fade(GREEN, 0.4f));
|
||||||
|
//else DrawRectangleRec(playerBounds, Fade(ORANGE, 0.4f));
|
||||||
|
|
||||||
|
// Draw enemies
|
||||||
|
for (int i = 0; i < MAX_ENEMIES; i++)
|
||||||
|
{
|
||||||
|
if (enemyActive[i])
|
||||||
|
{
|
||||||
|
// Draw enemies
|
||||||
|
switch(enemyType[i])
|
||||||
|
{
|
||||||
|
case 0: DrawTexture(shark, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
|
||||||
|
case 1: DrawTexture(orca, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
|
||||||
|
case 2: DrawTexture(swhale, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
|
||||||
|
case 3: DrawTexture(fish, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw enemies bounding boxes
|
||||||
|
/*
|
||||||
|
switch(enemyType[i])
|
||||||
|
{
|
||||||
|
case 0: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
|
||||||
|
case 1: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
|
||||||
|
case 2: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
|
||||||
|
case 3: DrawRectangleRec(enemyBounds[i], Fade(GREEN, 0.5f)); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw gameplay interface
|
||||||
|
DrawRectangle(20, 20, 400, 40, Fade(GRAY, 0.4f));
|
||||||
|
DrawRectangle(20, 20, foodBar, 40, ORANGE);
|
||||||
|
DrawRectangleLines(20, 20, 400, 40, BLACK);
|
||||||
|
|
||||||
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, font.size, -2, ORANGE);
|
||||||
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
|
if (gameraMode)
|
||||||
|
{
|
||||||
|
DrawText("GAMERA MODE", 60, 22, 40, GRAY);
|
||||||
|
DrawTexture(gframe, 0, 0, Fade(WHITE, 0.5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
} break;
|
||||||
|
case ENDING:
|
||||||
|
{
|
||||||
|
// Draw a transparent black rectangle that covers all screen
|
||||||
|
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
|
||||||
|
|
||||||
|
DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, font.size*3, -2, MAROON);
|
||||||
|
|
||||||
|
DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, font.size, -2, GOLD);
|
||||||
|
DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, font.size, -2, GOLD);
|
||||||
|
DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, font.size, -2, ORANGE);
|
||||||
|
DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, font.size, -2, ORANGE);
|
||||||
|
|
||||||
|
// Draw blinking text
|
||||||
|
if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, font.size, -2, LIGHTGRAY);
|
||||||
|
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
186
games/drturtle/makefile
Normal file
186
games/drturtle/makefile
Normal file
|
|
@ -0,0 +1,186 @@
|
||||||
|
#**************************************************************************************************
|
||||||
|
#
|
||||||
|
# raylib - makefile to compile Dr.Turtle game
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
||||||
|
#
|
||||||
|
# This software is provided "as-is", without any express or implied warranty. In no event
|
||||||
|
# will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
#
|
||||||
|
# Permission is granted to anyone to use this software for any purpose, including commercial
|
||||||
|
# applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||||
|
#
|
||||||
|
# 1. The origin of this software must not be misrepresented; you must not claim that you
|
||||||
|
# wrote the original software. If you use this software in a product, an acknowledgment
|
||||||
|
# in the product documentation would be appreciated but is not required.
|
||||||
|
#
|
||||||
|
# 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||||
|
# as being the original software.
|
||||||
|
#
|
||||||
|
# 3. This notice may not be removed or altered from any source distribution.
|
||||||
|
#
|
||||||
|
#**************************************************************************************************
|
||||||
|
|
||||||
|
# define raylib platform if not defined (by default, compile for RPI)
|
||||||
|
# Other possible platform: PLATFORM_DESKTOP
|
||||||
|
PLATFORM ?= PLATFORM_DESKTOP
|
||||||
|
|
||||||
|
# determine PLATFORM_OS in case PLATFORM_DESKTOP selected
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
# No uname.exe on MinGW!, but OS=Windows_NT on Windows! ifeq ($(UNAME),Msys) -> Windows
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
PLATFORM_OS=WINDOWS
|
||||||
|
LIBPATH=win32
|
||||||
|
else
|
||||||
|
UNAMEOS:=$(shell uname)
|
||||||
|
ifeq ($(UNAMEOS),Linux)
|
||||||
|
PLATFORM_OS=LINUX
|
||||||
|
LIBPATH=linux
|
||||||
|
else
|
||||||
|
ifeq ($(UNAMEOS),Darwin)
|
||||||
|
PLATFORM_OS=OSX
|
||||||
|
LIBPATH=osx
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define compiler: gcc for C program, define as g++ for C++
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
# define emscripten compiler
|
||||||
|
CC = emcc
|
||||||
|
else
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
# define llvm compiler for mac
|
||||||
|
CC = clang
|
||||||
|
else
|
||||||
|
# define default gcc compiler
|
||||||
|
CC = gcc
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define compiler flags:
|
||||||
|
# -O2 defines optimization level
|
||||||
|
# -Wall turns on most, but not all, compiler warnings
|
||||||
|
# -std=c99 use standard C from 1999 revision
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
CFLAGS = -O2 -Wall -std=gnu99 -fgnu89-inline
|
||||||
|
else
|
||||||
|
CFLAGS = -O2 -Wall -std=c99
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 --preload-file resources -s ALLOW_MEMORY_GROWTH=1 --shell-file ../../templates/web_shell/shell.html
|
||||||
|
#-s ASSERTIONS=1 --preload-file resources
|
||||||
|
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
||||||
|
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define any directories containing required header files
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
INCLUDES = -I. -I../../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
|
||||||
|
else
|
||||||
|
INCLUDES = -I. -I../../src
|
||||||
|
# external libraries headers
|
||||||
|
# GLFW3
|
||||||
|
INCLUDES += -I../../external/glfw3/include
|
||||||
|
# GLEW
|
||||||
|
INCLUDES += -I../../external/glew/include
|
||||||
|
# OpenAL Soft
|
||||||
|
INCLUDES += -I../../external/openal_soft/include
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define library paths containing required libs
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
LFLAGS = -L. -L../../src -L/opt/vc/lib
|
||||||
|
else
|
||||||
|
LFLAGS = -L. -L../../src
|
||||||
|
# external libraries to link with
|
||||||
|
# GLFW3
|
||||||
|
LFLAGS += -L../../external/glfw3/lib/$(LIBPATH)
|
||||||
|
ifneq ($(PLATFORM_OS),OSX)
|
||||||
|
# OpenAL Soft
|
||||||
|
LFLAGS += -L../../external/openal_soft/lib/$(LIBPATH)
|
||||||
|
# GLEW
|
||||||
|
LFLAGS += -L../../external/glew/lib/$(LIBPATH)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define any libraries to link into executable
|
||||||
|
# if you want to link libraries (libname.so or libname.a), use the -lname
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
ifeq ($(PLATFORM_OS),LINUX)
|
||||||
|
# libraries for Debian GNU/Linux desktop compiling
|
||||||
|
# requires the following packages:
|
||||||
|
# libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev
|
||||||
|
LIBS = -lraylib -lglfw -lGLEW -lGL -lopenal
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
# libraries for OS X 10.9 desktop compiling
|
||||||
|
# requires the following packages:
|
||||||
|
# libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev
|
||||||
|
LIBS = -lraylib -lglfw -framework OpenGL -framework OpenAl -framework Cocoa
|
||||||
|
else
|
||||||
|
# libraries for Windows desktop compiling
|
||||||
|
# NOTE: GLFW3 and OpenAL Soft libraries should be installed
|
||||||
|
LIBS = -lraylib -lglfw3 -lglew32 -lopengl32 -lopenal32 -lgdi32
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
# libraries for Raspberry Pi compiling
|
||||||
|
# NOTE: OpenAL Soft library should be installed (libopenal1 package)
|
||||||
|
LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
LIBS = ../../src/libraylib.bc
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define additional parameters and flags for windows
|
||||||
|
ifeq ($(PLATFORM_OS),WINDOWS)
|
||||||
|
# resources file contains windows exe icon
|
||||||
|
# -Wl,--subsystem,windows hides the console window
|
||||||
|
WINFLAGS = ../../src/resources -Wl,--subsystem,windows
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
EXT = .html
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define all screen object files required
|
||||||
|
SCREENS = \
|
||||||
|
|
||||||
|
# typing 'make' will invoke the first target entry in the file,
|
||||||
|
# in this case, the 'default' target entry is advance_game
|
||||||
|
default: drturtle
|
||||||
|
|
||||||
|
# compile template - advance_game
|
||||||
|
drturtle: drturtle_final_web.c $(SCREENS)
|
||||||
|
$(CC) -o $@$(EXT) $< $(SCREENS) $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# clean everything
|
||||||
|
clean:
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
find . -type f -perm +ugo+x -delete
|
||||||
|
rm -f *.o
|
||||||
|
else
|
||||||
|
ifeq ($(PLATFORM_OS),LINUX)
|
||||||
|
find . -type f -executable -delete
|
||||||
|
rm -f *.o
|
||||||
|
else
|
||||||
|
del *.o *.exe
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
find . -type f -executable -delete
|
||||||
|
rm -f *.o
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
del *.o *.html *.js
|
||||||
|
endif
|
||||||
|
@echo Cleaning done
|
||||||
|
|
||||||
|
# instead of defining every module one by one, we can define a pattern
|
||||||
|
# this pattern below will automatically compile every module defined on $(OBJS)
|
||||||
|
#%.exe : %.c
|
||||||
|
# $(CC) -o $@ $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM)
|
||||||
246
games/floppy.c
Normal file
246
games/floppy.c
Normal file
|
|
@ -0,0 +1,246 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: floppy
|
||||||
|
*
|
||||||
|
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Some Defines
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
#define MAX_TUBES 100
|
||||||
|
#define FLOPPY_RADIUS 24
|
||||||
|
#define TUBES_WIDTH 80
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
typedef struct Floppy {
|
||||||
|
Vector2 position;
|
||||||
|
int radius;
|
||||||
|
Color color;
|
||||||
|
} Floppy;
|
||||||
|
|
||||||
|
typedef struct Tubes {
|
||||||
|
Rectangle rec;
|
||||||
|
Color color;
|
||||||
|
bool active;
|
||||||
|
} Tubes;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static int framesCounter;
|
||||||
|
static bool gameOver;
|
||||||
|
static bool pause;
|
||||||
|
static int score;
|
||||||
|
static int hiScore = 0;
|
||||||
|
|
||||||
|
static Floppy floppy;
|
||||||
|
static Tubes tubes[MAX_TUBES*2];
|
||||||
|
static Vector2 tubesPos[MAX_TUBES];
|
||||||
|
static int tubesSpeedX;
|
||||||
|
static bool superfx;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
static void DrawGame(void); // Draw game (one frame)
|
||||||
|
static void UnloadGame(void); // Unload game
|
||||||
|
static void UpdateDrawFrame(void); // Update and Draw (one frame)
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: floppy");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
floppy.radius = FLOPPY_RADIUS;
|
||||||
|
floppy.position = (Vector2){80, screenHeight/2 - floppy.radius};
|
||||||
|
tubesSpeedX = 2;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TUBES; i++)
|
||||||
|
{
|
||||||
|
tubesPos[i].x = 400 + 280*i;
|
||||||
|
tubesPos[i].y = -GetRandomValue(0, 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TUBES*2; i += 2)
|
||||||
|
{
|
||||||
|
tubes[i].rec.x = tubesPos[i/2].x;
|
||||||
|
tubes[i].rec.y = tubesPos[i/2].y;
|
||||||
|
tubes[i].rec.width = TUBES_WIDTH;
|
||||||
|
tubes[i].rec.height = 255;
|
||||||
|
|
||||||
|
tubes[i+1].rec.x = tubesPos[i/2].x;
|
||||||
|
tubes[i+1].rec.y = 600 + tubesPos[i/2].y - 255;
|
||||||
|
tubes[i+1].rec.width = TUBES_WIDTH;
|
||||||
|
tubes[i+1].rec.height = 255;
|
||||||
|
|
||||||
|
tubes[i/2].active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
score = 0;
|
||||||
|
|
||||||
|
gameOver = false;
|
||||||
|
superfx = false;
|
||||||
|
pause = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MAX_TUBES; i++) tubesPos[i].x -= tubesSpeedX;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TUBES*2; i += 2)
|
||||||
|
{
|
||||||
|
tubes[i].rec.x = tubesPos[i/2].x;
|
||||||
|
tubes[i+1].rec.x = tubesPos[i/2].x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyDown(KEY_SPACE) && !gameOver) floppy.position.y -= 3;
|
||||||
|
else floppy.position.y += 1;
|
||||||
|
|
||||||
|
// Check Collisions
|
||||||
|
for (int i = 0; i < MAX_TUBES*2; i++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircleRec(floppy.position, floppy.radius, tubes[i].rec))
|
||||||
|
{
|
||||||
|
gameOver = true;
|
||||||
|
pause = false;
|
||||||
|
}
|
||||||
|
else if ((tubesPos[i/2].x < floppy.position.x) && tubes[i/2].active && !gameOver)
|
||||||
|
{
|
||||||
|
score += 100;
|
||||||
|
tubes[i/2].active = false;
|
||||||
|
|
||||||
|
superfx = true;
|
||||||
|
|
||||||
|
if (score > hiScore) hiScore = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
DrawCircle(floppy.position.x, floppy.position.y, floppy.radius, DARKGRAY);
|
||||||
|
|
||||||
|
// Draw tubes
|
||||||
|
for (int i = 0; i < MAX_TUBES; i++)
|
||||||
|
{
|
||||||
|
DrawRectangle(tubes[i*2].rec.x, tubes[i*2].rec.y, tubes[i*2].rec.width, tubes[i*2].rec.height, GRAY);
|
||||||
|
DrawRectangle(tubes[i*2 + 1].rec.x, tubes[i*2 + 1].rec.y, tubes[i*2 + 1].rec.width, tubes[i*2 + 1].rec.height, GRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw flashing fx (one frame only)
|
||||||
|
if (superfx)
|
||||||
|
{
|
||||||
|
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
|
||||||
|
superfx = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawText(FormatText("%04i", score), 20, 20, 40, GRAY);
|
||||||
|
DrawText(FormatText("HI-SCORE: %04i", hiScore), 20, 70, 20, LIGHTGRAY);
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
UpdateGame();
|
||||||
|
DrawGame();
|
||||||
|
}
|
||||||
288
games/gold_fever.c
Normal file
288
games/gold_fever.c
Normal file
|
|
@ -0,0 +1,288 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: gold fever
|
||||||
|
*
|
||||||
|
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
typedef struct Player {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
int radius;
|
||||||
|
} Player;
|
||||||
|
|
||||||
|
typedef struct Enemy {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
int radius;
|
||||||
|
int radiusBounds;
|
||||||
|
bool moveRight; // RAY: o__O
|
||||||
|
} Enemy;
|
||||||
|
|
||||||
|
typedef struct Points {
|
||||||
|
Vector2 position;
|
||||||
|
int radius;
|
||||||
|
int value;
|
||||||
|
bool active;
|
||||||
|
} Points;
|
||||||
|
|
||||||
|
typedef struct Home {
|
||||||
|
Rectangle rec;
|
||||||
|
bool active;
|
||||||
|
bool save;
|
||||||
|
Color color;
|
||||||
|
} Home;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static int framesCounter;
|
||||||
|
static bool gameOver;
|
||||||
|
static bool pause;
|
||||||
|
static int score;
|
||||||
|
static int hiScore = 0;
|
||||||
|
|
||||||
|
static Player player;
|
||||||
|
static Enemy enemy;
|
||||||
|
static Points points;
|
||||||
|
static Home home;
|
||||||
|
static bool follow;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
static void DrawGame(void); // Draw game (one frame)
|
||||||
|
static void UnloadGame(void); // Unload game
|
||||||
|
static void UpdateDrawFrame(void); // Update and Draw (one frame)
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: gold fever");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
pause = false;
|
||||||
|
score = 0;
|
||||||
|
|
||||||
|
player.position = (Vector2){50, 50};
|
||||||
|
player.radius = 20;
|
||||||
|
player.speed = (Vector2){5, 5};
|
||||||
|
|
||||||
|
enemy.position = (Vector2){screenWidth - 50, screenHeight/2};
|
||||||
|
enemy.radius = 20;
|
||||||
|
enemy.radiusBounds = 150;
|
||||||
|
enemy.speed = (Vector2){3, 3};
|
||||||
|
enemy.moveRight = true;
|
||||||
|
follow = false;
|
||||||
|
|
||||||
|
points.radius = 10;
|
||||||
|
points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)};
|
||||||
|
points.value = 100;
|
||||||
|
points.active = true;
|
||||||
|
|
||||||
|
home.rec.width = 50;
|
||||||
|
home.rec.height = 50;
|
||||||
|
home.rec.x = GetRandomValue(0, screenWidth - home.rec.width);
|
||||||
|
home.rec.y = GetRandomValue(0, screenHeight - home.rec.height);
|
||||||
|
home.active = false;
|
||||||
|
home.save = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
//Control player
|
||||||
|
if (IsKeyDown(KEY_RIGHT)) player.position.x += player.speed.x;
|
||||||
|
if (IsKeyDown(KEY_LEFT)) player.position.x -= player.speed.x;
|
||||||
|
if (IsKeyDown(KEY_UP)) player.position.y -= player.speed.y;
|
||||||
|
if (IsKeyDown(KEY_DOWN)) player.position.y += player.speed.y;
|
||||||
|
|
||||||
|
//wall behaviour player
|
||||||
|
if (player.position.x - player.radius <= 0) player.position.x = player.radius;
|
||||||
|
if (player.position.x + player.radius >= screenWidth) player.position.x = screenWidth - player.radius;
|
||||||
|
if (player.position.y - player.radius <= 0) player.position.y = player.radius;
|
||||||
|
if (player.position.y + player.radius >= screenHeight) player.position.y = screenHeight - player.radius;
|
||||||
|
|
||||||
|
//IA Enemy
|
||||||
|
if ( (follow || CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radiusBounds)) && !home.save)
|
||||||
|
{
|
||||||
|
if (player.position.x > enemy.position.x) enemy.position.x += enemy.speed.x;
|
||||||
|
if (player.position.x < enemy.position.x) enemy.position.x -= enemy.speed.x;
|
||||||
|
|
||||||
|
if (player.position.y > enemy.position.y) enemy.position.y += enemy.speed.y;
|
||||||
|
if (player.position.y < enemy.position.y) enemy.position.y -= enemy.speed.y;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (enemy.moveRight) enemy.position.x += enemy.speed.x;
|
||||||
|
else enemy.position.x -= enemy.speed.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
//wall behaviour enemy
|
||||||
|
if (enemy.position.x - enemy.radius <= 0) enemy.moveRight = true;
|
||||||
|
if (enemy.position.x + enemy.radius >= screenWidth) enemy.moveRight = false;
|
||||||
|
|
||||||
|
if (enemy.position.x - enemy.radius <= 0) enemy.position.x = enemy.radius;
|
||||||
|
if (enemy.position.x + enemy.radius >= screenWidth) enemy.position.x = screenWidth - enemy.radius;
|
||||||
|
if (enemy.position.y - enemy.radius <= 0) enemy.position.y = enemy.radius;
|
||||||
|
if (enemy.position.y + enemy.radius >= screenHeight) enemy.position.y = screenHeight - enemy.radius;
|
||||||
|
|
||||||
|
//Collisions
|
||||||
|
if (CheckCollisionCircles(player.position, player.radius, points.position, points.radius) && points.active)
|
||||||
|
{
|
||||||
|
follow = true;
|
||||||
|
points.active = false;
|
||||||
|
home.active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radius) && !home.save)
|
||||||
|
{
|
||||||
|
gameOver = true;
|
||||||
|
|
||||||
|
if (hiScore < score) hiScore = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CheckCollisionCircleRec(player.position, player.radius, home.rec))
|
||||||
|
{
|
||||||
|
follow = false;
|
||||||
|
|
||||||
|
if (!points.active)
|
||||||
|
{
|
||||||
|
score += points.value;
|
||||||
|
points.active = true;
|
||||||
|
enemy.speed.x += 0.5;
|
||||||
|
enemy.speed.y += 0.5;
|
||||||
|
points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)};
|
||||||
|
}
|
||||||
|
|
||||||
|
home.save = true;
|
||||||
|
}
|
||||||
|
else home.save = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (follow)
|
||||||
|
{
|
||||||
|
DrawRectangle(0, 0, screenWidth, screenHeight, RED);
|
||||||
|
DrawRectangle(10, 10, screenWidth - 20, screenHeight - 20, RAYWHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawRectangleLines(home.rec.x, home.rec.y, home.rec.width, home.rec.height, BLUE);
|
||||||
|
|
||||||
|
DrawCircleLines(enemy.position.x, enemy.position.y, enemy.radiusBounds, RED);
|
||||||
|
DrawCircleV(enemy.position, enemy.radius, MAROON);
|
||||||
|
|
||||||
|
DrawCircleV(player.position, player.radius, GRAY);
|
||||||
|
if (points.active) DrawCircleV(points.position, points.radius, GOLD);
|
||||||
|
|
||||||
|
DrawText(FormatText("SCORE: %04i", score), 20, 15, 20, GRAY);
|
||||||
|
DrawText(FormatText("HI-SCORE: %04i", hiScore), 300, 15, 20, GRAY);
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
UpdateGame();
|
||||||
|
DrawGame();
|
||||||
|
}
|
||||||
571
games/gorilas.c
Normal file
571
games/gorilas.c
Normal file
|
|
@ -0,0 +1,571 @@
|
||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib - sample game: gorilas
|
||||||
|
*
|
||||||
|
* Sample game Marc Palau and Ramon Santamaria
|
||||||
|
*
|
||||||
|
* This game has been created using raylib v1.3 (www.raylib.com)
|
||||||
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Some Defines
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
#define MAX_BUILDINGS 15
|
||||||
|
#define MAX_EXPLOSIONS 200
|
||||||
|
#define MAX_PLAYERS 2
|
||||||
|
|
||||||
|
#define BUILDING_RELATIVE_ERROR 30 // Building size random range %
|
||||||
|
#define BUILDING_MIN_RELATIVE_HEIGHT 20 // Minimum height in % of the screenHeight
|
||||||
|
#define BUILDING_MAX_RELATIVE_HEIGHT 60 // Maximum height in % of the screenHeight
|
||||||
|
#define BUILDING_MIN_GRAYSCALE_COLOR 120 // Minimum gray color for the buildings
|
||||||
|
#define BUILDING_MAX_GRAYSCALE_COLOR 200 // Maximum gray color for the buildings
|
||||||
|
|
||||||
|
#define MIN_PLAYER_POSITION 5 // Minimum x position %
|
||||||
|
#define MAX_PLAYER_POSITION 20 // Maximum x position %
|
||||||
|
|
||||||
|
#define GRAVITY 9.81f
|
||||||
|
#define DELTA_FPS 60
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Types and Structures Definition
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
typedef struct Player {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 size;
|
||||||
|
|
||||||
|
Vector2 aimingPoint;
|
||||||
|
int aimingAngle;
|
||||||
|
int aimingPower;
|
||||||
|
|
||||||
|
Vector2 previousPoint;
|
||||||
|
int previousAngle;
|
||||||
|
int previousPower;
|
||||||
|
|
||||||
|
Vector2 impactPoint;
|
||||||
|
|
||||||
|
bool isLeftTeam; // This player belongs to the left or to the right team
|
||||||
|
bool isPlayer; // If is a player or an AI
|
||||||
|
bool isAlive;
|
||||||
|
} Player;
|
||||||
|
|
||||||
|
typedef struct Building {
|
||||||
|
Rectangle rectangle;
|
||||||
|
Color color;
|
||||||
|
} Building;
|
||||||
|
|
||||||
|
typedef struct Explosion {
|
||||||
|
Vector2 position;
|
||||||
|
int radius;
|
||||||
|
bool active;
|
||||||
|
} Explosion;
|
||||||
|
|
||||||
|
typedef struct Ball {
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 speed;
|
||||||
|
int radius;
|
||||||
|
bool active;
|
||||||
|
} Ball;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Global Variables Declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static int screenWidth = 800;
|
||||||
|
static int screenHeight = 450;
|
||||||
|
|
||||||
|
static bool gameOver = false;
|
||||||
|
static bool pause = false;
|
||||||
|
|
||||||
|
static Player player[MAX_PLAYERS];
|
||||||
|
static Building building[MAX_BUILDINGS];
|
||||||
|
static Explosion explosion[MAX_EXPLOSIONS];
|
||||||
|
static Ball ball;
|
||||||
|
|
||||||
|
static int playerTurn = 0;
|
||||||
|
static bool ballOnAir = false;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Declaration (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void InitGame(void); // Initialize game
|
||||||
|
static void UpdateGame(void); // Update game (one frame)
|
||||||
|
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 InitBuildings(void);
|
||||||
|
static void InitPlayers(void);
|
||||||
|
static bool UpdatePlayer(int playerTurn);
|
||||||
|
static bool UpdateBall(int playerTurn);
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
InitWindow(screenWidth, screenHeight, "sample game: gorilas");
|
||||||
|
|
||||||
|
InitGame();
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||||
|
#else
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
DrawGame();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadGame(); // Unload loaded data (textures, sounds, models...)
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definitions (local)
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Initialize game variables
|
||||||
|
void InitGame(void)
|
||||||
|
{
|
||||||
|
// Init shoot
|
||||||
|
ball.radius = 10;
|
||||||
|
ballOnAir = false;
|
||||||
|
ball.active = false;
|
||||||
|
|
||||||
|
InitBuildings();
|
||||||
|
InitPlayers();
|
||||||
|
|
||||||
|
// Init explosions
|
||||||
|
for (int i = 0; i < MAX_EXPLOSIONS; i++)
|
||||||
|
{
|
||||||
|
explosion[i].position = (Vector2){ 0.0f, 0.0f };
|
||||||
|
explosion[i].radius = 30;
|
||||||
|
explosion[i].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update game (one frame)
|
||||||
|
void UpdateGame(void)
|
||||||
|
{
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
if (IsKeyPressed('P')) pause = !pause;
|
||||||
|
|
||||||
|
if (!pause)
|
||||||
|
{
|
||||||
|
if (!ballOnAir) ballOnAir = UpdatePlayer(playerTurn); // If we are aiming
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (UpdateBall(playerTurn)) // If collision
|
||||||
|
{
|
||||||
|
// Game over logic
|
||||||
|
bool leftTeamAlive = false;
|
||||||
|
bool rightTeamAlive = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
||||||
|
{
|
||||||
|
if (player[i].isAlive)
|
||||||
|
{
|
||||||
|
if (player[i].isLeftTeam) leftTeamAlive = true;
|
||||||
|
if (!player[i].isLeftTeam) rightTeamAlive = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leftTeamAlive && rightTeamAlive)
|
||||||
|
{
|
||||||
|
ballOnAir = false;
|
||||||
|
ball.active = false;
|
||||||
|
|
||||||
|
playerTurn++;
|
||||||
|
|
||||||
|
if (playerTurn == MAX_PLAYERS) playerTurn = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gameOver = true;
|
||||||
|
|
||||||
|
// if (leftTeamAlive) left team wins
|
||||||
|
// if (rightTeamAlive) right team wins
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_ENTER))
|
||||||
|
{
|
||||||
|
InitGame();
|
||||||
|
gameOver = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw game (one frame)
|
||||||
|
void DrawGame(void)
|
||||||
|
{
|
||||||
|
BeginDrawing();
|
||||||
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
if (!gameOver)
|
||||||
|
{
|
||||||
|
// Draw buildings
|
||||||
|
for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(building[i].rectangle, building[i].color);
|
||||||
|
|
||||||
|
// Draw explosions
|
||||||
|
for (int i = 0; i < MAX_EXPLOSIONS; i++)
|
||||||
|
{
|
||||||
|
if (explosion[i].active) DrawCircle(explosion[i].position.x, explosion[i].position.y, explosion[i].radius, RAYWHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw players
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
||||||
|
{
|
||||||
|
if (player[i].isAlive)
|
||||||
|
{
|
||||||
|
if (player[i].isLeftTeam) DrawRectangle(player[i].position.x - player[i].size.x/2, player[i].position.y - player[i].size.y/2,
|
||||||
|
player[i].size.x, player[i].size.y, BLUE);
|
||||||
|
else DrawRectangle(player[i].position.x - player[i].size.x/2, player[i].position.y - player[i].size.y/2,
|
||||||
|
player[i].size.x, player[i].size.y, RED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw ball
|
||||||
|
if (ball.active) DrawCircle(ball.position.x, ball.position.y, ball.radius, MAROON);
|
||||||
|
|
||||||
|
// Draw the angle and the power of the aim, and the previous ones
|
||||||
|
if (!ballOnAir)
|
||||||
|
{
|
||||||
|
// Draw shot information
|
||||||
|
/*
|
||||||
|
if (player[playerTurn].isLeftTeam)
|
||||||
|
{
|
||||||
|
DrawText(FormatText("Previous Point %i, %i", (int)player[playerTurn].previousPoint.x, (int)player[playerTurn].previousPoint.y), 20, 20, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Previous Angle %i", player[playerTurn].previousAngle), 20, 50, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Previous Power %i", player[playerTurn].previousPower), 20, 80, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Point %i, %i", (int)player[playerTurn].aimingPoint.x, (int)player[playerTurn].aimingPoint.y), 20, 110, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Angle %i", player[playerTurn].aimingAngle), 20, 140, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Power %i", player[playerTurn].aimingPower), 20, 170, 20, DARKBLUE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DrawText(FormatText("Previous Point %i, %i", (int)player[playerTurn].previousPoint.x, (int)player[playerTurn].previousPoint.y), screenWidth*3/4, 20, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Previous Angle %i", player[playerTurn].previousAngle), screenWidth*3/4, 50, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Previous Power %i", player[playerTurn].previousPower), screenWidth*3/4, 80, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Point %i, %i", (int)player[playerTurn].aimingPoint.x, (int)player[playerTurn].aimingPoint.y), screenWidth*3/4, 110, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Angle %i", player[playerTurn].aimingAngle), screenWidth*3/4, 140, 20, DARKBLUE);
|
||||||
|
DrawText(FormatText("Aiming Power %i", player[playerTurn].aimingPower), screenWidth*3/4, 170, 20, DARKBLUE);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Draw aim
|
||||||
|
if (player[playerTurn].isLeftTeam)
|
||||||
|
{
|
||||||
|
// Previous aiming
|
||||||
|
DrawTriangle((Vector2){ player[playerTurn].position.x - player[playerTurn].size.x/4, player[playerTurn].position.y - player[playerTurn].size.y/4 },
|
||||||
|
(Vector2){ player[playerTurn].position.x + player[playerTurn].size.x/4, player[playerTurn].position.y + player[playerTurn].size.y/4 },
|
||||||
|
player[playerTurn].previousPoint, GRAY);
|
||||||
|
|
||||||
|
// Actual aiming
|
||||||
|
DrawTriangle((Vector2){ player[playerTurn].position.x - player[playerTurn].size.x/4, player[playerTurn].position.y - player[playerTurn].size.y/4 },
|
||||||
|
(Vector2){ player[playerTurn].position.x + player[playerTurn].size.x/4, player[playerTurn].position.y + player[playerTurn].size.y/4 },
|
||||||
|
player[playerTurn].aimingPoint, DARKBLUE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Previous aiming
|
||||||
|
DrawTriangle((Vector2){ player[playerTurn].position.x - player[playerTurn].size.x/4, player[playerTurn].position.y + player[playerTurn].size.y/4 },
|
||||||
|
(Vector2){ player[playerTurn].position.x + player[playerTurn].size.x/4, player[playerTurn].position.y - player[playerTurn].size.y/4 },
|
||||||
|
player[playerTurn].previousPoint, GRAY);
|
||||||
|
|
||||||
|
// Actual aiming
|
||||||
|
DrawTriangle((Vector2){ player[playerTurn].position.x - player[playerTurn].size.x/4, player[playerTurn].position.y + player[playerTurn].size.y/4 },
|
||||||
|
(Vector2){ player[playerTurn].position.x + player[playerTurn].size.x/4, player[playerTurn].position.y - player[playerTurn].size.y/4 },
|
||||||
|
player[playerTurn].aimingPoint, MAROON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
|
||||||
|
}
|
||||||
|
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
|
||||||
|
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload game variables
|
||||||
|
void UnloadGame(void)
|
||||||
|
{
|
||||||
|
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and Draw (one frame)
|
||||||
|
void UpdateDrawFrame(void)
|
||||||
|
{
|
||||||
|
UpdateGame();
|
||||||
|
DrawGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
// Additional module functions
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
static void InitBuildings(void)
|
||||||
|
{
|
||||||
|
// Horizontal generation
|
||||||
|
int currentWidth = 0;
|
||||||
|
|
||||||
|
// We make sure the absolute error randomly generated for each building, has as a minimum value the screenWidth.
|
||||||
|
// This way all the screen will be filled with buildings. Each building will have a different, random width.
|
||||||
|
|
||||||
|
float relativeWidth = 100/(100 - BUILDING_RELATIVE_ERROR);
|
||||||
|
float buildingWidthMean = (screenWidth*relativeWidth/MAX_BUILDINGS) + 1; // We add one to make sure we will cover the whole screen.
|
||||||
|
|
||||||
|
// Vertical generation
|
||||||
|
int currentHeighth = 0;
|
||||||
|
int grayLevel;
|
||||||
|
|
||||||
|
// Creation
|
||||||
|
for (int i = 0; i < MAX_BUILDINGS; i++)
|
||||||
|
{
|
||||||
|
// Horizontal
|
||||||
|
building[i].rectangle.x = currentWidth;
|
||||||
|
building[i].rectangle.width = GetRandomValue(buildingWidthMean*(100 - BUILDING_RELATIVE_ERROR/2)/100 + 1, buildingWidthMean*(100 + BUILDING_RELATIVE_ERROR)/100);
|
||||||
|
|
||||||
|
currentWidth += building[i].rectangle.width;
|
||||||
|
|
||||||
|
// Vertical
|
||||||
|
currentHeighth = GetRandomValue(BUILDING_MIN_RELATIVE_HEIGHT, BUILDING_MAX_RELATIVE_HEIGHT);
|
||||||
|
building[i].rectangle.y = screenHeight - (screenHeight*currentHeighth/100);
|
||||||
|
building[i].rectangle.height = screenHeight*currentHeighth/100 + 1;
|
||||||
|
|
||||||
|
// Color
|
||||||
|
grayLevel = GetRandomValue(BUILDING_MIN_GRAYSCALE_COLOR, BUILDING_MAX_GRAYSCALE_COLOR);
|
||||||
|
building[i].color = (Color){ grayLevel, grayLevel, grayLevel, 255 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void InitPlayers(void)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
||||||
|
{
|
||||||
|
player[i].isAlive = true;
|
||||||
|
|
||||||
|
// Decide the team of this player
|
||||||
|
if (i % 2 == 0) player[i].isLeftTeam = true;
|
||||||
|
else player[i].isLeftTeam = false;
|
||||||
|
|
||||||
|
// Now there is no AI
|
||||||
|
player[i].isPlayer = true;
|
||||||
|
|
||||||
|
// Set size, by default by now
|
||||||
|
player[i].size = (Vector2){ 40, 40 };
|
||||||
|
|
||||||
|
// Set position
|
||||||
|
if (player[i].isLeftTeam) player[i].position.x = GetRandomValue(screenWidth*MIN_PLAYER_POSITION/100, screenWidth*MAX_PLAYER_POSITION/100);
|
||||||
|
else player[i].position.x = screenWidth - GetRandomValue(screenWidth*MIN_PLAYER_POSITION/100, screenWidth*MAX_PLAYER_POSITION/100);
|
||||||
|
|
||||||
|
for (int j = 0; j < MAX_BUILDINGS; j++)
|
||||||
|
{
|
||||||
|
if (building[j].rectangle.x > player[i].position.x)
|
||||||
|
{
|
||||||
|
// Set the player in the center of the building
|
||||||
|
player[i].position.x = building[j-1].rectangle.x + building[j-1].rectangle.width/2;
|
||||||
|
// Set the player at the top of the building
|
||||||
|
player[i].position.y = building[j-1].rectangle.y - player[i].size.y/2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set statistics to 0
|
||||||
|
player[i].aimingPoint = player[i].position;
|
||||||
|
player[i].previousAngle = 0;
|
||||||
|
player[i].previousPower = 0;
|
||||||
|
player[i].previousPoint = player[i].position;
|
||||||
|
player[i].aimingAngle = 0;
|
||||||
|
player[i].aimingPower = 0;
|
||||||
|
|
||||||
|
player[i].impactPoint = (Vector2){ -100, -100 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool UpdatePlayer(int playerTurn)
|
||||||
|
{
|
||||||
|
// If we are aiming at the firing quadrant, we calculate the angle
|
||||||
|
if (GetMousePosition().y <= player[playerTurn].position.y)
|
||||||
|
{
|
||||||
|
// Left team
|
||||||
|
if (player[playerTurn].isLeftTeam && GetMousePosition().x >= player[playerTurn].position.x)
|
||||||
|
{
|
||||||
|
// Distance (calculating the fire power)
|
||||||
|
player[playerTurn].aimingPower = sqrt(pow(player[playerTurn].position.x - GetMousePosition().x, 2) + pow(player[playerTurn].position.y - GetMousePosition().y, 2));
|
||||||
|
// Calculates the angle via arcsin
|
||||||
|
player[playerTurn].aimingAngle = asin((player[playerTurn].position.y - GetMousePosition().y)/player[playerTurn].aimingPower)*RAD2DEG;
|
||||||
|
// Point of the screen we are aiming at
|
||||||
|
player[playerTurn].aimingPoint = GetMousePosition();
|
||||||
|
|
||||||
|
// Ball fired
|
||||||
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
|
{
|
||||||
|
player[playerTurn].previousPoint = player[playerTurn].aimingPoint;
|
||||||
|
player[playerTurn].previousPower = player[playerTurn].aimingPower;
|
||||||
|
player[playerTurn].previousAngle = player[playerTurn].aimingAngle;
|
||||||
|
ball.position = player[playerTurn].position;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Right team
|
||||||
|
else if (!player[playerTurn].isLeftTeam && GetMousePosition().x <= player[playerTurn].position.x)
|
||||||
|
{
|
||||||
|
// Distance (calculating the fire power)
|
||||||
|
player[playerTurn].aimingPower = sqrt(pow(player[playerTurn].position.x - GetMousePosition().x, 2) + pow(player[playerTurn].position.y - GetMousePosition().y, 2));
|
||||||
|
// Calculates the angle via arcsin
|
||||||
|
player[playerTurn].aimingAngle = asin((player[playerTurn].position.y - GetMousePosition().y)/player[playerTurn].aimingPower)*RAD2DEG;
|
||||||
|
// Point of the screen we are aiming at
|
||||||
|
player[playerTurn].aimingPoint = GetMousePosition();
|
||||||
|
|
||||||
|
// Ball fired
|
||||||
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
||||||
|
{
|
||||||
|
player[playerTurn].previousPoint = player[playerTurn].aimingPoint;
|
||||||
|
player[playerTurn].previousPower = player[playerTurn].aimingPower;
|
||||||
|
player[playerTurn].previousAngle = player[playerTurn].aimingAngle;
|
||||||
|
ball.position = player[playerTurn].position;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player[playerTurn].aimingPoint = player[playerTurn].position;
|
||||||
|
player[playerTurn].aimingPower = 0;
|
||||||
|
player[playerTurn].aimingAngle = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player[playerTurn].aimingPoint = player[playerTurn].position;
|
||||||
|
player[playerTurn].aimingPower = 0;
|
||||||
|
player[playerTurn].aimingAngle = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool UpdateBall(int playerTurn)
|
||||||
|
{
|
||||||
|
static int explosionNumber = 0;
|
||||||
|
|
||||||
|
// Activate ball
|
||||||
|
if (!ball.active)
|
||||||
|
{
|
||||||
|
if (player[playerTurn].isLeftTeam)
|
||||||
|
{
|
||||||
|
ball.speed.x = cos(player[playerTurn].previousAngle*DEG2RAD)*player[playerTurn].previousPower*3/DELTA_FPS;
|
||||||
|
ball.speed.y = -sin(player[playerTurn].previousAngle*DEG2RAD)*player[playerTurn].previousPower*3/DELTA_FPS;
|
||||||
|
ball.active = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ball.speed.x = -cos(player[playerTurn].previousAngle*DEG2RAD)*player[playerTurn].previousPower*3/DELTA_FPS;
|
||||||
|
ball.speed.y = -sin(player[playerTurn].previousAngle*DEG2RAD)*player[playerTurn].previousPower*3/DELTA_FPS;
|
||||||
|
ball.active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ball.position.x += ball.speed.x;
|
||||||
|
ball.position.y += ball.speed.y;
|
||||||
|
ball.speed.y += GRAVITY/DELTA_FPS;
|
||||||
|
|
||||||
|
// Collision
|
||||||
|
if (ball.position.x + ball.radius < 0) return true;
|
||||||
|
else if (ball.position.x - ball.radius > screenWidth) return true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Player collision
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircleRec(ball.position, ball.radius, (Rectangle){ player[i].position.x - player[i].size.x/2, player[i].position.y - player[i].size.y/2,
|
||||||
|
player[i].size.x, player[i].size.y }))
|
||||||
|
{
|
||||||
|
// We can't hit ourselves
|
||||||
|
if (i == playerTurn) return false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We set the impact point
|
||||||
|
player[playerTurn].impactPoint.x = ball.position.x;
|
||||||
|
player[playerTurn].impactPoint.y = ball.position.y + ball.radius;
|
||||||
|
|
||||||
|
// We destroy the player
|
||||||
|
player[i].isAlive = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Building collision
|
||||||
|
// NOTE: We only check building collision if we are not inside an explosion
|
||||||
|
for (int i = 0; i < MAX_BUILDINGS; i++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircles(ball.position, ball.radius, explosion[i].position, explosion[i].radius - ball.radius))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_BUILDINGS; i++)
|
||||||
|
{
|
||||||
|
if (CheckCollisionCircleRec(ball.position, ball.radius, building[i].rectangle))
|
||||||
|
{
|
||||||
|
// We set the impact point
|
||||||
|
player[playerTurn].impactPoint.x = ball.position.x;
|
||||||
|
player[playerTurn].impactPoint.y = ball.position.y + ball.radius;
|
||||||
|
|
||||||
|
// We create an explosion
|
||||||
|
explosion[explosionNumber].position = player[playerTurn].impactPoint;
|
||||||
|
explosion[explosionNumber].active = true;
|
||||||
|
explosionNumber++;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
@ -72,7 +72,7 @@ else
|
||||||
CFLAGS = -O2 -Wall -std=c99
|
CFLAGS = -O2 -Wall -std=c99
|
||||||
endif
|
endif
|
||||||
ifeq ($(PLATFORM),PLATFORM_WEB)
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3
|
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 --shell-file ../../templates/web_shell/shell.html
|
||||||
#-s ASSERTIONS=1 --preload-file resources
|
#-s ASSERTIONS=1 --preload-file resources
|
||||||
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
||||||
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# raylib - Advance Game
|
# raylib - Advance Game
|
||||||
#
|
#
|
||||||
# makefile to compile advance game
|
# makefile to compile advance game for desktop platforms, Raspberry Pi and HTML5 (emscripten)
|
||||||
#
|
#
|
||||||
# Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
# Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
||||||
#
|
#
|
||||||
|
|
@ -71,7 +71,7 @@ else
|
||||||
CFLAGS = -O2 -Wall -std=c99
|
CFLAGS = -O2 -Wall -std=c99
|
||||||
endif
|
endif
|
||||||
ifeq ($(PLATFORM),PLATFORM_WEB)
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 --preload-file resources -s ALLOW_MEMORY_GROWTH=1
|
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 --preload-file resources -s ALLOW_MEMORY_GROWTH=1 --shell-file ../../templates/web_shell/shell.html
|
||||||
#-s ASSERTIONS=1 --preload-file resources
|
#-s ASSERTIONS=1 --preload-file resources
|
||||||
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
||||||
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
||||||
|
|
@ -81,7 +81,7 @@ endif
|
||||||
ifeq ($(PLATFORM),PLATFORM_RPI)
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
INCLUDES = -I. -I../../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
|
INCLUDES = -I. -I../../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
|
||||||
else
|
else
|
||||||
INCLUDES = -I. -IC:/raylib/raylib/src -IC:/raylib/raylib/src
|
INCLUDES = -I. -I../../src
|
||||||
# external libraries headers
|
# external libraries headers
|
||||||
# GLFW3
|
# GLFW3
|
||||||
INCLUDES += -I../../external/glfw3/include
|
INCLUDES += -I../../external/glfw3/include
|
||||||
|
|
@ -95,7 +95,7 @@ endif
|
||||||
ifeq ($(PLATFORM),PLATFORM_RPI)
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
LFLAGS = -L. -L../../src -L/opt/vc/lib
|
LFLAGS = -L. -L../../src -L/opt/vc/lib
|
||||||
else
|
else
|
||||||
LFLAGS = -L. -LC:/raylib/raylib/src -L../../../src
|
LFLAGS = -L. -L../../src
|
||||||
# external libraries to link with
|
# external libraries to link with
|
||||||
# GLFW3
|
# GLFW3
|
||||||
LFLAGS += -L../../external/glfw3/lib/$(LIBPATH)
|
LFLAGS += -L../../external/glfw3/lib/$(LIBPATH)
|
||||||
|
|
@ -133,14 +133,14 @@ ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal
|
LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal
|
||||||
endif
|
endif
|
||||||
ifeq ($(PLATFORM),PLATFORM_WEB)
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
LIBS = C:/raylib/raylib/src/libraylib.bc
|
LIBS = ../../src/libraylib.bc
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# define additional parameters and flags for windows
|
# define additional parameters and flags for windows
|
||||||
ifeq ($(PLATFORM_OS),WINDOWS)
|
ifeq ($(PLATFORM_OS),WINDOWS)
|
||||||
# resources file contains windows exe icon
|
# resources file contains windows exe icon
|
||||||
# -Wl,--subsystem,windows hides the console window
|
# -Wl,--subsystem,windows hides the console window
|
||||||
WINFLAGS = C:/raylib/raylib/src/resources -Wl,--subsystem,windows
|
WINFLAGS = ../../src/resources -Wl,--subsystem,windows
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(PLATFORM),PLATFORM_WEB)
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
|
|
||||||
254
games/makefile
Normal file
254
games/makefile
Normal file
|
|
@ -0,0 +1,254 @@
|
||||||
|
#**************************************************************************************************
|
||||||
|
#
|
||||||
|
# raylib makefile for desktop platforms, Raspberry Pi and HTML5 (emscripten)
|
||||||
|
#
|
||||||
|
# Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
||||||
|
#
|
||||||
|
# This software is provided "as-is", without any express or implied warranty. In no event
|
||||||
|
# will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
#
|
||||||
|
# Permission is granted to anyone to use this software for any purpose, including commercial
|
||||||
|
# applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||||
|
#
|
||||||
|
# 1. The origin of this software must not be misrepresented; you must not claim that you
|
||||||
|
# wrote the original software. If you use this software in a product, an acknowledgment
|
||||||
|
# in the product documentation would be appreciated but is not required.
|
||||||
|
#
|
||||||
|
# 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||||
|
# as being the original software.
|
||||||
|
#
|
||||||
|
# 3. This notice may not be removed or altered from any source distribution.
|
||||||
|
#
|
||||||
|
#**************************************************************************************************
|
||||||
|
|
||||||
|
# define raylib platform to compile for
|
||||||
|
# possible platforms: PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB
|
||||||
|
# WARNING: To compile to HTML5, code must be redesigned to use emscripten.h and emscripten_set_main_loop()
|
||||||
|
PLATFORM ?= PLATFORM_DESKTOP
|
||||||
|
|
||||||
|
# determine PLATFORM_OS in case PLATFORM_DESKTOP selected
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
# No uname.exe on MinGW!, but OS=Windows_NT on Windows! ifeq ($(UNAME),Msys) -> Windows
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
PLATFORM_OS=WINDOWS
|
||||||
|
LIBPATH=win32
|
||||||
|
else
|
||||||
|
UNAMEOS:=$(shell uname)
|
||||||
|
ifeq ($(UNAMEOS),Linux)
|
||||||
|
PLATFORM_OS=LINUX
|
||||||
|
LIBPATH=linux
|
||||||
|
else
|
||||||
|
ifeq ($(UNAMEOS),Darwin)
|
||||||
|
PLATFORM_OS=OSX
|
||||||
|
LIBPATH=osx
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define compiler: gcc for C program, define as g++ for C++
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
# define emscripten compiler
|
||||||
|
CC = emcc
|
||||||
|
else
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
# define llvm compiler for mac
|
||||||
|
CC = clang
|
||||||
|
else
|
||||||
|
# define default gcc compiler
|
||||||
|
CC = gcc
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define compiler flags:
|
||||||
|
# -O2 defines optimization level
|
||||||
|
# -Wall turns on most, but not all, compiler warnings
|
||||||
|
# -std=c99 use standard C from 1999 revision
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
CFLAGS = -O2 -Wall -std=gnu99 -fgnu89-inline
|
||||||
|
else
|
||||||
|
CFLAGS = -O2 -Wall -std=c99
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 --shell-file ../../templates/web_shell/shell.html
|
||||||
|
#-s ASSERTIONS=1 # to check for memory allocation errors (-O1 disables it)
|
||||||
|
#-s ALLOW_MEMORY_GROWTH=1 # to allow memory resizing
|
||||||
|
#-s TOTAL_MEMORY=16777216 # to specify heap memory size (default = 16MB)
|
||||||
|
endif
|
||||||
|
|
||||||
|
#CFLAGSEXTRA = -Wextra -Wmissing-prototypes -Wstrict-prototypes
|
||||||
|
|
||||||
|
# define any directories containing required header files
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
INCLUDES = -I. -I../src -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads
|
||||||
|
else
|
||||||
|
INCLUDES = -I. -I../src
|
||||||
|
# external libraries headers
|
||||||
|
# GLFW3
|
||||||
|
INCLUDES += -I../external/glfw3/include
|
||||||
|
# GLEW - Not required any more, replaced by GLAD
|
||||||
|
#INCLUDES += -I../external/glew/include
|
||||||
|
# OpenAL Soft
|
||||||
|
INCLUDES += -I../external/openal_soft/include
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define library paths containing required libs
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
LFLAGS = -L. -L../src -L/opt/vc/lib
|
||||||
|
else
|
||||||
|
LFLAGS = -L. -L../src
|
||||||
|
# external libraries to link with
|
||||||
|
# GLFW3
|
||||||
|
LFLAGS += -L../external/glfw3/lib/$(LIBPATH)
|
||||||
|
ifneq ($(PLATFORM_OS),OSX)
|
||||||
|
# OpenAL Soft
|
||||||
|
LFLAGS += -L../external/openal_soft/lib/$(LIBPATH)
|
||||||
|
# GLEW: Not used, replaced by GLAD
|
||||||
|
#LFLAGS += -L../../external/glew/lib/$(LIBPATH)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define any libraries to link into executable
|
||||||
|
# if you want to link libraries (libname.so or libname.a), use the -lname
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
ifeq ($(PLATFORM_OS),LINUX)
|
||||||
|
# libraries for Debian GNU/Linux desktop compiling
|
||||||
|
# requires the following packages:
|
||||||
|
# libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev
|
||||||
|
LIBS = -lraylib -lglfw3 -lGLEW -lGL -lopenal -lm -pthread
|
||||||
|
# on XWindow could require also below libraries, just uncomment
|
||||||
|
#LIBS += -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor
|
||||||
|
else
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
# libraries for OS X 10.9 desktop compiling
|
||||||
|
# requires the following packages:
|
||||||
|
# libglfw3-dev libopenal-dev libglew-dev libegl1-mesa-dev
|
||||||
|
LIBS = -lraylib -lglfw -framework OpenGL -framework OpenAl -framework Cocoa
|
||||||
|
else
|
||||||
|
# libraries for Windows desktop compiling
|
||||||
|
# NOTE: GLFW3 and OpenAL Soft libraries should be installed
|
||||||
|
LIBS = -lraylib -lglfw3 -lopengl32 -lopenal32 -lgdi32
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
# libraries for Raspberry Pi compiling
|
||||||
|
# NOTE: OpenAL Soft library should be installed (libopenal1 package)
|
||||||
|
LIBS = -lraylib -lGLESv2 -lEGL -lpthread -lrt -lm -lbcm_host -lopenal
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
# NOTE: Set the correct path to libraylib.bc
|
||||||
|
LIBS = ../src/libraylib.bc
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define additional parameters and flags for windows
|
||||||
|
ifeq ($(PLATFORM_OS),WINDOWS)
|
||||||
|
# resources file contains windows exe icon
|
||||||
|
# -Wl,--subsystem,windows hides the console window
|
||||||
|
WINFLAGS = ../src/resources -Wl,--subsystem,windows
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
EXT = .html
|
||||||
|
endif
|
||||||
|
|
||||||
|
# define all object files required
|
||||||
|
SAMPLES = \
|
||||||
|
arkanoid \
|
||||||
|
asteroids \
|
||||||
|
asteroids_survival \
|
||||||
|
floppy \
|
||||||
|
gold_fever \
|
||||||
|
gorilas \
|
||||||
|
missile_commander \
|
||||||
|
pang \
|
||||||
|
snake \
|
||||||
|
space_invaders \
|
||||||
|
tetris \
|
||||||
|
fix_dylib \
|
||||||
|
|
||||||
|
|
||||||
|
# typing 'make' will invoke the first target entry in the file,
|
||||||
|
# in this case, the 'default' target entry is raylib
|
||||||
|
default: samples
|
||||||
|
|
||||||
|
# compile all game samples
|
||||||
|
samples: $(SAMPLES)
|
||||||
|
|
||||||
|
# compile game sample - arkanoid
|
||||||
|
arkanoid: arkanoid.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - steroids
|
||||||
|
asteroids: asteroids.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - asteroids_survival
|
||||||
|
asteroids_survival: asteroids_survival.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - floppy
|
||||||
|
floppy: floppy.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - gold_fever
|
||||||
|
gold_fever: gold_fever.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - gorilas
|
||||||
|
gorilas: gorilas.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - missile_commander
|
||||||
|
missile_commander: missile_commander.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - pang
|
||||||
|
pang: pang.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - snake
|
||||||
|
snake: snake.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - space_invaders
|
||||||
|
space_invaders: space_invaders.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# compile game sample - tetris
|
||||||
|
tetris: tetris.c
|
||||||
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS)
|
||||||
|
|
||||||
|
# fix dylib install path name for each executable (MAC)
|
||||||
|
fix_dylib:
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
find . -type f -perm +ugo+x -print0 | xargs -t -0 -R 1 -I file install_name_tool -change libglfw.3.0.dylib ../external/glfw3/lib/osx/libglfw.3.0.dylib file
|
||||||
|
endif
|
||||||
|
|
||||||
|
# clean everything
|
||||||
|
clean:
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_DESKTOP)
|
||||||
|
ifeq ($(PLATFORM_OS),OSX)
|
||||||
|
find . -type f -perm +ugo+x -delete
|
||||||
|
rm -f *.o
|
||||||
|
else
|
||||||
|
ifeq ($(PLATFORM_OS),LINUX)
|
||||||
|
find -type f -executable | xargs file -i | grep -E 'x-object|x-archive|x-sharedlib|x-executable' | rev | cut -d ':' -f 2- | rev | xargs rm -f
|
||||||
|
else
|
||||||
|
del *.o *.exe
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_RPI)
|
||||||
|
find . -type f -executable -delete
|
||||||
|
rm -f *.o
|
||||||
|
endif
|
||||||
|
ifeq ($(PLATFORM),PLATFORM_WEB)
|
||||||
|
del *.o *.html *.js
|
||||||
|
endif
|
||||||
|
@echo Cleaning done
|
||||||
|
|
||||||
|
# instead of defining every module one by one, we can define a pattern
|
||||||
|
# this pattern below will automatically compile every module defined on $(OBJS)
|
||||||
|
#%.exe : %.c
|
||||||
|
# $(CC) -o $@ $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM)
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue