[英]Why aren't tiles appearing pygame
I have a tile map that makes all 1
'sa tile and the whole outer layer is 1
's.我有一个瓷砖 map ,它使所有
1
的瓷砖和整个外层都是1
的。 However, when I run my code, the tiles only appear at the top of the screen.但是,当我运行我的代码时,图块只出现在屏幕顶部。 Shouldn't they appear around the entire map since I have
1
's all around?它们不应该出现在整个 map 周围,因为我周围都有
1
吗?
# import modules
import pygame
from pygame.locals import *
#init
pygame.init()
#screen creation
screen_width = 1000
screen_height = 1000
tile_size = 50
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Crashlandingv6')
#load images
bg_img = pygame.image.load('background.jpg')
bg_img = pygame.transform.scale(bg_img, (1000,1000))
rect = bg_img.get_rect()
class World():
def __init__(self,data):
self.tile_list = []
#load images
dirt_img = pygame.image.load('dirt.png')
for row in data:
row_count = 0
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (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_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
[1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 7, 0, 5, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1],
[1, 7, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 2, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
world = World(world_data)
#main loop
run = True
while run:
screen.blit(bg_img,rect)
world.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
What am I doing wrong?我究竟做错了什么?
Lets focus on this part of your code:让我们专注于这部分代码:
def __init__(self,data):
self.tile_list = []
#load images
dirt_img = pygame.image.load('dirt.png')
for row in data:
row_count = 0
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (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
As you can see, you have a nested for
-loop;如您所见,您有一个嵌套
for
循环; during each iteration of the outer for
loop, you set the values of both row_count
and col_count
to 0
.在外部
for
循环的每次迭代中,您将row_count
和col_count
的值都设置为0
。
So, the row_count
will always remain zero when it gets called in the inner for
-loop no matter how many times you increment it by 1
at the end of each iteration of the outer for
-loop, because it just gets set back to 0
at the beginning of each iteration of the outer for
-loop.因此,当在内部
for
循环中调用row_count
时,无论在外部for
循环的每次迭代结束时将其增加多少1
,它都将始终保持0
,因为它只是在外部for
循环每次迭代的开始。
Simply move the row_count = 0
out and above the outer for
-loop:只需将
row_count = 0
移到外部for
循环上方:
def __init__(self,data):
self.tile_list = []
#load images
dirt_img = pygame.image.load('dirt.png')
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.