TypeError: invalid destination position for blit
TypeError: invalid destination position for blit
请帮忙。我一直在寻找,但我仍然不太明白如何解决这个问题。我不知道我是否忘记了什么,或者 x 和 y 的值由于某种原因不被认为是整数(我过去曾尝试修复它:int(x))
我想从 Google 创建恐龙游戏,但是 "Hidrante" class 的 .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 的原因并了解如何修复它。
传递给pygame.Surface.blit()
的坐标必须在一定的范围内。
似乎坐标的限制必须在(ctypes
)数据类型int
的范围内,即从-2147483648到2147483647。
您在对象 hidrante
中超出了此限制,因为行:
hidrante.x = (hidrante.x ** 2) * -1
这个公式可能没有达到您的预期。
无论如何,验证 hidrante.x
是否在屏幕范围内,以解决问题:
class Hidrante(object):
# [...]
def draw(self, win):
if self.walkCount + 1 >= 33: #fill code, trying things not actual util
self.walkCount = 0
# validate if object is in the window
if self.x > -30 and self.x < 1000:
if self.vel > 0:
window.blit(img, (self.x, self.y))
self.walkCount += 1
else:
window.blit(img, (self.x, self.y))
self.walkCount += 1
self.hitbox = (self.x + 17, self.y + 2, 30, 60)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
请帮忙。我一直在寻找,但我仍然不太明白如何解决这个问题。我不知道我是否忘记了什么,或者 x 和 y 的值由于某种原因不被认为是整数(我过去曾尝试修复它:int(x))
我想从 Google 创建恐龙游戏,但是 "Hidrante" class 的 .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 的原因并了解如何修复它。
传递给pygame.Surface.blit()
的坐标必须在一定的范围内。
似乎坐标的限制必须在(ctypes
)数据类型int
的范围内,即从-2147483648到2147483647。
您在对象 hidrante
中超出了此限制,因为行:
hidrante.x = (hidrante.x ** 2) * -1
这个公式可能没有达到您的预期。
无论如何,验证 hidrante.x
是否在屏幕范围内,以解决问题:
class Hidrante(object):
# [...]
def draw(self, win):
if self.walkCount + 1 >= 33: #fill code, trying things not actual util
self.walkCount = 0
# validate if object is in the window
if self.x > -30 and self.x < 1000:
if self.vel > 0:
window.blit(img, (self.x, self.y))
self.walkCount += 1
else:
window.blit(img, (self.x, self.y))
self.walkCount += 1
self.hitbox = (self.x + 17, self.y + 2, 30, 60)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)