Moving Text - SCreen Saver in C++
Feb 04, 2014 Simple C/C Rocket Animation. February 4, 2014 6 Comments animation, c / c. But i want more c coding for rocket and i have to move it from bottom to top or left to right if possible than please help me more this is my computer graphic project. And i have some idea but i can’t go through it because of time limit. Try to help me fast as. Apr 06, 2009 Word wrap in Dev C Forum: Bloodshed Software Forum. Creator: Holly Benton. I no longer use Dev-C or have it installed) has an option to enable/disable line wrapping. It is not on by default, so you must presumably have set it. Line wrapping is a really bad idea in a code editor IMO, I cannot think of a good reason to have it, but most.
Moving Text - SCreen Saver in C++
Although it is exam time but i just can't help myself from compiler;; below is just code, nothing much to explain; just copy past and run it is compatible with visual studio as no graphic thing is used; enjoy
- #include<iostream>
- #include<windows.h>
- HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
- void clr(int y){SetConsoleTextAttribute(out,y);}
- void go(int a,int b){COORD cord; cord.X = a; cord.Y = b; SetConsoleCursorPosition(out,cord);}
- using namespace std;
- void G(int x,int y)
- {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(223)<<char(219)<<endl;y++;
- go(x,y);cout<<char(223)<<char(223)<<char(223)<<char(223)<<char(223)<<endl;}
- void A(int x,int y)
- {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(220)<<char(220)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<endl;}
- void M(int x,int y)
- {go(x,y);cout<<char(219)<<char(220)<<char(32)<<char(220)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(287)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;}
- void E(int x,int y)
- {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl;y++;
- go(x,y);cout<<char(219)<<char(205)<<char(205)<<char(32)<<char(32)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(32)<<endl;y++;
- go(x,y);cout<<char(223)<<char(223)<<char(223)<<char(223)<<char(223)<<endl;}
- void R(int x,int y)
- {go(x,y);cout<<char(220)<<char(220)<<char(220)<<char(220)<<char(220)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(32)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(220)<<char(220)<<char(220)<<char(219)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<char(32)<<endl;y++;
- go(x,y);cout<<char(219)<<char(32)<<char(32)<<char(219)<<char(220)<<endl;}
- int main()
- {
- int X=0,x1,x2,x3,x4,x5,x6,y=5; x1=0; x2=6; x3=12; x4=18; x5=24; x6=30;
- for (int i=0;i<1;)
- {clr(9);go(x1,y);G(x1,y);if(x1>72){x1=1;}x1++;clr(12);go(x2,y);A(x2,y);if(x2>72){x2=1;}x2++;
- clr(6);go(x3,y);M(x3,y);if(x3>72){x3=1;}x3++;clr(9);go(x4,y);M(x4,y);if(x4>72){x4=1;}x4++;
- clr(10);go(x5,y);E(x5,y);if(x5>72){x5=1;}x5++;clr(12);go(x6,y);R(x6,y);if(x6>72){x6=1;}x6++;
- if(x12){y+=3;} if(y>50){y=3;}system('cls');}
- return 0;
- }
Snake game using C++ complete code is demanded by many friend ! so i decided to write an article, providing you complete code  of Snake game in c++ without any error 🙂
Change the headers according to your compiler.
here you go snake game 🙂
Things you need to do are :
- Copy the code below
- Paste into your desired environment (dev c++, visual studio)
- compile and runÂ
#include <iostream>
#include <conio.h>
void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();
char getMapValue(int value);
// Map dimensions
const int mapwidth = 20;
const int mapheight = 20;
const int mapwidth = 20;
const int mapheight = 20;
const int size = mapwidth * mapheight;
// The tile values for the map
int map[size];
int map[size];
// Snake head details
int headxpos;
int headypos;
int direction;
int headxpos;
int headypos;
int direction;
// Amount of food the snake has (How long the body is)
int food = 3;
int food = 3;
// Determine if game is running
bool running;
bool running;
int main()
{
run();
return 0;
}
{
run();
return 0;
}
// Main game function
void run()
{
// Initialize the map
initMap();
running = true;
while (running) {
// If a key is pressed
if (kbhit()) {
// Change to direction determined by key pressed
changeDirection(getch());
}
// Upate the map
update();
void run()
{
// Initialize the map
initMap();
running = true;
while (running) {
// If a key is pressed
if (kbhit()) {
// Change to direction determined by key pressed
changeDirection(getch());
}
// Upate the map
update();
// Clear the screen
clearScreen();
clearScreen();
// Print the map
printMap();
printMap();
// wait 0.5 seconds
_sleep(500);
}
_sleep(500);
}
// Print out game over text
std::cout << “tt!!!Game over!” << std::endl << “ttYour score is: ” << food;
std::cout << “tt!!!Game over!” << std::endl << “ttYour score is: ” << food;
// Stop console from closing instantly
std::cin.ignore();
}
std::cin.ignore();
}
// Changes snake direction from input
void changeDirection(char key) {
/*
W
A + D
S
void changeDirection(char key) {
/*
W
A + D
S
1
4 + 2
3
*/
switch (key) {
case ‘w’:
if (direction != 2) direction = 0;
break;
case ‘d’:
if (direction != 3) direction = 1;
break;
case ‘s’:
if (direction != 4) direction = 2;
break;
case ‘a’:
if (direction != 5) direction = 3;
break;
}
}
4 + 2
3
*/
switch (key) {
case ‘w’:
if (direction != 2) direction = 0;
break;
case ‘d’:
if (direction != 3) direction = 1;
break;
case ‘s’:
if (direction != 4) direction = 2;
break;
case ‘a’:
if (direction != 5) direction = 3;
break;
}
}
// Moves snake head to new location
void move(int dx, int dy) {
// determine new head position
int newx = headxpos + dx;
int newy = headypos + dy;
void move(int dx, int dy) {
// determine new head position
int newx = headxpos + dx;
int newy = headypos + dy;
// Check if there is food at location
if (map[newx + newy * mapwidth] -2) {
// Increase food value (body length)
food++;
if (map[newx + newy * mapwidth] -2) {
// Increase food value (body length)
food++;
// Generate new food on map
generateFood();
}
generateFood();
}
// Check location is free
else if (map[newx + newy * mapwidth] != 0) {
running = false;
}
else if (map[newx + newy * mapwidth] != 0) {
running = false;
}
// Move head to new location
headxpos = newx;
headypos = newy;
map[headxpos + headypos * mapwidth] = food + 1;
headxpos = newx;
headypos = newy;
map[headxpos + headypos * mapwidth] = food + 1;
}
// Clears screen
void clearScreen() {
// Clear the screen
system(“cls”);
}
void clearScreen() {
// Clear the screen
system(“cls”);
}
// Generates new food on map
void generateFood() {
int x = 0;
int y = 0;
do {
// Generate random x and y values within the map
x = rand() % (mapwidth – 2) + 1;
y = rand() % (mapheight – 2) + 1;
void generateFood() {
int x = 0;
int y = 0;
do {
// Generate random x and y values within the map
x = rand() % (mapwidth – 2) + 1;
y = rand() % (mapheight – 2) + 1;
// If location is not free try again
} while (map[x + y * mapwidth] != 0);
} while (map[x + y * mapwidth] != 0);
// Place new food
map[x + y * mapwidth] = -2;
}
map[x + y * mapwidth] = -2;
}
// Updates the map
void update() {
// Move in direction indicated
switch (direction) {
case 0: move(-1, 0);
break;
case 1: move(0, 1);
break;
case 2: move(1, 0);
break;
case 3: move(0, -1);
break;
}
void update() {
// Move in direction indicated
switch (direction) {
case 0: move(-1, 0);
break;
case 1: move(0, 1);
break;
case 2: move(1, 0);
break;
case 3: move(0, -1);
break;
}
// Reduce snake values on map by 1
for (int i = 0; i < size; i++) {
if (map[i] > 0) map[i]–;
}
}
for (int i = 0; i < size; i++) {
if (map[i] > 0) map[i]–;
}
}
// Initializes map
void initMap()
{
// Places the initual head location in middle of map
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[headxpos + headypos * mapwidth] = 1;
void initMap()
{
// Places the initual head location in middle of map
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[headxpos + headypos * mapwidth] = 1;
// Places top and bottom walls
for (int x = 0; x < mapwidth; ++x) {
map[x] = -1;
map[x + (mapheight – 1) * mapwidth] = -1;
}
for (int x = 0; x < mapwidth; ++x) {
map[x] = -1;
map[x + (mapheight – 1) * mapwidth] = -1;
}
// Places left and right walls
for (int y = 0; y < mapheight; y++) {
map[0 + y * mapwidth] = -1;
map[(mapwidth – 1) + y * mapwidth] = -1;
}// Generates first food
generateFood();
}
for (int y = 0; y < mapheight; y++) {
map[0 + y * mapwidth] = -1;
map[(mapwidth – 1) + y * mapwidth] = -1;
}// Generates first food
generateFood();
}
// Prints the map to console
void printMap()
{
for (int x = 0; x < mapwidth; ++x) {
for (int y = 0; y < mapheight; ++y) {
// Prints the value at current x,y location
std::cout << getMapValue(map[x + y * mapwidth]);
}
// Ends the line for next x value
std::cout << std::endl;
}
}
void printMap()
{
for (int x = 0; x < mapwidth; ++x) {
for (int y = 0; y < mapheight; ++y) {
// Prints the value at current x,y location
std::cout << getMapValue(map[x + y * mapwidth]);
}
// Ends the line for next x value
std::cout << std::endl;
}
}
// Returns graphical character for display from map value
char getMapValue(int value)
{
// Returns a part of snake body
if (value > 0) return ‘o’;
char getMapValue(int value)
{
// Returns a part of snake body
if (value > 0) return ‘o’;
switch (value) {
// Return wall
case -1: return ‘X’;
// Return food
case -2: return ‘O’;
}
}
// Return wall
case -1: return ‘X’;
// Return food
case -2: return ‘O’;
}
}
OutPut [Snake Game using C++] :
You can get many authentic stuff regarding snake game using c++ at Tutorials Point.
Also don’t miss to enjoy:
Wrapping it up:
Hope so code for snake game using c++ works for you. If does’t feel free to ask any question or give any  suggestion related to C++/this snake game , in the comment box,
Dev C++ Moving Text Message
Also share ! Because sharing is caring 😉