I'm trying to detect if the player has hit the block, and if so, stopping them of course (eg if it hits a wall on the left it can't move left). So it works fine, I've done the collision for up and down, but for some reason when I go on to do the collision for the left side (the last if statement) the player is able to just move through the block and I can't figure out why, because the if statement is basically the same.
FYI: pX is the x co-ordinate of the player, pY is the y co-ordinate of the player, XnY[1] is the y co-ordinate of the block, XnY[0] is the x co - ordinate of the block, playerSize is it's a square, so 50x50
level = ["W","W","W","W","W","W","W","W","W","W","W","W",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","P","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L",
"N","N","N","N","N","N","N","N","N","N","N","N","L","S"]
blockXY = []
for element in level:
if element == "N":
x += block_width
if element == "L":
y += block_height
x = 0
if element == "P":
drawBlock(block_width,block_height,x,y)
blockXY.append(x)
blockXY.append(y)
if appendBlockXY:
if len(collisionArray) > P_count:
del(collisionArray[P_count])
print(collisionArray)
appendBlockXY = False
collisionArray.append(blockXY)
blockXY = []
x += block_width
if element == "S":
y = 0
for XnY in collisionArray
if pX >= XnY[0] and pX <= XnY[0] + block_width or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:
if pY - block_height == XnY[1]:
canMoveUp = False
y_change = 0
if pX >= XnY[0] and pX <= XnY[0] + block_width or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:
if XnY[1] - playerSize == pY:
canMoveDown = False
y_change = 0
if pY >= XnY[1] and pY <= XnY[1] + block_height or pY + playerSize >= XnY[1] and pY + playerSize <= XnY[1] + block_height:
if pX - block_width == XnY[0]:
canMoveLeft = False
y_change = 0
I got it working! It was a silly mistake on my part, instead of writing y_change = 0 it should have been x_change = 0. Ooops!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.