TypeError:blit的无效目标位置

问题描述 投票:0回答:1

请帮助。我一直在搜索,但我仍然不太了解如何解决该问题。我不知道我是否忘记某些东西,或者由于某些原因x和y的值不被认为是int的(我过去曾尝试对其进行修复:int(x))

我想通过Google创建一个恐龙游戏,但是“ Hidrante”类的.blit失败了(但玩家一个正在运行)。我把它设置为正常值,而不是self.x和self.y,但是仍然遇到问题,我试图说x和y是整数,试图更改名称,正在加载的图像并复制播放器的代码,但似乎不起作用。

这里是所有代码

import pygame


pygame.init()

window = pygame.display.set_mode((1000, 1000))

pygame.display.set_caption("Dinosaurio")

clock = pygame.time.Clock()
char = pygame.image.load('dino.png')
bg = pygame.image.load('bg2.jpg')
img = pygame.image.load("H2.jpg")


class Player(object):
#values of player
    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.jumpCount = 10
        self.hitbox = (self.x + 20, self.y, 30, 64)
#putting the player on screem
    def draw(self, window):
        window.blit(char, (self.x, self.y))
        self.hitbox = (self.x + 20, self.y, 30, 64)
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)

#Obstacle
class Hidrante(object):

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self. height = height
        self.vel = 8
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.walkCount = 10
        self.path = 0
#putting the obstacle in screem
    def draw(self, win):
        if self.walkCount + 1 >= 33: #fill code, trying things not actual util
            self.walkCount = 0

        if self.vel > 0:
            window.blit(img, (self.x, self.y)) #here is the problem
            self.walkCount += 1
        else:
            window.blit(img, (self.x, self.y))#these too
            self.walkCount += 1
        self.hitbox = (self.x + 17, self.y + 2, 30, 60)  
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)


def gmv():
#putting on creem the objects
    window.blit(bg, (0, 0))
    dino.draw(window)
    hidrante.draw(window)
    pygame.display.update()

#creating the objects
dino = Player(110, 620, 64, 64)
hidrante = Hidrante(1100, 620, 64, 64)
run = True
neg = 1
while run:
    clock.tick(30) #frames
    hidrante.x = (hidrante.x ** 2) * -1 #Function to not jump infinit.

    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            run = False
#Space to jump
    keys = pygame.key.get_pressed()

    if not (dino.isJump):
        if keys[pygame.K_SPACE]:
            dino.isJump = True
    else:
        if dino.jumpCount >= -10:
            dino.y -= (dino.jumpCount * abs(dino.jumpCount)) * 0.5
            dino.jumpCount -= 1
        else:
            dino.jumpCount = 10
            dino.isJump = False

    gmv()


pygame.quit()

我只想知道对象值干扰.blit的原因,并学习如何修复它。

python pygame pycharm
1个回答
0
投票

传递给pygame.Surface.blit()的坐标必须在一定范围内。由于以下行,您超出了对象pygame.Surface.blit()中的此限制:

hidrante

此公式可能无法满足您的期望。无论如何,请验证hidrante.x = (hidrante.x ** 2) * -1 是否在您的屏幕范围内,以解决此问题:

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