#include <stdio.h>
#include <windows.h>
void display_border();
COORD c = {0, 0};
void setxy (int x, int y)
{
c.X = x; c.Y = y; // Set X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
int batx=15,baty=13, ballx=25, bally=7;
int x=25, y=10, ch1, ch2, b;
setxy(14,1);
printf("...Find The Ball...") ;
display_border();
setxy(batx, baty);
printf("===");
setxy(ballx, bally);
printf("%c",2);
ch1 = getch();
ch2 = 0;
/*When accepting arrow key, function must be called twice; first call returns 0/0xE0; second call returns actual key code.*/
if (ch1 == 0xE0)
{
while(ch2=getch())
{
if(ch2 == 'X') exit(0);
if(ch2 == 75)//Left
{
if(batx>11) //Restrict bat on left side wall
{
setxy(--batx,baty);
printf("===");
setxy(batx+3,baty);//Space to clear bat route
printf(" ");
}
}
if(ch2 == 77) //Right
{
if(batx<37)//Restrict bat on right side wall
{
setxy(++batx,baty);
printf("===");
setxy(batx-1,baty);//Space to clear bat route
printf(" ");
}
}
if(ch2 == 72) //Up
{
if(baty>6)
{
setxy(batx,--baty);
printf("===");
setxy(batx,baty+1);
printf(" ");
}
}
if(ch2 == 80) //Down
{
if(baty<14)
{
setxy(batx,++baty);
printf("===");
setxy(batx,baty-1);
printf(" ");
}
}
if(batx==ballx-1 && baty==bally)
{
setxy(ballx-9,bally+4);
printf("...Congratulations...");
setxy(ballx-9,bally+5);
printf(" ...Ball Found..");
setxy(ballx-9,bally+6);
printf(" ...Game Over..");
getch();
exit(0);
}
}
}
getch();
return 0;
}
void display_border()
{
int i, j;
//Top border line...
setxy(10,5);
for(j=0; j<30; j++)
printf("%c", 223);
//Bottom border line...
setxy(10,15);
for(j=0; j<=30; j++)
printf("%c", 223);
//Left and Right border line...
for(j=0; j<10; j++)
{
setxy(10,5+j);
printf("%c",219);
setxy(40,5+j);
printf("%c",219);
}
printf("\n");
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}