Python 图像不会显示在pygame窗口中

Python 图像不会显示在pygame窗口中,python,image,pygame,Python,Image,Pygame,在关闭脚本之前,图像不会显示在窗口中。我正在做一个双人多人游戏。我想使用网格中显示的图像显示地图,但这些图像根本不显示。我曾尝试添加print语句,以确定函数是否已执行(它是),但似乎window.blit(image,(x,y))不起作用,但我无法找出原因 import pygame from time import sleep pygame.init() clock = pygame.time.Clock() WIDTH = 624 HEIGHT = 360 window = pygame

在关闭脚本之前,图像不会显示在窗口中。我正在做一个双人多人游戏。我想使用网格中显示的图像显示地图,但这些图像根本不显示。我曾尝试添加print语句,以确定函数是否已执行(它是),但似乎
window.blit(image,(x,y))
不起作用,但我无法找出原因

import pygame
from time import sleep
pygame.init()
clock = pygame.time.Clock()

WIDTH = 624
HEIGHT = 360
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('snoeppot')
#plaatjes
gras = pygame.image.load('sprites/Grass.png')
struik = pygame.image.load('sprites/struik.png')
vloerRed = pygame.image.load('sprites/blauwe_vloer.png')
vloerBlue = pygame.image.load('sprites/rode_vloer.png')
SafeSpace = pygame.image.load('sprites/safe_space.png')
IngangPortaal = pygame.image.load('sprites/ingang_portaal.png')
uitgangPortaal = pygame.image.load('sprites/uitgang_portaal.png')
water = pygame.image.load('sprites/water.png')
weg = pygame.image.load('sprites/weg.png')
ijs = pygame.image.load('sprites/ijs.png')
snoeppotPlatform = pygame.image.load('sprites/snoeppot_platform.png')
#uiterlijk class each uiterlijk is one tile in the map
class uiterlijk:
    blokken = [gras, struik, 0, SafeSpace, IngangPortaal, #0-4
               uitgangPortaal, water, weg, ijs, 0, #5-9
               snoeppotPlatform]
    def __init__(self, soort, X, Y):
        self.soort = soort #defines the kind of tile 0-26
        self.pos = [X, Y]
        if 11 <= self.soort <=26: #11 trought 26 use the same image.
            self.image = pygame.transform.scale(gras, (tegel, tegel))
        else:
            self.image = pygame.transform.scale(self.blokken[soort], (tegel, tegel)) #looks in list blokken for an image
    def draw(self):
        window.blit(self.image, (tegel*self.pos[0], tegel*self.pos[1]))
#the map I want to display each number equals a image
speelveld = [
[1, 1, 0, 11, 11, 21, 11, 11, 0, 1, 0, 12, 12, 22, 22, 12, 12, 0, 1, 0, 13, 13, 23, 13, 13, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[17, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 19],
[17, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 19],
[17, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 19],
[17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19],
[18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20],
[17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19],
[17, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 19],
[17, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 19],
[17, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 19],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 14, 14, 24, 14, 14, 0, 1, 0, 15, 15, 25, 25, 15, 15, 0, 1, 0, 16, 16, 26, 16, 16, 0, 1, 1]]

tegel = 18 #size of the images

speelveld_tiles = [[uiterlijk(speelveld[Y][X], X, Y) for X in range(len(speelveld[0]))] for Y in range(len(speelveld))]

while True:
    #draws whole window white
    window.fill((0, 255, 255))
    #draws the map
    for rij in range(len(speelveld)):
        for x in range(len(speelveld[0])):
            speelveld_tiles[rij][x].draw()
    #updates screen
    pygame.display.update()
    clock.tick(60)
导入pygame
从时间上导入睡眠
pygame.init()
clock=pygame.time.clock()
宽度=624
高度=360
window=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题('snoeppot')
#普拉特耶斯
gras=pygame.image.load('sprites/Grass.png')
struik=pygame.image.load('sprites/struik.png')
vloerRed=pygame.image.load('sprites/blauwe_vloer.png')
vloerBlue=pygame.image.load('sprites/rode\u vloer.png')
SafeSpace=pygame.image.load('sprites/safe_space.png')
ingangportal=pygame.image.load('sprites/ingang_portal.png')
uitgangportal=pygame.image.load('sprites/uitgang\u portal.png')
water=pygame.image.load('sprites/water.png')
weg=pygame.image.load('sprites/weg.png')
ijs=pygame.image.load('sprites/ijs.png')
snoeppotPlatform=pygame.image.load('sprites/snoeppot_platform.png')
#uiterlijk类每个uiterlijk都是地图中的一个平铺
uiterlijk类:
blokken=[gras,struik,0,SafeSpace,IngangPortal,#0-4
UITGANGANAL、水、weg、ijs、0、#5-9
SnoEPPot平台]
定义初始(self,soort,X,Y):
self.soort=soort#定义瓷砖的类型0-26
self.pos=[X,Y]

如果11您必须处理应用程序循环中的事件。分别见:

对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保了您的程序可以在内部与操作系统的其余部分交互

run=True
运行时:
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
#将整个窗口绘制为白色
窗口填充((0,255,255))
#绘制地图
对于范围内的下钻(len(speelveld)):
对于范围内的x(len(speelveld[0]):
speelveld_tiles[rij][x].draw()
#更新屏幕
pygame.display.update()
时钟滴答(60)

谢谢!这很有效。我不知道有必要要求举办活动。