Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

891140 Posts in 33524 Topics- by 24768 Members - Latest Member: Stome

June 19, 2013, 01:44:02 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)SDL_Rect won't move!
Pages: [1]
Print
Author Topic: SDL_Rect won't move!  (Read 348 times)
indietom
Level 2
**

Tom


View Profile Email
« on: July 21, 2012, 08:07:59 AM »

Hi!
The SDL_Rect ene2C, playerC won't move.
Here's is the code!
Code:
#include "sdl\sdl.h"
#include "iostream"
#undef main

const int WIN_W = 640;
const int WIN_H = 480;
const char* WIN_T = " Sentoki's quest ";

 bool collision(SDL_Rect* playerC, SDL_Rect* ene2C)
    {
         if(playerC->y >= ene2C->y + ene2C->h)
         return 0;
         if(playerC->x >= ene2C->x + ene2C->w)
         return 0;
         if(playerC->y + playerC->h <= ene2C->y)
         return 0;
         if(playerC->x + playerC->w <= ene2C->x)
         return 0;
        return 1;
    }

void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height);

using namespace std;

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface* screen = SDL_SetVideoMode(WIN_W,WIN_H, 0, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
    SDL_WM_SetCaption(WIN_T, 0);
    SDL_Surface* bitmap = SDL_LoadBMP("sprite.bmp");
    SDL_Surface* ene2 = NULL;
    SDL_Surface* back = SDL_LoadBMP("back.bmp");
    SDL_Surface* bul = NULL;
    SDL_Surface* gui1 = SDL_LoadBMP("GUI1.bmp");
    SDL_Surface* fore = SDL_LoadBMP("fore.bmp");
    SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));
    SDL_SetColorKey(gui1, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255));

    int playerX = 300;
    int playerY = 200;
    int playerW = 32;
    int playerH = 46;
    int playerImx = 9;
    int playerImy = 0;

    int bulX = 10000;
    int bulY = 10000;
    int bulW = 57;
    int bulH = 36;
    int bulImx = 40;
    int bulImy = 80;

    int backX = 0;
    int backY = 0;
    int backW = 1280;
    int backH = 480;
    int backImx = 1;
    int backImy = 1;

    int ene2X = 700;
    int ene2Y = 200;
    int ene2W = 50;
    int ene2H = 50;
    int ene2Imx = 504;
    int ene2Imy = 352;

    int foreX = 1000;
    int foreY = 0;
    int foreW = 640;
    int foreH = 480;
    int foreImx = 0;
    int foreImy = 0;

    int gui1X = 40000;
    int gui1Y = 440;
    int gui1W = 120;
    int gui1H = 36;
    int gui1Imx = 21;
    int gui1Imy = 33;

     SDL_Event event;
     bool gameRunning = true;
     bool keysHeld[323] = {false};
     while(gameRunning)
     {
         if(SDL_PollEvent(&event))
         {
             if(event.type == SDL_QUIT)
             {
                 gameRunning = false;
             }
             if(event.type == SDL_KEYDOWN)
             {
                 keysHeld[event.key.keysym.sym] = true;
             }
             if(event.type == SDL_KEYUP)
             {
                 keysHeld[event.key.keysym.sym] = false;
             }
         }
         if(keysHeld[SDLK_ESCAPE])
         {
             gameRunning = false;
         }
         if(keysHeld[SDLK_UP])
         {
             playerY -= 1;
             cout << "up \n";
         }
         if(keysHeld[SDLK_LEFT])
         {
             playerX -= 1;
             cout << "left \n";
         }
         if(keysHeld[SDLK_RIGHT])
         {
             playerX += 1;
             cout << "Right \n";
         }
         if(keysHeld[SDLK_DOWN])
         {
             playerY += 1;
             cout << "down \n";
         }
         if(keysHeld[SDLK_SPACE])
         {
             bulX = playerX -10;
             bulY = playerY +22;
         }

         if(keysHeld[SDLK_r])
         {
             ene2X = 700;
             ene2Y = 20;
         gui1X = 320;
         gui1Y = 240;
         }


            SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));




      drawSprite(back,
                 screen,
                 backImx, backImy,
                 backX, backY,
                 backW, backH);

      drawSprite(bitmap,
                 screen,
                 playerImx, playerImy,
                 playerX, playerY,
                 playerW, playerH);

      drawSprite(bitmap,
                 screen,
                 bulImx, bulImy,
                 bulX, bulY,
                 bulW, bulH);

         drawSprite(gui1,
                screen,
                gui1Imx, gui1Imy,
                gui1X, gui1Y,
                gui1W, gui1H);

     drawSprite(bitmap,
                screen,
                ene2Imx, ene2Imy,
                ene2X, ene2Y,
                ene2W, ene2H);



SDL_Rect playerC;
SDL_FillRect  (screen,&playerC, SDL_MapRGB(screen->format, 255, 0, 255));
playerC.x = 400;
playerC.y = 100;
playerC.w = 50;
playerC.h = 50;


   SDL_Rect ene2C;
   SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
   ene2C.x = 1;
   ene2C.y = 1;
   ene2C.w = 50;
   ene2C.h = 50;


   SDL_Rect bulC{10000,10000,50,30};
   SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
      bulX += 3;
      ene2X -= 1;

      if(keysHeld[SDLK_1])
      {
          playerC.x++;
      }

      ene2C.x++;
     

      if(playerY >= ene2Y);
      else ene2Y -= 1;

      if(playerY <= ene2Y);
      else ene2Y += 1;

      if(playerY >= 71);
      else playerY += 1;

      if(playerY + 44 <= 397);
      else playerY -= 1;

      if(collision(&playerC,&ene2C))
      {
         gui1X = 320;
         gui1Y = 240;
      }

            SDL_Flip(screen);
            }
    SDL_FreeSurface(bitmap);
    SDL_FreeSurface(bul);
    SDL_FreeSurface(gui1);
    SDL_FreeSurface(fore);
    SDL_FreeSurface(ene2);

    SDL_Quit();
    return 0;
}

void drawSprite(SDL_Surface* imageSurface,
                SDL_Surface* screenSurface,
                int srcX, int srcY,
                int dstX, int dstY,
                int width, int height)


{
   SDL_Rect srcRect;
   srcRect.x = srcX;
   srcRect.y = srcY;
   srcRect.w = width;
   srcRect.h = height;
   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;
   dstRect.w = width;
   dstRect.h = height;
   SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}
 

I think the problem is somewhere at
Code:
SDL_Rect playerC;
SDL_FillRect  (screen,&playerC, SDL_MapRGB(screen->format, 255, 0, 255));
playerC.x = 400;
playerC.y = 100;
playerC.w = 50;
playerC.h = 50;


   SDL_Rect ene2C;
   SDL_FillRect(screen,&ene2C, SDL_MapRGB(screen->format, 255, 0, 255));
   ene2C.x = 1;
   ene2C.y = 1;
   ene2C.w = 50;
   ene2C.h = 50;


   SDL_Rect bulC{10000,10000,50,30};
   SDL_FillRect(screen,&bulC, SDL_MapRGB(screen->format, 255, 0, 255));
      bulX += 3;
      ene2X -= 1;

      if(keysHeld[SDLK_1])
      {
          playerC.x++;
      }

      ene2C.x++;
     

      if(playerY >= ene2Y);
      else ene2Y -= 1;

      if(playerY <= ene2Y);
      else ene2Y += 1;

      if(playerY >= 71);
      else playerY += 1;

      if(playerY + 44 <= 397);
      else playerY -= 1;

      if(collision(&playerC,&ene2C))
      {
         gui1X = 320;
         gui1Y = 240;
      }


Thanks for any help!
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Claw
Level 4
****



View Profile WWW Email
« Reply #1 on: July 21, 2012, 08:15:07 AM »

playerC.x = 400;
playerC.y = 100;

You seem to be setting the player back to these coords each iteration of the game loop as far as I can tell. Also you should really separate the code out into smaller classes or files, for the sake of your own sanity.
Logged

indietom
Level 2
**

Tom


View Profile Email
« Reply #2 on: July 21, 2012, 08:20:58 AM »

playerC.x = 400;
playerC.y = 100;

You seem to be setting the player back to these coords each iteration of the game loop as far as I can tell. Also you should really separate the code out into smaller classes or files, for the sake of your own sanity.


how should I make it so the player still start at a cordinat?
(The reason it's quite spaghetti code is because I'm at a game jam)
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
rivon
Level 10
*****



View Profile
« Reply #3 on: July 21, 2012, 09:10:41 AM »

WTF is this?
Code:
if(playerY >= ene2Y);
You check a condition but you don't do anything if it's true? You're seriously doing it wrong Facepalm No No NO
Logged
indietom
Level 2
**

Tom


View Profile Email
« Reply #4 on: July 21, 2012, 09:24:24 AM »

WTF is this?
Code:
if(playerY >= ene2Y);
You check a condition but you don't do anything if it's true? You're seriously doing it wrong Facepalm No No NO


OOHHH that's my "ai" code.
The ene2 is supposed to chase the player. ene2 is separate from ene2C
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Laurent C
Level 0
**


View Profile
« Reply #5 on: July 21, 2012, 12:56:53 PM »

Code:
      if(playerY >= ene2Y);
      else ene2Y -= 1;
WTF is this? It's some code that needs explanation instead of just saying "you're doing something wrong"

Here you can just use < instead of >= and an "else" on the next line.

Code:
         if(keysHeld[SDLK_r])
         {
             ene2X = 700;
             ene2Y = 20;
         gui1X = 320;
         gui1Y = 240;
         }
Use proper spacing. It's not completely unreadable though.

Quote from: indietom
how should I make it so the player still start at a cordinat?
You need 3 procedures: Initialize, Gameloop, Terminate.
When the game starts, you call Initialize. Then the game gets stuck in the Gameloop. Then there's Terminate which is called when the game closes and you may want to save some data from there or ask the player "Are you sure you want to quit without saving?".

Feel free to rename them to whatever you want.


// Also put comments in your code more often.
Logged
indietom
Level 2
**

Tom


View Profile Email
« Reply #6 on: July 23, 2012, 09:45:25 AM »

Guys I fixed it.
Logged

Code::Blocks
This is my profile, there are many like it but this one is mine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic