Pygame碰撞检测错误

时间:2014-02-07 18:53:03

标签: python pygame

import pygame, random, time

class Game(object):
    def main(self, screen):
        bg = pygame.image.load('data/bg.png')
        house1 = pygame.image.load('data/house1.png')

        playerIdle = pygame.image.load('data/playerIdle.png')
        playerRight = pygame.image.load('data/playerRight.png')
        playerLeft = pygame.image.load('data/playerLeft.png')
        playerUp = pygame.image.load('data/playerUp.png')
        playerX = 0
        playerY = 50

        clock = pygame.time.Clock()
        spriteList = []
        gameIsRunning = False

        house1Selected = False


        while 1:
            clock.tick(30)

            mouse = pygame.mouse.get_pos()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return
        #delete all sprites on the game(only able to do this in god mode)
        if event.type == pygame.KEYDOWN and event.key == pygame.K_d and gameIsRunning == False:
            spriteList = []
        #press 6 to select the house1 image to be placed
        if event.type == pygame.KEYDOWN and event.key == pygame.K_6 and gameIsRunning == False:
            house1Selected = True
        #spawning the image"house1" at the position of the mouse by pressing space
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and gameIsRunning == False and house1Selected == True:
            spriteList.append((house1, mouse))
            house1XY = mouse

        #run mode where you cannot build and you move(where I want collision)
        if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
            gameIsRunning = True
        #god mode where you can build and place the house1 image
        if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
            gameIsRunning = False

        #this is run mode where you can move around and where I want collision
        if(gameIsRunning == True):
            #player Movements
            if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
                if playerY <= 0:
                    playerY = 0
                if playerY >= 0:
                    screen.blit(playerUp, (playerX, playerY))
                    playerY = playerY - 2
            if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
                PLAYERDOWNLIMIT = 700 - playerIdle.get_height()
                if(playerY >= PLAYERDOWNLIMIT):
                    playerY = PLAYERDOWNLIMIT
                if(playerY <= PLAYERDOWNLIMIT):
                    screen.blit(playerIdle, (playerX, playerY))
                    playerY = playerY + 2
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                if playerX <= 0:
                    playerX = 0
                if playerX >= 0:
                    screen.blit(playerLeft, (playerX, playerY))
                    playerX = playerX - 2
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                PLAYERRIGHTLIMIT = 1200 - playerIdle.get_width()
                if(playerX >= PLAYERRIGHTLIMIT):
                    playerX = PLAYERRIGHTLIMIT
                if(playerX <= PLAYERRIGHTLIMIT):
                    screen.blit(playerRight, (playerX, playerY))
                    playerX = playerX + 2

            #collision

            house1Rect = (house1XY, 64, 64)
            playerRect = (playerX, playerY, 32, 32)
            #print(house1Rect)
            #print(playerRect)
            if playerRect.colliderect(house1Rect):
                print("collision")

        #this is godmode where you can move around fast n preview where you place images but I don't want collision here
        if(gameIsRunning == False):
            if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
                if playerY <= 0:
                    playerY = 0
                if playerY >= 0:
                    screen.blit(playerUp, (playerX, playerY))
                    playerY = playerY - 8
            if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
                PLAYERDOWNLIMIT = 700 - playerIdle.get_height()
                if(playerY >= PLAYERDOWNLIMIT):
                    playerY = PLAYERDOWNLIMIT
                if(playerY <= PLAYERDOWNLIMIT):
                    screen.blit(playerIdle, (playerX, playerY))
                    playerY = playerY + 8
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                if playerX <= 0:
                    playerX = 0
                if playerX >= 0:
                    screen.blit(playerLeft, (playerX, playerY))
                    playerX = playerX - 8
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                PLAYERRIGHTLIMIT = 1200 - playerIdle.get_width()
                if(playerX >= PLAYERRIGHTLIMIT):
                    playerX = PLAYERRIGHTLIMIT
                if(playerX <= PLAYERRIGHTLIMIT):
                    screen.blit(playerRight, (playerX, playerY))
                    playerX = playerX + 8

        screen.fill((200, 200, 200))
        screen.blit(bg, (0, 0))
        #what block are you placing(displays preview)
        if(gameIsRunning == False and house1Selected == True):
            screen.blit(house1, (mouse))

        if(gameIsRunning == True):
            playerXSTR = str(playerX)
            playerYSTR = str(playerY)
            playerXYSTR = ("(" + playerXSTR + ", " + playerYSTR + ")")
            font = pygame.font.SysFont("monospace", 15)
            playercord = font.render(playerXYSTR, 1, (255,255,255))
            screen.blit(playercord, (0, 0))
            runMode = font.render("Run Mode(Cannot build)", 1, (255,255,255))
            screen.blit(runMode, (0, 20))
        if(gameIsRunning == False):
            playerXSTR = str(playerX)
            playerYSTR = str(playerY)
            playerXYSTR = ("(" + playerXSTR + ", " + playerYSTR + ")")
            font = pygame.font.SysFont("monospace", 15)
            playercord = font.render(playerXYSTR, 1, (255,255,255))
            screen.blit(playercord, (0, 0))
            godMode = font.render("God Mode(Can build)", 1, (255,255,255))
            screen.blit(godMode, (0, 20))

        for (img, pos) in spriteList:
            screen.blit(img, pos)
        screen.blit(playerIdle, (playerX, playerY))

        pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    infoObject = pygame.display.Info()
    #screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
    screen = pygame.display.set_mode((1200, 700))
    pygame.display.set_caption('Sandbox')
    Game().main(screen)

我正在尝试在玩家和house1图像之间添加碰撞。在线330,当用户按下空间以在鼠标的位置处产生图像(house1)时,它设置变量house1XY。在第393行是我碰撞的地方。我设置house1Rect =(house1XY,64,64)和playerRect =(playerX,playerY,32,32)然后如果playerRect.colliderect(house1Rect):print(“collision”),则执行if语句,但是我收到错误:< / p>

Traceback (most recent call last):
  File "C:\Users\Dusty\Desktop\Dropbox\OR Software\Projects\Sandbox Project\game.py", line 512, in <module>
    Game().main(screen)
  File "C:\Users\Dusty\Desktop\Dropbox\OR Software\Projects\Sandbox Project\game.py", line 397, in main
    if playerRect.colliderect(house1Rect):
AttributeError: 'tuple' object has no attribute 'colliderect'  

我真的开始对这些碰撞事物感到困惑,所以如果有人能告诉我如何解决这个问题,那就太棒了!

1 个答案:

答案 0 :(得分:3)

playerRect是一个元组,如下所示:

playerRect = (playerX, playerY, 32, 32)

您需要使用以下代码将其设为pygame.rect

playerRect = pygame.Rect(playerX, playerY, 32, 32)

然后您可以使用colliderect方法


编辑:对house1Rect执行相同操作:

house1Rect = pygame.Rect(house1XY[0], house1XY[1], 64, 64)