我正在使用 Python 和 Pygame 制作吃豆人的副本,但在尝试删除药丸中的点时遇到问题,我不知道该怎么做,我尝试使用“del”并不起作用,我还尝试将点添加到列表中,然后创建一个 for 循环,在每个点中迭代并在玩家与其中一个点发生碰撞时将其删除,但这会使游戏变得滞后,我该如何删除屏幕上有药丸吗?
from pygame.locals import *
pygame.init()
#Variables del juego
windowWidth = 504
windowHeight = 648
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
mainClock = pygame.time.Clock()
FPS = 30
pygame.display.set_caption('Pac-Man')
#Colores
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
yellow = (255, 255, 0)
cyan = (0, 255, 255)
navyBlue = (0, 0, 53)
pink = (255, 192, 203)
#Tamaño del bloque
blockSize = 24
dotSize = 4
pillSize = 8
#Datos del jugador
playerSize = 24
player = pygame.Rect(240, 480, playerSize, playerSize)
speed = 5
#Variables de movimiento
moveLeft = moveRight = moveDown = moveUp = False
#Laberinto
maze = [
"xxxxxxxxxxxxxxxxxxxxx",
"x.........x.........x",
"x.xxx.xxx.x.xxx.xxx.x",
"x-xxx.xxx.x.xxx.xxx-x",
"x.xxx.xxx.x.xxx.xxx.x",
"x...................x",
"x.xxx.x.xxxxx.x.xxx.x",
"x.xxx.x.xxxxx.x.xxx.x",
"x.....x...x...x.....x",
"xxxxx.xxxoxoxxx.xxxxx",
"oooox.xooooooox.xoooo",
"oooox.xoxxoxxox.xoooo",
"xxxxx.xoxoooxox.xxxxx",
"ooooo.ooxoooxoo.ooooo",
"xxxxx.xoxxxxxox.xxxxx",
"oooox.xooooooox.xoooo",
"oooox.xoxxxxxox.xoooo",
"xxxxx.xoxxxxxox.xxxxx",
"x.........x.........x",
"x.xxx.xxx.x.xxx.xxx.x",
"x-..x.....o.....x..-x",
"xxx.x.x.xxxxx.x.x.xxx",
"xxx.x.x.xxxxx.x.x.xxx",
"x.....x...x...x.....x",
"x.xxxxxxx.x.xxxxxxx.x",
"x...................x",
"xxxxxxxxxxxxxxxxxxxxx"
]
def createMaze():
pointX = 0
pointY = 0
for i in maze:
for j in i:
if j == "x":
block = pygame.Rect(pointX, pointY, blockSize, blockSize)
pygame.draw.rect(windowSurface, navyBlue, block)
if block.colliderect(player):
if player.bottom - block.top < 10:
player.y = block.top - playerSize
if block.bottom - player.top < 10:
player.y = block.bottom
if block.right - player.left < 10:
player.x = block.right
if player.right - block.left < 10:
player.x = block.left - playerSize
if j == ".":
dot = pygame.Rect(pointX + (blockSize / 2) - (dotSize / 2), pointY + (blockSize / 2) - (dotSize / 2), dotSize, dotSize)
pygame.draw.rect(windowSurface, white, dot)
if j == "-":
pygame.draw.circle(windowSurface, white, (pointX + pillSize + (pillSize / 2), pointY + pillSize + (pillSize / 2)), pillSize)
if j == "o":
pass
pointX += blockSize
pointX = 0
pointY += blockSize
#Función para cerrar juego
def close():
pygame.quit()
sys.exit()
#Ciclo principal del juego
while True:
#Buscar evento QUIT
for event in pygame.event.get():
if event.type == QUIT:
close()
if event.type == KEYDOWN:
if event.key == K_w:
moveUp = True
moveDown = False
moveLeft = False
moveRight = False
if event.key == K_s:
moveUp = False
moveDown = True
moveLeft = False
moveRight = False
if event.key == K_a:
moveUp = False
moveDown = False
moveLeft = True
moveRight = False
if event.key == K_d:
moveUp = False
moveDown = False
moveLeft = False
moveRight = True
#Mover jugador
if moveLeft:
player.move_ip(-1 * speed, 0)
if moveRight:
player.move_ip(speed, 0)
if moveUp:
player.move_ip(0, -1 * speed)
if moveDown:
player.move_ip(0, speed)
#Portal
if player.left > windowWidth:
player.x = 0 - playerSize
if player.right < 0:
player.x = windowWidth
windowSurface.fill(black)
createMaze()
pygame.draw.rect(windowSurface, yellow, player)
pygame.display.update()
mainClock.tick(FPS)```
我改变了“。”与“o”(代表空空格)创建一个新字符串
if dot.colliderect(player):
maze[y] = maze[y][:x] + "o" + maze[y][x + 1:]