“ TypeError:参数1必须为pygame.Surface,而不是元组。”我该如何解决此错误?

问题描述 投票:0回答:1
**So what my issue is I can't get my left image to appear but my right image work fine?**

import pygame
import sys

pygame.init()#We always need to initalize our pygame IN EVERY PROJECT/FILE

win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480

pygame.display.set_caption("Dragon Ball Z Mini-game")#We are giving our Window/Screen a name

walkRight = [pygame.image.load('image/young_goku_right_image0 - t.png'), pygame.image.load('image/young_goku_right_image1 - t.png'), pygame.image.load('image/young_goku_right_image2 - t.png'), pygame.image.load('image/young_goku_right_image3 - t.png'), pygame.image.load('image/young_goku_right_image4 - t.png')]
walkLeft = [pygame.image.load('image/young_goku_left_image0 - t.png'), pygame.image.load('image/young_goku_left_image1 - t.png'), pygame.image.load('image/young_goku_left_image2 - t.png'), pygame.image.load('image/young_goku_left_image3 - t.png'), pygame.image.load('image/young_goku_left_image4 - t.png')]
bg = pygame.image.load('image/bg2.jpg')
char = pygame.image.load('image/young_goku - standing - t.png')

clock = pygame.time.Clock()

bulletSound = pygame.mixer.Sound("image/kiblast.wav")
hitSound = pygame.mixer.Sound("image/Bomb+1.wav")
#bulletSound.play()
music = pygame.mixer.music.load("image/16 - Dragon Ball Z - title song.mp3")
pygame.mixer.music.play(-1)

score = 0

class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.isJump = False
        self.left = False
        self.right = False
        self.walkCount = 0
        self.jumpCount = 10
        self.standing = True
        self.hitbox = (self.x + 17, self.y + 11, 29, 52)
**Here is where i keep on trying to tweak the code but my image still won't change**
    def draw(self, win):
        global ki_stance
        if self.walkCount + 1 >= 10:
            self.walkCount = 0
        if not (self.standing):
            if self.left:
                win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            elif self.right:
                win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
                keys = pygame.key.get_pressed()
                if keys[pygame.K_a]:
                    ki_stance = pygame.image.load("image/goku-ki.png"), pygame.image.load("image/goku-ki_left.png")**Have both my left and right image yet still don't know why it won't work?**
                    win.blit(ki_stance, (self.x, self.y))
                self.walkCount += 1
        else:
            if self.right:
                win.blit(walkRight[0], (self.x, self.y))
            else:
                win.blit(walkLeft[0], (self.x, self.y))
        self.hitbox = (self.x + 17, self.y + 11, 29, 52)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            ki_stance = pygame.image.load("image/goku-ki.png"), pygame.image.load("image/goku-ki_left.png")
            win.blit(ki_stance, (self.x, self.y))
        # pygame.draw.rect(win, (255,0,0), self.hitbox,2)

    def hit(self):
        self.isJump = False
        self.jumpCount = 10
        self.x = 100
        self.y = 410
        self.walkCount = 0
        font1 = pygame.font.SysFont('comicsans', 100)
        text = font1.render('-5', 1, (255, 0, 0))
        win.blit(text, (250 - (text.get_width() / 2), 200))
        pygame.display.update()
        i = 0
        while i < 200:
            pygame.time.delay(10)
            i += 1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    i = 201
                    pygame.quit()


class projectile(object):
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        ki = pygame.image.load("image/ki.png")
        win.blit(ki, (self.x, self.y))
        ki_stance = pygame.image.load("image/goku-ki.png", "image/goku-ki_left.png")
        win.blit(ki, (self.x, self.y))
        pygame.draw.circle(win, (255,255,0), (self.x, self.y), self.radius)
"""
    def draw2(self, win):
        ki2 = pygame.image.load("image/b4.png")
        win.blit(ki2, (self.x, self.y))
        ki_stance = pygame.image.load("image/goku-ki.png")
        win.blit(ki2, (self.x, self.y))
        pygame.draw.rect(win, (255, 255, 0), (self.x, self.y), self.radius)
"""

class projectile2():
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        ki = pygame.image.load("image/b4.png")
        win.blit(ki, (self.x, self.y))
        ki_stance = pygame.image.load("image/goku-ki.png", "image/goku-ki_left.png")
        win.blit(ki, (self.x, self.y))
        pygame.draw.circle(win, (0,0,139), (self.x, self.y), self.radius)







class enemy(object):
    walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"),
                 pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"),
                 pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"),
                 pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"),
                 pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"),
                 pygame.image.load("image/R11E.png")]
    walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"),
                pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"),
                pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"),
                pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"),
                pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"),
                pygame.image.load("image/L11E.png")]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkCount = 0
        self.vel = 3
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.health = 10
        self.visible = True

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkCount + 1 >= 33:
                self.walkCount = 0

            if self.vel > 0:
                win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1

            pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
            pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 17, self.y + 2, 31, 57)
            # pygame.draw.rect(win, (255,0,0), self.hitbox,2)

    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0
        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0

    def hit(self):
        if self.health > 0:
            self.health -= 1
        else:
            self.visible = False
        print('hit')


class enemy2():
    walkRight = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-move_right-T.gif")]
    walkLeft = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left2-T.gif")]
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkcount = 0
        self.vel = 3
        self.hitbox = (self.x + 20, self.y, 28, 60)
        self.health = 10
        self.visible = True

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkcount + 0 >= 10:
                self.walkcount = 0

            if self.vel > 0:
                win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
                self.walkcount += 1
            else:
                win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
                self.walkcount += 1
            pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
            pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 20, self.y, 28, 60)

    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0

        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0

    def hit(self):
        if self.health > 0:
            self.health -= 1
        else:
            self.visible = False
        print("Hit!!")
        pass


def redrawGameWindow():
    win.blit(bg, (0, 0))
    text = font.render('Score: ' + str(score), 1, (0, 0, 0))
    win.blit(text, (350, 10))
    man.draw(win)
    goblin.draw(win)
    goblin2.draw(win)
    for bullet in bullets:
        bullet.draw(win)

    pygame.display.update()


# mainloop
font = pygame.font.SysFont("comicsans", 30, True, True)
man = player(200, 420, 64, 64)
goblin = enemy(50, 410, 64, 64, 450)
goblin2 = enemy2(250, 410, 64, 64, 450)
shootloop = 0
bullets = []
run = True
while run:
    clock.tick(27)
    if goblin.visible == True:
        if man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1]:
            if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
                man.hit()
                score -= 5
                print("Hurt")

    if goblin2.visible == True:
        if man.hitbox[1] < goblin2.hitbox[1] + goblin2.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin2.hitbox[1]:
            if man.hitbox[0] + man.hitbox[2] > goblin2.hitbox[0] and man.hitbox[0] < goblin2.hitbox[0] + goblin2.hitbox[2]:
                man.hit()
                score -= 5
                print("Hurt")

    if shootloop > 0:
        shootloop += 1
    if shootloop > 3:
        shootloop = 0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            #pygame.quit()
            sys.exit()
            run = False

    for bullet in bullets:
        if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
            if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
                #hitSound.play()
                goblin.hit()
                score += 1
                #bullets.pop(bullets.index(bullet))

    for bullet in bullets:
        if bullet.y - bullet.radius < goblin2.hitbox[1] + goblin2.hitbox[3] and bullet.y + bullet.radius > goblin2.hitbox[1]:
             if bullet.x + bullet.radius > goblin2.hitbox[0] and bullet.x - bullet.radius < goblin2.hitbox[0] + goblin2.hitbox[2]:
                #hitSound.play()
                goblin2.hit()
                score += 1
                #bullets.pop(bullets.index(bullet))

        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE] and shootloop == 0:
        bulletSound.play()
        if man.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(projectile(round(man.x + man.width // 3), round(man.y + man.height // 3), 6, (0, 0, 0), facing))
        shootloop = 1

    if keys[pygame.K_a] and shootloop == 0:
        bulletSound.play()
        if man.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(projectile2(round(man.x + man.width // 3), round(man.y + man.height // 3), 12, (0, 0, 0), facing))
        shootloop = 1

    if keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
    else:
        man.standing = True
        man.walkCount = 0

    if not (man.isJump):
        if keys[pygame.K_UP]:
            man.isJump = True
            man.right = False
            man.left = False
            man.walkCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10

    redrawGameWindow()

pygame.quit()

这是我得到的错误?

“ C:\ Users \ RafaelPerez \ AppData \ Local \ Temp \ pygame1.py \ venv \ Python37 \ Scripts \ python.exe““ C:/用户/拉斐尔·佩雷斯/PycharmProjects/game_two/game_2_precheck.py”pygame 1.9.6 pygame社区的您好。https://www.pygame.org/contribute.html伤害追踪(最新最后调用):文件“ C:/用户/拉斐尔Perez / PycharmProjects / game_two / game_2_precheck.py“,第380行,在redrawGameWindow()文件“ C:/ Users / Rafael Perez / PycharmProjects / game_two / game_2_precheck.py”,行260,在redrawGameWindowman.draw(win)文件“ C:/ Users / Rafael Perez / PycharmProjects / game_two / game_2_precheck.py”,第53行,绘制win.blit(ki_stance,(self.x,self.y))TypeError:参数1必须是pygame.Surface,而不是tuple

python pygame pycharm pygame-surface
1个回答
0
投票

错误在这里:

ki_stance = pygame.image.load("image/goku-ki.png"), pygame.image.load("image/goku-ki_left.png")
win.blit(ki_stance, (self.x, self.y))

blit()方法需要一个Surface作为其第一个参数,而pygame.image.load()确实返回一个Surface。但是,分配ki_stance的行具有两个用逗号分隔的load(),该逗号用这两个已加载的Surface创建一个元组并将该元组分配给ki_stance。

然后,将元组ki_stance作为第一个参数传递给win.blit(),它需要一个Surface而不是两个Surface的元组。我不确定您的意图是什么,但是您只能传递blit()其中之一。我猜这两个图像是您要为播放器交替显示的两个帧。您需要一次选择一个传递给blit,然后仅发送它。

进一步的评论,您确实不应该在每次调用player.draw()方法时都加载图像。您应该只将它们加载一次,并将它们分配给稍后在draw方法中使用的变量。我可以在这里快速解释的最简单的建议是,至少将负载移到init()中,尽管这实际上不是最佳解决方案,因为将调用init(但总比每次都做得好)在抽奖中)。理想情况下,您也不希望它也位于init中,因为这将在创建每个新实例时调用。您确实想将图像移动到类变量中,以便仅将它们加载一次,但是在这里我不会赘述,因为这将花费一些时间,而且与您的问题无关。除了将其作为类变量处理之外,还可以在类访问的全局变量中进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.