我10岁的儿子正试图在这个2D游戏中实现一个天气系统的新功能,他使用pygame进行教程,但遇到了IdententationError问题。
我一直在帮助他,但我还在学习,我认为这是随机和显示,但我不知道如何解决它,因为这是他的第一个pygame方法之一。
他试图让它产生随机天气以在屏幕上发送。他还希望在线下添加碰撞检测,但他仍然在学习该主题。
这是他的代码: all files我还包括原始完成的未完整编辑的
问题区域1:
获取IdententationError第259行就是这个
randomNumber = random.randint(0,20)
从这里下来4行
#a list of resources
WEATHER = [CLOUD,RAIN,THUNDER,TORNADO]
#loop through each weather type and choose weather type for it to be
#pick a random number between 0 and 20
randomNumber = random.randint(0,20)
#if a zero, then the weather is TORNADO
if randomNumber == 0:
WEATHER = TORNADO
#water if the random number is a 1 or a 2
elif randomNumber in [1,2]:
WEATHER = THUNDER
elif randomNumber in [3,4,5,6,7,8]:
WEATHER = RAIN
else:
WEATHER = CLOUD
问题区域2:
#display the weather
DISPLAYSURF.blit(textures[WEATHER].convert_alpha(),(weatherx,weathery))
#move the cloud to the left slightly
weatherx+=1
#if the weather has moved past the map
if weatherx > MAPWIDTH*TILESIZE:
#pick a new position to place the weather
weathery = random.randint(0,(MAPHEIGHT*TILESIZE) - 150)
weatherx = -200
以下是他尝试添加天气之前的原始云系统
#commented out the original cloud only weather system
#display the cloud
DISPLAYSURF.blit(textures[CLOUD].convert_alpha(),(cloudx,cloudy))
#move the cloud to the left slightly
cloudx+=1
if the cloud has moved past the map
if cloudx > MAPWIDTH*TILESIZE:
#pick a new position to place the cloud
cloudy = random.randint(0,(MAPHEIGHT*TILESIZE) - 150)
cloudx = -200
守则的其他部分
但不是下面的完整代码我们认为是正确的
我在pygame中导入并随机设置时钟
import pygame, sys, random
from pygame.locals import *
fpsClock = pygame.time.Clock()
我加载了天气关闭屏幕
#weather position
weatherx = -200
weathery = 0
天气类型的选择,我可以在雪下等地方添加更多信息
#the number of each weather type that we have
weather = {
CLOUD : 0,
RAIN : 0,
THUNDER : 0,
TORNADO : 0
}
这是我添加纹理或png图像的方法,我遗漏了许多其他图像
DIRT : pygame.image.load('C:\\Users\\Daddy\\Documents\\python\\working_34\\mincraft2d\\dirt.png'),
有些时候为了让它工作,我必须显示如上所述的完整路径
#a dictionary linking resources to textures
textures = {
DIRT : pygame.image.load('dirt.png'),
GRASS : pygame.image.load('grass.png'),
....
CLOUD : pygame.image.load('cloud.png'),
RAIN : pygame.image.load('rain.png'),
THUNDER : pygame.image.load('thunder.png'),
TORNADO : pygame.image.load('tornado.png')
}
我在第261行收到错误,如果randomNumber == 0,则以开头:
#pick a random number between 0 and 20
randomNumber = random.randint(0,20)
#if a zero, then the weather is TORNADO
if randomNumber == 0:
WEATHER = TORNADO
#water if the random number is a 1 or a 2
elif randomNumber in [1,2]:
WEATHER = THUNDER
elif randomNumber in [3,4,5,6,7,8]:
WEATHER = RAIN
else:
WEATHER = CLOUD
答案 0 :(得分:1)
以下是代码对我来说的样子..(对于第一个问题,第259行)。
#a list of resources
WEATHER = [CLOUD,RAIN,THUNDER,TORNADO] # <-- problem here.
#loop through each weather type and choose weather type for it to be
#pick a random number between 0 and 20
randomNumber = random.randint(0,20)
#if a zero, then the weather is TORNADO
if yada yada ...
你需要缩小从WEATHER开始的行或者从开始randomNumber的行开始。缩进在python中用于代码块。 WEATHER和randomNumber行之间没有任何内容(如if语句或while语句),这意味着randomNumber行应该相对于WEATHER行缩进。与你得到的所有其他缩进问题相同。在randomNumber分配之后的if行有类似的问题。除非有理由不(例如if语句的主体,而不是if语句本身),否则使用相同的缩进深度获取所有行。
要清楚..它应该是什么样的(所有缩进的4个空格):
#a list of resources
WEATHER = [CLOUD,RAIN,THUNDER,TORNADO]
#loop through each weather type and choose weather type for it to be
#pick a random number between 0 and 20
randomNumber = random.randint(0,20)
#if a zero, then the weather is TORNADO
if yada yada ...
这将为您提供一些背景http://www.python-course.eu/python3_blocks.php同时选择一个不错的编辑器通常可以帮助您找到这些错误。这是一个列表http://pedrokroger.net/choosing-best-python-ide/
也很好,你花时间和你的孩子一起做这件事。