someone please help

hello thank you for clicking this topic.
I'm making a simple 2d game and I already have of my question solved, I can block off the left and the top parts of the map I do not want the sprite to walk in (the black) outside of the grass. Now I cannot figure out how to block off the bottom and right? someone please help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  #include "SDL.h"
#include "CCamera.h"
#define SCREEN_WIDTH  640   
#define SCREEN_HEIGHT 480    
#define SPRITE_SIZE   32

int main ( int argc, char *argv[] )
{
  SDL_Surface *screen, *temp, *sprite, *grass;
  SDL_Rect rcSprite, rcGrass;
  SDL_Event event;
  Uint8 *keystate;
  
  int colorkey, gameover;

  /* initialize SDL */
  SDL_Init(SDL_INIT_VIDEO);

  /* set the title bar */
  SDL_WM_SetCaption("SDL Move", "SDL Move");

  /* create window */
  screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);                   


  /* load sprite */
  temp   = SDL_LoadBMP("sprite.bmp");
  sprite = SDL_DisplayFormat(temp);
  SDL_FreeSurface(temp);

 
CCamera CCamera::CameraControl;
 
CCamera::CCamera() {
    X = Y = 0;
 
    TargetX = TargetY = NULL;
 
    TargetMode = TARGET_MODE_NORMAL;
}
 
void CCamera::OnMove(int MoveX, int MoveY) {
    X += MoveX;
    Y += MoveY;
}
 
int CCamera::GetX() {
    if(TargetX != NULL) {
        if(TargetMode == TARGET_MODE_CENTER) {
            return *TargetX - (WWIDTH / 2);
        }
 
        return *TargetX;
    }
 
    return X;
}
 
int CCamera::GetY() {
    if(TargetY != NULL) {
        if(TargetMode == TARGET_MODE_CENTER) {
            return *TargetY - (WHEIGHT / 2);
        }
 
        return *TargetY;
    }
 
    return Y;
}
 
void CCamera::SetPos(int X, int Y) {
    this->X = X;
    this->Y = Y;
}
 
void CCamera::SetTarget(int* X, int* Y) {
    TargetX = X;
    TargetY = Y;
}
  /* setup sprite colorkey and turn on RLE */
  colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
  SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);


  /* load grass */
  temp  = SDL_LoadBMP("grass.bmp");
  grass = SDL_DisplayFormat(temp);
  SDL_FreeSurface(temp);

  /* set sprite position */
  rcSprite.x = 145;      
  rcSprite.y = 160;

  gameover = 0;

  /* message pump */
  while (!gameover)
  {
        
    /* look for an event */
    if (SDL_PollEvent(&event)) {
      /* an event was found */
      switch (event.type) {
        /* close button clicked */
        case SDL_QUIT:
          gameover = 1;
          break;

        /* handle the keyboard */
        case SDL_KEYDOWN:
          switch (event.key.keysym.sym) {
            case SDLK_ESCAPE:
            case SDLK_q:
              gameover = 1;
              break;
          }
          break;
      }
    }

    /* handle sprite movement */
    keystate = SDL_GetKeyState(NULL);
    if (keystate[SDLK_LEFT] ) {
      rcSprite.x -= 2;
    }
    if (keystate[SDLK_RIGHT] ) {
      rcSprite.x += 2;
    }
    if (keystate[SDLK_UP] ) {
      rcSprite.y -= 2;
    }
    if (keystate[SDLK_DOWN] ) {
      rcSprite.y += 2;
    }
    /* collide with edges of screen */
    if ( rcSprite.x < 150) {       
      rcSprite.x = 150;        
    }
    else if ( rcSprite.x > SCREEN_WIDTH-SPRITE_SIZE ) {
      rcSprite.x = SCREEN_WIDTH-SPRITE_SIZE;
    }
    if ( rcSprite.y < 70) {
      rcSprite.y = 70;
    }
    else if ( rcSprite.y > SCREEN_HEIGHT-SPRITE_SIZE ) {
      rcSprite.y = SCREEN_HEIGHT-SPRITE_SIZE;
    }


    /* draw the grass */
    for (int x = 11; x < SCREEN_WIDTH / SPRITE_SIZE; x++) {       
      for (int y = 11; y < SCREEN_HEIGHT / SPRITE_SIZE; y++) { 
        rcGrass.x = 0 * SPRITE_SIZE;  
        rcGrass.y = 0 * SPRITE_SIZE;
        SDL_BlitSurface(grass, NULL, screen, &rcGrass);
      }
    }
    /* draw the sprite */
    SDL_BlitSurface(sprite, NULL, screen, &rcSprite);

    /* update the screen */
    SDL_UpdateRect(screen, 0, 0, 0, 0);
  }

  /* clean up */
  SDL_FreeSurface(sprite);
  SDL_FreeSurface(grass);
  SDL_Quit();

  return 0;
}
It looks like this is the important part:
135
136
137
138
139
140
141
142
143
144
145
146
147
    /* collide with edges of screen */
    if ( rcSprite.x < 150) {       
      rcSprite.x = 150;        
    }
    else if ( rcSprite.x > SCREEN_WIDTH-SPRITE_SIZE ) {
      rcSprite.x = SCREEN_WIDTH-SPRITE_SIZE;
    }
    if ( rcSprite.y < 70) {
      rcSprite.y = 70;
    }
    else if ( rcSprite.y > SCREEN_HEIGHT-SPRITE_SIZE ) {
      rcSprite.y = SCREEN_HEIGHT-SPRITE_SIZE;
    }

Here, the left boundary is 150. A similar adjustment presumably needs to be made for the right boundary, SCREEN_WIDTH-SPRITE_SIZE-150
You might consider using a named constant such as
const int border_width = 150; rather than having the literal 150 in several places in the code.
Note in C++ this is the preferred method, rather than using #define .
Topic archived. No new replies allowed.