首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于天气系统的Python Py对策随机Imgs

用于天气系统的Python Py对策随机Imgs
EN

Stack Overflow用户
提问于 2015-07-05 20:26:12
回答 1查看 548关注 0票数 0

我10岁的儿子正试图在这个2d游戏中实现一个天气系统的新功能,他在游戏教程的基础上做了一个基础,但是遇到了一个IdententationError问题。

我一直在帮助他,但我仍然在学习,我认为这是随机和显示,但我不知道如何修复它,因为这是他的第一个方法之一的游戏。

他正试图让它产生随机天气来传送到屏幕上。他还想增加碰撞检测与球员的底线,但他仍然学习的主题。

这里是他的代码: 所有档案,我还包括了原始的、未被修改过的完整编辑的代码

问题领域1:

得到IdententationError第259行,这是哪一行?

代码语言:javascript
复制
randomNumber = random.randint(0,20)

从这里下来4条线

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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

下面是他尝试添加天气之前的原始云系统

代码语言:javascript
复制
#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

代码的其他部分

但我们认为下面的代码是正确的

我导入游戏和随机设置时钟。

代码语言:javascript
复制
import pygame, sys, random
from pygame.locals import *

fpsClock = pygame.time.Clock()

我把天气调到屏幕上

代码语言:javascript
复制
#weather position
weatherx = -200
weathery = 0

天气类型的选择,我可以增加更多的线,除雪等。

代码语言:javascript
复制
   #the number of each weather type that we have
weather =   {
                CLOUD    : 0,
                RAIN     : 0,
                THUNDER  : 0,
                TORNADO  : 0
                         }

这就是我如何添加纹理或png图像,我忽略了许多其他图像。

代码语言:javascript
复制
DIRT    : pygame.image.load('C:\\Users\\Daddy\\Documents\\python\\working_34\\mincraft2d\\dirt.png'),

有些时候,为了让它开始工作,我必须像上面那样显示出完整的路径。

代码语言:javascript
复制
    #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')
             }

I在第261行得到错误,该行以(如果randomNumber == 0)开头:

代码语言:javascript
复制
    #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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-06 02:27:48

这是代码在我看来的样子。(关于第一个问题,第259行)。

代码语言:javascript
复制
    #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 ...

您需要缩进开始天气的行,或者缩进启动randomNumber的行。缩进在python中用于代码块。天气和randomNumber行之间没有任何东西(比如if语句或while语句),这意味着相对于天气行,randomNumber行应该缩进。其他的缩进问题也是一样。randomNumber赋值之后的if行也有类似的问题。使所有行都具有相同的缩进深度,除非有理由不这样做(例如if语句的正文,而不是if语句本身)。

说清楚..。它应该是这样的(所有缩进的4个空格):

代码语言:javascript
复制
    #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 ...

这将为您提供一些背景信息,blocks.php还可以选择一个合适的编辑器来帮助您找到这些bug。这是一个列表http://pedrokroger.net/choosing-best-python-ide/

也很适合你花时间和你的孩子一起做这件事。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31234674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档