Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "functions.h"
- #include <iostream>
- #include <SDL/SDL.h>
- #include <SDL/SDL_mixer.h>
- #include <SDL/SDL_ttf.h>
- using namespace std;
- int main() {
- //Iniciamos
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- cout << "Error inesperado";
- cin.sync();
- cin.get();
- return 0;
- }
- if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) < 0 ) {
- cout << "Error inesperado";
- return 0;
- }
- //Variables
- SDL_Surface *nave;
- SDL_Surface *fondo;
- SDL_Surface *screen;
- SDL_Surface *texto;
- SDL_Rect destino;
- Uint8 * teclas = SDL_GetKeyState ( NULL );
- SDL_Event suceso;
- int yNave = 230/2, xNave = 240/2;
- int yFondo = 0, xFondo = 0;
- int terminado = 0;
- int tecla;
- TTF_Font* font = TTF_OpenFont("air.ttf",36);
- SDL_Color color = {250,250,250};
- // The music that will be played
- Mix_Music *musica = NULL;
- //The sound effects that will be used
- Mix_Chunk *disparos = NULL;
- Mix_Chunk *motor = NULL;
- Mix_Chunk *explosion = NULL;
- Mix_VolumeMusic(50);
- musica = Mix_LoadMUS("pixeluniverse.wav");
- if (!Mix_PlayMusic(musica, -1)) {
- cout << "Error inesperado";
- }
- //Definir variables multimedia
- nave = SDL_LoadBMP("nave.bmp");
- fondo = SDL_LoadBMP("fondo.bmp");
- texto = TTF_RenderText_Solid(font,"Score : 0",color);
- //Definir variables numericas
- int yPos = fondo->h;
- //Poner transparencia a la nave
- quit_color(nave, 0, 255, 0);
- //Creamos ventana
- screen = SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
- if (screen == NULL) {
- cout << "Error inesperado";
- cin.sync();
- cin.get();
- SDL_Quit();
- return 0;
- }
- // El getch cada 20 segundos
- SDL_EnableKeyRepeat(5, 5);
- //Nombre de ventana
- SDL_WM_SetCaption( "Naves", NULL );
- while (terminado == 0) {
- //Dibujamos fondo
- SDL_Rect posFondo;
- posFondo.y = yFondo;
- posFondo.x = xFondo;
- SDL_BlitSurface(fondo, NULL, screen, &posFondo);
- apply_surface( 0, yPos, fondo, screen );
- //Dibujamos texto
- SDL_BlitSurface(texto,NULL,screen,NULL);
- //Dibujamos nave
- destino.y = yNave;
- destino.x = xNave;
- SDL_BlitSurface(nave, NULL , screen, &destino);
- SDL_Flip(screen);
- //Miramos tecla
- while (SDL_PollEvent(&suceso)) {
- if (suceso.type == SDL_QUIT) terminado = 1;
- if (suceso.type == SDL_KEYDOWN)
- if (suceso.key.keysym.sym == SDLK_ESCAPE) terminado = 1;
- }
- if (teclas[SDLK_ESCAPE]) { terminado = 1; }
- if (teclas[SDLK_UP]) { yNave -= 2; }
- if (teclas[SDLK_DOWN]) { yNave += 2; }
- if (teclas[SDLK_LEFT]) { xNave -= 3; }
- if (teclas[SDLK_RIGHT]) { xNave += 3; }
- yPos -= -1;
- yFondo -= -1;
- //Scrolling background
- if( yFondo >= fondo->h )
- {
- yFondo = 0; }
- if( yPos >= fondo->h ) { yPos = fondo->h; }
- //Bucle pause
- SDL_Delay( 5 );
- }
- Mix_FreeMusic(musica);
- SDL_Quit();
- cout << "EXIT";
- return 0;;
- }
Advertisement
Add Comment
Please, Sign In to add comment