0

code:

import pygame, sys

x = 640
y = 480
screen = pygame.display.set_mode([x,y], pygame.RESIZABLE | pygame.OPENGL)

while True:
    x, y = pygame.Surface.get_size()
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
            pygame.quit()
            sys.exit()

error

Traceback (most recent call last):
  File "C:/Users/Mendel/My programs/game Project/test.py", line 8, in <module>
    x, y = pygame.Surface.get_size()
TypeError: descriptor 'get_size' of 'pygame.Surface' object needs an argument

When I look up the docs it says that i should have no error

4
  • Where do you look up the docs that say that? Commented Apr 19, 2013 at 18:56
  • Also, can you explain what your code is supposed to do? Because the more I read it, the less I understand what you were trying to accomplish, which makes it very hard to tell you how to accomplish it… Commented Apr 19, 2013 at 19:03
  • @abarnert Here is the link to the docs for that function: pygame.org/docs/ref/surface.html#pygame.Surface.get_size Commented Apr 19, 2013 at 19:27
  • @Haz: But that doesn't tell anyone else who knows how to read Python documentation, that his code should work. (It's not a classmethod, and it explicitly refers to "the Surface", which pretty strongly implies that there must be an instance of Surface in the self…) Commented Apr 19, 2013 at 19:38

3 Answers 3

2

You need to use screen object instead:

x, y = screen.get_size()

According to docs, it returns a Surface object

pygame.display.set_mode()
Initialize a window or screen for display
set_mode(resolution=(0,0), flags=0, depth=0) -> Surface
Sign up to request clarification or add additional context in comments.

2 Comments

screen is the result of calling pygame.display.set_mode(), which I'm pretty sure is not a Surface or anything else with a get_size method.
@abarnert see my updated answer, it returns a Surface object.
1

You can't call a regular instance method on a class, you have to call it on an instance of that class.*

So, how do you get an instance of a class? Well, in general, you either construct an instance by doing something like my_instance = MyClass(my_params), or you call some helper function that does it for you.

For specifics related to pygame… you really need to work through a pygame tutorial. A surface can be anything from the main "drawing canvas" for your screen or window to an image you loaded. It's not clear what surface you're trying to use here, so I can't tell you how to get one.

You might want to start with A Newbie Guide to pygame, and then go on to the official tutorials.


* This isn't quite true. You can call a method on a class instead of an instance, but then you have to pass an instance explicitly as the first (self) parameter, and you very rarely want to do this. At any rate, that's why you get the somewhat-misleading error message saying that it "needs an argument".

Comments

-1

Your syntax is wrong; you are calling get_size on Surface, not a variable.Your code should look like this:

variable = pygame.Surface(variable.get_size())

so you would call this on your screen variable. The way I formatted this was if your variable is a pygame variable but not Surface (just like your screen variable). To look at how you format the code slightly differently depending on your variable , please look at pygame's examples

3 Comments

I don't think he wants to construct a new Surface the size of his screen every time through the event loop. (Unfortunately, I have no idea what he does want to do… This was a valiant attempt at a guess, but I don't think it's right, and I don't think we have nearly enough information to have much chance of getting anything right.)
I do agree, we have no idea of knowing what he is using this for. I had a feeling that he scraped his code to give us where the error was, so I just gave him the correct syntax. On the topic of getting a new size every time through the loop, unless the size is supposed to change throughout the game, pulling this Surface out of the loop would be much more efficient
Well, it's the current syntax for something, but it's not the correct syntax for calling the get_size method on a Surface instance… which is a pretty common thing to do, and a much more reasonable thing to do in an event loop. And it seems likely to be closer to what he thought the docs told him he could do. Anyway, if he ever comes back, he can tell us what he intended; if not, we can close the question…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.