14 Replies - 977 Views - Last Post: 12 July 2012 - 01:59 AM Rate Topic: -----

#1 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Problem with method, function inside class

Posted 08 July 2012 - 12:56 AM

Ok so I been thinking thinking and trying and trying to fix this piece of code I put together while trying to teach myself some Phyton however I seem to have very much trouble with a function of mine.

The program is a game I'm making in PYGAME, I want to make it so you play a character (a cat) and run around while a Zombie will be chasing you and if you go to the middle of the screen there'll be a circle there which will protect you because the zombie will be unable to walk into that circle.
If the Zombie somehow catches you, I want it to prompt a message saying YOU LOSE.
Also, this is the thing I'm having problem with right now.. I want it so that when the user press SPACE I want the character (the cat) to double up in size.

here is my code:

bif="bg.jpg"
mif="cat.png"
wif="Zombie.png"

import pygame, sys
from pygame.locals import *

class FUNCTIONS:
    #def __init__(self):
        #a, b = self.mouse_c.size
        #return a*2, b*2
    
    def RESIZE(self, OBJ):
        #global a, b
        #a, b = mouse_c.size
        #a*2, b*2
        #OBJ = pygame.transform.scale(OBJ, (a,B)/>)
        #return self.OBJ
        self.OBJ = pygame.transform.scale(self.OBJ, (64,64))
        return self.OBJ

pygame.init()
screen=pygame.display.set_mode((700,525),0,32)




background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha() # This is the character
zombie=pygame.image.load(wif).convert_alpha() # The zombie/enemy

# This is the characters "attributes"
x, y = 350, 262
movex, movey = 0, 0
speed = 250
sizex, sizey = 0, 0
a, b = 0, 0

# This is the zombies "attributes"
x2, y2 = 0, 0
movex2, movey2 = 0, 0
speed2 = 100

# Clock and using the function
clock=pygame.time.Clock()
Thing=FUNCTIONS()
Funct=Thing.RESIZE(mouse_c)


#Making a circle for safe area
color=(135,17,70)
position=(350, 262)
radius=(120)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN: # Movements/keys
            if event.key == K_LEFT:
                movex-=dm
            elif event.key == K_RIGHT:
                movex+=dm
            elif event.key == K_UP:
                movey-=dm
            elif event.key == K_DOWN:
                movey+=dm
            elif event.key == K_SPACE: # Scale the character 2x
                Funct()
        if event.type == KEYUP:
            if event.key == K_LEFT:
                movex=0
            elif event.key == K_RIGHT:
                movex=0
            elif event.key == K_UP:
                movey=0
            elif event.key == K_DOWN:
                movey=0
    
    if x >= 0 and x <= 700: # prevent walking outside screen
        x += movex
    else:
        x = 350
        
    if y >= 0 and y <= 525: # prevent walking outside screen
        y += movey
    else:
        y = 263

    #Drawing the circle
    screen.lock()
    pygame.draw.circle(screen, color, position, radius)
    #pygame.draw.rect(screen, color, 
    screen.unlock()

    # Setting the character/zombie speed
    milli=clock.tick()
    seconds=milli/1000.0
    dm=seconds*speed        
    dm2=seconds*speed2

    # Displaying them
    screen.blit(background, (0,0))
    screen.blit(mouse_c, (x,y))
    screen.blit(zombie, (x2,y2))

    
    pygame.display.update()




If you have time, you could help me how to make the Zombie chase the character as well and also how to make the Zombie unable to enter the circle because I know I'll be having trouble with those two.

The actual issue I have here is that I want to rescale a picture through the function inside my class and I want the size to double up in size every time I do it.
As you can see in the code I been trying to make it scale up to 64,64 pixels first because I thought it'd be easier to do one step at a time but even this does not seem to work.

Error message:
"Traceback (most recent call last):
File "C:\Python27\Cat game v2.2.py", line 47, in <module>
Funct=Thing.RESIZE(mouse_c)
File "C:\Python27\Cat game v2.2.py", line 19, in RESIZE
self.OBJ = pygame.transform.scale(self.OBJ, (64,64))
AttributeError: FUNCTIONS instance has no attribute 'OBJ'
"

Thanks.
Regards, Marcus.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with method, function inside class

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 08 July 2012 - 02:52 AM

On line 19 you're using self.OBJ as an argument to the scale function. Since you never set self.OBJ to any value before that line executes, self.OBJ doesn't exist and you get the error you do.

Since I know absolutely nothing about what that code is trying to achieve and why you use self.OBJ instead of OBJ, I can't tell you how to fix it so that it does what you want.

The fact that you have a parameter called OBJ makes me think that maybe you meant to use that instead of self.OBJ, but apparently you already did that and commented it out, so it must not have done what you want (again I don't know what that is, so I can't be more helpful).
Was This Post Helpful? 0
  • +
  • -

#3 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 08 July 2012 - 05:15 AM

Indeed I did try without the self parameter but it did not work at all.
What I want to do is simple, I have an image and I want to scale that image 2x as big.. in other words, I want to transform the image/picture making it twice as big whenever I press SPACE.
And I want this through a function inside a class.

I don't understand why it won't work. I do know how to do other functions inside class but this one somehow doesn't seem to work.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 08 July 2012 - 05:40 AM

"Didn't work at all" is not a very clear error description.

And yes, I get that you want to zoom in when the user presses space. What I meant is that I don't know what you want your method to do exactly. If you just want the method to return the resized image (which would be quite sensible), then the version without self. will work perfectly fine for that.

However your code that uses RESIZE seems to expect that RESIZE returns a function. So obviously it won't work. I have no idea why you want RESIZE to return a function or what you want the function returned by RESIZE to do. So that's why I can't help out with that part.

Your best bet would probably to drop the whole trying-to-use-the-return-value-as-a-function thing and use the return value as an image, which it is.
Was This Post Helpful? 0
  • +
  • -

#5 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 08 July 2012 - 06:18 AM

Ok, so can you type how you would do it that way?
I think I tried it that way as well but it gave an error when I did but perhaps I missed something.

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 08 July 2012 - 06:31 AM

def scale_to_64x64(image):
    image = pygame.transform.scale(image, (64,64))
    return image



Or just:

def scale_to_64x64(image):
    return pygame.transform.scale(image, (64, 64))



Note that I defined it as free function outside of any class because in your code, you're not actually using any object oriented features anyway. I also renamed the function and its parameter because using ALL_CAPS variable and function names is really bad style.

And again be advised that the above function will return an image, not a function, so take that into account when using its result.
Was This Post Helpful? 0
  • +
  • -

#7 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 08 July 2012 - 06:36 AM

I see.. Ok thank you.
I will continue trying to do it with a class anyway though so if you know how to do it that way too please let me know how because I'm doing this program 99% to learn xD
I might got an idea of what I did wrong before so I'll try that tomorrow cause I'm already in bed now replying from my iPhone.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 08 July 2012 - 06:41 AM

Just put it inside a class and make "image" the second argument inside of the first. Like what you already had. There is very little point in doing that though, unless you actually use the first argument for something.

One approach that would make sense would be to create a Resizer class that takes the width and height as arguments to __init__ and stores them as instance variables (i.e. self.width and self.height). Then your scale method could use those instance variables as arguments to pygame.transform.scale.
Was This Post Helpful? 0
  • +
  • -

#9 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 10 July 2012 - 08:31 PM

Ok, I done it like this now but I still recieve error.
Code:
bif="bg.jpg"
mif="cat.png"
wif="Zombie.png"

import pygame, sys
from pygame.locals import *

def scale_img(image):
    #global a, b
    #a, b = mouse_c.size
    #a*2, b*2
    #OBJ = pygame.transform.scale(OBJ, (a,B)/>)
    #return self.OBJ
    image = pygame.transform.scale(image, (64,64))
    return image

pygame.init()
screen=pygame.display.set_mode((700,525),0,32)




background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha() # This is the character
zombie=pygame.image.load(wif).convert_alpha() # The zombie/enemy

# This is the characters "attributes"
x, y = 350, 262
movex, movey = 0, 0
speed = 250
sizex, sizey = 0, 0
a, b = 0, 0

# This is the zombies "attributes"
x2, y2 = 0, 0
movex2, movey2 = 0, 0
speed2 = 100

# Clock and using the function
clock=pygame.time.Clock()
Funct=scale_img(mouse_c)


#Making a circle for safe area
color=(135,17,70)
position=(350, 262)
radius=(120)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN: # Movements/keys
            if event.key == K_LEFT:
                movex-=dm
            elif event.key == K_RIGHT:
                movex+=dm
            elif event.key == K_UP:
                movey-=dm
            elif event.key == K_DOWN:
                movey+=dm
            elif event.key == K_SPACE: # Scale the character 2x
                Funct()
        if event.type == KEYUP:
            if event.key == K_LEFT:
                movex=0
            elif event.key == K_RIGHT:
                movex=0
            elif event.key == K_UP:
                movey=0
            elif event.key == K_DOWN:
                movey=0
    
    if x >= 0 and x <= 700: # prevent walking outside screen
        x += movex
    else:
        x = 350
        
    if y >= 0 and y <= 525: # prevent walking outside screen
        y += movey
    else:
        y = 263

    #Drawing the circle
    screen.lock()
    pygame.draw.circle(screen, color, position, radius)
    #pygame.draw.rect(screen, color, 
    screen.unlock()

    # Setting the character/zombie speed
    milli=clock.tick()
    seconds=milli/1000.0
    dm=seconds*speed        
    dm2=seconds*speed2

    # Displaying them
    screen.blit(background, (0,0))
    screen.blit(mouse_c, (x,y))
    screen.blit(zombie, (x2,y2))

    
    pygame.display.update()





I get this error:

Quote

Traceback (most recent call last):
File "C:\Python27\Cat game v2.2.py", line 64, in <module>
Funct()
TypeError: 'pygame.Surface' object is not callable

Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 11 July 2012 - 06:43 AM

View Postsepp2k, on 08 July 2012 - 02:40 PM, said:

However your code that uses RESIZE seems to expect that RESIZE returns a function. So obviously it won't work. I have no idea why you want RESIZE to return a function or what you want the function returned by RESIZE to do. So that's why I can't help out with that part.


View Postsepp2k, on 08 July 2012 - 03:31 PM, said:

And again be advised that the above function will return an image, not a function, so take that into account when using its result.

Was This Post Helpful? 0
  • +
  • -

#11 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 11 July 2012 - 07:22 AM

Yea I know it returns an image, that's why I don't know why it won't work.
Cause later in the loop I blog he image right... Shouldn't that automatically update the image to the resized one?
And even if it doesn't, should I really get an error for it?

How can I then fix it?

I know how to do it without a function, then I just put the code itself at the keypress.

Ohhhh I just realized I think I didn't add the key_press_down .. Does that matter?
I know at least better for me to add it, but can it be the cause of the error?
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 11 July 2012 - 07:28 AM

On line 41 you're setting Funct to the result of calling scale_img. As I said (and as you claim you know), the result of calling scale_img is an image. That means Funct is an image (even though its name implies that it's a function).

On line 64 you're doing Funct(). () means "call this function with no arguments". As I said a thousand times Funct is not a function, so that line simply makes no sense whatsoever. And that's why you get the error message you do.

This post has been edited by sepp2k: 11 July 2012 - 07:28 AM

Was This Post Helpful? 1
  • +
  • -

#13 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 11 July 2012 - 06:55 PM

Damn I learnt a lot by this!
Now I got two more issues to fix...
I want to learn how to add music, I tried different ways but it doesn't work.
This method I used now doesn't work,it keeps giving a "dut dut dut dut dut dut" sound however if I were to get an error it would start playing the music.
What could be wrong?

The second thing I want to do, is to make the zombie chase the cat.

bif="bg.jpg"
mif="cat.png"
wif="Zombie.png"

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

class Class:
    # Represents the new img size after scaling it
    a, b = 42, 42
   
    def scale_img(self, image, a, B)/>:
        self.image = pygame.transform.scale(image, (a,B)/>)
        Class.a += 10 # increasing size by 10
        Class.b += 10 # increasing size by 10
        return self.image

pygame.init()
screen=pygame.display.set_mode((700,525),0,32)




background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha() # This is the character
zombie=pygame.image.load(wif).convert_alpha() # The zombie/enemy

# This is the characters "attributes"
x, y = 350, 262
movex, movey = 0, 0
speed = 250
sizex, sizey = 0, 0


# This is the zombies "attributes"
x2, y2 = 0, 0
movex2, movey2 = 0, 0
speed2 = 100

# Clock and using the function
clock=pygame.time.Clock()
thing = Class()

#Making a circle for safe area
color=(135,17,70)
position=(350, 262)
radius=(120)


#music=pygame.mixer.Sound("scooby.wav")
pygame.mixer.music.load("scooby.wav") 
pygame.mixer.music.set_volume(.5) # value 0.1-1.0 only



while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN: # Movements/keys
            if event.key == K_LEFT:
                movex-=dm
            elif event.key == K_RIGHT:
                movex+=dm
            elif event.key == K_UP:
                movey-=dm
            elif event.key == K_DOWN:
                movey+=dm
            elif event.key == K_SPACE: # Scale the character 2x
                mouse_c = thing.scale_img(mouse_c, Class.a, Class.B)/>
        if event.type == KEYUP:
            if event.key == K_LEFT:
                movex=0
            elif event.key == K_RIGHT:
                movex=0
            elif event.key == K_UP:
                movey=0
            elif event.key == K_DOWN:
                movey=0
    
    if x >= 0 and x <= 700: # prevent walking outside screen
        x += movex
    else:
        x = 350
        
    if y >= 0 and y <= 525: # prevent walking outside screen
        y += movey
    else:
        y = 263

    #Drawing the circle
    screen.lock()
    pygame.draw.circle(screen, color, position, radius)
    #pygame.draw.rect(screen, color, 
    screen.unlock()

    pygame.mixer.music.play()
    #music.play()

    
    # Setting the character/zombie speed
    milli=clock.tick()
    seconds=milli/1000.0
    dm=seconds*speed        
    dm2=seconds*speed2

    # Displaying them
    screen.blit(background, (0,0))
    screen.blit(mouse_c, (x,y))
    screen.blit(zombie, (x2,y2))

    
    pygame.display.update()




Try the game..
press SPACE to "grow" xD
Sad the it becomes very pixelated when its bigger

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: Problem with method, function inside class

Posted 11 July 2012 - 07:19 PM

You're calling the play method inside the loop. This means that the play method will be called over and over again as long as the loop runs. Every time the play method is called, the music starts from the beginning. So that's why you get the dut-dut-dut sound: The first sound of the song is played over and over. That's also why it starts working when an error occurs: the loop is no longer running, so the song can play without being rewound to the beginning all the time.

To fix this, you should move the call to play before the loop.
Was This Post Helpful? 0
  • +
  • -

#15 s1mple  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 06-July 12

Re: Problem with method, function inside class

Posted 12 July 2012 - 01:59 AM

I'm embarrassed with myself for that one, I don't know what went through my head, somehow I got the idea it had to be in the loop otherwise it would just start and then not continue playing lol ;P

Any idea how to do the zombie one?
I thought some how I could do like ..
If cat x > zombie x then movex
Or something like that but doesn't seem good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1