calling functions python (pygame)

2019-09-14 02:46发布

I am tying to have a load of functions where by when i call each function on the screen function, it displays stuff i want on the screen. Im having trouble with this simple program. I would like to display text on the screen when i write to it. but all its doing is displaying the screen.

import pygame
from pygame.locals import *

pygame.init()

def screen(width,height,name):
    screen = pygame.display.set_mode((600,600))
    screen=pygame.display.set_mode((width,height))
    return screen

def name(name=""):
    pygame.font.init()
    myfont = pygame.font.SysFont("monospace", 15)
    label = myfont.render("Some text!", 1, (255,255,0))
    result=screen(640,480,name).blit(label, (100, 100))
    return result

screen(640,480,name("donkey from shrek"))

标签: python pygame
2条回答
手持菜刀,她持情操
2楼-- · 2019-09-14 03:17

this is what happens (if I get it right):

  • pass the result of name("donkey from shrek") to screen() [line 18]
    • name() gets called [line 18]
      • name: creates label [line 14]
      • name: calls screen [line 15]
      • screen: create new display and return it [line 6-9]
      • name: blit label to returned display [line 15]
      • return the "blitted" display [line 16]
  • the blitted display gets passed to screen() [line 18]
    • screen: doesn't care about the display object in "name" [line 6-9]
    • screen: creates and returns a blank display [line 6-9]

Hope that helps ;)

查看更多
Deceive 欺骗
3楼-- · 2019-09-14 03:36

On first glance, i believe you are calling the screen function twice, and thus creating two screens.

The function "name" creates a screen with a label. This function is called in the last line of your code, before the "screen" function is called.

When eventually the "screen" function is called (again) in the last line, this will create a new screen, without label... .

Also the function "screen" does not utilize the argument "name" that is given to the function. If you want to set the caption of the screen, then i would like to refer to http://www.pygame.org/docs/ref/display.html#pygame.display.set_caption

查看更多
登录 后发表回答