Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:blit的目标位置无效_Python_Pygame_Pycharm_Pygame Surface - Fatal编程技术网

Python TypeError:blit的目标位置无效

Python TypeError:blit的目标位置无效,python,pygame,pycharm,pygame-surface,Python,Pygame,Pycharm,Pygame Surface,请帮忙。我一直在寻找,但我仍然不太明白如何解决这个问题。我不知道我是不是;我忘记了什么,或者x和y的值由于某种原因不被认为是int(我在过去尝试过修复它:int(x)) 我想从Google创建恐龙游戏,但是“Hidrante”类的.blit失败了(但是玩家一个正在运行)。我把它的值改为正常值,而不是self.x和self.y,但仍然有问题,我试着说它的x和y是整数,试着改变名字,我加载的图像,复制播放器的代码,但似乎不起作用 这是所有的代码 import pygame pygame.init

请帮忙。我一直在寻找,但我仍然不太明白如何解决这个问题。我不知道我是不是;我忘记了什么,或者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()


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

传递的坐标必须在某个限制内。 坐标的限制似乎必须在()数据类型
int
的范围内,即从-2147483648到2147483647

您在对象
隐藏
中超出了此限制,因为行:

hidrante.x=(hidrante.x**2)*-1
也许这个公式没有达到你期望的效果

无论如何,如果
hidrante.x
在电子屏幕范围内,请验证以解决问题:

类隐藏(对象):
# [...]
def抽签(自我,赢):
如果self.walkCount+1>=33:#填充代码,尝试不实际的事情
self.walkCount=0
#验证对象是否在窗口中
如果self.x>-30和self.x<1000:
如果self.vel>0:
窗口blit(img,(self.x,self.y))
self.walkCount+=1
其他:
窗口blit(img,(self.x,self.y))
self.walkCount+=1
self.hitbox=(self.x+17,self.y+2,30,60)
pygame.draw.rect(窗口,(255,0,0),self.hitbox,2)