Professional Documents
Culture Documents
Xonix
Xonix
Xonix
hpp>
#include <time.h>
using namespace sf;
struct Enemy
{int x,y,dx,dy;
Enemy()
{
x=y=300;
dx=4-rand()%8;
dy=4-rand()%8;
}
void move()
{
x+=dx; if (grid[y/ts][x/ts]==1) {dx=-dx; x+=dx;}
y+=dy; if (grid[y/ts][x/ts]==1) {dy=-dy; y+=dy;}
}
};
int main()
{
srand(time(0));
Texture t1,t2,t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/gameover.png");
t3.loadFromFile("images/enemy.png");
int enemyCount = 4;
Enemy a[10];
bool Game=true;
int x=0, y=0, dx=0, dy=0;
float timer=0, delay=0.07;
Clock clock;
for (int i=0;i<M;i++)
for (int j=0;j<N;j++)
if (i==0 || j==0 || i==M-1 || j==N-1) grid[i][j]=1;
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 (e.type == Event::KeyPressed)
if (e.key.code==Keyboard::Escape)
{
for (int i=1;i<M-1;i++)
for (int j=1;j<N-1;j++)
grid[i][j]=0;
x=10;y=0;
Game=true;
}
}
if (Keyboard::isKeyPressed(Keyboard::Left)) {dx=-1;dy=0;};
if (Keyboard::isKeyPressed(Keyboard::Right)) {dx=1;dy=0;};
if (Keyboard::isKeyPressed(Keyboard::Up)) {dx=0;dy=-1;};
if (Keyboard::isKeyPressed(Keyboard::Down)) {dx=0;dy=1;};
if (!Game) continue;
if (timer>delay)
{
x+=dx;
y+=dy;
if (grid[y][x]==2) Game=false;
if (grid[y][x]==0) grid[y][x]=2;
timer=0;
}
if (grid[y][x]==1)
{
dx=dy=0;
/////////draw//////////
window.clear();
sTile.setTextureRect(IntRect(36,0,ts,ts));
sTile.setPosition(x*ts,y*ts);
window.draw(sTile);
sEnemy.rotate(10);
for (int i=0;i<enemyCount;i++)
{
sEnemy.setPosition(a[i].x,a[i].y);
window.draw(sEnemy);
}
if (!Game) window.draw(sGameover);
window.display();
}
return 0;
}