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

时间:2015-02-25 21:39:15

标签: python pygame sprite collision-detection

我正在研究RPG的骨架,使用Sprites进行碰撞检测。然而,在spawn上,pygame打印出碰撞时实际上没有碰撞:

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()

如果有人能够提供任何反馈或帮助,我们非常感谢!!

1 个答案:

答案 0 :(得分:0)

据我所知,碰撞的问题是因为" wall4"大小填充了大部分显示器,包括播放器。从0,0到480,480,作为玩家在380,380碰撞。

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

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

这对于玩家来说:

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

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

Walls OutPut:

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

球员OutPut:

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

现在要修理它,改变墙壁大小和坐标,准备就绪!

对不起,如果您需要更多帮助,我可以给您更多帮助,我不会真正接触经常知道更多的精灵。

相关问题