Python Pygame 天气系统的随机图像

Python Pygame Random Imgs for Weather System

我 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 行收到错误,该行以 if 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

我觉得代码是这样的..(第一个问题,第 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 Also choosing a decent editor will often help find these bugs for you. Here's a list http://pedrokroger.net/choosing-best-python-ide/

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