Python 查找随机数时Pygame列表索引超出范围

Python 查找随机数时Pygame列表索引超出范围,python,pygame,Python,Pygame,在pygame中,我尝试创建一个简单的程序,可以为我正在制作的游戏随机生成2d地图,不同的材质具有不同的稀有性。然而,直到我决定将稀有部分添加到程序中,它工作得非常好 在我尝试实现这个特性之后,它给了我这个错误 : 回溯(最近一次呼叫最后一次): 文件“/home/pi/pygame/2D game.py”,第63行,在 瓷砖贴图[rw][cl]=瓷砖 索引器:列表索引超出范围 这是我的密码: import pygame, sys, random from pygame.locals impo

在pygame中,我尝试创建一个简单的程序,可以为我正在制作的游戏随机生成2d地图,不同的材质具有不同的稀有性。然而,直到我决定将稀有部分添加到程序中,它工作得非常好

在我尝试实现这个特性之后,它给了我这个错误 :

回溯(最近一次呼叫最后一次):
文件“/home/pi/pygame/2D game.py”,第63行,在
瓷砖贴图[rw][cl]=瓷砖
索引器:列表索引超出范围
这是我的密码:

import pygame, sys, random
from pygame.locals import *

#List variables/constants
DIRT = 0
GRASS = 1
WATER = 2
COAL = 3

BLACK = (0, 0, 0)
BROWN = (153, 76, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#Useful dimensions
TILESIZE = 40
MAPWIDTH = 30
MAPHEIGHT = 20

#A dictionary linking recources to textures
textures = {
            DIRT : pygame.image.load("dirt.png"),
            GRASS : pygame.image.load("grass.png"),
            WATER : pygame.image.load("water.png"),
            COAL : pygame.image.load("coal.png"),

        }

#A list of resources
resources = [DIRT, GRASS, WATER, COAL]

#Using list comprehension to create a tilemap
tilemap = [ [DIRT for w in range(MAPWIDTH)] for h in range(MAPHEIGHT)]

#Initialise pygame module
pygame.init()
#Creating a new draw surface
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH * TILESIZE, MAPHEIGHT * TILESIZE))
#Naming the window
pygame.display.set_caption("2D game")

#Loop through each row
for rw in range(MAPWIDTH):
    #Loop through each column
    for cl in range(MAPHEIGHT):
        randomNumber = random.randint(0, 30)
        #If a 0,the tile = coal
        if randomNumber == 0:
            tile = COAL
        #If 1 or 2, tile = water
        elif randomNumber == 1 or randomNumber == 2:
            tile = WATER
        #If 3 - 7, tile = grass
        elif randomNumber >= 3 and randomNumber <= 7:
            tile = DIRT
        #if anything else, tile = dirt
        else:
            tile = GRASS
        #Set the position on the tilemap to the randomly chosen tile
        tilemap[rw][cl] = tile


#Loop forever
while True:

    #Collects all the user events
    for event in pygame.event.get():
        #if the user wants to quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    #Loop through each row
    for row in range(MAPHEIGHT):
        #Loop through each column
        for column in range(MAPWIDTH):
            #Draw an image of resource at the correct position on the tilemap, using the correct texture
            DISPLAYSURF.blit(textures[tilemap[row][column]], (column * TILESIZE, row * TILESIZE))


#Update the display
pygame.display.update()
import pygame,sys,random
从pygame.locals导入*
#列出变量/常量
污垢=0
草=1
水=2
煤=3
黑色=(0,0,0)
布朗=(153,76,0)
绿色=(0,255,0)
蓝色=(0,0255)
#有用尺寸
TILESIZE=40
地图宽度=30
地图高度=20
#将资源链接到纹理的词典
纹理={
DIRT:pygame.image.load(“DIRT.png”),
GRASS:pygame.image.load(“GRASS.png”),
水:pygame.image.load(“WATER.png”),
COAL:pygame.image.load(“COAL.png”),
}
#资源清单
资源=[泥土、草、水、煤]
#使用列表理解创建tilemap
tilemap=[[范围内w的污垢(贴图宽度)]范围内h的污垢(贴图高度)]
#初始化pygame模块
pygame.init()
#创建新的绘制曲面
DISPLAYSURF=pygame.display.set_模式((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))
#命名窗口
pygame.display.set_标题(“2D游戏”)
#循环遍历每一行
对于范围内的rw(贴图宽度):
#循环遍历每一列
对于范围内的cl(地图高度):
randomNumber=random.randint(0,30)
#如果为0,则瓷砖=煤炭
如果randomNumber==0:
瓷砖=煤
#如果为1或2,则瓷砖=水
elif randomNumber==1或randomNumber==2:
瓷砖=水
#如果3-7,瓷砖=草

elif randomNumber>=3且randomNumber此矩阵的存储方式为第一个索引表示高度,第二个索引表示宽度。因此,您必须使用
tilemap[x-direction][y-direction]
而不是
tilemap[y-direction][x-direction]
。所以在你的例子中,
tilemap[cl][rw]=tile


这种行为在PIL这样的软件包中也可以看到。

您是否尝试过打印
rw
cl
的值?您将
rw
cl
的范围颠倒过来。好的,干杯,我试试
import pygame, sys, random
from pygame.locals import *

#List variables/constants
DIRT = 0
GRASS = 1
WATER = 2
COAL = 3

BLACK = (0, 0, 0)
BROWN = (153, 76, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#Useful dimensions
TILESIZE = 40
MAPWIDTH = 30
MAPHEIGHT = 20

#A dictionary linking recources to textures
textures = {
            DIRT : pygame.image.load("dirt.png"),
            GRASS : pygame.image.load("grass.png"),
            WATER : pygame.image.load("water.png"),
            COAL : pygame.image.load("coal.png"),

        }

#A list of resources
resources = [DIRT, GRASS, WATER, COAL]

#Using list comprehension to create a tilemap
tilemap = [ [DIRT for w in range(MAPWIDTH)] for h in range(MAPHEIGHT)]

#Initialise pygame module
pygame.init()
#Creating a new draw surface
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH * TILESIZE, MAPHEIGHT * TILESIZE))
#Naming the window
pygame.display.set_caption("2D game")

#Loop through each row
for rw in range(MAPWIDTH):
    #Loop through each column
    for cl in range(MAPHEIGHT):
        randomNumber = random.randint(0, 30)
        #If a 0,the tile = coal
        if randomNumber == 0:
            tile = COAL
        #If 1 or 2, tile = water
        elif randomNumber == 1 or randomNumber == 2:
            tile = WATER
        #If 3 - 7, tile = grass
        elif randomNumber >= 3 and randomNumber <= 7:
            tile = DIRT
        #if anything else, tile = dirt
        else:
            tile = GRASS
        #Set the position on the tilemap to the randomly chosen tile
        tilemap[rw][cl] = tile


#Loop forever
while True:

    #Collects all the user events
    for event in pygame.event.get():
        #if the user wants to quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    #Loop through each row
    for row in range(MAPHEIGHT):
        #Loop through each column
        for column in range(MAPWIDTH):
            #Draw an image of resource at the correct position on the tilemap, using the correct texture
            DISPLAYSURF.blit(textures[tilemap[row][column]], (column * TILESIZE, row * TILESIZE))


#Update the display
pygame.display.update()