12 Replies - 1650 Views - Last Post: 06 July 2012 - 07:04 PM Rate Topic: -----

#1 s1mple  Icon User is offline

  • New D.I.C Head

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

Function takes exactly two arguments

Posted 06 July 2012 - 06:00 AM

Ok, I'm trying to make a simple game/program where you move a cat with the <^> and \/ buttons on your keyboard and then I'll have a Zombie that will chase the cat, once it catch the you (the cat) you will lose.

now my problem is I get the error saying like..
"Code takes excatly 2 arguments"

Also.. I drew a circle in the middle of the screen, the Zombie won't ever enter this area so basically this will be a safe spot for you (the cat).
I do not know how to make the zombie unable to go there nor how to make it chase you, I would know how to make it unable to go to the circle by controlling the cordinates but since it's a circle it'd take tons of cordinates unless I use derivata somehow but I don't remember how cause it's a long time ago I studied that in school.
please, help me someone.

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

import pygame, sys
from pygame.locals import *

class FUNCTIONS(object):
    #def __init__(self):
        #a, b = mouse_c.size
        #return a*2, b*2
        #a = mouse_c.getWidth()
        #return a*2
    
    #def __init__(self):
        #b = mouse_c.getHeight()
        #return b*2
    
    def RESIZE(self, ResizeOBJ):
        a, b = mouse_c.size
        a*2, b*2
        ResizeOBJ = pygame.transform.scale(ResizeOBJ, (a,B)/>)

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

# 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()


#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(mouse_c)
        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()








kind regards, Marcus

Is This A Good Question/Topic? 0
  • +

Replies To: Function takes exactly two arguments

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Function takes exactly two arguments

Posted 06 July 2012 - 06:12 AM

Please include the exact error message in your post, including (and this is important!) the line number on which the error occurs. Make it easy for people to help you.
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: Function takes exactly two arguments

Posted 06 July 2012 - 06:33 AM

View Postsepp2k, on 06 July 2012 - 06:12 AM, said:

Please include the exact error message in your post, including (and this is important!) the line number on which the error occurs. Make it easy for people to help you.

Ok, sorry for being unclear.

Here is the error:
"Traceback (most recent call last):
File "C:\Python27\Cat game v2.2.py", line 47, in <module>
Funct=Thing.RESIZE()
TypeError: RESIZE() takes exactly 2 arguments (1 given)"


I tried this just now:
I removed Funct=Thing.RESIZE() from line 47 and now I managed to start the program, however when I click SPACE button it's supposed to double the size of cat.jpg (variable: mif) using the function RESIZE though, when I press SPACE the program crash/freeze so I need to exit it.

This is how my code looks like now... barely any difference though:
bif="bg.jpg"
mif="cat.png"
wif="Zombie.png"

import pygame, sys
from pygame.locals import *

class FUNCTIONS(object):
    #def __init__(self):
        #a, b = mouse_c.size
        #return a*2, b*2
        #a = mouse_c.getWidth()
        #return a*2
    
    #def __init__(self):
        #b = mouse_c.getHeight()
        #return b*2
    
    def RESIZE(self, ResizeOBJ):
        a, b = mouse_c.size
        a*2, b*2
        ResizeOBJ = pygame.transform.scale(ResizeOBJ, (a,B)/>)

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

# 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()



#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
                Thing(mouse_c)
        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()






Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Function takes exactly two arguments

Posted 06 July 2012 - 06:42 AM

Your previous code didn't work because because you called RESIZE with only the self-argument, but you defined RESIZE to take two arguments. So a call to RESIZE should look like foo.RESIZE(bar), not just foo.RESIZE().

Your current code doesn't work because when the user presses spaces, you execute the line Thing(mouse_c). Since Thing is not a function (or an instance of a callable class), this does not work.
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: Function takes exactly two arguments

Posted 06 July 2012 - 06:42 AM

How come I can't edit my post?
Wanted some..

anyhow, now I did some small changes and here's the error I got now after the changes

"Traceback (most recent call last):
File "C:\Python27\Cat game v2.2.py", line 70, in <module>
Thing(mouse_c)
TypeError: 'FUNCTIONS' object is not callable"


here's the newest code after my newest attempt to fix it :P

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

import pygame, sys
from pygame.locals import *

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

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

# 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()



#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
                Thing(mouse_c)
        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()




Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Function takes exactly two arguments

Posted 06 July 2012 - 06:44 AM

See my previous post re: Thing not being a function or callable object.
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: Function takes exactly two arguments

Posted 06 July 2012 - 06:53 AM

Ok.. I don't quite get what you mean by "foo" or "bar".
I'm rather new to programming so I'm not familiar with most terms and such so please excuse my noobiness.

I got a question ..
since you say it's not callable. . How do I make it callable? I have the problem because I want it to take the values/arguments itself through the function, otherwise it would been easier.

So to call it I need something like I had before like this?
Funct=Thing.RESIZE()
and then later I call the Funct instead of "Thing".
Is that correct?

and by doing that I just do Funct() since I don't want any values/arguments only the ones in the function
    def RESIZE(self):
        a, b = mouse_c.size
        a*2, b*2
        mouse_c = pygame.transform.scale(mouse_c, (a,B)/>)

so basically I want to use the a, b (as in width and height) which I grab using the a, b = mouse_c.size ..
if you get what I mean?
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Function takes exactly two arguments

Posted 06 July 2012 - 07:11 AM

View Posts1mple, on 06 July 2012 - 03:53 PM, said:

Ok.. I don't quite get what you mean by "foo" or "bar".
I'm rather new to programming so I'm not familiar with most terms and such so please excuse my noobiness.


They're just nonsense words that don't mean anything. I could just as well have written x.RESIZE(y), bla.RESIZE(blobb) or chunky.RESIZE(bacon). The point is: if RESIZE is defined to take two arguments, there needs to be something between the parentheses.

Quote

since you say it's not callable. . How do I make it callable?


I think if I'd answer that, that'd just send you off in the wrong direction. I don't think making the object callable will get you where you want to go.

Quote

I have the problem because I want it to take the values/arguments itself through the function, otherwise it would been easier.


The point is you're not calling a function. There is no function involved when you write Thing(mouse_c) because Thing is not a function.

Quote

So to call it I need something like I had before like this?
Funct=Thing.RESIZE()
and then later I call the Funct instead of "Thing".
Is that correct?


No. If you want to call RESIZE on that line, you should write Thing.RESIZE(mouse_c).
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: Function takes exactly two arguments

Posted 06 July 2012 - 07:34 AM

Ok, I really can't figure out how to make this work.
Could you please give me an example by writing the code out for me?
I tried lots of different ways but I cannot seem to do it properly.
Was This Post Helpful? -1
  • +
  • -

#10 s1mple  Icon User is offline

  • New D.I.C Head

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

Re: Function takes exactly two arguments

Posted 06 July 2012 - 07:40 AM

Traceback (most recent call last):
File "C:\Python27\Cat game v2.2.py", line 40, in <module>
Thing=FUNCTIONS(a, B )
NameError: name 'a' is not defined

new code I wrote:
bif="bg.jpg"
mif="cat.png"
wif="Zombie.png"

import pygame, sys
from pygame.locals import *

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

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=FUNCTIONS(a, B)/>
Funct=Thing.RESIZE()


#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
                Thing.RESIZE(mouse_c, a, 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()

    # 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()








This is the last way I tried but it doesn't work either.
I don't know what to do now really, tried so many different ways.

This post has been edited by atraub: 06 July 2012 - 08:23 AM
Reason for edit:: removed smiley

Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Function takes exactly two arguments

Posted 06 July 2012 - 08:10 AM

Maybe you should start by writing more simple programs and learn the basics of functions and classes first. Because currently you're basically trying things at random and you don't really seem to get anywhere.
Was This Post Helpful? 1
  • +
  • -

#12 s1mple  Icon User is offline

  • New D.I.C Head

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

Re: Function takes exactly two arguments

Posted 06 July 2012 - 08:28 AM

View Postsepp2k, on 06 July 2012 - 08:10 AM, said:

Maybe you should start by writing more simple programs and learn the basics of functions and classes first. Because currently you're basically trying things at random and you don't really seem to get anywhere.

Well I find it easier to learn if I do something I enjoy and I don't really enjoy doing too easy programs.
I choose this one at a basic level then I improve it with ideas I come up with in time.

Like the zombie/scaling ideas.
Please, could you help me? It's easier for me to learn this way.
Was This Post Helpful? 0
  • +
  • -

#13 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: Function takes exactly two arguments

Posted 06 July 2012 - 07:04 PM

View Posts1mple, on 06 July 2012 - 10:28 AM, said:

View Postsepp2k, on 06 July 2012 - 08:10 AM, said:

Maybe you should start by writing more simple programs and learn the basics of functions and classes first. Because currently you're basically trying things at random and you don't really seem to get anywhere.

Well I find it easier to learn if I do something I enjoy and I don't really enjoy doing too easy programs.
I choose this one at a basic level then I improve it with ideas I come up with in time.

Like the zombie/scaling ideas.
Please, could you help me? It's easier for me to learn this way.


problem with that mindset though is you lose out on learning proper syntactical structuring that would aid you in understanding what exactly is going wrong and how to address the problem. However, I played with your code in couple ways as well, I can't seem to resolve the issue you are trying to address either. sepp2k is probably right, you need to start a little bit simpler or find some code someone else has done in a similar manner and try to absorb their process.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1