dimanche 5 juillet 2015

Python Pygame Random Imgs for Weather System

My 10 year old son is trying to implement new feature a weather system in this 2d game he made base off tutorial with pygame but is running into a issue.

I been helping him but I am still learning also, I think it is the random and the display but I don't know how to fix it since is one of his first approach to pygame.

He is try to have it generate random weather to send across the screen. He also wants to add collision detection with player down the line but he still learning that subject.

Here is his code:

Problem area 1:

#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

Problem area 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

Below is the original cloud only system before he tried to add weather

#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

Other Parts Of the Code

But not the full code below we believe to be correct

I import in pygame and the random and set clock

import pygame, sys, random
from pygame.locals import *

fpsClock = pygame.time.Clock()

I load in the weather off screen

#cloud position
cloudx = -200
cloudy = 0

The Choices of Weather Types which I can add more down the line ex snow, etc

   #the number of each weather type that we have
weather =   {
                CLOUD    : 0,
                RAIN     : 0,
                THUNDER  : 0,
                TORNADO  : 0
                         }

This is how I add the texture or png images I left out the many others

DIRT    : pygame.image.load('C:\\Users\\Daddy\\Documents\\python\\working_34\\mincraft2d\\dirt.png'),

Some times in order to get it to work I got to show full path like above

    #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')
             }




Aucun commentaire:

Enregistrer un commentaire