[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] My solid tile detection doesn't work



In my little RPG engine-like thing that no one tested, *wink wink*, I
tried implementing solid tiles. My algorithm doesn't work, and I don't
know why.

This is the code I use to place tiles onscreen, only the important
half of the function:
---code---
for y in range(starty, endy):
	for x in range(startx, endx):
		tilepos = y*(self.mapw) + x; #The position in our array of tiles
		showx = x - startx; showy = y - starty  #Adjust the position of map
by blitting it to the top
		truex = (showx*tilesize-xoff); truey = (showy*tilesize-yoff)
		#print endy
		tilenum = self.tiles[tilepos]
		self.put_tile(tilenum,(truex,truey), screen)
---code---

and this is the code I used to look for a solid tile:

---code---
player_x, player_y = player_pos
		p_tilex = player_x//tilesize; p_tiley = player_y//tilesize
		n_tilex = n_tiley = 0

		if direction == 'left':
			n_tilex = (player_x - speed)// tilesize
		if direction == 'right':	
			n_tilex = (player_x + speed)//tilesize
		if direction == 'up':
			n_tiley = (player_y - speed)//tilesize
		if direction == 'down':
			n_tiley = (player_y + speed)//tilesize

		if (n_tiley == p_tiley) and (n_tilex == p_tilex):
			return False

		if (p_tilex != n_tilex): # we are moving to a new tile horizontally
			n_tilepos = p_tiley*self.mapw + n_tilex	# the position in our array of tiles

		if (p_tiley != n_tiley): #we are moving to a new tile vertically
			n_tilepos = n_tiley*self.mapw + p_tilex	# the position in our array of tiles

		tilenum = self.tiles[n_tilepos]; print tilenum
		return (tilenum in self.solid)
---code---

I didn't want to put all that in... what I basically wanted to point
out was that I was getting the  tile number the exact same way I did
when I was drawing the map. Yet I usually get a completely different
tile to the one the player is walking into. Can anybody help me out
here?

Adeola