Public Group

# help with 2d game (Directx 9/C++)

This topic is 3338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive made a little 2D game using Directx 9 The game contains 2 layers: background sprite and terrain sprite , and the player sprite Full Game: http://img249.imageshack.us/i/gamea.png/ Background Sprite: http://img217.imageshack.us/i/backgroundo.png/ Terrain Sprite: http://img109.imageshack.us/i/terrain.png/ Player Sprite: http://img513.imageshack.us/i/playerk.png/ My question is how can I know playerY based on the terrainY in playerX position Like when player clicks right button, player sprite moves x = x+1 and y = terrain.getY(player.x) Can someone tell me how can I do that? I think i need to get the pixel data of terrain but i dont know how to do that...

##### Share on other sites
here how you can get the color of a certain pixel in a texture.

http://www.gamedev.net/community/forums/topic.asp?topic_id=547224

first find the height of the terrain for each x value.

int terrainheight[terrainwidth];for(int i=0; i<terrainwidth; i++){  int j=0;  while( [pixel at i,j] is white ){    j++;  }  terrainheight = j;}

I assumed that 0,0 is the top left of the screen. It first starts scanning from top of the screen and move down until it finds a green spot. It could be better if you start searching from nearby pixels of terrainheight[i-1] but since this is a one time job it is not that important.

after that you can use this array to adjust y position

x++; //or x--y = terrainheight[x] - radiusofcircle;

I hope that helps

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013759
×