如何感知墙和玩家之间的碰撞:Pygame

How to sense collisions between wall and player: Pygame

我有一个代码,我需要感知玩家和墙壁之间的碰撞。我看过其他答案和主题,但无法理解它们:

代码如下:

    import pygame

    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    BLUE = (0, 0, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)
    PURPLE = (255, 0, 255)

    class Wall(pygame.sprite.Sprite):
            def __init__(self, x, y, width, height, color):

                    pygame.sprite.Sprite.__init__(self)

                    self.image = pygame.Surface([width, height])
                    self.image.fill(color)

                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

    class Player(pygame.sprite.Sprite):
            speedx = 0
            speedy = 0
            def __init__(self, x, y):

                    pygame.sprite.Sprite.__init__(self)
                    self.image = pygame.Surface([15, 15])
                    self.image.fill(PURPLE)

                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

            def changespeed(self, x, y):

                    self.speedx += x
                    self.speedy += y

            def move(self, walls):
                    self.rect.x += self.speedx

                    block_hit_list = pygame.sprite.spritecollide(self, walls, False)
                    for block in block_hit_list:

                            if self.speedx > 0:
                                    self.rect.right = block.rect.left
                            else:
                                    self.rect.left = block.rect.right

                    self.rect.y += self.speedy

                    block_hit_list = pygame.sprite.spritecollide(self, walls, False)
                    for block in block_hit_list:
                            if self.speedy > 0:
                                    self.rect.bottom = block.rect.top
                            else:
                                    self.rect.top = block.rect.bottom

                    play_col = (self.rect.left, self.rect.right, 10, 10)
                    wall = (10, 200, 10, 10)

                    if ship1.colliderect(ship2):
                            print "ajasj"

    class StartScreen():
            def __init__(self, background, startButton, sbx, sby, sbw, sbh):
                    self.background = background
                    self.startButton = startButton
                    self.sbx = sbx
                    self.sby = sby
                    self.sbw = sbw
                    self.sbh = sbh
                    self.active = True
                    self.b = None
            def drawScreen(self, screen):
                    print "a"
                    print self.background
                    screen.blit(self.background, (0, 0))
                    self.b = screen.blit(self.startButton, (self.sbx, self.sby))
            def onClick(self, event):
                    pos = pygame.mouse.get_pos()
                    if self.b.collidepoint(pos):
                            self.active = False

    class EndScreen():
            def __init__(self, background):
                    self.background = background
                    self.active = True
            def drawBG(self, screen):
                    screen.blit(self.background, (0,0))
    class Room(object):

            wall_list = None
            enemy_sprites = None

            def __init__(self):
                    self.wall_list = pygame.sprite.Group()
                    self.enemy_sprites = pygame.sprite.Group()

    class Room1(Room):
            def __init__(self):
                    Room.__init__(self)

                    walls = [[0, 0, 20, 250, PURPLE],
                                     [0, 350, 20, 250, PURPLE],
                                     [780, 0, 20, 250, PURPLE],
                                     [780, 350, 20, 250, PURPLE],
                                     [20, 0, 760, 20, PURPLE],
                                     [20, 580, 760, 20, PURPLE],
                                     [190, 50, 20, 500, PURPLE],
                                     [590, 50, 20, 500, PURPLE]
                    ]

                    for item in walls:
                            wall = Wall(item[0], item[1], item[2], item[3], item[4])
                            self.wall_list.add(wall)

    class Room2(Room):
            def __init__(self):
                    Room.__init__(self)

                    walls = [[0, 0, 20, 250, PURPLE],
                                     [0, 350, 20, 250, PURPLE],
                                     [780, 0, 20, 250, PURPLE],
                                     [780, 350, 20, 250, PURPLE],
                                     [20, 0, 760, 20, PURPLE],
                                     [20, 580, 760, 20, PURPLE]
                    ]

                    for item in walls:
                            wall = Wall(item[0], item[1], item[2], item[3], item[4])
                            self.wall_list.add(wall)
                    for x in range(100, 800, 100):
                            wall = Wall(x, 100, 20, 400, PURPLE)
                            self.wall_list.add(wall)


    class Room3(Room):
            def __init__(self):
                    Room.__init__(self)

                    walls = [[0, 0, 20, 250, PURPLE],
                                     [0, 350, 20, 250, PURPLE],
                                     [780, 0, 20, 250, PURPLE],
                                     [780, 350, 20, 250, PURPLE],
                                     [20, 0, 760, 20, PURPLE],
                                     [20, 580, 760, 20, PURPLE]
                    ]

                    for item in walls:
                            wall = Wall(item[0], item[1], item[2], item[3], item[4])
                            self.wall_list.add(wall)

                    for x in range(100, 800, 100):
                            for y in range(50, 451, 300):
                                    wall = Wall(x, y, 20, 200, PURPLE)
                                    self.wall_list.add(wall)

                    for x in range(150, 700, 100):
                            wall = Wall(x, 200, 20, 200, PURPLE)
                            self.wall_list.add(wall)

    def main():
            pygame.init()

            screen = pygame.display.set_mode([800, 600])

            pygame.display.set_caption("Wonder Maze")

            player = Player(50, 50)
            movingsprites = pygame.sprite.Group()
            movingsprites.add(player)

            rooms = []

            room = Room1()
            rooms.append(room)
            room = Room2()
            rooms.append(room)
            room = Room3()
            rooms.append(room)
            screen.blit(pygame.image.load("bg.png"), (0,0))
            startScreen = StartScreen(pygame.image.load("bg.png"), pygame.image.load("startbutton.png"), 240, 190, 320, 60)

            current_room_no = 0
            current_room = rooms[current_room_no]

            clock = pygame.time.Clock()

            ingame = True

            while ingame:
                    while startScreen.active:
                            for event in pygame.event.get():
                                    if event.type == pygame.QUIT:
                                            startScreen.active = False
                                            ingame = False
                                            pygame.quit()
                                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                                            startScreen.onClick(event)
                            startScreen.drawScreen(screen)
                            pygame.display.flip()

                    for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                    done = True
                            if event.type == pygame.KEYDOWN:
                                    if event.key == pygame.K_LEFT:
                                            player.changespeed(-5, 0)
                                    if event.key == pygame.K_RIGHT:
                                            player.changespeed(5, 0)
                                    if event.key == pygame.K_UP:
                                            player.changespeed(0, -5)
                                    if event.key == pygame.K_DOWN:
                                            player.changespeed(0, 5)

                            if event.type == pygame.KEYUP:
                                    if event.key == pygame.K_LEFT:
                                            player.changespeed(5, 0)
                                    if event.key == pygame.K_RIGHT:
                                            player.changespeed(-5, 0)
                                    if event.key == pygame.K_UP:
                                            player.changespeed(0, 5)
                                    if event.key == pygame.K_DOWN:
                                            player.changespeed(0, -5)

                    player.move(current_room.wall_list)

                    if player.rect.x < -15:
                            if current_room_no == 0:
                                    current_room_no = 2
                                    current_room = rooms[current_room_no]
                                    player.rect.x = 790
                            elif current_room_no == 2:
                                    current_room_no = 1
                                    current_room = rooms[current_room_no]
                                    player.rect.x = 790
                            else:
                                    current_room_no = 0
                                    current_room = rooms[current_room_no]
                                    player.rect.x = 790

                    if player.rect.x > 801:
                            if current_room_no == 0:
                                    current_room_no = 1
                                    current_room = rooms[current_room_no]
                                    player.rect.x = 0
                            elif current_room_no == 1:
                                    current_room_no = 2
                                    current_room = rooms[current_room_no]
                                    player.rect.x = 0
                            else:
                                    ingame = False

                    print current_room, current_room_no

                    # --- Drawing ---
                    screen.fill(WHITE)

                    movingsprites.draw(screen)
                    current_room.wall_list.draw(screen)

                    pygame.display.flip()

                    clock.tick(60)

            endScreen = EndScreen(pygame.image.load("wp.png"))
            while endScreen.active:
                    endScreen.drawBG(screen)
                    pygame.display.flip()
                    clock.tick(60)
                    for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                    endScreen.active = False
                                    pygame.quit()
            pygame.quit()

    if __name__ == "__main__":
            main()

我不知道该怎么做 如果这是重复的,我深表歉意。 非常感谢任何和所有的帮助,感谢所有做出贡献的人。

在 'Player' class 的 'move' 方法中检测到冲突。

这是通过调用 'spritecollide' 方法完成的。第一个参数是 'self',代表一个玩家。第二个参数是代表墙壁的所有精灵的列表。第三个参数是一个布尔值,告诉不要移除墙精灵(否则当玩家与墙碰撞时墙会消失)。它 returns 与玩家相交的方块列表(用于表示墙的精灵)。

您可以查看http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite

中的文档