#include <SFML/Graphics.hpp> #include <time.h> using namespace sf; int N=30,M=20; int size=16; int w = size*N; int h = size*M; int dir,num=4; struct Snake { int x,y;} s[100]; struct Fruit { int x,y;} f; void Tick() { for (int i=num;i>0;--i) {s[i].x=s[i-1].x; s[i].y=s[i-1].y;} if (dir==0) s[0].y+=1; if (dir==1) s[0].x-=1; if (dir==2) s[0].x+=1; if (dir==3) s[0].y-=1; if ((s[0].x==f.x) && (s[0].y==f.y)) {num++; f.x=rand()%N; f.y=rand()%M;} if (s[0].x>N) s[0].x=0; if (s[0].x<0) s[0].x=N; if (s[0].y>M) s[0].y=0; if (s[0].y<0) s[0].y=M; for (int i=1;i<num;i++) if (s[0].x==s[i].x && s[0].y==s[i].y) num=i; } int main() { srand(time(0)); RenderWindow window(VideoMode(w, h), "Snake Game!"); Texture t1,t2; t1.loadFromFile("images/white.png"); t2.loadFromFile("images/red.png"); Sprite sprite1(t1); Sprite sprite2(t2); Clock clock; float timer=0, delay=0.1; f.x=10; f.y=10; while (window.isOpen()) { float time = clock.getElapsedTime().asSeconds(); clock.restart(); timer+=time; Event e; while (window.pollEvent(e)) { if (e.type == Event::Closed) window.close(); } if (Keyboard::isKeyPressed(Keyboard::Left)) dir=1; if (Keyboard::isKeyPressed(Keyboard::Right)) dir=2; if (Keyboard::isKeyPressed(Keyboard::Up)) dir=3; if (Keyboard::isKeyPressed(Keyboard::Down)) dir=0; if (timer>delay) {timer=0; Tick();} ////// draw /////// window.clear(); for (int i=0; i<N; i++) for (int j=0; j<M; j++) { sprite1.setPosition(i*size, j*size); window.draw(sprite1); } for (int i=0;i<num;i++) { sprite2.setPosition(s[i].x*size, s[i].y*size); window.draw(sprite2); } sprite2.setPosition(f.x*size, f.y*size); window.draw(sprite2); window.display(); } return 0; }
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
}