Created
February 15, 2012 23:28
-
-
Save cloakdood/1840022 to your computer and use it in GitHub Desktop.
Pong Game made in CSFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************** | |
Pong | |
Created by cloakdood | |
Enjoi. | |
******************************/ | |
#include <SFML/Graphics.h> | |
#include <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
void movePaddles (float directionX, float directionY, | |
float speed, sfShape* paddleRed, | |
sfShape* paddleBlue, float ElapsedTime, | |
int aiMode, sfInput *keyInput, | |
sfShape *ball, int aiDifficulty, | |
float aiSpeed, float paddleSpeed); | |
void paddleLimits (sfShape* Shape); | |
float predictBall (sfShape* ball, float directionX, | |
float directionY, float speed, | |
int aiDifficulty); | |
void detectCollision (float *pdirectionX, float *pdirectionY, | |
float *pspeedFactor, float paddleBlueY, | |
float paddleRedY, float speed, | |
int *ppointsBlue, int *ppointsRed, | |
int *ppointWasScored, float incrementSpeed, | |
sfShape* ball); | |
float shiftDirectionY (float newBallY, float paddle, | |
float directionY); | |
void mainMenu (sfRenderWindow *window, sfFont *font, | |
int mainMenuSelection); | |
void about (sfRenderWindow *window, sfFont *font); | |
void simpleSettingsMenu (sfRenderWindow *window, sfFont *font, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int aiMode, int aiPreset, int ballSpeedPreset, | |
int scoreToWin); | |
void pauseMenu (sfRenderWindow *window, sfFont *font, | |
int pauseSelection); | |
void settingsMenu (sfRenderWindow *window, sfFont *font, | |
int settingSelection); | |
void aiSettingsMenu (sfFont *Font, sfRenderWindow *window, | |
int aiSettingSelection, int aiDifficulty, | |
int aiMode, float aiSpeed, int aiPreset, | |
int changeAISettings); | |
void changingAISettings (sfEvent Event, int aiSettingSelection, | |
int *paiPreset, int *paiMode, | |
int *paiDifficulty, float *paiSpeed, | |
int changeAISettings, int inAISettings, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int inSimpleSettings); | |
void gameSettingsMenu (sfRenderWindow *window, sfFont *font, | |
int gameSettingSelection, float ballSpeedPreset, | |
float initialSpeed, float incrementSpeed, | |
float paddleSpeed, int scoreToWin, | |
float changeGameSettings); | |
void changingGameSettings(sfEvent Event, int gameSettingSelection, | |
int *pballSpeedPreset, float *pinitialSpeed, | |
float *pincrementSpeed, float *ppaddleSpeed, | |
int changeGameSettings, int inGameSettings, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int inSimpleSettings, int *pscoreToWin); | |
void selectMenu (sfString *string); | |
void formatMenuOptions (sfString *string, char stringliteral[], | |
sfFont *font, float size, | |
float x, float y, sfColor color); | |
void displayFadingScore (float scoreColorAlpha, sfRenderWindow *window, | |
sfString *blueScoreText, sfString *redScoreText); | |
void gameOverScreen (sfRenderWindow *window, sfFont *font, | |
int pointsRed, int pointsBlue, int scoreToWin); | |
int frame=0; | |
int main() | |
{ | |
// Create main window | |
sfVideoMode mode = {1280, 720, 32}; | |
sfWindowSettings settings = {24, 8, 0}; | |
sfRenderWindow *App = sfRenderWindow_Create (mode, "Bleep Bloop!", sfClose, settings); | |
sfRenderWindow_UseVerticalSync(App, sfTrue); | |
int resolutionPreset=2; | |
unsigned int resolution[2]={1280,720}; | |
sfClock *Clock = sfClock_Create(); | |
sfInput *keyInput; | |
keyInput = sfRenderWindow_GetInput (App); | |
// Set Colors | |
sfColor rectOutlineColorBlue= {25,25,255,255}; | |
sfColor rectOutlineColorRed = {255,25,25,255}; | |
sfColor rectInsideColorBlue = {55,55,255,100}; | |
sfColor rectInsideColorRed = {255,25,55,100}; | |
sfColor lineDarkBlue = {55,55,255,80}; | |
sfColor lineDarkRed = {255,55,55,80}; | |
sfColor ballGlow = {255,255,255,150}; | |
sfColor pauseScreenColor = {0,0,0,175}; | |
sfColor scoreTextColor = {255,255,255,255}; | |
float scoreColorAlpha = 0; | |
// Create Shapes | |
sfShape *paddleBlue = sfShape_CreateRectangle (20,270,35,450,rectInsideColorBlue,2.0f,rectOutlineColorBlue); | |
sfShape *paddleRed = sfShape_CreateRectangle (1260,270,1245,450,rectInsideColorRed,2.0f,rectOutlineColorRed); | |
sfShape *centerLine = sfShape_CreateLine (640,0,640,720,2,sfWhite,0.0f,sfWhite); | |
sfShape *ball = sfShape_CreateRectangle (634,354,646,366,ballGlow,2.0f,sfWhite); | |
sfShape *pauseScreen= sfShape_CreateRectangle (0,0,1280,720,pauseScreenColor,0.0f,sfWhite); | |
sfShape *trail[5]; | |
// Grid | |
int gridLoop; | |
sfShape *blueLineVert[16], *redLineVert[16]; | |
sfShape *blueLineHoriz[18], *redLineHoriz[18]; | |
for (gridLoop=0;gridLoop<16;gridLoop++) | |
{ | |
blueLineVert[gridLoop]= sfShape_CreateLine (40*gridLoop,0,40*gridLoop,720,1,lineDarkBlue,0,sfWhite); | |
redLineVert[gridLoop] = sfShape_CreateLine ((40*gridLoop)+640,0,(40*gridLoop)+640,720,1,lineDarkRed,0,sfWhite); | |
} | |
for (gridLoop=0;gridLoop<18;gridLoop++) | |
{ | |
blueLineHoriz[gridLoop]= sfShape_CreateLine (0,40*gridLoop,640,40*gridLoop,1,lineDarkBlue,0,sfWhite); | |
redLineHoriz[gridLoop] = sfShape_CreateLine (640,40*gridLoop,1280,40*gridLoop,1,lineDarkRed,0,sfWhite); | |
} | |
sfImage *background = sfImage_CreateFromFile("background2.png"); | |
if (!background) | |
{ | |
printf("\n\nFailure to load image \"background2.png\"\n\n"); | |
return EXIT_FAILURE; | |
} | |
sfSprite *backgroundSprite = sfSprite_Create(); | |
sfSprite_SetImage(backgroundSprite, background); | |
sfSprite_SetX(backgroundSprite, 0); | |
sfSprite_SetY(backgroundSprite, 0); | |
// Font and Strings | |
sfFont *Font = sfFont_CreateFromFile ("OCRAStd.otf",64,NULL); | |
if (!Font) | |
{ | |
printf("\n\nFailure to load font \"OCRAStd.otf\"\n\n"); | |
return EXIT_FAILURE; | |
} | |
int pointsBlue=0; | |
int pointsRed=0; | |
char stringBlue[20]="\0"; | |
sprintf(stringBlue,"%i",pointsBlue); | |
char stringRed[20]="\0"; | |
sprintf(stringRed,"%i",pointsRed); | |
sfString *blueScoreText = sfString_Create(); | |
formatMenuOptions(blueScoreText, stringBlue, Font, 42, 540, 20, scoreTextColor); | |
sfString *redScoreText = sfString_Create(); | |
formatMenuOptions(redScoreText, stringRed, Font, 42, 711, 20, scoreTextColor); | |
// Ball Variables | |
float directionX=-1; | |
float directionY=sfRandom_Float(-0.5625f, 0.5625f); | |
float *pdirectionX=&directionX; | |
float *pdirectionY=&directionY; | |
float speedFactor=11.5; | |
float *pspeedFactor=&speedFactor; | |
float speed=1/sqrt((directionX*directionX)+(directionY*directionY))*speedFactor; | |
// Gameplay Variables | |
int ballSpeedPreset=1, *pballSpeedPreset=&ballSpeedPreset; | |
float initialSpeed=12.5, *pinitialSpeed=&initialSpeed; | |
float incrementSpeed=1.75, *pincrementSpeed=&incrementSpeed; | |
float paddleSpeed=.15, *ppaddleSpeed=&paddleSpeed; | |
int scoreToWin=10, *pscoreToWin=&scoreToWin; | |
// AI Variables | |
int aiPreset=1, *paiPreset=&aiPreset; | |
int aiMode=1, *paiMode=&aiMode; | |
int aiDifficulty=10,*paiDifficulty=&aiDifficulty; | |
float aiSpeed=0.10, *paiSpeed=&aiSpeed; | |
// Menu Variables | |
int inMainMenu=1; | |
int mainMenuSelection=0; | |
int inAbout=0; | |
int gameIsPaused=0; | |
int pauseSelection=0; | |
int inAISettings=0; | |
int aiSettingSelection=0; | |
int changeAISettings=0; | |
int inSettings=0; | |
int settingSelection=0; | |
int inGameSettings=0; | |
int gameSettingSelection=0; | |
int changeGameSettings=0; | |
int inSimpleSettings=0; | |
int simpleSettingsSelection=0; | |
int changeSimpleSettings=0; | |
// Diagnostics | |
int diagnosticsOn=0; | |
float fps; | |
char cfps[15]; | |
sfString *showfps = sfString_Create(); | |
char cframe[15]; | |
sfString *stringframe = sfString_Create(); | |
char cdirectionX[15]; | |
sfString *stringdirectionX = sfString_Create(); | |
char cdirectionY[15]; | |
sfString *stringdirectionY = sfString_Create(); | |
char cspeed[15]; | |
sfString *stringspeed = sfString_Create(); | |
char cspeedFactor[15]; | |
sfString *stringspeedFactor = sfString_Create(); | |
char cballX[15]; | |
sfString *stringballX = sfString_Create(); | |
char cballY[15]; | |
sfString *stringballY = sfString_Create(); | |
// Ball Trail Variables | |
int ballTrailOn=1; | |
int i; | |
float coordinates[5][2]; | |
for (i=0;i<5;i++) | |
{ | |
coordinates[i][0]=0.0f; | |
coordinates[i][1]=0.0f; | |
trail[i] = sfShape_CreateCircle(640.0f, 360.0f, 6.4f, sfWhite, 0.0f, sfWhite); | |
} | |
// Other Variables | |
int frameCounter=0; | |
int frameCounterFade=0; | |
int gameOverFrameCounter=0; | |
int pointWasScored=1; | |
int inGameOver=0; | |
sfRenderWindow_SetFramerateLimit(App,60); | |
// Start game loop | |
while (sfRenderWindow_IsOpened(App)) | |
{ | |
frame++; | |
// Get time | |
float ElapsedTime = (1.f / sfClock_GetTime(Clock)); | |
sfClock_Reset(Clock); | |
if (ElapsedTime > 60) | |
ElapsedTime = 60; | |
if (ElapsedTime < 30) | |
ElapsedTime = 30; | |
// Diagnostics | |
float ballX=sfShape_GetX(ball); | |
float ballY=sfShape_GetY(ball); | |
fps = ElapsedTime; | |
sprintf(cfps,"%f",fps); | |
formatMenuOptions(showfps, cfps, Font, 10, 0, 0, sfWhite); | |
sprintf(cframe,"%i",frame); | |
formatMenuOptions(stringframe, cframe, Font, 10, 90, 0, sfWhite); | |
sprintf(cdirectionX,"%f",directionX); | |
formatMenuOptions(stringdirectionX, cdirectionX, Font, 10, 0, 12, sfWhite); | |
sprintf(cdirectionY,"%f",directionY); | |
formatMenuOptions(stringdirectionY, cdirectionY, Font, 10, 90, 12, sfWhite); | |
sprintf(cspeed,"%f",speed); | |
formatMenuOptions(stringspeed, cspeed, Font, 10, 0, 24, sfWhite); | |
sprintf(cspeedFactor,"%f",speedFactor); | |
formatMenuOptions(stringspeedFactor, cspeedFactor, Font, 10, 90, 24, sfWhite); | |
sprintf(cballX,"%f",ballX); | |
formatMenuOptions(stringballX, cballX, Font, 10, 0, 36, sfWhite); | |
sprintf(cballY,"%f",ballY); | |
formatMenuOptions(stringballY, cballY, Font, 10, 90, 36, sfWhite); | |
//////////// Process events /////////// | |
sfEvent Event; | |
while (sfRenderWindow_GetEvent(App, &Event)) | |
{ | |
if (Event.Type == sfEvtClosed) | |
sfRenderWindow_Close(App); | |
// Ball trail on/off | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyF1) | |
ballTrailOn=!ballTrailOn; | |
// Diagnostics on/off | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyF2) | |
diagnosticsOn=!diagnosticsOn; | |
// Change Resolution | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyF3 && resolutionPreset>0) | |
resolutionPreset--; | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyF4 && resolutionPreset<5) | |
resolutionPreset++; | |
if (resolutionPreset==0) | |
{ | |
resolution[0]=640; | |
resolution[1]=360+22; | |
} | |
else if (resolutionPreset==1) | |
{ | |
resolution[0]=960; | |
resolution[1]=540+22; | |
} | |
else if (resolutionPreset==2) | |
{ | |
resolution[0]=1280; | |
resolution[1]=720+22; | |
} | |
else if (resolutionPreset==3) | |
{ | |
resolution[0]=1600; | |
resolution[1]=900+22; | |
} | |
else if (resolutionPreset==4) | |
{ | |
resolution[0]=1920; | |
resolution[1]=1080+22; | |
} | |
else if (resolutionPreset==5) | |
{ | |
resolution[0]=2560; | |
resolution[1]=1440+22; | |
} | |
if (Event.Type == sfEvtKeyPressed && (Event.Key.Code == sfKeyF3 || Event.Key.Code == sfKeyF4)) | |
sfRenderWindow_SetSize (App, resolution[0], resolution[1]); | |
// Pause Game | |
if (Event.Type == sfEvtLostFocus && !inMainMenu) | |
{ | |
if (!gameIsPaused) | |
gameIsPaused=1; | |
} | |
// Escape | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyEscape) | |
{ | |
if (inMainMenu && !inSettings && !inSimpleSettings && !inAbout) | |
{ | |
sfRenderWindow_Close(App); | |
} | |
else if (inAbout) | |
{ | |
inAbout=0; | |
} | |
if (inSimpleSettings && !changeSimpleSettings) | |
{ | |
inSimpleSettings=!inSimpleSettings; | |
} | |
else if (changeSimpleSettings) | |
{ | |
changeSimpleSettings=0; | |
} | |
if (!gameIsPaused && !inMainMenu && !inGameOver) | |
{ | |
gameIsPaused=1; | |
pauseSelection=0; | |
} | |
else if (gameIsPaused && !inSettings) | |
{ | |
gameIsPaused=0; | |
pauseSelection=0; | |
} | |
else if (inSettings && !inAISettings && !inGameSettings) | |
{ | |
inSettings=0; | |
} | |
else if (inGameSettings && !changeGameSettings) | |
{ | |
inGameSettings=0; | |
} | |
else if (changeGameSettings) | |
{ | |
changeGameSettings=0; | |
} | |
else if (inAISettings && !changeAISettings) | |
{ | |
inAISettings=0; | |
} | |
else if (changeAISettings) | |
{ | |
changeAISettings=0; | |
} | |
} | |
// Enter | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyReturn) | |
{ | |
/////////// If Game is Paused /////////// | |
if (inMainMenu && !inSettings && !inSimpleSettings && !inAbout) | |
{ | |
if (mainMenuSelection==0) | |
{ | |
inSimpleSettings=1; | |
simpleSettingsSelection=0; | |
} | |
else if (mainMenuSelection==1) | |
{ | |
inSettings=1; | |
settingSelection=0; | |
} | |
else if (mainMenuSelection==2) | |
{ | |
inAbout=1; | |
} | |
else if (mainMenuSelection==3) | |
{ | |
sfRenderWindow_Close(App); | |
} | |
} | |
else if (inAbout) | |
{ | |
inAbout=0; | |
} | |
else if (inSimpleSettings) | |
{ | |
if (simpleSettingsSelection==0) | |
{ | |
inMainMenu=!inMainMenu; | |
inSimpleSettings=!inSimpleSettings; | |
pointWasScored=1; | |
pointsBlue=0; | |
pointsRed=0; | |
directionX=-1; | |
} | |
else if (simpleSettingsSelection==1) | |
changeSimpleSettings=!changeSimpleSettings; | |
else if (simpleSettingsSelection==2) | |
changeSimpleSettings=!changeSimpleSettings; | |
else if (simpleSettingsSelection==3) | |
changeSimpleSettings=!changeSimpleSettings; | |
else if (simpleSettingsSelection==4) | |
changeSimpleSettings=!changeSimpleSettings; | |
else if (simpleSettingsSelection==5) | |
inSimpleSettings=!inSimpleSettings; | |
} | |
else if (gameIsPaused && !inSettings) | |
{ | |
if (pauseSelection==0) | |
{ | |
gameIsPaused=0; | |
pauseSelection=0; | |
} | |
else if (pauseSelection==1) | |
{ | |
inSettings=1; | |
settingSelection=0; | |
gameSettingSelection=0; | |
aiSettingSelection=0; | |
} | |
else if (pauseSelection==2) | |
{ | |
gameIsPaused=!gameIsPaused; | |
inMainMenu=!inMainMenu; | |
sfShape_SetY(paddleRed, 0); | |
sfShape_SetY(paddleBlue, 0); | |
sfShape_SetX(ball,0); | |
sfShape_SetY(ball,0); | |
directionY=0; | |
speedFactor=0; | |
speed=0; | |
} | |
} | |
else if (inMainMenu && !inSettings && mainMenuSelection==1) | |
{ | |
inSettings=1; | |
settingSelection=0; | |
gameSettingSelection=0; | |
aiSettingSelection=0; | |
} | |
else if (inSettings && !inAISettings && !inGameSettings) | |
{ | |
if (settingSelection==0) | |
{ | |
inAISettings=1; | |
aiSettingSelection=0; | |
} | |
else if (settingSelection==1) | |
{ | |
inGameSettings=1; | |
gameSettingSelection=0; | |
} | |
else if (settingSelection==2) | |
{ | |
inSettings=0; | |
} | |
} | |
else if (inGameSettings && !changeGameSettings) | |
{ | |
if (gameSettingSelection==0) | |
{ | |
changeGameSettings=1; | |
} | |
else if (gameSettingSelection==1) | |
{ | |
changeGameSettings=1; | |
} | |
else if (gameSettingSelection==2) | |
{ | |
changeGameSettings=1; | |
} | |
else if (gameSettingSelection==3) | |
{ | |
changeGameSettings=1; | |
} | |
else if (gameSettingSelection==4) | |
{ | |
changeGameSettings=1; | |
} | |
else if (gameSettingSelection==5) | |
{ | |
inGameSettings=0; | |
} | |
} | |
else if (changeGameSettings) | |
{ | |
changeGameSettings=0; | |
} | |
else if (inAISettings && !changeAISettings) | |
{ | |
if (aiSettingSelection==0) | |
{ | |
changeAISettings=1; | |
} | |
else if (aiSettingSelection==1) | |
{ | |
changeAISettings=1; | |
} | |
else if (aiSettingSelection==2) | |
{ | |
changeAISettings=1; | |
} | |
else if (aiSettingSelection==3) | |
{ | |
changeAISettings=1; | |
} | |
else if (aiSettingSelection==4) | |
{ | |
inAISettings=0; | |
} | |
} | |
else if (changeAISettings) | |
{ | |
changeAISettings=0; | |
} | |
} | |
// Up or Down | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyUp) | |
{ | |
if (inMainMenu && !inSettings && !inSimpleSettings) | |
{ | |
mainMenuSelection--; | |
inAbout=0; | |
} | |
else if (inSimpleSettings) | |
{ | |
simpleSettingsSelection--; | |
changeSimpleSettings=0; | |
} | |
else if (gameIsPaused && !inSettings) | |
{ | |
pauseSelection--; | |
} | |
else if (inSettings && !inAISettings && !inGameSettings) | |
{ | |
settingSelection--; | |
} | |
else if (inGameSettings) | |
{ | |
changeGameSettings=0; | |
gameSettingSelection--; | |
} | |
else if (inAISettings) | |
{ | |
changeAISettings=0; | |
aiSettingSelection--; | |
} | |
} | |
if (Event.Type == sfEvtKeyPressed && Event.Key.Code == sfKeyDown) | |
{ | |
if (inMainMenu && !inSettings && !inSimpleSettings) | |
{ | |
mainMenuSelection++; | |
inAbout=0; | |
} | |
else if (inSimpleSettings) | |
{ | |
simpleSettingsSelection++; | |
changeSimpleSettings=0; | |
} | |
else if (gameIsPaused && !inSettings) | |
{ | |
pauseSelection++; | |
} | |
else if (inSettings && !inAISettings && !inGameSettings) | |
{ | |
settingSelection++; | |
} | |
else if (inGameSettings) | |
{ | |
changeGameSettings=0; | |
gameSettingSelection++; | |
} | |
else if (inAISettings) | |
{ | |
changeAISettings=0; | |
aiSettingSelection++; | |
} | |
} | |
// AI Settings + Change Simple Settings | |
if (Event.Type == sfEvtKeyPressed && (changeAISettings || changeSimpleSettings)) | |
changingAISettings(Event, aiSettingSelection, paiPreset, | |
paiMode, paiDifficulty, paiSpeed, | |
changeAISettings, inAISettings, simpleSettingsSelection, | |
changeSimpleSettings, inSimpleSettings); | |
if (Event.Type == sfEvtKeyPressed && (changeGameSettings || changeSimpleSettings)) | |
changingGameSettings(Event, gameSettingSelection, pballSpeedPreset, | |
pinitialSpeed, pincrementSpeed, ppaddleSpeed, | |
changeGameSettings, inGameSettings, simpleSettingsSelection, | |
changeSimpleSettings, inSimpleSettings, pscoreToWin); | |
// Top or bottom of menus | |
if (mainMenuSelection==-1) | |
mainMenuSelection=3; | |
if (mainMenuSelection==4) | |
mainMenuSelection=0; | |
if (simpleSettingsSelection==-1) | |
simpleSettingsSelection=5; | |
if (simpleSettingsSelection==6) | |
simpleSettingsSelection=0; | |
if (pauseSelection==-1) | |
pauseSelection=2; | |
if (pauseSelection==3) | |
pauseSelection=0; | |
if (settingSelection==-1) | |
settingSelection=2; | |
if (settingSelection==3) | |
settingSelection=0; | |
if (gameSettingSelection==-1) | |
gameSettingSelection=5; | |
if (gameSettingSelection==6) | |
gameSettingSelection=0; | |
if (aiSettingSelection==-1) | |
aiSettingSelection=4; | |
if (aiSettingSelection==5) | |
aiSettingSelection=0; | |
} | |
/////////// Main Game Logic /////////// | |
if (!gameIsPaused && !inMainMenu) | |
{ | |
//////////// Move Paddle /////////// | |
movePaddles(directionX, directionY, speed, | |
paddleRed, paddleBlue, ElapsedTime, | |
aiMode, keyInput, ball, aiDifficulty, | |
aiSpeed, paddleSpeed); | |
////////// On Point Score - Display Points ///////// | |
if (pointWasScored) | |
{ | |
sfShape_SetX(ball,0); | |
sfShape_SetY(ball,0); | |
directionY=0; | |
speedFactor=0; | |
speed=0; | |
stringBlue[10]='\0'; | |
sprintf(stringBlue,"%i",pointsBlue); | |
sfString_SetText(blueScoreText,stringBlue); | |
stringRed[10]='\0'; | |
sprintf(stringRed,"%i",pointsRed); | |
sfString_SetText(redScoreText,stringRed); | |
frameCounter++; | |
if (frameCounter>=75 && !inGameOver) | |
{ | |
pointWasScored=0; | |
frameCounter=0; | |
speedFactor=initialSpeed; | |
directionY=sfRandom_Float(-0.5625f, 0.5625f); | |
} | |
} | |
//////////// Ball Handling and Collision Detection /////////// | |
if (!pointWasScored) | |
{ | |
sfShape_Move(ball,directionX*speed,directionY*speed); | |
int *ppointWasScored=&pointWasScored; | |
int *ppointsBlue=&pointsBlue; | |
int *ppointsRed=&pointsRed; | |
float paddleBlueY=sfShape_GetY(paddleBlue); | |
float paddleRedY=sfShape_GetY(paddleRed); | |
detectCollision(pdirectionX, pdirectionY, | |
pspeedFactor, paddleBlueY, | |
paddleRedY, speed, | |
ppointsBlue, ppointsRed, | |
ppointWasScored, incrementSpeed, | |
ball); | |
speed=1/sqrt((directionX*directionX)+(directionY*directionY))*speedFactor*52*(1.f/ElapsedTime); | |
} | |
//////// Check for Game Over //////// | |
if ((pointsRed>=scoreToWin || pointsBlue>=scoreToWin) && scoreToWin>0) | |
inGameOver=1; | |
} // - main loop | |
///////////// Game Over stuff //////////// | |
if (inGameOver) | |
{ | |
pointWasScored=1; | |
gameOverFrameCounter++; | |
if (gameOverFrameCounter>=180) | |
{ | |
inGameOver=0; | |
inMainMenu=1; | |
frameCounter=0; | |
gameOverFrameCounter=0; | |
pointsRed=0; | |
pointsBlue=0; | |
sfShape_SetY(paddleRed, 0); | |
sfShape_SetY(paddleBlue, 0); | |
} | |
} | |
//////////// Ball Trail //////////// | |
if (!gameIsPaused) | |
{ | |
//////////// Ball Trail //////////// | |
for (i=4;i>=0;i--) | |
{ | |
if (i!=0) | |
{ | |
coordinates[i][0]=coordinates[i-1][0]; | |
coordinates[i][1]=coordinates[i-1][1]; | |
sfColor ballTrail = {255,255,255,200-i*40}; | |
sfShape_SetColor(trail[i], ballTrail); | |
sfShape_SetX(trail[i], coordinates[i][0]); | |
sfShape_SetY(trail[i], coordinates[i][1]); | |
} | |
else | |
{ | |
coordinates[i][0]=sfShape_GetX(ball); | |
coordinates[i][1]=sfShape_GetY(ball); | |
sfColor ballTrail = {255,255,255,0}; | |
sfShape_SetColor(trail[0], ballTrail); | |
} | |
} | |
} | |
//////////// Display Grahpics //////////// | |
sfRenderWindow_Clear(App, sfBlack); | |
for (gridLoop=0;gridLoop<16;gridLoop++) | |
{ | |
sfRenderWindow_DrawShape(App, blueLineVert[gridLoop]); | |
sfRenderWindow_DrawShape(App, redLineVert[gridLoop]); | |
} | |
for (gridLoop=0;gridLoop<18;gridLoop++) | |
{ | |
sfRenderWindow_DrawShape(App, blueLineHoriz[gridLoop]); | |
sfRenderWindow_DrawShape(App, redLineHoriz[gridLoop]); | |
} | |
sfRenderWindow_DrawSprite(App, backgroundSprite); | |
sfRenderWindow_DrawShape(App, centerLine); | |
sfRenderWindow_DrawShape(App, paddleBlue); | |
sfRenderWindow_DrawShape(App, paddleRed); | |
if (ballTrailOn) | |
for (i=0;i<5;i++) | |
sfRenderWindow_DrawShape(App, trail[i]); | |
sfRenderWindow_DrawShape(App, ball); | |
if (diagnosticsOn) | |
{ | |
sfRenderWindow_DrawString(App, showfps); | |
sfRenderWindow_DrawString(App, stringframe); | |
sfRenderWindow_DrawString(App, stringdirectionX); | |
sfRenderWindow_DrawString(App, stringdirectionY); | |
sfRenderWindow_DrawString(App, stringspeed); | |
sfRenderWindow_DrawString(App, stringspeedFactor); | |
sfRenderWindow_DrawString(App, stringballX); | |
sfRenderWindow_DrawString(App, stringballY); | |
} | |
/////////// Display Score /////////// | |
if (pointWasScored) | |
{ | |
if (scoreColorAlpha<251.6) | |
scoreColorAlpha+=3.4; | |
displayFadingScore(scoreColorAlpha, App, blueScoreText, redScoreText); | |
frameCounterFade=0; | |
} | |
else if (scoreColorAlpha>2.5 && !inGameOver) | |
{ | |
if (frameCounterFade>60) | |
scoreColorAlpha-=2.5; | |
displayFadingScore(scoreColorAlpha, App, blueScoreText, redScoreText); | |
frameCounterFade++; | |
} | |
//////////// Game Over Screen //////////// | |
if (inGameOver) | |
{ | |
sfRenderWindow_DrawShape(App, pauseScreen); | |
sfColor scoreTextColor={255,255,255,255}; | |
sfString_SetColor(blueScoreText,scoreTextColor); | |
sfRenderWindow_DrawString(App, blueScoreText); | |
sfString_SetColor(redScoreText,scoreTextColor); | |
sfRenderWindow_DrawString(App, redScoreText); | |
gameOverScreen(App, Font, pointsRed, pointsBlue, scoreToWin); | |
} | |
//////////// Main Menu Management //////////// | |
if (inMainMenu) | |
sfRenderWindow_DrawShape(App, pauseScreen); | |
if (inMainMenu && !inSettings && !inSimpleSettings) | |
mainMenu(App, Font, mainMenuSelection); | |
if (inAbout) | |
about(App, Font); | |
if (inMainMenu && inSimpleSettings) | |
simpleSettingsMenu(App, Font, simpleSettingsSelection, changeSimpleSettings, aiMode, aiPreset, ballSpeedPreset, scoreToWin); | |
//////////// Paused Game Management //////////// | |
if (gameIsPaused) | |
{ | |
sfRenderWindow_DrawShape(App, pauseScreen); | |
sfColor scoreTextColor={255,255,255,255}; | |
sfString_SetColor(blueScoreText,scoreTextColor); | |
sfRenderWindow_DrawString(App, blueScoreText); | |
sfString_SetColor(redScoreText,scoreTextColor); | |
sfRenderWindow_DrawString(App, redScoreText); | |
} | |
if (gameIsPaused && !inSettings) | |
pauseMenu(App, Font, pauseSelection); | |
if (inSettings && !inAISettings && !inGameSettings) | |
settingsMenu(App, Font, settingSelection); | |
if (inGameSettings) | |
gameSettingsMenu(App, Font, gameSettingSelection, ballSpeedPreset, initialSpeed, incrementSpeed, paddleSpeed, scoreToWin, changeGameSettings); | |
if (inAISettings) | |
aiSettingsMenu(Font, App, aiSettingSelection, aiDifficulty, aiMode, aiSpeed, aiPreset, changeAISettings); | |
sfRenderWindow_Display(App); | |
} | |
/////////// Clean Up ////////// | |
sfShape_Destroy(centerLine); | |
sfShape_Destroy(paddleBlue); | |
sfShape_Destroy(paddleRed); | |
sfShape_Destroy(ball); | |
sfRenderWindow_Destroy(App); | |
return EXIT_SUCCESS; | |
} | |
void movePaddles (float directionX, float directionY, | |
float speed, sfShape* paddleRed, | |
sfShape* paddleBlue, float ElapsedTime, | |
int aiMode, sfInput *keyInput, | |
sfShape *ball, int aiDifficulty, | |
float aiSpeed, float paddleSpeed) | |
{ | |
if (aiMode==0 || aiMode==1) | |
{ | |
if (sfInput_IsKeyDown(keyInput, sfKeyW)) sfShape_Move(paddleBlue,0,-4000*paddleSpeed*(1.f/ElapsedTime)); | |
if (sfInput_IsKeyDown(keyInput, sfKeyS)) sfShape_Move(paddleBlue,0,4000*paddleSpeed*(1.f/ElapsedTime)); | |
if (aiMode==0) | |
{ | |
if (sfInput_IsKeyDown(keyInput, sfKeyUp)) sfShape_Move(paddleRed,0,-4000*paddleSpeed*(1.f/ElapsedTime)); | |
if (sfInput_IsKeyDown(keyInput, sfKeyDown)) sfShape_Move(paddleRed,0,4000*paddleSpeed*(1.f/ElapsedTime)); | |
} | |
} | |
if (aiMode==1 || aiMode==2) | |
{ | |
if (directionX==1) | |
{ | |
float ballYCoord = predictBall(ball, directionX, directionY, speed, aiDifficulty); | |
if (sfShape_GetY(paddleRed)>=ballYCoord+10) | |
sfShape_Move(paddleRed,0,-aiSpeed*4000*(1.f/ElapsedTime)); | |
else if (sfShape_GetY(paddleRed)<=ballYCoord-10) | |
sfShape_Move(paddleRed,0,aiSpeed*4000*(1.f/ElapsedTime)); | |
} | |
else | |
{ | |
if (sfShape_GetY(paddleRed)!=0) | |
{ | |
if (sfShape_GetY(paddleRed)<-3) | |
sfShape_Move(paddleRed,0,aiSpeed*4000*(1.f/ElapsedTime)); | |
else if (sfShape_GetY(paddleRed)>3) | |
sfShape_Move(paddleRed,0,-aiSpeed*4000*(1.f/ElapsedTime)); | |
} | |
} | |
} | |
if (aiMode==2) | |
{ | |
if (directionX==-1) | |
{ | |
float ballYCoord = predictBall(ball, directionX, directionY, speed, aiDifficulty); | |
if (sfShape_GetY(paddleBlue)>=ballYCoord+10) | |
sfShape_Move(paddleBlue,0,-aiSpeed*4000*(1.f/ElapsedTime)); | |
else if (sfShape_GetY(paddleBlue)<=ballYCoord-10) | |
sfShape_Move(paddleBlue,0,aiSpeed*4000*(1.f/ElapsedTime)); | |
} | |
else | |
{ | |
if (sfShape_GetY(paddleBlue)!=0) | |
{ | |
if (sfShape_GetY(paddleBlue)<-3) | |
sfShape_Move(paddleBlue,0,aiSpeed*4000*(1.f/ElapsedTime)); | |
else if (sfShape_GetY(paddleBlue)>3) | |
sfShape_Move(paddleBlue,0,-aiSpeed*4000*(1.f/ElapsedTime)); | |
} | |
} | |
} | |
paddleLimits(paddleBlue); | |
paddleLimits(paddleRed); | |
} | |
void paddleLimits (sfShape* Shape) | |
{ | |
if (sfShape_GetY(Shape)>269) | |
sfShape_SetY(Shape,269); | |
if (sfShape_GetY(Shape)<-269) | |
sfShape_SetY(Shape,-269); | |
} | |
float predictBall (sfShape* ball, float directionX, | |
float directionY, float speed, | |
int aiDifficulty) | |
{ | |
float ballXCoord=sfShape_GetX(ball); | |
float ballYCoord=sfShape_GetY(ball); | |
int i=0; | |
while (i<aiDifficulty && ballXCoord<599 && directionX==1) | |
{ | |
if (directionX==1) | |
{ | |
ballXCoord+=directionX*speed; | |
ballYCoord+=directionY*speed; | |
} | |
if (ballYCoord<-354||ballYCoord>354) | |
{ | |
directionY=-directionY; | |
if (ballYCoord>354) | |
ballYCoord=354; | |
if (ballYCoord<-354) | |
ballYCoord=-354; | |
} | |
i++; | |
} | |
i=0; | |
while (i<aiDifficulty && ballXCoord>-599 && directionX==-1) | |
{ | |
if (directionX==-1) | |
{ | |
ballXCoord+=directionX*speed; | |
ballYCoord+=directionY*speed; | |
} | |
if (ballYCoord<-354||ballYCoord>354) | |
{ | |
directionY=-directionY; | |
if (ballYCoord>354) | |
ballYCoord=354; | |
if (ballYCoord<-354) | |
ballYCoord=-354; | |
} | |
i++; | |
} | |
return ballYCoord; | |
} | |
void detectCollision (float *pdirectionX, float *pdirectionY, | |
float *pspeedFactor, float paddleBlueY, | |
float paddleRedY, float speed, | |
int *ppointsBlue, int *ppointsRed, | |
int *ppointWasScored, float incrementSpeed, | |
sfShape* ball) | |
{ | |
float ballX=sfShape_GetX(ball); | |
float ballY=sfShape_GetY(ball); | |
float newBallY=0; | |
if (ballY<-354 || ballY>354) | |
{ | |
if (ballY>354) | |
sfShape_SetY(ball, 354); | |
if (ballY<-354) | |
sfShape_SetY(ball, -354); | |
ballY=sfShape_GetY(ball); | |
*pdirectionY=-*pdirectionY; | |
printf("\nCOLLISION: Wall - Frame: %d",frame); | |
printf("\nDirection X: %f\tDirection Y:%f\nSpeed: %f \t\tSpeed Factor: %f\n",*pdirectionX,*pdirectionY,speed,*pspeedFactor); | |
printf("BallX: %f\t\tBallY: %f\n\n",ballX,ballY); | |
} | |
if | |
(*pdirectionX==-1 | |
&& ballX<-599 | |
&& ballX>-614+(*pdirectionX*speed) | |
&& ballY<=paddleBlueY+92 | |
&& ballY>=paddleBlueY-92) | |
{ | |
printf("\nCOLLISION: Blue Paddle - Frame: %d",frame); | |
printf("\nDirection X: %f\tDirection Y:%f\nSpeed: %f \t\tSpeed Factor: %f\n",*pdirectionX,*pdirectionY,speed,*pspeedFactor); | |
printf("BallX: %f\t\tBallY: %f\n\n",ballX,ballY); | |
*pdirectionX=-*pdirectionX; | |
*pspeedFactor+=incrementSpeed; | |
*pdirectionY=shiftDirectionY(ballY, paddleBlueY, *pdirectionY); | |
} | |
else if | |
(*pdirectionX==1 | |
&& ballX>599 | |
&& ballX<614+(*pdirectionX*speed) | |
&& ballY<=paddleRedY+92 | |
&& ballY>=paddleRedY-92) | |
{ | |
printf("\nCOLLISION: Red Paddle - Frame: %d",frame); | |
printf("\nDirection X: %f\tDirection Y:%f\nSpeed: %f \t\tSpeed Factor: %f\n",*pdirectionX,*pdirectionY,speed,*pspeedFactor); | |
printf("BallX: %f\t\tBallY: %f\n\n",ballX,ballY); | |
*pdirectionX=-*pdirectionX; | |
*pspeedFactor+=incrementSpeed; | |
*pdirectionY=shiftDirectionY(ballY, paddleRedY, *pdirectionY); | |
} | |
else if (ballX>648) | |
{ | |
*ppointWasScored=1; | |
*ppointsBlue+=1; | |
printf("\nSCORED: Blue - Frame: %d",frame); | |
printf("\nDirection X: %f\tDirection Y:%f\nSpeed: %f \t\tSpeed Factor: %f\n",*pdirectionX,*pdirectionY,speed,*pspeedFactor); | |
printf("BallX: %f\t\tBallY: %f\n",ballX,ballY); | |
printf("Red Paddle Y: %f\t\tNewBallY: %f\n\n",paddleRedY,newBallY); | |
} | |
else if (ballX<-648) | |
{ | |
*ppointWasScored=1; | |
*ppointsRed+=1; | |
printf("\nSCORED: Red - Frame: %d",frame); | |
printf("\nDirection X: %f\tDirection Y:%f\nSpeed: %f \t\tSpeed Factor: %f\n",*pdirectionX,*pdirectionY,speed,*pspeedFactor); | |
printf("BallX: %f\t\tBallY: %f\n",ballX,ballY); | |
printf("Blue Paddle Y: %f\t\tNewBallY: %f\n\n",paddleBlueY,newBallY); | |
} | |
} | |
float shiftDirectionY (float newBallY, float paddle, | |
float directionY) | |
{ | |
if ( newBallY<=paddle+10 | |
&& newBallY>=paddle-10) | |
directionY+=0; | |
if ( newBallY<=paddle+20 | |
&& newBallY>paddle+10) | |
directionY+=.125; | |
if ( newBallY>=paddle-20 | |
&& newBallY<paddle-10) | |
directionY-=.125; | |
if ( newBallY<=paddle+30 | |
&& newBallY>paddle+20) | |
directionY+=.25; | |
if ( newBallY>=paddle-30 | |
&& newBallY<paddle-20) | |
directionY-=.25; | |
if ( newBallY<=paddle+40 | |
&& newBallY>paddle+30) | |
directionY+=.375; | |
if ( newBallY>=paddle-40 | |
&& newBallY<paddle-30) | |
directionY-=.375; | |
if ( newBallY<=paddle+50 | |
&& newBallY>paddle+40) | |
directionY+=.5; | |
if ( newBallY>=paddle-50 | |
&& newBallY<paddle-40) | |
directionY-=.5; | |
if ( newBallY<=paddle+60 | |
&& newBallY>paddle+50) | |
directionY+=.625; | |
if ( newBallY>=paddle-60 | |
&& newBallY<paddle-50) | |
directionY-=.625; | |
if ( newBallY<=paddle+70 | |
&& newBallY>paddle+60) | |
directionY+=.75; | |
if ( newBallY>=paddle-70 | |
&& newBallY<paddle-60) | |
directionY-=.75; | |
if ( newBallY<=paddle+80 | |
&& newBallY>paddle+70) | |
directionY+=.875; | |
if ( newBallY>=paddle-80 | |
&& newBallY<paddle-70) | |
directionY-=.875; | |
if ( newBallY<=paddle+91 | |
&& newBallY>paddle+80) | |
directionY+=1; | |
if ( newBallY>=paddle-91 | |
&& newBallY<paddle-80) | |
directionY-=1; | |
directionY+=sfRandom_Float(-0.01f, 0.01f); | |
if (directionY>2.5) | |
directionY=2.5; | |
if (directionY<-2.5) | |
directionY=-2.5; | |
return directionY; | |
} | |
void mainMenu (sfRenderWindow *window, sfFont *font, | |
int mainMenuSelection) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *mainMenuHeader = sfString_Create(); | |
formatMenuOptions(mainMenuHeader, "Bleep Bloop!", font, 60, 400, 120, sfWhite); | |
sfString *play = sfString_Create(); | |
formatMenuOptions(play, "play game", font, 32, 500, 320, lightGray); | |
sfString *settings = sfString_Create(); | |
formatMenuOptions(settings, "settings", font, 32, 500, 360, lightGray); | |
sfString *about =sfString_Create(); | |
formatMenuOptions(about, "about", font, 32, 500, 400, lightGray); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "quit", font, 32, 500, 440, lightGray); | |
if (mainMenuSelection==0) | |
selectMenu(play); | |
if (mainMenuSelection==1) | |
selectMenu(settings); | |
if (mainMenuSelection==2) | |
selectMenu(about); | |
if (mainMenuSelection==3) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, mainMenuHeader); | |
sfRenderWindow_DrawString(window, play); | |
sfRenderWindow_DrawString(window, settings); | |
sfRenderWindow_DrawString(window, about); | |
sfRenderWindow_DrawString(window, exit); | |
sfString_Destroy(mainMenuHeader); | |
sfString_Destroy(play); | |
sfString_Destroy(settings); | |
sfString_Destroy(about); | |
sfString_Destroy(exit); | |
} | |
void about (sfRenderWindow *window, sfFont *font) | |
{ | |
sfShape *aboutBox = sfShape_CreateRectangle (320, 240, 960, 600, sfBlack, 3.0f, sfWhite); | |
sfString *text = sfString_Create(); | |
formatMenuOptions(text, "Keys\n\n" | |
" w/s - move blue (P1) paddle up and down\n\n" | |
" up/down - move red (P2) paddle up and down\n\n" | |
" F1 - toggle ball trail\n\n" | |
" F2 - toggle debugging info\n\n" | |
" F3/F4 - change resolution\n\n" | |
"\n\n" | |
"Created by cloakdood - cloakdood.com\n\n" | |
"Built using C and the CSFML library - sfml-dev.org\n\n" | |
"Font used - OCRAStd.otf\n\n" | |
"\n\n" | |
, font, 14, 360, 280, sfWhite); | |
sfRenderWindow_DrawShape(window, aboutBox); | |
sfRenderWindow_DrawString(window, text); | |
sfShape_Destroy(aboutBox); | |
sfString_Destroy(text); | |
} | |
void pauseMenu (sfRenderWindow *window, sfFont *font, | |
int pauseSelection) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *pauseScreenText = sfString_Create(); | |
formatMenuOptions(pauseScreenText, "Game Paused", font, 54, 425, 220, sfWhite); | |
sfString *resume = sfString_Create(); | |
formatMenuOptions(resume, "resume", font, 32, 500, 320, lightGray); | |
sfString *settings = sfString_Create(); | |
formatMenuOptions(settings, "settings", font, 32, 500, 360, lightGray); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "main menu", font, 32, 500, 400, lightGray); | |
if (pauseSelection==0) | |
selectMenu(resume); | |
if (pauseSelection==1) | |
selectMenu(settings); | |
if (pauseSelection==2) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, pauseScreenText); | |
sfRenderWindow_DrawString(window, resume); | |
sfRenderWindow_DrawString(window, settings); | |
sfRenderWindow_DrawString(window, exit); | |
sfString_Destroy(pauseScreenText); | |
sfString_Destroy(resume); | |
sfString_Destroy(settings); | |
sfString_Destroy(exit); | |
} | |
void settingsMenu (sfRenderWindow *window, sfFont *font, | |
int settingSelection) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *pauseScreenText = sfString_Create(); | |
formatMenuOptions(pauseScreenText, "Settings", font, 54, 485, 220, sfWhite); | |
sfString *aiSetttingString = sfString_Create(); | |
formatMenuOptions(aiSetttingString, "ai settings", font, 28, 500, 320, lightGray); | |
sfString *gameSettingString = sfString_Create(); | |
formatMenuOptions(gameSettingString, "game settings", font, 28, 500, 360, lightGray); | |
// sfString *graphicsSettingString = sfString_Create(); | |
// formatMenuOptions(graphicsSettingString, "graphics settings", font, 28, 500, 400, lightGray); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "return", font, 28, 500, 400, lightGray); | |
if (settingSelection==0) | |
selectMenu(aiSetttingString); | |
if (settingSelection==1) | |
selectMenu(gameSettingString); | |
// if (settingSelection==2) | |
// selectMenu(graphicsSettingString); | |
if (settingSelection==2) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, pauseScreenText); | |
sfRenderWindow_DrawString(window, aiSetttingString); | |
sfRenderWindow_DrawString(window, gameSettingString); | |
// sfRenderWindow_DrawString(window, graphicsSettingString); | |
sfRenderWindow_DrawString(window, exit); | |
sfString_Destroy(pauseScreenText); | |
sfString_Destroy(aiSetttingString); | |
sfString_Destroy(gameSettingString); | |
// sfString_Destroy(graphicsSettingString); | |
sfString_Destroy(exit); | |
} | |
void gameSettingsMenu (sfRenderWindow *window, sfFont *font, | |
int gameSettingSelection, float ballSpeedPreset, | |
float initialSpeed, float incrementSpeed, | |
float paddleSpeed, int scoreToWin, | |
float changeGameSettings) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *pauseScreenText = sfString_Create(); | |
formatMenuOptions(pauseScreenText, "Game Settings", font, 54, 404, 220, sfWhite); | |
sfString *speedPresetString = sfString_Create(); | |
formatMenuOptions(speedPresetString, "Ball Speed: ", font, 25, 400, 320, lightGray); | |
sfString *speedPresetSelect = sfString_Create(); | |
formatMenuOptions(speedPresetSelect, " ", font, 25, 750, 320, lightGray); | |
sfString *initialSpeedString = sfString_Create(); | |
formatMenuOptions(initialSpeedString, "Initial Speed: ", font, 25, 400, 360, lightGray); | |
char initialSpeedBuffer[10]="\0"; | |
sprintf(initialSpeedBuffer,"%.1f",initialSpeed); | |
sfString *initialSpeedNumber = sfString_Create(); | |
formatMenuOptions(initialSpeedNumber, initialSpeedBuffer, font, 25, 750, 360, lightGray); | |
sfString *incrementSpeedString = sfString_Create(); | |
formatMenuOptions(incrementSpeedString, "Speed Increment: ", font, 25, 400, 400, lightGray); | |
char incrementSpeedBuffer[10]="\0"; | |
sprintf(incrementSpeedBuffer,"%.2f",incrementSpeed); | |
sfString *incrementSpeedNumber = sfString_Create(); | |
formatMenuOptions(incrementSpeedNumber, incrementSpeedBuffer, font, 25, 750, 400, lightGray); | |
sfString *paddleSpeedString = sfString_Create(); | |
formatMenuOptions(paddleSpeedString, "Paddle Speed: ", font, 25, 400, 440, lightGray); | |
char paddleSpeedBuffer[10]="\0"; | |
sprintf(paddleSpeedBuffer,"%.0f",paddleSpeed*100); | |
sfString *paddleSpeedNumber = sfString_Create(); | |
formatMenuOptions(paddleSpeedNumber, paddleSpeedBuffer, font, 25, 750, 440, lightGray); | |
sfString *scoreToWinString = sfString_Create(); | |
formatMenuOptions(scoreToWinString, "Score To Win: ", font, 25, 400, 480, lightGray); | |
char scoreToWinBuffer[10]="\0"; | |
sprintf(scoreToWinBuffer,"%d",scoreToWin); | |
sfString *scoreToWinNumber = sfString_Create(); | |
formatMenuOptions(scoreToWinNumber, scoreToWinBuffer, font, 25, 750, 480, lightGray); | |
if (scoreToWin==-1) | |
sfString_SetText(scoreToWinNumber, "Infinity"); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "return", font, 25, 400, 520, lightGray); | |
if (ballSpeedPreset==0) | |
sfString_SetText(speedPresetSelect, "Slow"); | |
else if (ballSpeedPreset==1) | |
sfString_SetText(speedPresetSelect, "Normal"); | |
else if (ballSpeedPreset==2) | |
sfString_SetText(speedPresetSelect, "Fast"); | |
else if (ballSpeedPreset==3) | |
sfString_SetText(speedPresetSelect, "HYPERSPEEEEDDD!"); | |
if (gameSettingSelection==0) | |
{ | |
selectMenu(speedPresetString); | |
if (changeGameSettings) | |
selectMenu(speedPresetSelect); | |
} | |
if (gameSettingSelection==1) | |
{ | |
selectMenu(initialSpeedString); | |
if (changeGameSettings) | |
selectMenu(initialSpeedNumber); | |
} | |
if (gameSettingSelection==2) | |
{ | |
selectMenu(incrementSpeedString); | |
if (changeGameSettings) | |
selectMenu(incrementSpeedNumber); | |
} | |
if (gameSettingSelection==3) | |
{ | |
selectMenu(paddleSpeedString); | |
if (changeGameSettings) | |
selectMenu(paddleSpeedNumber); | |
} | |
if (gameSettingSelection==4) | |
{ | |
selectMenu(scoreToWinString); | |
if (changeGameSettings) | |
selectMenu(scoreToWinNumber); | |
} | |
if (gameSettingSelection==5) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, pauseScreenText); | |
sfRenderWindow_DrawString(window, exit); | |
sfRenderWindow_DrawString(window, speedPresetString); | |
sfRenderWindow_DrawString(window, initialSpeedString); | |
sfRenderWindow_DrawString(window, incrementSpeedString); | |
sfRenderWindow_DrawString(window, paddleSpeedString); | |
sfRenderWindow_DrawString(window, speedPresetSelect); | |
sfRenderWindow_DrawString(window, initialSpeedNumber); | |
sfRenderWindow_DrawString(window, incrementSpeedNumber); | |
sfRenderWindow_DrawString(window, paddleSpeedNumber); | |
sfRenderWindow_DrawString(window, scoreToWinString); | |
sfRenderWindow_DrawString(window, scoreToWinNumber); | |
sfString_Destroy(pauseScreenText); | |
sfString_Destroy(exit); | |
sfString_Destroy(speedPresetString); | |
sfString_Destroy(initialSpeedString); | |
sfString_Destroy(incrementSpeedString); | |
sfString_Destroy(paddleSpeedString); | |
sfString_Destroy(speedPresetSelect); | |
sfString_Destroy(initialSpeedNumber); | |
sfString_Destroy(incrementSpeedNumber); | |
sfString_Destroy(paddleSpeedNumber); | |
sfString_Destroy(scoreToWinString); | |
sfString_Destroy(scoreToWinNumber); | |
} | |
void aiSettingsMenu (sfFont *font, sfRenderWindow *window, | |
int aiSettingSelection, int aiDifficulty, | |
int aiMode, float aiSpeed, int aiPreset, | |
int changeAISettings) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *pauseScreenText = sfString_Create(); | |
formatMenuOptions(pauseScreenText, "AI Settings", font, 54, 427, 220, sfWhite); | |
sfString *aiPresetSelect = sfString_Create(); | |
formatMenuOptions(aiPresetSelect, "AI Difficulty: ", font, 25, 400, 320, lightGray); | |
sfString *aiPresetString = sfString_Create(); | |
formatMenuOptions(aiPresetString, " ", font, 25, 750, 320, lightGray); | |
sfString *aiDifficultyString = sfString_Create(); | |
formatMenuOptions(aiDifficultyString, "AI Look-ahead: ", font, 25, 400, 360, lightGray); | |
char aiDifficultyBuffer[10]="\0"; | |
sprintf(aiDifficultyBuffer,"%i",aiDifficulty); | |
sfString *aiDifficultyNumber = sfString_Create(); | |
formatMenuOptions(aiDifficultyNumber, aiDifficultyBuffer, font, 25, 750, 360, lightGray); | |
sfString *aiSpeedSelect = sfString_Create(); | |
formatMenuOptions(aiSpeedSelect, "AI Speed: ", font, 25, 400, 400, lightGray); | |
char aiSpeedBuffer[10]="\0"; | |
sprintf(aiSpeedBuffer,"%.0f",aiSpeed*100); | |
sfString *aiSpeedNumber = sfString_Create(); | |
formatMenuOptions(aiSpeedNumber, aiSpeedBuffer, font, 25, 750, 400, lightGray); | |
sfString *aiModeSelect = sfString_Create(); | |
formatMenuOptions(aiModeSelect, "AI Mode: ", font, 25, 400, 440, lightGray); | |
sfString *aiModeString = sfString_Create(); | |
formatMenuOptions(aiModeString, " ", font, 25, 750, 440, lightGray); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "return", font, 25, 400, 480, lightGray); | |
if (aiPreset==0) | |
sfString_SetText(aiPresetString, "Easy"); | |
else if (aiPreset==1) | |
sfString_SetText(aiPresetString, "Normal"); | |
else if (aiPreset==2) | |
sfString_SetText(aiPresetString, "Hard"); | |
else if (aiPreset==3) | |
sfString_SetText(aiPresetString, "Impossible"); | |
if (aiMode==0) | |
sfString_SetText(aiModeString, "P1 vs P2"); | |
else if (aiMode==1) | |
sfString_SetText(aiModeString, "P1 vs AI"); | |
else if (aiMode==2) | |
sfString_SetText(aiModeString, "AI vs AI"); | |
if (aiSettingSelection==0) | |
{ | |
selectMenu(aiPresetSelect); | |
if (changeAISettings) | |
selectMenu(aiPresetString); | |
} | |
if (aiSettingSelection==1) | |
{ | |
selectMenu(aiDifficultyString); | |
if (changeAISettings) | |
selectMenu(aiDifficultyNumber); | |
} | |
if (aiSettingSelection==2) | |
{ | |
selectMenu(aiSpeedSelect); | |
if (changeAISettings) | |
selectMenu(aiSpeedNumber); | |
} | |
if (aiSettingSelection==3) | |
{ | |
selectMenu(aiModeSelect); | |
if (changeAISettings) | |
selectMenu(aiModeString); | |
} | |
if (aiSettingSelection==4) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, pauseScreenText); | |
sfRenderWindow_DrawString(window, exit); | |
sfRenderWindow_DrawString(window, aiPresetSelect); | |
sfRenderWindow_DrawString(window, aiPresetString); | |
sfRenderWindow_DrawString(window, aiDifficultyString); | |
sfRenderWindow_DrawString(window, aiDifficultyNumber); | |
sfRenderWindow_DrawString(window, aiSpeedSelect); | |
sfRenderWindow_DrawString(window, aiSpeedNumber); | |
sfRenderWindow_DrawString(window, aiModeSelect); | |
sfRenderWindow_DrawString(window, aiModeString); | |
sfString_Destroy(pauseScreenText); | |
sfString_Destroy(exit); | |
sfString_Destroy(aiPresetSelect); | |
sfString_Destroy(aiPresetString); | |
sfString_Destroy(aiDifficultyString); | |
sfString_Destroy(aiDifficultyNumber); | |
sfString_Destroy(aiSpeedSelect); | |
sfString_Destroy(aiSpeedNumber); | |
sfString_Destroy(aiModeSelect); | |
sfString_Destroy(aiModeString); | |
} | |
void changingAISettings (sfEvent Event, int aiSettingSelection, | |
int *paiPreset, int *paiMode, | |
int *paiDifficulty, float *paiSpeed, | |
int changeAISettings, int inAISettings, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int inSimpleSettings) | |
{ | |
if (Event.Key.Code == sfKeyLeft) | |
{ | |
if (inAISettings) | |
{ | |
if (aiSettingSelection==0) | |
{ | |
if (*paiPreset>0) | |
(*paiPreset)--; | |
} | |
if (aiSettingSelection==1) | |
{ | |
if (*paiDifficulty>1) | |
(*paiDifficulty)--; | |
} | |
if (aiSettingSelection==2) | |
{ | |
if (*paiSpeed>0.02) | |
*paiSpeed-=0.01; | |
} | |
if (aiSettingSelection==3) | |
{ | |
if (*paiMode>0) | |
(*paiMode)--; | |
} | |
} | |
if (inSimpleSettings) | |
{ | |
if (simpleSettingsSelection==1) | |
{ | |
if (*paiMode>0) | |
(*paiMode)--; | |
} | |
if (simpleSettingsSelection==2) | |
{ | |
if (*paiPreset>0) | |
(*paiPreset)--; | |
} | |
} | |
} | |
else if (Event.Key.Code == sfKeyRight) | |
{ | |
if (inAISettings) | |
{ | |
if (aiSettingSelection==0) | |
{ | |
if (*paiPreset<3) | |
(*paiPreset)++; | |
} | |
if (aiSettingSelection==1) | |
{ | |
if (*paiDifficulty<100) | |
(*paiDifficulty)++; | |
} | |
if (aiSettingSelection==2) | |
{ | |
if (*paiSpeed<0.3) | |
*paiSpeed+=0.01; | |
} | |
if (aiSettingSelection==3) | |
{ | |
if (*paiMode<2) | |
(*paiMode)++; | |
} | |
} | |
if (inSimpleSettings) | |
{ | |
if (simpleSettingsSelection==1) | |
{ | |
if (*paiMode<2) | |
(*paiMode)++; | |
} | |
if (simpleSettingsSelection==2) | |
{ | |
if (*paiPreset<3) | |
(*paiPreset)++; | |
} | |
} | |
} | |
if ((aiSettingSelection==0 || simpleSettingsSelection==2) | |
&& (changeAISettings || changeSimpleSettings) | |
&& (Event.Key.Code == sfKeyLeft || Event.Key.Code == sfKeyRight)) | |
{ | |
if (*paiPreset==0) | |
{ | |
*paiDifficulty=5; | |
*paiSpeed=0.08; | |
} | |
else if (*paiPreset==1) | |
{ | |
*paiDifficulty=10; | |
*paiSpeed=0.10; | |
} | |
else if (*paiPreset==2) | |
{ | |
*paiDifficulty=15; | |
*paiSpeed=0.12; | |
} | |
else if (*paiPreset==3) | |
{ | |
*paiDifficulty=100; | |
*paiSpeed=0.15; | |
} | |
} | |
} | |
void changingGameSettings (sfEvent Event, int gameSettingSelection, | |
int *pballSpeedPreset, float *pinitialSpeed, | |
float *pincrementSpeed, float *ppaddleSpeed, | |
int changeGameSettings, int inGameSettings, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int inSimpleSettings, int *pscoreToWin) | |
{ | |
if (Event.Key.Code == sfKeyLeft) | |
{ | |
if (inGameSettings) | |
{ | |
if (gameSettingSelection==0) | |
{ | |
if (*pballSpeedPreset>0) | |
(*pballSpeedPreset)--; | |
} | |
if (gameSettingSelection==1) | |
{ | |
if (*pinitialSpeed>0.5) | |
(*pinitialSpeed)-=0.5; | |
} | |
if (gameSettingSelection==2) | |
{ | |
if (*pincrementSpeed>0) | |
(*pincrementSpeed)-=0.25; | |
} | |
if (gameSettingSelection==3) | |
{ | |
if (*ppaddleSpeed>0.02) | |
(*ppaddleSpeed)-=0.01; | |
} | |
if (gameSettingSelection==4) | |
{ | |
if (*pscoreToWin>1) | |
(*pscoreToWin)--; | |
if (*pscoreToWin==-1) | |
*pscoreToWin=25; | |
} | |
} | |
if (inSimpleSettings) | |
{ | |
if (simpleSettingsSelection==3) | |
{ | |
if (*pballSpeedPreset>0) | |
(*pballSpeedPreset)--; | |
} | |
if (simpleSettingsSelection==4) | |
{ | |
if (*pscoreToWin>1) | |
(*pscoreToWin)--; | |
if (*pscoreToWin==-1) | |
*pscoreToWin=25; | |
} | |
} | |
} | |
else if (Event.Key.Code == sfKeyRight) | |
{ | |
if (inGameSettings) | |
{ | |
if (gameSettingSelection==0) | |
{ | |
if (*pballSpeedPreset<3) | |
(*pballSpeedPreset)++; | |
} | |
if (gameSettingSelection==1) | |
{ | |
if (*pinitialSpeed<50) | |
(*pinitialSpeed)+=0.5; | |
} | |
if (gameSettingSelection==2) | |
{ | |
if (*pincrementSpeed<10) | |
(*pincrementSpeed)+=0.25; | |
} | |
if (gameSettingSelection==3) | |
{ | |
if (*ppaddleSpeed<0.30) | |
(*ppaddleSpeed)+=0.01; | |
} | |
if (gameSettingSelection==4) | |
{ | |
if (*pscoreToWin<26 && *pscoreToWin!=-1) | |
(*pscoreToWin)++; | |
if (*pscoreToWin==26) | |
*pscoreToWin=-1; | |
} | |
} | |
if (inSimpleSettings) | |
{ | |
if (simpleSettingsSelection==3) | |
{ | |
if (*pballSpeedPreset<3) | |
(*pballSpeedPreset)++; | |
} | |
if (simpleSettingsSelection==4) | |
{ | |
if (*pscoreToWin<26 && *pscoreToWin!=-1) | |
(*pscoreToWin)++; | |
if (*pscoreToWin==26) | |
*pscoreToWin=-1; | |
} | |
} | |
} | |
if ((gameSettingSelection==0 || simpleSettingsSelection==3) | |
&& (changeGameSettings || changeSimpleSettings) | |
&& (Event.Key.Code == sfKeyLeft || Event.Key.Code == sfKeyRight)) | |
{ | |
if (*pballSpeedPreset==0) | |
{ | |
*pinitialSpeed=9.0; | |
*pincrementSpeed=1.0; | |
*ppaddleSpeed=0.15; | |
} | |
else if (*pballSpeedPreset==1) | |
{ | |
*pinitialSpeed=12.5; | |
*pincrementSpeed=1.75; | |
*ppaddleSpeed=0.15; | |
} | |
else if (*pballSpeedPreset==2) | |
{ | |
*pinitialSpeed=15.0; | |
*pincrementSpeed=2.5; | |
*ppaddleSpeed=0.16; | |
} | |
else if (*pballSpeedPreset==3) | |
{ | |
*pinitialSpeed=50.0; | |
*pincrementSpeed=5.0; | |
*ppaddleSpeed=0.20; | |
} | |
} | |
} | |
void simpleSettingsMenu (sfRenderWindow *window, sfFont *font, | |
int simpleSettingsSelection, int changeSimpleSettings, | |
int aiMode, int aiPreset, int ballSpeedPreset, | |
int scoreToWin) | |
{ | |
sfColor lightGray = {125, 125, 125, 255}; | |
sfString *playsettings = sfString_Create(); | |
formatMenuOptions(playsettings, "Gameplay", font, 54, 490, 220, sfWhite); | |
sfString *play = sfString_Create(); | |
formatMenuOptions(play, "Play!", font, 30, 400, 320, lightGray); | |
sfString *aiModeSelect = sfString_Create(); | |
formatMenuOptions(aiModeSelect, "Mode: ", font, 25, 400, 370, lightGray); | |
sfString *aiModeString = sfString_Create(); | |
formatMenuOptions(aiModeString, " ", font, 25, 700, 370, lightGray); | |
sfString *aiPresetSelect = sfString_Create(); | |
formatMenuOptions(aiPresetSelect, "Difficulty: ", font, 25, 400, 410, lightGray); | |
sfString *aiPresetString = sfString_Create(); | |
formatMenuOptions(aiPresetString, " ", font, 25, 700, 410, lightGray); | |
sfString *ballSpeedSelect = sfString_Create(); | |
formatMenuOptions(ballSpeedSelect, "Ball Speed: ", font, 25, 400, 450, lightGray); | |
sfString *ballSpeedString = sfString_Create(); | |
formatMenuOptions(ballSpeedString, " ", font, 25, 700, 450, lightGray); | |
sfString *scoreToWinString = sfString_Create(); | |
formatMenuOptions(scoreToWinString, "Score To Win: ", font, 25, 400, 490, lightGray); | |
char scoreToWinBuffer[10]="\0"; | |
sprintf(scoreToWinBuffer,"%d",scoreToWin); | |
sfString *scoreToWinNumber = sfString_Create(); | |
formatMenuOptions(scoreToWinNumber, scoreToWinBuffer, font, 25, 700, 490, lightGray); | |
if (scoreToWin==-1) | |
sfString_SetText(scoreToWinNumber, "Infinity"); | |
sfString *exit = sfString_Create(); | |
formatMenuOptions(exit, "return", font, 25, 400, 530, lightGray); | |
if (aiPreset==0) | |
sfString_SetText(aiPresetString, "Easy"); | |
else if (aiPreset==1) | |
sfString_SetText(aiPresetString, "Normal"); | |
else if (aiPreset==2) | |
sfString_SetText(aiPresetString, "Hard"); | |
else if (aiPreset==3) | |
sfString_SetText(aiPresetString, "Impossible"); | |
if (aiMode==0) | |
sfString_SetText(aiModeString, "P1 vs P2"); | |
else if (aiMode==1) | |
sfString_SetText(aiModeString, "P1 vs AI"); | |
else if (aiMode==2) | |
sfString_SetText(aiModeString, "AI vs AI"); | |
if (ballSpeedPreset==0) | |
sfString_SetText(ballSpeedString, "Slow"); | |
else if (ballSpeedPreset==1) | |
sfString_SetText(ballSpeedString, "Normal"); | |
else if (ballSpeedPreset==2) | |
sfString_SetText(ballSpeedString, "Fast"); | |
else if (ballSpeedPreset==3) | |
sfString_SetText(ballSpeedString, "HYPERSPEEEEDDD!"); | |
if (simpleSettingsSelection==0) | |
selectMenu(play); | |
if (simpleSettingsSelection==1) | |
{ | |
selectMenu(aiModeSelect); | |
if (changeSimpleSettings) | |
selectMenu(aiModeString); | |
} | |
if (simpleSettingsSelection==2) | |
{ | |
selectMenu(aiPresetSelect); | |
if (changeSimpleSettings) | |
selectMenu(aiPresetString); | |
} | |
if (simpleSettingsSelection==3) | |
{ | |
selectMenu(ballSpeedSelect); | |
if (changeSimpleSettings) | |
selectMenu(ballSpeedString); | |
} | |
if (simpleSettingsSelection==4) | |
{ | |
selectMenu(scoreToWinString); | |
if (changeSimpleSettings) | |
selectMenu(scoreToWinNumber); | |
} | |
if (simpleSettingsSelection==5) | |
selectMenu(exit); | |
sfRenderWindow_DrawString(window, playsettings); | |
sfRenderWindow_DrawString(window, play); | |
sfRenderWindow_DrawString(window, aiModeSelect); | |
sfRenderWindow_DrawString(window, aiModeString); | |
sfRenderWindow_DrawString(window, aiPresetSelect); | |
sfRenderWindow_DrawString(window, aiPresetString); | |
sfRenderWindow_DrawString(window, ballSpeedSelect); | |
sfRenderWindow_DrawString(window, ballSpeedString); | |
sfRenderWindow_DrawString(window, scoreToWinString); | |
sfRenderWindow_DrawString(window, scoreToWinNumber); | |
sfRenderWindow_DrawString(window, exit); | |
sfString_Destroy(playsettings); | |
sfString_Destroy(play); | |
sfString_Destroy(aiModeSelect); | |
sfString_Destroy(aiModeString); | |
sfString_Destroy(aiPresetSelect); | |
sfString_Destroy(aiPresetString); | |
sfString_Destroy(ballSpeedSelect); | |
sfString_Destroy(ballSpeedString); | |
sfString_Destroy(scoreToWinString); | |
sfString_Destroy(scoreToWinNumber); | |
sfString_Destroy(exit); | |
} | |
void selectMenu (sfString *string) | |
{ | |
sfString_SetColor(string, sfWhite); | |
sfString_Move(string, 20.0f, 0.0f); | |
} | |
void formatMenuOptions (sfString *string, char stringliteral[], | |
sfFont *font, float size, | |
float x, float y, sfColor color) | |
{ | |
sfString_SetText(string, stringliteral); | |
sfString_SetFont(string, font); | |
sfString_SetSize(string, size); | |
sfString_SetX(string, x); | |
sfString_SetY(string, y); | |
sfString_SetColor(string, color); | |
} | |
void displayFadingScore (float scoreColorAlpha, sfRenderWindow *window, | |
sfString *blueScoreText, sfString *redScoreText) | |
{ | |
sfColor scoreTextColor={255,255,255,scoreColorAlpha}; | |
sfString_SetColor(blueScoreText, scoreTextColor); | |
sfString_SetColor(redScoreText, scoreTextColor); | |
sfRenderWindow_DrawString(window, blueScoreText); | |
sfRenderWindow_DrawString(window, redScoreText); | |
} | |
void gameOverScreen (sfRenderWindow *window, sfFont *font, | |
int pointsRed, int pointsBlue, int scoreToWin) | |
{ | |
sfString *gameOverText = sfString_Create(); | |
if (pointsRed>pointsBlue) | |
formatMenuOptions(gameOverText, "Red Wins!", font, 48, 800, 325, sfWhite); | |
else if (pointsRed<pointsBlue) | |
formatMenuOptions(gameOverText, "Blue Wins!", font, 48, 170, 325, sfWhite); | |
else if (pointsRed==pointsBlue) | |
formatMenuOptions(gameOverText, "Tie Game!", font, 48, 480, 325, sfWhite); | |
sfRenderWindow_DrawString(window, gameOverText); | |
sfString_Destroy(gameOverText); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did you use allegro?