我想用字符串
创建一个关卡:
level = []
在关卡里面你放了W
我如何绘制和显示里面的数据?
我见过有人这样做贴图。基本上,你列一个清单的清单。最外面的列表表示整个关卡,每个内部列表表示该关卡中的每一行tile。内部列表中的内容决定了每行由哪些tile组成。如何遍历它并显示磁贴取决于您,但这里有一种方法。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 600))
level = [["w", "w", "w", "w", "w", "w"],
["w", "r", "g", "w", "b", "w"],
["w", "w", "r", "g", "b", "w"],
["w", "g", "b", "b", "w", "w"],
["w", "b", "w", "g", "g", "w"],
["w", "w", "w", "w", "w", "w"]]
wsize = 100
while True:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
for y, row in enumerate(level):
for x, tileType in enumerate(row):
if tileType == "w":
pygame.draw.rect(screen, (0, 0 ,0), (x * wsize, y * wsize, wsize, wsize))
if tileType == "g":
pygame.draw.rect(screen, (0, 255 ,0), (x * wsize, y * wsize, wsize, wsize))
if tileType == "b":
pygame.draw.rect(screen, (0, 0 ,255), (x * wsize, y * wsize, wsize, wsize))
if tileType == "r":
pygame.draw.rect(screen, (255, 0 ,0 ), (x * wsize, y * wsize, wsize, wsize))
pygame.display.update()