Pygame 图像无法正确显示

问题描述 投票:0回答:1

我正在尝试使用 pygame 在我的程序中显示草、泥土和灌木丛。目前,这些块无法正确显示,唯一显示的块是草块,并且它不在正确的位置。

我使用数组列表,所以我假设问题出在某个地方,但我不确定。这就是我运行程序时发生并显示的情况。 enter image description here这是我的代码

import pygame
from pygame.locals import *

pygame.init()

screen_height = 800
screen_width = 800

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Girl On an Adventure")




moon2Img = pygame.image.load("moon2.png")
bg2Img = pygame.image.load("bg2.png")

tile_size = 200



bg2Img = pygame.transform.scale_by(bg2Img, 4.8)
moon2Img = pygame.transform.scale_by(moon2Img, 1)

def draw_grid():
    for line in range(0, 6):
        pygame.draw.line(screen, (255, 255, 255), (0, line * tile_size), (screen_width, line * tile_size))
        pygame.draw.line(screen, (255, 255, 255), (line * tile_size, 0), (line * tile_size, screen_height))

class World():
    def __init__(self, data):
        self.tile_list = []
        dirtImg = pygame.image.load("dirt.png")
        grassImg = pygame.image.load("grass.png")
        topImg = pygame.image.load("top.png")
        
        for row in data:
            row_count = 0
            col_count = 0
            for tile in row:
               if tile == 1:
                   img = pygame.transform.scale(topImg, (tile_size, tile_size ))
                   img_rect = img.get_rect()
                   img_rect.x = col_count * tile_size
                   img_rect.y = row_count * tile_size
                   tile = (img, img_rect)
                   self.tile_list.append(tile)
               elif  tile == 2:
                   img = pygame.transform.scale(dirtImg, (tile_size, tile_size ))
                   img_rect = img.get_rect()
                   img_rect.x = col_count * tile_size
                   img_rect.y = row_count * tile_size
                   tile = (img, img_rect)
                   self.tile_list.append(tile)
               elif tile == 3:
                    img = pygame.transform.scale(grassImg, (tile_size, tile_size ))
                    img_rect = img.get_rect()
                    img_rect.x = col_count * tile_size
                    img_rect.y = row_count * tile_size
                    tile = (img, img_rect)
                    self.tile_list.append(tile)
            
            
                    col_count += 1
                    row_count += 1
            
    def draw(self):
        for tile in self.tile_list:
            screen.blit(tile[0], tile[1])
           
            
            
world_info = [
[1,1,1,1,1],
[2,0,0,0,2],
[2,0,0,0,2],
[2,0,0,0,2],
[3,3,3,3,3],
]


world = World(world_info)
run = True
while run == True:
    
   
    screen.blit(bg2Img, (.5, .5))
    screen.blit(moon2Img, (30,30))
    
    world.draw()
    draw_grid()
    
    print(world.tile_list)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    pygame.display.update()
pygame.quit()
python visual-studio-code pygame
1个回答
0
投票

您必须增加外循环中的行数:

row_count = 0
for row in data:
    col_count = 0
    for tile in row:

        # add tiles
        # [...]

        col_count += 1              
    row_count += 1

改变本机你可以使用内置函数

enumerate
:

for row_count, row in enumerate(data):
    for col_count , tile in enumerate(row):

        # add tiles
        # [...]
© www.soinside.com 2019 - 2024. All rights reserved.