forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneLoneCoder_Frogger.cpp
More file actions
201 lines (166 loc) · 7.69 KB
/
OneLoneCoder_Frogger.cpp
File metadata and controls
201 lines (166 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
OneLoneCoder.com - Code-It-Yourself! Frogger at the command prompt (quick and simple c++)
"Ribbet" - @Javidx9
Disclaimer
~~~~~~~~~~
I don't care what you use this for. It's intended to be educational, and perhaps
to the oddly minded - a little bit of fun. Please hack this, change it and use it
in any way you see fit. BUT, you acknowledge that I am not responsible for anything
bad that happens as a result of your actions. However, if good stuff happens, I
would appreciate a shout out, or at least give the blog some publicity for me.
Cheers!
Background
~~~~~~~~~~
This is a bit of an unusual one. The games mechanics are very simple, but as soon
as you add graphics the code almost doubles in size! By standardising how the
lanes work and interact with the frog, the game pretty much controls itself, meaning
the game code is really really short. Everything else is eye candy.
Controls
~~~~~~~~
Arrow keys move frog. Cross the road, then the river, to get frog to safety.
Author
~~~~~~
Twitter: @javidx9
Blog: www.onelonecoder.com
Video:
~~~~~~
https://youtu.be/QJnZ5QmpXOE
Last Updated: 20/08/2017
*/
#include <iostream>
#include <string>
using namespace std;
#include "olcConsoleGameEngine.h"
class OneLoneCoder_Frogger : public olcConsoleGameEngine
{
public:
OneLoneCoder_Frogger()
{
m_sAppName = L"Frogger";
}
private:
vector<pair<float, wstring>> vecLanes =
{
{0.0f, L"wwwhhwwwhhwwwhhwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"}, // 64 elements per lane
{-3.0f, L",,,jllk,,jllllk,,,,,,,jllk,,,,,jk,,,jlllk,,,,jllllk,,,,jlllk,,,,"},
{3.0f, L",,,,jllk,,,,,jllk,,,,jllk,,,,,,,,,jllk,,,,,jk,,,,,,jllllk,,,,,,,"},
{2.0f, L",,jlk,,,,,jlk,,,,,jk,,,,,jlk,,,jlk,,,,jk,,,,jllk,,,,jk,,,,,,jk,,"},
{0.0f, L"pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"},
{-3.0f, L"....asdf.......asdf....asdf..........asdf........asdf....asdf..."},
{ 3.0f, L".....ty..ty....ty....ty.....ty........ty..ty.ty......ty.......ty"},
{-4.0f, L"..zx.....zx.........zx..zx........zx...zx...zx....zx...zx...zx.."},
{2.0f, L"..ty.....ty.......ty.....ty......ty..ty.ty.......ty....ty......."},
{0.0f, L"pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"}
};
float fTimeSinceStart = 0.0f;
float fFrogX = 8.0f;
float fFrogY = 9.0f;
int nLaneWidth = 18;
int nCellSize = 8;
bool *bufDanger = nullptr;
olcSprite *spriteBus = nullptr;
olcSprite *spriteLog = nullptr;
olcSprite *spriteWater = nullptr;
olcSprite *spriteFrog = nullptr;
olcSprite *spritePavement = nullptr;
olcSprite *spriteCar1 = nullptr;
olcSprite *spriteCar2 = nullptr;
olcSprite *spriteWall = nullptr;
olcSprite *spriteHome = nullptr;
protected:
// Called by olcConsoleGameEngine
virtual bool OnUserCreate()
{
bufDanger = new bool[ScreenWidth() * ScreenHeight()];
memset(bufDanger, 0, ScreenWidth() * ScreenHeight() * sizeof(bool));
// You'll need these sprites!
spriteBus = new olcSprite(L"FroggerSprites/bus.spr");
spriteLog = new olcSprite(L"FroggerSprites/log.spr");
spriteWater = new olcSprite(L"FroggerSprites/water.spr");
spriteFrog = new olcSprite(L"FroggerSprites/frog.spr");
spritePavement = new olcSprite(L"FroggerSprites/pavement.spr");
spriteWall = new olcSprite(L"FroggerSprites/wall.spr");
spriteHome = new olcSprite(L"FroggerSprites/home.spr");
spriteCar1 = new olcSprite(L"FroggerSprites/car1.spr");
spriteCar2 = new olcSprite(L"FroggerSprites/car2.spr");
return true;
}
// Called by olcConsoleGameEngine
virtual bool OnUserUpdate(float fElapsedTime)
{
fTimeSinceStart += fElapsedTime;
// Handle Input
if (m_keys[VK_UP].bReleased) fFrogY -= 1.0f;
if (m_keys[VK_DOWN].bReleased) fFrogY += 1.0f;
if (m_keys[VK_LEFT].bReleased) fFrogX -= 1.0f;
if (m_keys[VK_RIGHT].bReleased) fFrogX += 1.0f;
// Frog is moved by platforms
if (fFrogY <= 3) fFrogX -= fElapsedTime * vecLanes[(int)fFrogY].first;
// Collision detection - check four corners of frog against danger buffer
bool tl = bufDanger[(int)(fFrogY*nCellSize + 1) * ScreenWidth() + (int)(fFrogX*nCellSize + 1)];
bool tr = bufDanger[(int)(fFrogY*nCellSize + 1) * ScreenWidth() + (int)((fFrogX+1)*nCellSize - 1)];
bool bl = bufDanger[(int)((fFrogY+1)*nCellSize - 1) * ScreenWidth() + (int)(fFrogX*nCellSize + 1)];
bool br = bufDanger[(int)((fFrogY+1)*nCellSize - 1) * ScreenWidth() + (int)((fFrogX+1)*nCellSize - 1)];
if (tl || tr || bl || br)
{
// Frogs been hit :-(
fFrogX = 8.0f;
fFrogY = 9.0f;
}
// Draw Lanes
int x=-1, y=0;
for (auto lane : vecLanes)
{
// Find lane offset start
int nStartPos = (int)(fTimeSinceStart * lane.first) % 64;
int nCellOffset = (int)((float)nCellSize* fTimeSinceStart * lane.first) % nCellSize;
if (nStartPos < 0) nStartPos = 64 - (abs(nStartPos) % 64);
for (int i = 0; i < nLaneWidth; i++)
{
// Draw Graphics
wchar_t graphic = lane.second[(nStartPos + i) % 64];
//Fill((x + i)*nCellSize - nCellOffset, y*nCellSize, (x + i + 1)*nCellSize - nCellOffset, (y + 1)*nCellSize, graphic);
switch (graphic) // Graphics always make code verbose and less clear
{
case L'a': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteBus, 0, 0, 8, 8); break; // Bus 1
case L's': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteBus, 8, 0, 8, 8); break; // Bus 2
case L'd': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteBus, 16, 0, 8, 8); break; // Bus 3
case L'f': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteBus, 24, 0, 8, 8); break; // Bus 4
case L'j': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteLog, 0, 0, 8, 8); break; // Log Start
case L'l': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteLog, 8, 0, 8, 8); break; // Log Middle
case L'k': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteLog, 16, 0, 8, 8); break; // Log End
case L'z': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteCar1, 0, 0, 8, 8); break; // Car1 Back
case L'x': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteCar1, 8, 0, 8, 8); break; // Car1 Front
case L't': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteCar2, 0, 0, 8, 8); break; // Car2 Back
case L'y': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteCar2, 8, 0, 8, 8); break; // Car2 Front
case L'w': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteWall, 0, 0, 8, 8); break; // Wall
case L'h': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteHome, 0, 0, 8, 8); break; // Home
case L',': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spriteWater, 0, 0, 8, 8); break; // Water
case L'p': DrawPartialSprite((x + i)*nCellSize - nCellOffset, y*nCellSize, spritePavement, 0, 0, 8, 8); break; // Pavement
case L'.': Fill((x + i)*nCellSize - nCellOffset, y*nCellSize, (x + i + 1)*nCellSize - nCellOffset, (y + 1)*nCellSize, L' '); break; // Road
}
// Fill Danger buffer
for (int j = (x + i)*nCellSize - nCellOffset; j < (x + i + 1)*nCellSize - nCellOffset; j++)
for (int k = y*nCellSize; k < (y + 1)*nCellSize; k++)
if (j >= 0 && j < ScreenWidth() && k >= 0 && k < ScreenHeight())
bufDanger[k*ScreenWidth() + j] =
!(graphic == L'.' ||
graphic == L'j' || graphic == L'k' || graphic == L'l' ||
graphic == L'p' ||
graphic == L'h');
}
y++;
}
// Draw Frog
DrawSprite(fFrogX*nCellSize, fFrogY*nCellSize, spriteFrog);
return true;
}
};
int main()
{
// Use olcConsoleGameEngine derived app
OneLoneCoder_Frogger game;
game.ConstructConsole(128, 80, 12, 12);
game.Start();
return 0;
}