Pygame墙精灵碰撞

问题描述 投票:1回答:1

我正在制作类似rogue的游戏,但是在编码方面,我还是新手。我已经移动了角色,墙壁和地板精灵,但是我的代码中存在一些错误,该错误允许角色在墙壁中移动。

[我用block_path在地板砖和墙砖之间进行选择,然后我尝试使用它来识别墙,但它实际上没有用。

接下来,您可以看到我的代码:

screenWidth = 800
screenHeight = 600
mapHeight = 30
mapWidth = 30
cellWidth = 32
cellHeight = 32

screen = pygame.display.set_mode((screenWidth, screenHeight))
walkRight = [pygame.image.load('model/r1.png'), pygame.image.load('model/r2.png'), pygame.image.load('model/r3.png'), pygame.image.load('model/r4.png'), pygame.image.load('model/r5.png'), pygame.image.load('model/r6.png')]
walkLeft = [pygame.image.load('model/l1.png'), pygame.image.load('model/l2.png'), pygame.image.load('model/l3.png'), pygame.image.load('model/l4.png'), pygame.image.load('model/l5.png'), pygame.image.load('model/l6.png')]
walkUp = [pygame.image.load('model/u1.png'), pygame.image.load('model/u2.png'), pygame.image.load('model/u3.png'), pygame.image.load('model/u4.png'), pygame.image.load('model/u5.png'), pygame.image.load('model/u6.png')]
Floor = pygame.image.load("map/floor.jpg")
wallRight = pygame.image.load("map/rightwall.png")

`

class struc_Tile():
    def __init__(self,block_path):
        self.block_path = block_path`

class player(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.walkCount = 0


    def draw(self,screen):
        if self.walkCount + 1 >= 18:
            self.walkCount = 0

        elif self.left:
            screen.blit(walkLeft[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
        elif self.right:
            screen.blit(walkRight[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
        elif self.up:
            screen.blit(walkUp[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
        elif self.down:
            screen.blit(walkDown[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
        else:
            screen.blit(Standing[self.walkCount//3], (self.x,self.y))
            self.walkCount = 0

    def move(self,dx,dy):
        if gamemap[self.x + dx][self.y + dy].block_path == False:
            self.x += dx
            self.y += dy

def createmap():
    newmap = [[struc_Tile(False) for y in range(0,mapHeight)] for x in range (0,mapWidth) ]
    newmap[10][10].block_path = True
    newmap[10][15].block_path = True

    return newmap
        def drawmap(maptodraw):

    for x in range(0,mapWidth):
        for y in range(0,mapHeight):
            if maptodraw[x][y].block_path == True:
                screen.blit(wallRight, (x*cellWidth, y*cellHeight))
            else:
                screen.blit(Floor, (x*cellWidth, y*cellHeight)
def redrawgamewindow():
screen.blit(bg, (0, 0))
drawmap(gamemap)
character.draw(screen)
pygame.display.update()

pygame.init()
gamemap = createmap()
clock = pygame.time.Clock()
character = player(0, 0, 32,32)

run = True
while run:
clock.tick(18)

for event in pygame.event.get():
    if event.type == pygame.QUIT:

        run = False

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and character.x > character.vel:
    character.x -= character.vel
    character.left = True
    character.right = False
    character.up = False
    character.down = False
    character.standing = False

elif keys[pygame.K_RIGHT] and character.x < 800 -character.width - character.vel:
    character.x += character.vel
    character.left = False
    character.right = True
    character.up = False
    character.down = False
    character.standing = False

elif keys[pygame.K_UP] and character.y > character.vel:
    character.y -= character.vel
    character.left = False
    character.right = False
    character.up = True
    character.down = False
    character.standing = False
elif keys[pygame.K_DOWN] and character.y < 600 - character.height - character.vel:
    character.y += character.vel
    character.left = False
    character.right = False
    character.up = False
    character.down = True
    character.standing = False
else:
    character.right = False
    character.left = False
    character.up = False
    character.down = False
    character.standing = True

redrawgamewindow()
python python-3.x pygame collision roguelike
1个回答
1
投票

createmap()函数更改为类似内容将在地图的钻孔器上创建5像素的缓冲区。我将其设置为5像素的原因是因为这就是您的角色移动。

© www.soinside.com 2019 - 2024. All rights reserved.