main.cpp
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
#include<iostream> #include<graphics.h> #include<conio.h> #include<dos.h> #include<time.h> #include<stdio.h> #define ScWidth 1400 #define ScHeight 700 #define OpWidth 400 #define KEY_UP 72 #define KEY_DOWN 80 #define KEY_LEFT 75 #define KEY_RIGHT 77 #define SPACEBAR 32 using namespace std; // Dispaly Gameover screen and reset score void GameOver(); // Draw game board and score / control area void DrawBoard(); // Drawing Nail void DrawEnemies(); // Drawing Player void DrawPlayer(); // Checking if player hit red nail or blue nail int PlayerHitEnemy(); // Global Variables char option, txtScore[5]; int score = 0; // Enemy Variables const int NoEnemies = 3; // Enemy (x, y, height, type) int Enemy[NoEnemies][4] = { { 300, 500, 0, 0 }, { 500, 500, 1, 0 }, { 700, 500, 0, 0 } }; // eGap-Enemy Gap, eSize-Enemy Width, eStep-Enemy Step int eSize = 50, eStep = -10, ei = 0; // Player Variables // px-Player X, py-Player Y, pSize-Player Size, pStep-Player Step int pw = 70, ph = 100, px = 500 - pw / 2, py = 150; // Line variables int lx = 200, ly = 400, lw = 800; // Other variables int i = 0, timer; int main() { // Generation of random number srand((unsigned) time(0)); timer = 5 + rand() % 5; // initializing timer for first time //initializing Graphics int gd = DETECT, gm, err, tmp; initgraph( & gd, & gm, "C:\\tc\\bgi"); while (1) { setcolor(WHITE); // Draw game board and score/controls DrawBoard(); // Draw reference line line(lx, ly, lw, ly); //Draw Enemies for (int i = 0; i < NoEnemies; i++) { setcolor(YELLOW); DrawEnemies(); } // Update enemy Enemy[ei][1] += eStep; if (Enemy[ei][1] < 350) // if enemy reach upper bound change direction eStep = -eStep; if (Enemy[ei][1] > 500) { // if enemy reach lower bound ei = rand() % 3; // choose next enemy randomly eStep = -10; // udpate enemy position Enemy[0][2] = Enemy[1][2] = Enemy[2][2] = 0; // reset all enemies } if (i == timer) { // if i(counter) reaches max timer Enemy[ei][2] = (Enemy[ei][2] == 0 ? 1 : 0); // change enemey type i = 0; // reset counter timer = 10 + rand() % 5; // randomly generate next timer } if (kbhit()) { // if any key is pressed option = getch(); // catch character in options variable if (option == KEY_LEFT) { // if left arrow key is pressed if (px + pw / 2 > 300) px -= 200; // move hammer to left } else if (option == KEY_RIGHT) { // if right arrow key is pressed if (px + pw / 2 < 700) px += 200; // move hammer to right } else if (option == SPACEBAR) { // if spacebar is pressed py += 150; // hit hammer down DrawPlayer(); delay(200); if (PlayerHitEnemy() == 1) { score++; } else if (PlayerHitEnemy() == 2) { GameOver(); } py -= 150; // reset hammer position } } DrawPlayer(); i++; delay(40); cleardevice(); } closegraph(); } int PlayerHitEnemy() { if (px + pw / 2 == Enemy[ei][0]) { if (py + ph >= Enemy[ei][1] - eSize) { if (Enemy[ei][2] == 0) return 1; else return 2; } } return 0; } // Drawing Nail void DrawEnemies() { for (int i = 0; i < NoEnemies; i++) { if (Enemy[i][2] == 0) { setcolor(YELLOW); } else { setcolor(LIGHTRED); } circle(Enemy[i][0], Enemy[i][1], eSize); circle(Enemy[i][0] - eSize / 3, Enemy[i][1] - eSize / 3, 5); circle(Enemy[i][0] + eSize / 3, Enemy[i][1] - eSize / 3, 5); arc(Enemy[i][0], Enemy[i][1], 180, 360, eSize / 2); } } // Draw Player - Hammer void DrawPlayer() { setcolor(WHITE); setfillstyle(1, WHITE); // drawing head bar(px, py, px + pw, py + ph); // drawing handle bar(px + pw, py + ph / 2 - 20, px + pw + 20, py + ph / 2 + 20); bar(px + pw + 25, py + ph / 2 - 20, px + pw + 45, py + ph / 2 + 20); bar(px + pw + 50, py + ph / 2 - 20, px + pw + 75, py + ph / 2 + 20); bar(px + pw + 80, py + ph / 2 - 20, px + pw + 105, py + ph / 2 + 20); } void GameOver() { cleardevice(); // clear screen setcolor(WHITE); // Print Game Over Text settextstyle(4, 0, 9); outtextxy(300, 300, "Game Over"); settextstyle(4, 0, 2); outtextxy(500, 420, "Press any key to restart..."); getch(); score = 0; // reset score } void DrawBoard() { setcolor(WHITE); // draw board rectangle(0, 0, ScWidth - OpWidth, ScHeight); rectangle(ScWidth - OpWidth, 0, ScWidth, ScHeight); // draw title settextstyle(4, 0, 5); outtextxy(ScWidth - 375, 50, "Smiley Hit"); // print score // itoa function converts number to character array settextstyle(4, 0, 9); if (score < 10) outtextxy(ScWidth - 270, 250, itoa(score, txtScore, 10)); else if (score >= 10) outtextxy(ScWidth - 320, 250, itoa(score, txtScore, 10)); settextstyle(4, 0, 1); outtextxy(ScWidth - 375, 500, "Controls: "); outtextxy(ScWidth - 300, 550, "Spacebar to Hit "); outtextxy(ScWidth - 300, 600, "Left Arrow Key "); outtextxy(ScWidth - 300, 650, "Right Arrow Key "); } |