请帮忙。我一直在寻找,但我仍然不太明白如何解决这个问题。我不知道我是不是忘记了什么,或者 x 和 y 的值由于某种原因不被认为是整数(我过去曾尝试修复它: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 的原因并学习如何修复它。
最佳答案
传递给 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)
关于python - 类型错误 : invalid destination position for blit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328606/