Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include "classes.h"
- using namespace std;
- bool gameLoop = true;
- int window_x = 640;
- int window_y = 480;
- SDL_Surface *screen = SDL_SetVideoMode(window_x, window_y, 32, SDL_SWSURFACE);
- SDL_Surface *background = SDL_LoadBMP("images/background.bmp");
- SDL_Surface *tileset = SDL_LoadBMP("images/tileset.bmp");
- int drawUnit(int x, int y, int i);
- void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
- SDL_Rect rect;
- rect.x = x;
- rect.y = y;
- SDL_BlitSurface(source, NULL, destination, &rect);
- }
- void load_sprite(int x, int y, SDL_Surface *source, SDL_Surface *destination, int bx, int by, int width, int height) {
- SDL_Rect rect;
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- rect.x = bx;
- rect.y = by;
- rect.w = width;
- rect.h = height;
- SDL_BlitSurface(source, &rect, destination, &offset);
- }
- int main(int argc, char *args[]) {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_WM_SetCaption("Kaktus - getIt", NULL);
- Uint8 *key;
- Uint32 colorkey = SDL_MapRGB( tileset->format, 255, 0, 255);
- SDL_SetColorKey( tileset, SDL_SRCCOLORKEY, colorkey );
- cPlayer.x = 32;
- cPlayer.y = 0;
- cPlayer.xspeed = 1;
- cPlayer.yspeed = 1;
- cKey.x = 40;
- cKey.y = 340;
- cCpu.unlock = false;
- //gameloop
- while(gameLoop) {
- SDL_PumpEvents();
- key = SDL_GetKeyState(NULL);
- if(key[SDLK_DOWN] || key[SDLK_s]) {
- cPlayer.y+=cPlayer.yspeed;
- }
- if(key[SDLK_UP] || key[SDLK_w]) {
- cPlayer.y-=cPlayer.yspeed;
- }
- if(key[SDLK_RIGHT] || key[SDLK_d]) {
- cPlayer.x+=cPlayer.xspeed;
- }
- if(key[SDLK_LEFT] || key[SDLK_a]) {
- cPlayer.x-=cPlayer.xspeed;
- }
- if(key[SDLK_ESCAPE]) {
- gameLoop = false;
- }
- if(!cCpu.unlock) {
- if((cPlayer.y + 32 > cKey.y && cPlayer.y < cKey.y + 32) || (cPlayer.y > cKey.y + 32 && cPlayer.y <cKey.y)) {
- if((cPlayer.x + 16 > cKey.x && cPlayer.x < cKey.x + 18) || (cPlayer.x < cKey.x + 18 && cPlayer.x + 16 > cKey.x)) {
- cKey.x = rand() % (640 - 16) + (0-16);
- cKey.y = rand() % 480 + (0+8);
- cCpu.unlock = true;
- }
- }
- }
- //if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0));
- if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0)) {
- if((cPlayer.y + 32 > (480/2) && cPlayer.y < (480/2) + 32)) {
- cPlayer.y++;
- }
- }
- if((cPlayer.x + 16 > 0 && cPlayer.x < (640-32)) || (cPlayer.x < (640-32) && cPlayer.x + 16 > 0)) {
- if(cPlayer.y > (480/2) + 32 && cPlayer.y < (480/2)) {
- cPlayer.y++;
- }
- }
- if(cPlayer.x < window_x-640) {
- cPlayer.x++;
- } else if(cPlayer.x + 16 > window_x) {
- cPlayer.x--;
- }
- if(cPlayer.y < window_y - 480) {
- cPlayer.y++;
- } else if(cPlayer.y + 32 > window_y) {
- cPlayer.y--;
- }
- apply_surface(0, 0, background, screen);
- if(!cCpu.unlock) {
- drawUnit(0, 0, 2);
- drawUnit(cKey.x, cKey.y, 4);
- } else if(cCpu.unlock) {
- drawUnit(0, 0, 3);
- }
- for(int i = 0; i<(640/32); i++) {
- drawUnit((i*31), (480/2), 5);
- }
- drawUnit(cPlayer.x, cPlayer.y, 1);
- SDL_Flip(screen);
- }
- SDL_FreeSurface(background);
- SDL_FreeSurface(tileset);
- SDL_Quit();
- }
- int drawUnit(int x, int y, int i) {
- //player
- if(i == 1) {
- load_sprite(x, y, tileset, screen, 1, 1, 16, 32);
- }
- //Locked
- else if(i == 2) {
- load_sprite(x, y, tileset, screen, 32+ 2, 1, 32, 32);
- }
- //Unlocked
- else if(i == 3) {
- load_sprite(x, y, tileset, screen, 64+3, 1, 32, 32);
- }
- //key
- else if(i == 4) {
- load_sprite(x, y, tileset, screen, 96+4, 1, 31, 18);
- }
- //line
- else if(i == 5) {
- load_sprite(x, y, tileset, screen, (96+32)+5, 1, 32, 32);
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment