Pygame迷宫-碰撞问题

时间:2019-04-23 10:14:09

标签: python python-3.x pygame collision

我是Python和Pygame的初学者,而我在pygame中的游戏有问题。我有Maze类和Player类,但无法进行适当的碰撞。我不知道为什么我的代码无法正常工作。

这是我的代码:

class Maze(pygame.sprite.Sprite):
    def __init__(self):
        self.W = 25
        self.H = 14
        self.maze = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
                     1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                     1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
                     1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
                     1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
                     1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]
    def draw(self,screen):
        block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
        bx = 0
        by = 0
        for i in range(0,self.W*self.H):
            if self.maze[ bx + (by*self.W) ] == 1:
                screen.blit(block_surf,( bx * 40 , by * 40))
            bx = bx + 1
            if bx > self.W-1:
                bx = 0 
                by = by + 1

    def collisions(self):
        bx = 0 
        by = 0 
        for i in range(0, self.W * self.H):
            if self.maze[ bx + (by * self.W) ] == 1:

                rect = pygame.Rect(bx * 40, by * 40, 40, 40)

                if rect.colliderect(Player.get_rect):
                    pass  # Collision!
                    return False

                bx = bx + 1
                if bx > self.W-1:
                    bx = 0 
                    by = by + 1
class Player(pygame.sprite.Sprite):
    def __init__(self): #sprite for a player
        pygame.sprite.Sprite.__init__(self) #for sprite working
        self.image = pygame.image.load(os.path.join(img_folder, "player.png")).convert() #look of the sprite
        self.image.set_colorkey(white) #to delete black things around rect img
        self.rect = self.image.get_rect() #kind of border around it
        self.rect.centerx = width/2 + 40
        self.rect.bottom = height- 120
        self.speedx = 0
        self.speedy = 0
    def update(self): #moving player
        maze = Maze()
        collisions = maze.collisions
        self.speedx = 0
        self.speedy = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            if collisions == False:
                self.speedx = 0
            else:
                self.speedx = -1
        elif keystate[pygame.K_RIGHT]:
            if self.rect.x > 920:
                self.speedx = 0
            # elif self.rect.y <= 636 and self.rect.y > 580: 
                # self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 482 and self.rect.y > 401:
                self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 399 and self.rect.y > 282:
                self.speedy = 0
            elif self.rect.x == 40 and self.rect.y < 279 and self.rect.y > 160:
                self.speedy = 0 
            elif self.rect.x == 40 and self.rect.y < 157 and self.rect.y > 41:
                self.speedy = 0
            else:
                self.speedx = 1
        elif keystate[pygame.K_UP]:
            if self.rect.y < 41:
                self.speedy = 0
            elif self.rect.y == 159 and self.rect.x > 46 and self.rect.x < 160:
                self.speedy = 0
            elif self.rect.y == 281 and self.rect.x > 45 and self.rect.x < 154:
                self.speedy = 0 
            else:
                self.speedy = -1
        elif keystate[pygame.K_DOWN]:
            if self.rect.y > 480:
                self.speedy = 0
            elif self.rect.x > 405 and self.rect.x < 634 and self.rect.y > 440:
                self.speedy = 0
            elif self.rect.y == 280 and self.rect.x > 46 and self.rect.x < 155:
                self.speedy = 0
            else:
                self.speedy = 1
        self.rect.x += self.speedx
        self.rect.y += self.speedy
    def get_rect(self):
        return pygame.Rect(self.rect.x, self.rect.y, 40, 40)

有人可以向我解释为什么它不起作用并帮助改进它吗?

我尝试了很多想法-甚至从手的位置x和y进行书写以产生碰撞。但这是一种非常糟糕的做法,因此我转向pygame选项sprite.colliderect(sprite2)

我想收到信息,为什么我的代码无法正常工作以及改进它的提示。

0 个答案:

没有答案