【发布时间】:2015-09-22 22:34:05
【问题描述】:
我 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')
}
如果 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
【问题讨论】:
-
请描述您遇到的问题是什么
-
第 261 行出现缩进错误,我将在上面添加
-
如果你得到一个 "IndentationError" 。 . .你试过检查缩进吗?
-
我确实尝试过移动它并对其进行测试,我认为问题不止于此
-
确保没有制表符,将代码中所有制表符替换为4个空格。
标签: python python-3.x random pygame