7 Replies - 809 Views - Last Post: 07 September 2011 - 06:13 AM Rate Topic: -----

#1 dmorgan7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 11

calling a function in python

Posted 06 September 2011 - 06:35 PM

okay i have this homework i'm working with for a week and i'm moving at snail's pace. i'm an extreme novice as it pertains to python so i'll try to be as descriptive as possible. in the code below i'm trying to call the drawThis function
'''
Created on Sep 5, 2011
Creates an animation of snowflakes falling against a night sky.
@author: dmorgan7
'''

import pygame
import random
from Snowflake import Snowflake

pygame.init()
bg_col = [0, 0, 0]
s_col = [255, 255, 255]
dim = [400, 400]
win = pygame.display.set_mode(dim)
pygame.display.set_caption("Snow Animation")
sfs = []
for i in range(60):
    x = Snowflake.xValue
    y = Snowflake.yValue
    sfs.append([x,y])
c = pygame.time.Clock()
d = False
while d == False:
    for e in pygame.event.get(): 
        if e.type == pygame.QUIT: 
            d = True 
    win.fill(bg_col)
    for i in range(len(sfs)):
        Snowflake.drawThis(win,s_col,sfs[i],2)
        sfs[i][1] += 1
        if sfs[i][1] > 400:
            y = random.randrange(-50,-10)
            sfs[i][1] = y
            x = random.randrange(0,400)
            sfs[i][0] = x
    pygame.display.flip()
    c.tick(60)
pygame.quit()


the drawThis function is located in the Snowflake class

'''
Created on Sep 5, 2011

@author: dmorgan7
'''
import pygame
import random


class Snowflake:
    '''
    represents 1 snowflake
    '''


    xValue = random.randrange(0,400)
    yValue = random.randrange(0,400)
    
    def __init__(self, initial_color):
        '''
        Creates a Snowflake with the specified color
        '''
        
        self.__color = initial_color
        
        def move(number):
            number += 1
        @property
        
        def color(self):
            '''Returns the color of the Snowflake '''
            return self.__color
        
        def drawThis(win, color, list, radius):
            pygame.draw.circle(win, color, list, radius)

i get this error
AttributeError: type object 'Snowflake' has no attribute 'drawThis'
any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: calling a function in python

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: calling a function in python

Posted 06 September 2011 - 07:17 PM

It's not that the class doesn't have the function, it's your indentation - which Python is very picky about.

Your code is:
class Snowflake:
    '''
    represents 1 snowflake
    '''

    xValue = random.randrange(0,400)
    yValue = random.randrange(0,400)
    
    def __init__(self, initial_color):
        '''
        Creates a Snowflake with the specified color
        '''
        
        self.__color = initial_color
        
        def move(number):
            number += 1
        @property
        
        def color(self):
            '''Returns the color of the Snowflake '''
            return self.__color
        
        def drawThis(win, color, list, radius):
            pygame.draw.circle(win, color, list, radius)



Where it should be:
class Snowflake:
    '''
    represents 1 snowflake
    '''

    xValue = random.randrange(0,400)
    yValue = random.randrange(0,400)
    
    def __init__(self, initial_color):
        '''
        Creates a Snowflake with the specified color
        '''
        
        self.__color = initial_color
        
    def move(number):
         number += 1
     @property
        
    def color(self):
      '''Returns the color of the Snowflake '''
       return self.__color
        
    def drawThis(win, color, list, radius):
        pygame.draw.circle(win, color, list, radius)



All the class functions are lined up and not indented further than __init__. I didn't change any of the code, at all.

Does changing that fix it? :)

This post has been edited by Simown: 06 September 2011 - 07:20 PM

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: calling a function in python

Posted 07 September 2011 - 04:41 AM

I'm getting the same error. I made the above changes in my snowflake class and still get the attribute error. :( I'm working on the same thing but my snow animation code is broken down into sections from the first two assignments.
'''
Created on Sep 5, 2011

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

import pygame
import random
import sys

from snowflake import Snowflake
from windy import Windy

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")
clock = pygame.time.Clock()
done = False
self = Snowflake
def initialize_snowflakes():
    ''' creates a series of snowflakes'''
    
    
    snowflake = [[random.randrange(0,400), random.randrange(0,400)]for _ in range(60)]
    windy = [[random.randrange(0,400), random.randrange(0,400)]for _ in range(60)]
    return snowflake
    return windy

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_snowflake(snowflake,window):
    '''displays the snowflakes in the window'''
    
    window.fill(BACKGROUND_COLOR)
    for _ in range(len(snowflake)):
        self.Snowflake.draw(window,self.color,self.position,6)
        snowflake[_][1] += 1
        if snowflake[_][1] > 400:
            y = random.randrange(-50,-10)
            snowflake[_][1] = y
            x = random.randrange(0,400)
            snowflake[_][0] = x  
             

def run(snowflakes, window):
    '''executes the animation loop to create a window and let the snow being'''
    
    while not pygame.event.peek(pygame.QUIT):
        self.Snowflake.draw(window,self.color,self.position,6)
        pygame.display.flip()
        pygame.event.clear()
        clock.tick(60)
           
def main(sysargs=None):
    pygame.init()
    snowflakes = initialize_snowflakes()
    window = initialize_gui()
    
    run(snowflakes, window)
     
    pygame.quit()

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


Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: calling a function in python

Posted 07 September 2011 - 04:46 AM

I don't think the solution is identical for your problem. Can you post the exact error message you are getting like dmorgan7 i.e.: "AttributeError: type object 'Snowflake' has no attribute 'drawThis'" - is that all the code you have?

It may be a better idea to start a new topic instead of hijacking this one.

This post has been edited by Simown: 07 September 2011 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 atticusmom  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: calling a function in python

Posted 07 September 2011 - 05:26 AM

I actually used the def draw not drawThis. It does seem that we are working on the same assignment. Didn't mean to hijack. As I posted before several times but the no attribute error is what I was also getting. I will start another topic.
Was This Post Helpful? 0
  • +
  • -

#6 dmorgan7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 11

Re: calling a function in python

Posted 07 September 2011 - 05:58 AM

it works, thanks guys but now it only draws one snowflake which doesn't even fall
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: calling a function in python

Posted 07 September 2011 - 06:06 AM

Sure, I didn't mean to complain about it, it's just easier to separate answers to different people in different topics. Can we help dmorgan7?
Was This Post Helpful? 0
  • +
  • -

#8 atticusmom  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: calling a function in python

Posted 07 September 2011 - 06:13 AM

we seem to be working on the same assignment. My code is now posted under my new topic http://www.dreaminco...ibute-position/

I have this within my snowflake class


def move(self, pixels):
55	         
56	        self.pixels =+ 1
57	         
58	         
59	        if Snowflake[_][1] > 400:
60	            y = random.randrange(-50,-10)
61	            Snowflake[_][1] = y
62	            x = random.randrange(0,400)
63	            Snowflake[_][0] = x

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1