【问题标题】:Pygame Error: Display surface quit (Why does this happen?)Pygame 错误:显示表面退出(为什么会发生这种情况?)
【发布时间】:2015-08-15 16:39:16
【问题描述】:

整个夏天,我决定使用 Python 中的 pygame 模块制作一个版本的 pong。我已经有了游戏逻辑,现在我只是在标题屏幕上工作以增加一些触感。我对这个特定功能的主要目标是能够在屏幕上选择一个选项并将其变为橙色突出显示。但是,每次我尝试通过向下按钮向下滚动时,程序都会结束并显示此错误:

    error: display Surface quit

从我读到的内容来看,这是在 pygame.quit() 启动后声明的,所以我暂时将它从我的代码中取出,但没有它也会发生同样的事情。什么鬼?

我的代码(它有一些未使用的变量,因为我只是将它复制并粘贴到另一个 python 文档):

 import pygame,random, time
 pygame.init()

#GLOBAL VARIABLES

mode='normal'#Sets game theme
playerx=0 #Paddle x for gamer
playery=0 #paddle y for gamer
aix=0 #paddle x for computer
aiy=0 #paddle y form computer
x=400 #ball x coord
y=300 #ball y coord
speed_ballx=0 #xball pixel change during post blit
speed_bally=0 #yball pixel change during post blit


r=0 #redval
g=0 #greval
b=0 #blueval

#GRAPHIC ART
paddle=pygame.image.load('paddle.PNG')
ball=pygame.image.load('ball.PNG')
logo=pygame.image.load('logo.PNG')
comicsans = pygame.font.SysFont("comicsansms", 22) #comicsans font initialization
startoption = comicsans.render("START", True, (255,255,255))
instructionoption = comicsans.render("HELP", True, (255,255,255))
select=pygame.image.load('optionselect.PNG')

def pong():
    main_surface.blit(ball, (x,y))#prints ball
def intsructions():
    pygame.quit()



def menu():
    end=False #will end while loop and end menu
    opt=1 #used to decide what option the player is selecting
    main_surface = pygame.display.set_mode((815,615))#creates new window
    while end==False:


        main_surface.blit(logo, (15,15))
        pygame.display.flip()#renders all blitted objects

        #The following if statements blit orange highlight on to the selected option
        if opt==1:
            main_surface.blit(select,(12,140))

        if opt==2:
            main_surface.blit(select,(12,170))

        main_surface.blit(startoption, (15,140))
        main_surface.blit(instructionoption, (15,170))

        pygame.display.flip()#renders all blitted objects



        for ev in pygame.event.get():

            if ev.type == pygame.QUIT:  # Window close button clicked?
                end=True

            if ev.type == pygame.KEYDOWN:
                if ev.key == pygame.K_DOWN and opt==1: #moving down an option
                    opt=opt+1
                    print 2

                if ev.key == pygame.K_DOWN and opt==2: #moving from last option (selection restarts at top)
                    opt=1

                if ev.key == pygame.K_KP_ENTER and opt==1: #selecting START
                    pong()

                if ev.key == pygame.K_DOWN and opt==1: #selecting INSTRUCTIONS
                    intsructions()
    pygame.quit()





menu()

【问题讨论】:

  • 添加一些调试代码是个好主意,例如使用简单的“if”检查“flip”函数的返回值。这样你就可以看到是什么函数导致了问题。
  • 能否请您发布整个错误回调?这样我们就可以知道代码的哪些部分导致了错误。
  • 如果按下了向下箭头键,则调用 pygame.quit() (intsructions())...

标签: python python-2.7 pygame


【解决方案1】:

我没有看到你在任何地方定义你的表面。 只需定义它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多