【问题标题】:Python Pygame Random Imgs for Weather System用于天气系统的 Python Pygame 随机图像
【发布时间】: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


【解决方案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/

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

【讨论】:

  • 我认为问题在于“在 WEATHER 和 randomNumber 行之间没有任何内容(如 if 语句或 while 语句)”在具有类似函数的其余代码中是带有 for 的行在随机数生成之前。在这种情况下,它用于瓷砖。我想也许他忘了给天气随机编号。不知道从哪里开始。我也会为我们俩寻找更好的编辑器。
  • 从我的代码中可以看出,根本不需要那个天气线。首先,他将 WEATHER 设置为列表 [CLOUD,RAIN,THUNDER,TORNADO],然后直接进入 if/then/elif/else 语句,将 WEATHER 设置为 TORNADO 或取决于随机数的其他内容。所以你可以注释掉 WEATHER = [CLOUD,RAIN,THUNDER,TORNADO] 行
  • 就编辑而言.. 我列出的那些可能是为专业程序员准备的。请查看这篇文章,reddit.com/r/learnpython/comments/1e5tg9/… 我引用“我会推荐 Sublime Text 2。不是真的和 IDE,而不仅仅是文本编辑器。在其中写作,就像在裸体的 Kiera Knightly 上涂抹蜂蜜”听起来不错。
  • 我想我已经修复了格式,挖掘了 Sublime Text 2 上的配色方案,我注释掉了 WEATHER = [CLOUD,RAIN,THUNDER,TORNADO] 行,但刚刚发现 NameError: name ' RAIN' 在他列出所有图像或纹理 RAIN 的开头没有定义:pygame.image.load('rain.png'),
  • 没有定义。您的儿子通过外观将其添加到代码中,而不是对其进行定义。您需要在第 31 行添加它.. 类似 RAIN = 13
猜你喜欢
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
相关资源
最近更新 更多