Pygame:<Surface:(Dead Display)>和Python的“with语句”

2024-10-02 02:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我正在用Pygame开发一个游戏,并试图抽象出很多代码。但在这个过程中,我得到一些奇怪的错误。也就是说,当我跑的时候主.py,我得到这个线索:

>>> 
initializing pygame...
initalizing screen...
initializing background...
<Surface(Dead Display)> #Here I print out the background instance
Traceback (most recent call last):
  File "C:\Users\Ceasar\Desktop\pytanks\main.py", line 19, in <module>
    background = Background(screen, BG_COLOR)
  File "C:\Users\Ceasar\Desktop\pytanks\background.py", line 8, in __init__
    self.fill(color)
error: display Surface quit

我想这和我使用main中的上下文来管理屏幕有关。在

^{pr2}$

有什么想法导致这里的错误?在


Tags: inpymain错误linescreensurfacepygame
2条回答

我也在尝试子类化游戏。表面因为我想给它添加属性。下面就可以实现这一点。我希望它能帮助未来的人们。在

在pygame.display.set_模式必须调用(),因为它初始化所有游戏机视频东西。看来pygame.display最终被吸引到屏幕上的表面。因此,我们需要将我们创建的任何曲面blit为pygame.display.set_模式()(这只是另一个游戏。表面对象)。在

import pygame
from pygame.locals import *

pygame.init()
SCREEN_SIZE = (800, 600)

font = pygame.font.SysFont('exocet', 16)

class Screen(pygame.Surface):

    def __init__(self):

        pygame.Surface.__init__(self, SCREEN_SIZE)
        self.screen = pygame.display.set_mode((SCREEN_SIZE))
        self.text = "ella_rox"

My_Screen = Screen()        

text_surface = font.render(My_Screen.text, 1, (155, 0, 0))

while True:
    My_Screen.fill((255, 255, 255))
    My_Screen.blit(text_surface, (50, 50))
    My_Screen.screen.blit(My_Screen, (0, 0))
    pygame.display.update()

从技术上讲,这不是我的答案,但问题是Surface不能用Python的super进行扩展。相反,它应该被称为Python老式类,如下所示:

class ExtendedSurface(pygame.Surface):
   def __init__(self, string):
       pygame.Surface.__init__(self, (100, 100))
       self.fill((220,22,22))
       # ...

来源:http://archives.seul.org/pygame/users/Jul-2009/msg00211.html

相关问题 更多 >

    热门问题