我10岁的儿子正在尝试在这个2d游戏中实现一个新功能天气系统,他用pygame制作了一个教程,但是遇到了一个识别错误的问题。在
我一直在帮助他,但我仍然在学习,我认为这是随机和显示,但我不知道如何修复它,因为这是他第一次使用pygame的方法之一。在
他试图让它产生随机天气发送到屏幕上。他还想加入碰撞检测与玩家下线,但他仍然在学习这个主题。在
这是他的密码:all files我还包括原稿未经修改的完整版
问题1:
获取标识错误行259,这一行是什么
randomNumber = random.randint(0,20)
从这里往下走4行
^{pr2}$问题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行得到了一个错误,它以开头,如果随机数==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
下面是代码在我看来是什么样子的。。(对于第一个问题,第259行)。在
您需要从WEATHER开始缩进一行,或者从randomNumber开始的行下移。在python中,缩进用于代码块。在WEATHER和randomNumber行之间没有任何东西(比如if语句或while语句)意味着randomNumber行应该相对于天气线缩进。和你遇到的其他缩进问题一样。随机数赋值后的if行也有类似的问题。使用相同的缩进深度获取所有行,除非有理由不这样做(例如,if语句的主体,而不是if语句本身)。在
说清楚。。它应该是这样的(全部缩进4个空格):
^{pr2}$这将为您提供一些背景知识http://www.python-course.eu/python3_blocks.php另外,选择一个像样的编辑器通常可以帮助您找到这些bug。这是一个列表http://pedrokroger.net/choosing-best-python-ide/
你花时间和你的孩子一起做这件事也很好。在
相关问题 更多 >
编程相关推荐