Pygame 在 Spawn 检测到碰撞,尽管没有现有的碰撞

Pygame detects collision at Spawn, despite no existing collisions

我正在研究 rpg 的骨架,使用 Sprites 进行碰撞检测。但是,在生成时,pygame 打印出存在碰撞,而实际上存在 none:

import pygame, sys
from pygame.locals import *
from pygame.draw import rect as square

winWidth = 800
winHeight = 800
black = (0, 0, 0)

everything = pygame.sprite.Group()

gameDisplay = pygame.display.set_mode((winWidth,winHeight))
pygame.display.set_caption('RPG Skeleton with Classes')


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y, w, h):
        pygame.sprite.Sprite.__init__(self) #super(Player, self).__init__()
        self.display = gameDisplay
        green = (0,255,0)

        self.xchange = 0
        self.ychange = 0

        self.x = x
        self.y = y
        self.w = w
        self.h = h

        self.x += self.xchange
        self.y += self.ychange

        self.color = green
        self.player = square(self.display, self.color, (self.x, self.y, self.w, self.h))
        self.add(Players)
        self.image = pygame.Surface([self.x, self.y])
        self.rect = self.image.get_rect()


    def draw(self):
        self.x += self.xchange
        self.y += self.ychange

        self.player = square(self.display, self.color, (self.x, self.y, self.w, self.h))
        #self.image = pygame.Surface([self.x, self.y])
        #self.rect = self.image.get_rect()

        #self.rect = self.get_rect()

        #self.rect = (self.x, self.y, self.w, self.h)

        #if self.rect.colliderect(walls.rect):
        #   print "Collision"

class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y, w, h):
        pygame.sprite.Sprite.__init__(self) #super(Wall, self).__init__()
        self.display = gameDisplay
        grey = (119, 136, 153)

        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.add(Walls)

        self.color = grey
        self.player = square(self.display, self.color, (self.x, self.y, self.w, self.h))
        #self.rect = (self.x, self.y, self.w, self.h)

        self.image = pygame.Surface([self.x, self.y])
        self.rect = self.image.get_rect()

    def draw(self):
        self.wall = square(self.display, self.color, (self.x, self.y, self.w, self.h))
        #self.image = pygame.Surface([self.x, self.y])
        #self.rect = self.image.get_rect()
        #self.rect = (self.x, self.y, self.w, self.h)

wallWidth = winWidth * .4
wallHeight = winWidth * .4
Players = pygame.sprite.Group()
Walls = pygame.sprite.Group()

link = Player(winWidth / 2 - 50, winHeight / 2 - 50, 50, 50)
wall1 = Wall(0, 0, wallWidth, wallHeight)
wall2 = Wall(winWidth - wallWidth, 0, wallWidth, wallHeight)
wall3 = Wall(0,winHeight - wallHeight, wallWidth, wallHeight)
wall4 = Wall(winWidth - wallWidth, winHeight - wallHeight, wallWidth, wallHeight)

pygame.init()


def main():




    pygame.display.update()

    run = True
    while run:


        gameDisplay.fill(black)

        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_k:
                    pygame.quit()
                    sys.exit()
                if event.key == K_UP or event.key == K_w:
                    link.ychange = -5
                if event.key == K_DOWN or event.key == K_s:
                    link.ychange = 5
                if event.key == K_LEFT or event.key == K_a:
                    link.xchange = -5
                if event.key == K_RIGHT or event.key == K_d:
                    link.xchange = 5


            if event.type == KEYUP:
                if event.key == K_UP or event.key == K_w:
                    link.ychange = 0
                if event.key == K_DOWN or event.key == K_s:
                    link.ychange = 0
                if event.key == K_LEFT or event.key == K_a:
                    link.xchange = 0
                if event.key == K_RIGHT or event.key == K_d:
                    link.xchange = 0




#       link.draw()
        # wall1.draw()
        # wall2.draw()
        # wall3.draw()
        # wall4.draw()

        hitWalls = pygame.sprite.groupcollide(Players, Walls, False, False)

        for collision in hitWalls:
            print "collision"

        pygame.display.update()

main()

如果有人能够提供任何反馈或帮助,我们将不胜感激!!

据我所知,碰撞问题是因为 "wall4" 尺寸填满了大部分显示器,包括播放器。从 0,0 到 480,480,当玩家在 380,380 时发生碰撞。

您可以通过简单地打印出精灵的详细信息来找到它。使用这个简单的代码:

for wall in Walls.sprites():
    print(wall.rect)

这是给玩家的:

for player in Players.sprites():
    print(player.rect)

在那里你可以看到4墙占据了大部分的显示和下一个玩家的位置。

墙输出:

 <rect(0, 0, 0, 480)>
 <rect(0, 0, 0, 0)>
 <rect(0, 0, 480, 0)>
 <rect(0, 0, 480, 480)>

玩家输出:

 <rect(0, 0, 350, 350)>

现在修复它,更改墙壁的大小和坐标,准备就绪!

对不起,如果你需要更多,我可以给你更多的帮助,我真的不经常接触精灵来了解更多。