All pastes #85862 Raw Edit

Someone

public text v1 · immutable
·published 2006-07-12 06:49 UTC
rendered paste body
#include <iostream>
#include <vector>
#include <fstream>
#include <SDL/SDL.h>
#include <time.h>

using namespace std;

int draw(SDL_Surface * image, SDL_Surface *source_src, int x, int y, int x1, int y1);
SDL_Surface * load(SDL_Surface * image, string s);

struct node {
	int x, y;
};

vector <node> make(vector <node>  & t, int x, int y)
{
	vector <node> p;
	node  r;
	r.x = x; r.y = y;
	int unsigned i=0;
	
	p.push_back(r);  // first element
	
	for (i=0; i < t.size()-1;i++)
		p.push_back(t[i]);
	t.clear();
	for  (i=0; i < p.size(); i++)
		  t.push_back(p[i]);
	return t;	
}

int main()
{
	int m=20, n=15;
	int i,j;
	int x_pradzia=0, y_pradzia=0;
	int x1, y1;
	int unsigned k=0;
    vector <node> g;	
    node p;
	vector <vector <int> > matrix;
	srand(time(0));
	x1 = rand() % 12+1;
	y1 = rand() % 13+1;
	int x=0,y=0;
	matrix.resize(m);
	for (i=0;i < m; i++)
		for (j=0; j<n; j++)
			matrix[i].push_back(0);
		
    for (i=0; i < m; i++)
		for (j=0; j < n; j++)
		matrix[i][j] = 0;
		
	matrix[x1][y1]= 1;
		
	p.x = 1; p.y = 6;
	g.push_back(p);		
		
	SDL_Surface *screen;
	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(640,480,24, SDL_SWSURFACE | SDL_ANYFORMAT);
	SDL_Surface *img = load(img,"/home/kestaz/veikejas.bmp");
	SDL_Surface *gyv = load(gyv,"/home/kestaz/gyv.bmp");
	SDL_Surface *fonas = load(fonas,"/home/kestaz/back.bmp");
	if (screen == NULL) exit(1);		
	bool t=0;
	int message = 0;
	int xp=0, yp=0;
	SDL_Event event;
	draw(fonas,screen,0, 0, 0, 0);
		while (t == 0)
	{
		
		for (i=0; i < m; i++)
		for (j=0; j < n; j++)
		matrix[i][j] = 0;
		
		g = make(g,g[0].x, g[0].y);
		
		for (k=0; k < g.size(); k++)
		{
			matrix[g[k].x][g[k].y] = 2;
		}
		matrix[x1][y1]= 1; 
		while (SDL_PollEvent(&event))
		{
		  if (event.type == SDL_QUIT)  { t = 1; }
		  if (event.key.keysym.sym == SDLK_RIGHT) { message = 1; }
		  if (event.key.keysym.sym == SDLK_LEFT) { message = 2; }
		  if (event.key.keysym.sym == SDLK_UP) { message = 3; }
		  if (event.key.keysym.sym == SDLK_DOWN) { message = 4; }
		  
		 }
        draw(fonas,screen,0, 0, 0, 0);
		 
		for (j = 0; j < n; j++)    	
	      {
            i=0;
	        for (i=0; i < m; i++)
	        {
		      if (matrix[i][j] == 1) draw(img,screen,x_pradzia + 32 * i, y_pradzia + 32 * j, 0, 0);
		      if (matrix[i][j] == 2) 
			  {
				 x = i;
				 y = j;
				 xp = x_pradzia + 32 * i;
			     yp = y_pradzia + 32 * j;
			    draw(gyv,screen,x_pradzia + 32 * i, y_pradzia + 32 * j, 0, 0);
			  }
			  if (matrix[i][0] == 2) exit(0);
			  if (matrix[i][14] == 2) exit(0);
		  }
		  if (matrix[0][j] == 2) exit(0);
		  if (matrix[19][j] == 2) exit(0);
          }
		  
	  
	   if (message == 1) g[0].x++;
	   if (message == 2) g[0].x--;
	   if (message == 3) g[0].y--;
	   if (message == 4) g[0].y++;
	   if (g[0].x == x1 && g[0].y == y1)  
	   {
		   p.x = x1; p.y=y1;
		   g.push_back(p);
		   x1 = rand() % 12+1;
	       y1 = rand() % 13+1;
	   }		   
	   SDL_Flip(screen); 	  
	   SDL_FillRect(screen, NULL, 0);
	   SDL_Delay(250);
	}
	SDL_FreeSurface(img);
	SDL_FreeSurface(gyv);
	SDL_Quit();	
	return 0;
}

int draw(SDL_Surface * image, SDL_Surface *source_src, int x, int y, int x1, int y1)
{
	SDL_Rect src, dst;
	src.x = x1;
	src.y = y1;
	src.w = image->w;
	src.h = image->h;
	dst.x = x;
	dst.y = y;
	dst.w = image->w;
	dst.h = image->h;
	SDL_BlitSurface(image, &src, source_src, &dst);
	return 0;
}

SDL_Surface * load(SDL_Surface * image, string s)
{
	SDL_Surface *tmp;
	tmp = SDL_LoadBMP(s.c_str());
	if (tmp == NULL) exit(0);
    image = SDL_DisplayFormat(tmp);
    SDL_FreeSurface(tmp);	
	if (image !=NULL)
	{
		Uint32 colorkey = SDL_MapRGB(image->format, 0xFF,0xFF,0xFF);
		SDL_SetColorKey( image, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
	}
	return image;
}