简体   繁体   English

调用函数python(pygame)

[英]calling functions python (pygame)

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"))

this is what happens (if I get it right): 这是发生的情况(如果我做对了):

  • pass the result of name("donkey from shrek") to screen() [line 18] 将名称的结果(“来自史莱克的驴子”)传递到screen()[第18行]
    • name() gets called [line 18] name()被调用[第18行]
      • name: creates label [line 14] 名称:创建标签[第14行]
      • name: calls screen [line 15] 名称:通话画面[第15行]
      • screen: create new display and return it [line 6-9] 屏幕:创建新的显示并将其返回[第6-9行]
      • name: blit label to returned display [line 15] 名称:blit标签以返回显示内容[第15行]
      • return the "blitted" display [line 16] 返回“变白”显示[第16行]
  • the blitted display gets passed to screen() [line 18] 发白的显示传递到screen()[第18行]
    • screen: doesn't care about the display object in "name" [line 6-9] 屏幕:不关心“名称”中的显示对象[第6-9行]
    • screen: creates and returns a blank display [line 6-9] 屏幕:创建并返回空白显示[第6-9行]

Hope that helps ;) 希望能有所帮助;)

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 如果您想设置屏幕的标题,那么我想参考http://www.pygame.org/docs/ref/display.html#pygame.display.set_caption

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM