TypeError:Rect参数无效,但语法看起来很好

时间:2018-03-07 10:37:40

标签: pygame python-3.2

我有一个绘制健康栏的功能,使用全局变量设置绿色矩形的宽度以覆盖预设的红色矩形。当我改变我的代码以便能够让怪物实际损坏墙壁时,rect参数停止工作。

我已经尝试了许多不同的方法来使它工作,但没有一个工作。

这是我的代码:

import pygame
import random

pygame.init()

displayWidth = 500
displayHeight = 600
currentWeapon = 'Bow'
projectileLength = 64
projectileHeight = 10
wallHealth = 100
coinCount = 0
currentWave = 0

black = (0, 0, 0)
white = (255, 255, 255)
darkGrey = (100, 100, 100)
orange = (200, 100, 0)
brightOrange = (250, 125, 0)
green = (0, 200, 0)
brightGreen = (0, 255, 0)
red = (200, 0, 0)

gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Project')

clock = pygame.time.Clock()

wallImg = pygame.image.load('buildings/wall.png')
blacksmithImg = pygame.image.load('buildings/blacksmith.png')
bookshelfImg = pygame.image.load('buildings/bookshelf_and_cauldron.png')

bowImg = pygame.image.load('weapons/bow.png')
arrowImg = pygame.image.load('weapons/arrow.png')
bowSelectedImg = pygame.image.load('weapons/weapon_select_bow.png')
magicSelectedImg = pygame.image.load('weapons/weapon_select_magic.png')
magicBoltImg = pygame.image.load('weapons/magic_bolt.png')

elementalFireImg = pygame.image.load('monsters/misc/elementalFire.png')
elementalWaterImg = pygame.image.load('monsters/misc/elementalWater.png')
lizardImg = pygame.image.load('monsters/misc/lizard.png')
medusaImg = pygame.image.load('monsters/misc/medusa.png')
slimeImg = pygame.image.load('monsters/misc/slime.png')
spiderImg = pygame.image.load('monsters/misc/spider.png')

coinImg = pygame.image.load('coin.png')

def background():
    gameDisplay.fill(green)
    gameDisplay.blit(wallImg, (0, 0))
    pygame.draw.rect(gameDisplay, black, (0, (displayHeight/2)-1, displayWidth, 2))
    button('(Price)', 225, 550, 50, 30, orange, brightOrange, None)
    button('(Price)', 80, 550, 50, 30, orange, brightOrange, None)
    button('(Price)', 380, 550, 50, 30, orange, brightOrange, None)

def blacksmith():
    gameDisplay.blit(blacksmithImg, (125,330))
    button('(Price)', 150, 430, 50, 30, orange, brightOrange, None)

def magicBuilding():
    gameDisplay.blit(bookshelfImg, (275, 330))
    button('(Price)', 300, 430, 50, 30, orange, brightOrange, None)

def coins():
    global coinCount
    gameDisplay.blit(coinImg, (displayWidth-100, 25))

def wallHealth(health):
    global wallHealth
    pygame.draw.rect(gameDisplay, red, (displayWidth - 110, 5, 100, 15))
    pygame.draw.rect(gameDisplay, brightGreen, (displayWidth - 110, 5, wallHealth, 15))

def text_objects(text, font, colour):
    textSurface = font.render(text, True, colour)
    return textSurface, textSurface.get_rect()

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()

    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    smallText = pygame.font.Font('freesansbold.ttf', 10)
    textSurf, textRect = text_objects(msg, smallText, black)
    textRect.center = ((x+(w/2)), (y+(h/2)))
    gameDisplay.blit(textSurf, textRect)

def weapon():
    global currentWeapon
    mouse = pygame.mouse.get_pos()

    if currentWeapon == 'Bow':
        if mouse[1] < (displayHeight/2)-20:
            gameDisplay.blit(bowImg, (0, mouse[1]-20))
        elif mouse[1] > (displayHeight/2)-20:
            gameDisplay.blit(bowImg, (0, (displayHeight/2)-40))
        gameDisplay.blit(bowSelectedImg, (50, 0))
    if currentWeapon == 'Magic':
        if mouse[1] < (displayHeight/2)-20:
            gameDisplay.blit(magicBoltImg, (0, mouse[1]-20))
        elif mouse[1] > (displayHeight/2)-20:
            gameDisplay.blit(magicBoltImg, (0, (displayHeight/2)-40))
        gameDisplay.blit(magicSelectedImg, (50, 0))

def projectile1(x, y):
    gameDisplay.blit(arrowImg, (x, y))

def monster(x, y):
    gameDisplay.blit(lizardImg, (x, y))

def game_loop():
    global currentWeapon
    global projectileLength
    global projectileHeight
    global wallHealth

    projectile1X = 0
    projectile1Xchange = 10
    projectile1Fired = False

    monsterX = displayWidth
    monsterXchange = 2
    monsterSpawned = False
    monsterDamage = 10

    wallDamaged = False

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    currentWeapon == 'Bow'
                if event.key == pygame.K_2:
                    currentWeapon == 'Magic'

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        background()
        weapon()
        blacksmith()
        magicBuilding()

        if click[0] == 1 and projectile1Fired == False and mouse[1] < displayHeight/2:
            projectile1Fired = True
            projectile1X = 0
            projectile1Y = mouse[1]-5

        if monsterSpawned == False:
            monsterSpawned = True
            monsterX = displayWidth
            monsterY = random.randrange(0, displayWidth/2-50)

        if monsterSpawned == True:
            monster(monsterX, monsterY)

        if monsterX == 50:
            monsterSpawned = False
            wallDamaged = True

        if projectile1Fired == True:
            projectile1(projectile1X, projectile1Y)

        projectile1X += projectile1Xchange
        monsterX -= monsterXchange

        if projectile1X > displayWidth:
            projectile1Fired = False

        if projectile1Fired == True:
            if projectile1X + projectileLength > monsterX:
                if projectile1Y > monsterY and projectile1Y + projectileHeight < monsterY + 50:
                    monsterSpawned = False
                    projectile1Fired = False

        if wallDamaged == True:
            wallHealth -= monsterDamage
            wallDamaged = False

        coins()
        wallHealth(wallHealth)

        pygame.display.update()
        clock.tick(60)
game_loop()

pygame.quit()
quit()

我认为我的代码部分可能是问题的一部分:

wallHealth = 100
brightGreen = (0, 255, 0)
red = (200, 0, 0)

def wallHealth(health):
    global wallHealth
    pygame.draw.rect(gameDisplay, red, (displayWidth - 110, 5, 100, 15))
    pygame.draw.rect(gameDisplay, brightGreen, (displayWidth - 110, 5, wallHealth, 15))

def game_loop():
    global wallHealth

    monsterDamage = 10
    wallDamaged = False

    if wallDamaged == True:
        wallHealth -= monsterDamage
        wallDamaged = False

我不知道它为什么不起作用,我希望得到一些有用的建议。

1 个答案:

答案 0 :(得分:1)

wallHealth函数中查看此代码:

pygame.draw.rect(gameDisplay, brightGreen, (displayWidth - 110, 5, wallHealth, 15))

这里你将一个元组传递给pygame.draw.rect,但该元组由一个整数,一个第二个整数,wallHealth函数和一个第三个整数组成。它应该是四个整数。

现在你看到了你的问题。

简而言之:要么将存储墙体健康量的变量重命名,要么重命名该功能。

您不能同时对不同的事物使用相同的名称。