【发布时间】:2018-12-24 15:44:09
【问题描述】:
如果这不是一个非常笼统的问题,我很抱歉,但这是一个非常具体的问题,我只知道它应该有效,但它却没有。
为什么绿色玩家撞到黄色盒子的碰撞检测不起作用?
对于所有其他混乱情况,我深表歉意,但我找不到任何可删除的内容以使代码更小,同时仍保留核心游戏。现在它主要是准系统。 ' 导入 pygame, sys, 时间
pygame.init()
isRunning = True
windowWidth = 1280
windowHeight = 720
playerHeight = 64
playerWidth = 64
playerX = (windowWidth / 2) - (playerWidth / 2) + 100
playerY = (windowHeight / 2) - (playerHeight / 2)
playerXLate = playerX
playerYLate = playerY
playerSpeed = 4
blockHeight = 64
blockWidth = 64
blockX = 0
blockY = 0
number = 3
playerMovingForewards = False
playerMovingLeft = False
playerMovingBackwards = False
playerMovingRight = False
playerColor = (0, 255, 0)
blockColor = (255, 255, 0)
backgroundColor = (0, 0, 255)
window = pygame.display.set_mode((windowWidth, windowHeight))
while isRunning == True:
Map = open('Map', 'r')
mapY = Map.readlines()
mapX = mapY[0].split()
for y in range(len(mapY)):
mapX = mapY[y]
for x in range(len(mapX)):
if mapX[x] == "1":
if playerX + playerWidth >= (x * 64) and playerX <= (x * 64) + blockWidth and playerY + playerHeight >= (y * 64) and playerY <= (y * 64) + blockHeight:
playerX = playerXLate
playerY = playerYLate
pygame.draw.rect (window, (blockColor), ((x / 2) * 64, y * 64, blockWidth, blockHeight))
Map.close()
playerXLate = playerX
playerYLate = playerY
for event in pygame.event.get():
if (event.type == pygame.QUIT):
isRunning = False
pygame.quit()
sys.exit()
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_w):
playerMovingForewards = True
if (event.key == pygame.K_a):
playerMovingLeft = True
if (event.key == pygame.K_s):
playerMovingBackwards = True
if (event.key == pygame.K_d):
playerMovingRight = True
if(event.type == pygame.KEYUP):
if (event.key == pygame.K_w):
playerMovingForewards = False
if (event.key == pygame.K_a):
playerMovingLeft = False
if (event.key == pygame.K_s):
playerMovingBackwards = False
if (event.key == pygame.K_d):
playerMovingRight = False
if playerMovingForewards == True:
playerY = playerY + -playerSpeed
if playerMovingLeft == True:
playerX = playerX + -playerSpeed
if playerMovingBackwards == True:
playerY = playerY + playerSpeed
if playerMovingRight == True:
playerX = playerX + playerSpeed
#Graphics (Start)
pygame.draw.rect (window, (playerColor), (playerX, playerY, playerWidth, playerHeight))
pygame.display.update()
pygame.draw.rect (window, (0, 0, 0), (0, 0, windowWidth, windowHeight))
pygame.draw.rect (window, (backgroundColor), (windowWidth - windowWidth, windowHeight - windowHeight, windowWidth, windowHeight))
#Graphics (End)
time.sleep(0.01)
pygame.quit()
sys.exit()
“地图”文件是:
1 1 1 1 0 0 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 2 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 0 0 1 1 1 1
【问题讨论】:
标签: python python-2.7 for-loop pygame collision-detection