图片未加载 pygame
Image not loading in pygame
所以我尝试了很多东西(首先代码是基于 OOP 但我改变了它,弄乱了变量但它没有用)而且我不知道为什么我的图像(Hero.png 在代码中) showing.I 对 python/pygame 来说不是新手所以这一定是个愚蠢的错误。无论如何,这里是 code.It 还不是完整的代码。
Import pygame
pygame.init()
winX = 720
winY = 1280
win = pygame.display.set_mode([winY, winX])
GameRun = True
clock = pygame.time.Clock()
HeroSprite = pygame.image.load('Hero.png')
pygame.display.set_caption("Game thingy")
bg = pygame.image.load('bg.jpg')
HeroX = winX / 2
HeroY = winY - 100
vel = 5
def redrawGameWindow(win, HeroX,HeroY):
win.blit(bg, (0,0))
win.blit(HeroSprite, (HeroX, HeroY))
pygame.display.update()
while GameRun:
clock.tick(60)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
HeroX -= vel if HeroX > 20 else 0
elif pressed[pygame.K_RIGHT]:
HeroX += vel if HeroX < winX - 20 else 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameRun = False
redrawGameWindow(win, HeroX, HeroY)
pygame.quit()
你已经交换了 winX
和 winY
:
winX = 720
winY = 1280
win = pygame.display.set_mode([winY, winX])
所以英雄初始位置不在window:
HeroX = winX / 2
HeroY = winY - 100
交换 window 大小的变量:
winX = 1280
winY = 720
win = pygame.display.set_mode([winX, winY])
所以我尝试了很多东西(首先代码是基于 OOP 但我改变了它,弄乱了变量但它没有用)而且我不知道为什么我的图像(Hero.png 在代码中) showing.I 对 python/pygame 来说不是新手所以这一定是个愚蠢的错误。无论如何,这里是 code.It 还不是完整的代码。
Import pygame
pygame.init()
winX = 720
winY = 1280
win = pygame.display.set_mode([winY, winX])
GameRun = True
clock = pygame.time.Clock()
HeroSprite = pygame.image.load('Hero.png')
pygame.display.set_caption("Game thingy")
bg = pygame.image.load('bg.jpg')
HeroX = winX / 2
HeroY = winY - 100
vel = 5
def redrawGameWindow(win, HeroX,HeroY):
win.blit(bg, (0,0))
win.blit(HeroSprite, (HeroX, HeroY))
pygame.display.update()
while GameRun:
clock.tick(60)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
HeroX -= vel if HeroX > 20 else 0
elif pressed[pygame.K_RIGHT]:
HeroX += vel if HeroX < winX - 20 else 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameRun = False
redrawGameWindow(win, HeroX, HeroY)
pygame.quit()
你已经交换了 winX
和 winY
:
winX = 720 winY = 1280 win = pygame.display.set_mode([winY, winX])
所以英雄初始位置不在window:
HeroX = winX / 2 HeroY = winY - 100
交换 window 大小的变量:
winX = 1280
winY = 720
win = pygame.display.set_mode([winX, winY])