Python Pygame 2D怪异数组

2024-04-20 06:03:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pygame里做了一个程序,你在迷宫里行走。迷宫是一个二维数组,包含
真的:墙在那里 或
错误:墙不在那里
很好用。但当我走到第8;9号位置时,我现在可以直接穿过一堵墙了。它只发生在这个y位置。当我检查right变量时,它是False。你知道吗

enter image description here

import pygame
from pygame.locals import *
from pygame import gfxdraw
white = (255, 255, 255)
width, height = 220, 220
screen = pygame.display.set_mode((width, height))
psurf = pygame.Surface((20, 20))
psurf.fill((0, 0, 0))
wallsurf = pygame.Surface((20, 20))
wallsurf.fill((255, 0, 0))

t = True
f = False

walls = [[f, t, f, f, f, f, f, f, f, f, f],
         [f, t, f, f, f, f, f, f, f, f, f],
         [f, t, f, f, t, t, t, t, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, f],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, t, f, t],
         [f, t, f, f, t, f, f, f, f, f, t],
         [f, t, t, f, t, t, t, t, t, t, t],
         [f, f, f, f, f, f, f, f, f, t, t]]

px = 0
py = 0
move_ticker = 0
right = True
left = True
delay = 500
maze = pygame.Surface((220, 220))
maze.fill(white)
placex = 0
placey = 0
for row in walls:
        placex = 0
        for wall in row:
            if wall == True:
               maze.blit(wallsurf, (placex * 20, placey * 20))
            placex += 1
        placey += 1
while True:
    pygame.event.get()
    screen.fill(white)
    screen.blit(maze, (0, 0))
    screen.blit(psurf, (px, py))
    keys=pygame.key.get_pressed()
    if keys[K_LEFT]:
        if left:
           if move_ticker == 0:
               move_ticker = delay
               px -= 20
               if px < -1:
                   px = 0
    if keys[K_RIGHT]:
        if right:
           if move_ticker == 0:   
               move_ticker = delay
               px += 20
               if px > 200:
                   px = 200
    if keys[K_DOWN]:
        if down:
            if move_ticker == 0:   
                move_ticker = delay   
                py += 20
                if py >= 200:
                    py = 200
    if keys[K_UP]:
        if up:
            if move_ticker == 0:   
                move_ticker = delay 
                py -= 20
                if py < 0:
                    py = 0
    if move_ticker > 0:
       move_ticker -= 1
    truex = int(px / 20)
    truey = int(py / 20)
    right = True
    left = True
    up = True
    down = True
    try:
        if walls[truey-1][truex]:
            up = False
        if walls[truey+1][truex]:
            down = False
        if walls[truey][truex-1]:
            left = False
        if walls[truey][truex+1]:
            right = False
    except IndexError:
        pass       
    pygame.display.flip()

Tags: pyrightfalsetruemoveifkeysscreen
1条回答
网友
1楼 · 发布于 2024-04-20 06:03:08

问题是你有一个try语句而不是4个。当到达最低行时walls[truey+1][truex]将返回一个索引错误。Right仍然设置为True,因此从不更新为False。一个快速的解决方法是:

try:
    if walls[truey-1][truex]:
        up = False
except IndexError:
        up = False
try:
    if walls[truey+1][truex]:
        down = False
except IndexError:
        down = False
try:
    if walls[truey][truex-1]:
        left = False
except IndexError:
        left = False    
try:
    if walls[truey][truex+1]:
        right = False
except IndexError:
        right = False

我认为一个更漂亮的方法是使用一个函数,一个函数是一段代码,你可以重复使用多次,这样你就不必一遍又一遍地重写同样的东西。你知道吗

def can_move(pos_x, pos_y, dx, dy, walls):
    move = True
    try:
        if walls[pos_y + dy][pos_x + dx]:
            move = False
    except IndexError:
        move = False
    return move

您可以将其缩短为:

def can_move(pos_x, pos_y, dx, dy, walls):
    try:
        move = not(walls[pos_y + dy][pos_x + dx])
    except IndexError:
        move = False
    return move

使用此功能,您可以定义您的移动:

right = can_move(truex, truey, 1, 0, walls)
left = can_move(truex, truey, -1, 0, walls)
up = can_move(truex, truey, 0, -1, walls)
down = can_move(truex, truey, 0, 1, walls)

相关问题 更多 >