Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdlib.h>
- #include <time.h>
- // Timer's ID, used to run the animations at 30FPS
- UINT_PTR IDT_TIMER1;
- // Resolution.
- // Change it if we want to scale up the resolution (or even make adjustable-resolution settings)
- short unsigned int ResolutionWidth = 640;
- short unsigned int ResolutionHeight = 480;
- int ObstacleSpawnPos; // Must be off-screen on the right
- short unsigned int ObstaclesDelay; // Number of meters [units] between obstacles.
- bool playing = true; // is set to false when player dies
- bool dying = false;
- unsigned int nScore = 0; // How many obstacles has player passed?
- HWND hScore, hWnd; // Window handlers
- /****************************** GRAPHIC DISPLAY *******************/
- // what obstacles should look like - currently unused because I don't know how to display these images properly ;/
- // (displaying them as separate window (using CreateWindow()) techinically works, but leads to a lot of random screen flickering)
- HBITMAP bmpObstacle = (HBITMAP) LoadImage(NULL, L"tube.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- // for obstacle's appearance, use this instead
- COLORREF rgbObstacle = RGB(102, 204, 102);
- COLORREF rgbPlayer = RGB(225, 0, 0);
- void SetColor(HDC handler, COLORREF color) {
- SelectObject(handler, GetStockObject(DC_PEN));
- SetDCPenColor(handler, color);
- SelectObject(handler, GetStockObject(DC_BRUSH));
- SetDCBrushColor(handler, color);
- }
- /****************************** UPDATING THE SCORE WINDOW *******************/
- HWND UpdateScore() {
- // Convert nScore int to LPCTSTR so it can be displayed as a static window
- wchar_t lScore[3];
- _itow_s (nScore, lScore, 10);
- return CreateWindow(L"STATIC", lScore, WS_VISIBLE | WS_CHILD,
- ResolutionWidth/2-25, 25, 50, 50, hWnd, NULL, NULL, NULL);
- }
- /* Player class. There's only one player so using a class isn't a MUST
- but I'd rather have a whole thing self-contained rather than use
- bunch of different variables all over the place*/
- class Player {
- private:
- unsigned short int SpawnWidth; // Spawn point (from screen's left side)
- unsigned short int FramesInTheAir; // starts increasing after jumping. Player doesn't immediatelly start
- // falling down, instead he's steady in the air for couple of seconds before going down
- void Jump() {
- if(CurHeight > Destination) CurHeight-=2;
- else {
- FramesInTheAir++;
- if(FramesInTheAir > 5) { FramesInTheAir = 0; Jumping = false; }
- }
- }
- public:
- int CurHeight; // How far up player jumped (or fell)
- int JumptHeight; // How far CAN player jump
- int FallSpeed; // How fast player
- int Size; // how high & wide the player is
- bool Jumping;
- int Destination; // Set when player jumps
- int DefPosition; // Same as SpawnWidth? Investigate WTF was I thinking
- Player() {SpawnWidth = 100; Size = 20; Jumping = false; CurHeight = ResolutionHeight/2-Size; JumptHeight=50; DefPosition=50;}
- void Create(HDC hDC) {
- // Player is an elipse
- SetColor(hDC, rgbPlayer);
- Ellipse(hDC, DefPosition, CurHeight, DefPosition+Size, CurHeight+Size);
- // player fell to the bottom
- if(CurHeight+Size*2 == ResolutionHeight) playing = false;
- if(!Jumping || dying) CurHeight+=2;
- else Jump();
- }
- };
- // used in game
- Player PC;
- // obstacles
- class Obstacle {
- private:
- int Gap;
- bool PointGiven; // Has the point for passing this obstacle been awarded yet?
- // used in Draw()
- void Display(int StartHeight, int FullHeight, HDC hDC) {
- MoveToEx(hDC, Position, StartHeight, NULL);
- // Color change
- SetColor(hDC, rgbObstacle);
- // I wrote so much code to draw lines and fill them with
- // green, but it turns out you can just use Rectangle() function
- // Now, I'm all for showing off complicated code, but sometimes less is more...
- // performance seems to agree with me.
- Rectangle(hDC, Position, StartHeight, Position+Width, FullHeight);
- // Check player's position relative to this obstacle
- if(((Position >= PC.DefPosition && Position <= PC.DefPosition+PC.Size) ||
- (Position+Width >= PC.DefPosition && Position+Width <= PC.DefPosition+PC.Size))
- && ((PC.CurHeight < 0) || (PC.CurHeight > StartHeight && PC.CurHeight < FullHeight))) dying = true;
- }
- // triggers when player passes through an obstacle
- void Passed() {
- nScore++;
- hScore = UpdateScore();
- PointGiven = true;
- }
- public:
- int Position; // position on the world map, going from the right side of the screen to the left
- int Height; // Randomized height the object appears at. (10- (ResolutionHeight-150))
- // Some obstales should be very low and others very high, but they must be always visible on screen
- int nID; // To check how many obstacles there are on screen
- int Width; // how wide the obstacle is
- bool Real;
- bool SpawnedNext;
- Obstacle() { // Constructor
- Position = ResolutionWidth + 50;
- Width = 30;
- Gap = PC.Size*4;
- PointGiven = false;
- Real = true;
- SpawnedNext = false;
- srand (time(NULL));
- Height = rand() % 32 + 10;
- Height*=10;
- }
- Obstacle(bool real) {
- Position = ResolutionWidth + 200;
- Real = false; // yeah, make it always false, because if we wanted a real one, we wouldnt put a bool parameter in there in the first place. Not-Real basically means that it won't appear.
- }
- // Creates an obstacle visible in game
- void Draw(HDC hDC) {
- Display(0, Height, hDC);
- Display(Height+Gap, ResolutionHeight, hDC);
- if(!PointGiven && Position+Width/2 < PC.DefPosition) Passed();
- }
- };
- /* Should actually be Screen.height/(Obstacle.width + SpawnDelay),
- but until I include support for more resolutions, "5" is a perfectly
- adequate solution too*/
- Obstacle aObstacles[5];
- /****************************** ALL THE CONTROLS FOR THE OBSTACLES *******************/
- void SpawnNextObstacle() {
- for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
- if(!aObstacles[i].Real) {
- aObstacles[i] = Obstacle();
- break;
- }
- }
- }
- void MovingObstacles() {
- if(dying) return;
- for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
- if(aObstacles[i].Real) {
- // Move an obstacle to the right
- aObstacles[i].Position--;
- if(aObstacles[i].Position + aObstacles[i].Width < 0) aObstacles[i] = Obstacle(false);
- // Spawn the next obstacle if this one's been on screen long enough
- if(aObstacles[i].Position < ResolutionWidth-ObstaclesDelay && !aObstacles[i].SpawnedNext) {
- aObstacles[i].SpawnedNext = true;
- SpawnNextObstacle();
- break;
- }
- }
- }
- }
- void DrawObstacles(HDC hDC) {
- for(int i=0; i<sizeof(aObstacles)/sizeof(*aObstacles); i++) {
- if(aObstacles[i].Real) aObstacles[i].Draw(hDC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement