简体   繁体   English

无法使用 blit 在 pygame 中显示图像

[英]Failing to display images in pygame using blit

I have the following code where i try to load two images using .blit:我有以下代码,我尝试使用 .blit 加载两个图像:

import pygame

pygame.init()

width = 1600
height = 900
screen = pygame.display.set_mode((width, height))

attackImg = pygame.image.load('attack.png')
healImg = pygame.image.load('heal.png')
manaImg = pygame.image.load('mana.png')
moveImg = pygame.image.load('move.png')
shieldImg = pygame.image.load('shield.png')
stunImg = pygame.image.load('stun.png')


while True:
  screen.fill((255, 255, 255))
  screen.blit(healImg, (100, 800))
  screen.blit(attackImg, (200, 800))
  pygame.display.update()

I get no error messages when running the code, but the images don´t show up.运行代码时我没有收到错误消息,但图像不显示。 The image files are available and their right names have been used.图像文件可用并且它们的正确名称已被使用。 I have read several tutorials and don´t see what I´ve done wrong.我已经阅读了几个教程,但没有看到我做错了什么。 The code is written and ran in repl.it if that makes any difference.如果这有什么不同,代码是在 repl.it 中编写和运行的。 Please help me understand why the images are not showing up and what needs to be changed.请帮助我理解为什么图像不显示以及需要更改的内容。

I copy-pasted your code and firstly, the window didn't show up.我复制粘贴了你的代码,首先,窗口没有出现。 Your code didn't have the 'event loop' in it.您的代码中没有“事件循环”。 You pretty much always have to do it, else the window won't launch.您几乎总是必须这样做,否则窗口将无法启动。 You also might usually want to make a running = True variable.您通常还可能希望创建一个running = True变量。 Without that your program is not likely to close normally.否则,您的程序不太可能正常关闭。 Heres the correct code:这是正确的代码:

import pygame

pygame.init()

width = 1600
height = 900
screen = pygame.display.set_mode((width, height))

attackImg = pygame.image.load('attack.png')
healImg = pygame.image.load('heal.png')

running = True
while running:
  screen.fill((255, 255, 255))
  for event in pygame.event.get(): # <--- 'event loop'
    if event.type == pygame.QUIT:
        running = False
  screen.blit(healImg, (100, 800))
  screen.blit(attackImg, (200, 800))
  pygame.display.update()

If that does not work it probably is just that the images cover each other or they are being 'blitted' outside the view.如果这不起作用,可能只是图像相互覆盖或者它们被“blitted”在视图之外。 In that case, write a comment.在这种情况下,请写评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM