3 Replies - 519 Views - Last Post: 09 September 2011 - 01:16 AM Rate Topic: -----

#1 atticusmom  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-February 11

creating a subclass changing only 50% of items from list of items

Posted 06 September 2011 - 06:05 AM

I'm still working on my snow animation. I've gotten the module to work but snow falls too fast, I believe I still have extra code somewhere. Need to Replace the condition of the while to use the return value of the appropriate call to the peek function from the pygame.event module. Then delete the declaration of the sentinel value and the loop and if statement that set the sentinel value. Here is the code I have for the snow animation module.
'''
Created on Sep 5, 2011

@author: Carolyn Leithner
'''
'''
Creates an animation of snowflakes falling against a night sky.
'''

import pygame
import random
import sys




BACKGROUND_COLOR = (0, 0, 0)
SNOWFLAKE_COLOR = (255, 255, 255)

WINDOW_DIMENSIONS = [400, 400]

window = pygame.display.set_mode(WINDOW_DIMENSIONS)
pygame.display.set_caption("Snow Animation")

def initialize_snowflakes():
    ''' creates a series of snowflakes'''
snowflakes = []
    
snowflakes = [[random.randrange(0,400), random.randrange(0,400)]for i in range(60)]

def initialize_gui():
    ''' creates a window and sets the title'''
    
    window = pygame.display.set_mode(WINDOW_DIMENSIONS)
    pygame.display.set_caption("Snow Animation")
    return window

def display_snowflakes():
    '''displays the snowflakes in the window'''
    
    window.fill(BACKGROUND_COLOR)
    
    for i in range (len(snowflakes)):
        pygame.draw.circle(window,SNOWFLAKE_COLOR,snowflakes[i],2)
        
   
        

clock = pygame.time.Clock()
done = False

def run(snowflakes, window):
    '''executes the animation loop to create a window and let the snow being'''
    
    display_snowflakes = snowflakes, window
    
    while not pygame.event.peek(pygame.QUIT):
        if pygame.event.peek(pygame.MOUSEBUTTONUP):
            display_snowflakes(snowflakes,window)
            pygame.event.clear()
            
            
while done == False:
    for e in pygame.event.get(): 
        if e.type == pygame.QUIT: 
            done = True 
    window.fill(BACKGROUND_COLOR)
    for i in range(len(snowflakes)):
        pygame.draw.circle(window,SNOWFLAKE_COLOR,snowflakes[i],2)
        snowflakes[i][1] += 1
        if snowflakes[i][1] > 400:
            y = random.randrange(-50,-10)
            snowflakes[i][1] = y
            x = random.randrange(0,400)
            snowflakes[i][0] = x
    pygame.display.flip()
    
def main(sysargs=None):
    pygame.init()
    snowflakes = initialize_snowflakes()
    window = initialize_gui()
    
    run(snowflakes, window)
    
    clock.tick(60)
pygame.quit()

if __name__=='__main__':
    sys.exit(main())



I've created my class snowflake that is to represent 1 and only 1 of the snowflakes that will be called when the animation runs. Here is the code for my snowflake class
'''
Created on Sep 5, 2011

@author: Carolyn Leithner
'''

import pygame
import random
from windy import Windy


class Snowflake(object):
    '''
    Represents 1 snowflake in the snow animation
    '''


    def __init__(self,_color,initial_position):
        '''
        creates a snowflake with the specified color, at a random position
        '''
        self.__color = _color
        self.__position = initial_position
    
    @property
    def position(self):
        '''returns the location of the snowflake'''
        
        return self.__position
    
    @property
    def color(self):
        return self.__color
    
    @position.setter
    def position(self, position):
        '''sets the position of the snowflake '''
        
        self.__positon = (random.randrange(0,400),random.randrange(0,400))
        
    @color.setter
    def color(self, value):
        
        self.__color = value
        
    def draw(self, window,snowflakes):
        
        pygame.draw.circle(window,self.color,snowflakes,self.position)
        



Now is my subclass creation. it is supposed to be a subclass of snowflake. I call it windy. This one is to make 50% of my snowflakes that are created in the snow animation module float up and to the left as if in the wind every few moves. I'm stuck on the issubclass function. Is this necessary or do I need to just do an import from my snowflake class. Our professor suggests using a list comprehensions in the animation module by using list slices.
'''
Created on Sep 6, 2011

@author: Owner
'''
import Snowflake

class Windy(Snowflake):
    '''
    Represents 1 wind blown snowflake in the snow animation
    '''


    def __init__(self):
        '''
        creates a snowflake with the specified color, at a random position
        '''
        


If it makes it easier I've attached the zip file for this. I'm just trying to find out if I'm even thinking on the right track and if not where do I need to look to make changes.

thanksAttached File  snow.zip (3.71K)
Number of downloads: 44

Is This A Good Question/Topic? 0
  • +

Replies To: creating a subclass changing only 50% of items from list of items

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: creating a subclass changing only 50% of items from list of items

Posted 06 September 2011 - 10:19 AM

This kind of solution will not work, as Python does not allow changing an object's class. If you used this method, you would end up with 50% of your snowflakes floating up permanently.

Perhaps a better solution would be to use a 50% random number generator to decide whether any particular snowflake will float up or down:

import random
...
    for i in range(len(snowflakes)):
        pygame.draw.circle(window,SNOWFLAKE_COLOR,snowflakes[i],2)
        if random.randint(0, 1) == 0:
            snowflakes[i][1] += 1
        else:
            snowflakes[i][1] -= 1
            snowflakes[i][0] -= 1



Additionally, you have some problems with scoping (indentation) in your code. You'll need to think through where each piece of code is happening, what its logical containing function is, and indent appropriately.

This post has been edited by Motoma: 06 September 2011 - 10:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 atticusmom  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-February 11

Re: creating a subclass changing only 50% of items from list of items

Posted 06 September 2011 - 11:23 AM

thanks for pointing that out. Last semester we used eclipse and I was used to the auto indent feature. I have not found this in aptana if there is one. I'm tempted to put my code there first so that I can auto indent it then copy it to aptana.

Unfortunately, my instructions are to have a module of snowanimation then a class for one snowflake then a subclass with a snowflake that moves with the wind. Then incorporate that into the snowanimation. Still plugging along.
Was This Post Helpful? 1
  • +
  • -

#4 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: creating a subclass changing only 50% of items from list of items

Posted 09 September 2011 - 01:16 AM

So 50% of the snowflakes are of the subclass? Just apply the random statement with an if statement when you instantiate it. Something like:

import random
random.seed()

x = randint(0,1)
if x == 1:
    SF = Snowflake()
else:
    SF = Windy()


I'm not sure how you're naming the instances (I've picked SF), but it should mean 50% of objects are Snowflakes, and 50% are Windy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1