0% found this document useful (0 votes)
35 views2 pages

Tron Game Source Code Download

The document describes code for a Tron game written in C++ using the SFML library. The code defines structures for players and game logic to move the players around a grid and detect collisions in 2D space. On each game loop iteration, it checks input and moves the players, draws their positions to a render texture, and displays the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Tron Game Source Code Download

The document describes code for a Tron game written in C++ using the SFML library. The code defines structures for players and game logic to move the players around a grid and detect collisions in 2D space. On each game loop iteration, it checks input and moves the players, draws their positions to a render texture, and displays the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <SFML/Graphics.

hpp>
#include <time.h>
using namespace sf;

const int W=600;


const int H=480;
int speed = 4;
bool field[W][H]={0};

struct player
{ int x,y,dir;
Color color;
player(Color c)
{
x=rand() % W;
y=rand() % H;
color=c;
dir=rand() % 4;
}
void tick()
{
if (dir==0) y+=1;
if (dir==1) x-=1;
if (dir==2) x+=1;
if (dir==3) y-=1;

if (x>=W) x=0; if (x<0) x=W-1;


if (y>=H) y=0; if (y<0) y=H-1;
}

Vector3f getColor()
{return Vector3f(color.r,color.g,color.b);}
};

int main()
{
srand(time(0));

RenderWindow window(VideoMode(W, H), "The Tron Game!");


[Link](60);

Texture texture;
[Link]("[Link]");
Sprite sBackground(texture);

player p1(Color::Red), p2(Color::Green);

Sprite sprite;
RenderTexture t;
[Link](W, H);
[Link](true);
[Link]([Link]());
[Link](); [Link](sBackground);

bool Game=1;

while ([Link]())
{
Event e;
while ([Link](e))
{
if ([Link] == Event::Closed)
[Link]();
}

if (Keyboard::isKeyPressed(Keyboard::Left)) if ([Link]!=2) [Link]=1;


if (Keyboard::isKeyPressed(Keyboard::Right)) if ([Link]!=1) [Link]=2;
if (Keyboard::isKeyPressed(Keyboard::Up)) if ([Link]!=0) [Link]=3;
if (Keyboard::isKeyPressed(Keyboard::Down)) if ([Link]!=3) [Link]=0;

if (Keyboard::isKeyPressed(Keyboard::A)) if ([Link]!=2) [Link]=1;


if (Keyboard::isKeyPressed(Keyboard::D)) if ([Link]!=1) [Link]=2;
if (Keyboard::isKeyPressed(Keyboard::W)) if ([Link]!=0) [Link]=3;
if (Keyboard::isKeyPressed(Keyboard::S)) if ([Link]!=3) [Link]=0;

if (!Game) continue;

for(int i=0;i<speed;i++)
{
[Link](); [Link]();
if (field[p1.x][p1.y]==1) Game=0;
if (field[p2.x][p2.y]==1) Game=0;
field[p1.x][p1.y]=1;
field[p2.x][p2.y]=1;

CircleShape c(3);
[Link](p1.x,p1.y); [Link]([Link]); [Link](c);
[Link](p2.x,p2.y); [Link]([Link]); [Link](c);
[Link]();
}

////// draw ///////


[Link]();
[Link](sprite);
[Link]();
}

return 0;
}

You might also like