5 Replies - 4915 Views - Last Post: 07 September 2011 - 06:47 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

AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 05:38 AM

Still working on snow animation assignment. I had my main module working until I tried to incorporate my snowflake class into the main module. I'm now getting AttributeError. I make changes then the type error is still AttributeError but with different conditions. Here is the code for my main and the code for my snowflake. I've also attached the zip file for my project.
'''
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

def initialize_snowflake():
    ''' 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)):
        Snowflake.draw(window, Snowflake.color, Snowflake.position, Snowflake.radius)
        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):
            Snowflake.draw(window, Snowflake.color, Snowflake.position, Snowflake.radius)
            pygame.display.flip()
            pygame.event.clear()
            clock.tick(60)
           
def main(sysargs=None):
    pygame.init()
    snowflake = initialize_snowflake()
    window = initialize_gui()
    
    run(snowflake, window)
     
    pygame.quit()

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




snowflake
'''
Created on Sep 5, 2011

@author: 
'''

import pygame
import random



class Snowflake(object):
    '''
    Represents 1 snowflake in the snow animation
    '''
    
    color = (255, 255, 255)
    radius = 2

    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 '''
        x = (random.randrange(0,400))
        y = (random.randrange(0,400))
        
        self.__positon = (x,y)
        
    @color.setter
    def color(self, value):
        
        self.__color = value
        
    def draw(self,window,color,radius):
        
        pygame.draw.circle(window,color,self.position,radius)
        
    def move(self, pixels):
        
        self.pixels =+ 1
        
        
        if Snowflake[_][1] > 400:
            y = random.randrange(-50,-10)
            Snowflake[_][1] = y
            x = random.randrange(0,400)
            Snowflake[_][0] = x
        

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: AttributeError: 'pygame.Surface' object has no attribute '

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 06:09 AM

Please post the full error message so we can properly debug your code.
Was This Post Helpful? 0
  • +
  • -

#3 atticusmom  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 06:16 AM

Traceback (most recent call last):
File "C:\Users\Owner\My Documents\Aptana Studio 3 Workspace\snow\src\snowanimation.py", line 77, in <module>
sys.exit(main())
File "C:\Users\Owner\My Documents\Aptana Studio 3 Workspace\snow\src\snowanimation.py", line 71, in main
run(snowflake, window)
File "C:\Users\Owner\My Documents\Aptana Studio 3 Workspace\snow\src\snowanimation.py", line 61, in run
Snowflake.draw(window, Snowflake.color, Snowflake.position, Snowflake.radius)
File "C:\Users\Owner\My Documents\Aptana Studio 3 Workspace\snow\src\snowflake.py", line 52, in draw
pygame.draw.circle(window,color,self.position,radius)
AttributeError: 'pygame.Surface' object has no attribute 'position'
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: AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 06:22 AM

I can see a few problems, I'll point a few out and see where that goes:

First: I get an error, as expected, when calling the draw() function, you have to pass an instance of the class to the function, not the class itself. I'll explain:

class MyClass(object):
     def __init__(self, var):
          self.varible = var

#Outside the class
def printVariable(instance):
     print instance.variable

>>> x = MyClass(123)
>>> x
<__main__.MyClass object at 0xb77c360c>
>>> printVariable(x)
123
>>> printVariable(MyClass)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in printVariable
AttributeError: type object 'MyClass' has no attribute 'variable'



Your display_snowflakes function is calling the class name and not an instance of the class:

def display_snowflake(snowflake,window):
    '''displays the snowflakes in the window'''
    
    window.fill(BACKGROUND_COLOR)
    for _ in range(len(snowflake)):
        Snowflake.draw(window, Snowflake.color, Snowflake.position, Snowflake.radius)
        snowflake[_][1] += 1
        if snowflake[_][1] > 400:
            y = random.randrange(-50,-10)
            snowflake[_][1] = y
            x = random.randrange(0,400)
            snowflake[_][0] = x  



You pass in a list (presumably of snowflakes, I'll get to that) but get the length of snowflake which is a list not in the same function.

Second thing I notice, initialize_snowflake() doesn't create a list of snowflakes, which you probably want, it produces a list of numbers. I'll use my example class again:

class MyClass(object):
     def __init__(self, var):
          self.varible = var
#Wrong
>>> myList = [i for i in range(5)]
# A list of numbers
[0, 1, 2, 3, 4]
#Right
myList = [MyClass(i) for i in range(5)]
#A list of class instances
[<__main__.MyClass object at 0xb77c350c>, <__main__.MyClass object at 0xb77c364c>, <__main__.MyClass object at 0xb77c362c>, <__main__.MyClass object at 0xb77c36ac>, <__main__.MyClass object at 0xb77c36cc>]



You don't even mention the class name Snowflake in that function, it won't create Snowflakes.

Finally, you have two unconditional returns in that function:

return snowflake
return windy



It will return the first one under all circumstances, the second return statement is unreachable in your code. If you want to return both at the same time (in a tuple):

return snowflake, windy


There are a few more errors but that should be enough to keep you going for a while, just think of classes as templates and instances as physical representations of those templates, you don't want to be passing the Snowflake class around.

This post has been edited by Simown: 07 September 2011 - 06:24 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: AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 06:41 AM

thank you. Yes, I will still plug along. I'm 43 and don't think of myself as one to quit a task, but learning this in 2 weeks seems a much for my old brain. Thanks for all you do. This will help me understand more than the one line code we get from class.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

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

Re: AttributeError: 'pygame.Surface' object has no attribute '

Posted 07 September 2011 - 06:47 AM

No problem, try and fix the things I mentioned and then, any other errors will probably become easier to understand and you can fix them too :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1