游戏不会正常退出

时间:2015-12-14 00:30:33

标签: pygame pacman

这是我的代码(这是一个pacman游戏类型)

它会运行,但是当这个角色在收割者与驱魔人发生碰撞时死亡时,游戏会崩溃而不是将游戏显示在标志上

    enter code here`#! /usr/bin/env python

import os, sys
import pygame
import menu
import level001
import basicSprite
from pygame.locals import *
from helpers import *
from ReaperSprite import *
from basicExorcist import Exorcist

if not pygame.font: print 
if not pygame.mixer: print 

BLOCK_SIZE = 24
class PyManMain:

def __init__(self, width=640,height=480):

    pygame.init()
    self.width = width
    self.height = height

    self.screen = pygame.display.set_mode((self.width
                                           , self.height))

def MainLoop(self):



    self.LoadSprites();


    self.background = pygame.Surface(self.screen.get_size())
    self.background = self.background.convert()
    self.background.fill((0,0,0))

    self.block_sprites.draw(self.screen)
    self.block_sprites.draw(self.background)
    #self.skull_sprites.draw(self.screen)
    #self.skull_sprites.draw(self.background)

    pygame.display.flip()
    while 1:

        self.reaper_sprites.clear(self.screen,self.background)
        self.exorcist_sprites.clear(self.screen,self.background)

        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                sys.exit()
            elif event.type == KEYDOWN:
                if ((event.key == K_RIGHT)
                or (event.key == K_LEFT)
                or (event.key == K_UP)
                or (event.key == K_DOWN)):
                    self.reaper.MoveKeyDown(event.key)
            elif event.type == KEYUP:
                if ((event.key == K_RIGHT)
                or (event.key == K_LEFT)
                or (event.key == K_UP)
                or (event.key == K_DOWN)):
                    self.reaper.MoveKeyUp(event.key)
            elif event.type == SUPER_STATE_OVER:
                self.reaper.superState = False

                pygame.time.set_timer(SUPER_STATE_OVER,0)
                for exorcist in self.exorcist_sprites.sprites():
                    exorcist.SetScared(False)
            elif event.type == SUPER_STATE_START:
                for exorcist in self.exorcist_sprites.sprites():
                    exorcist.SetScared(True)
            elif event.type == REAPER_EATEN:
                """The reaper is dead!"""
                """He has become a human again for that his soul will burn in hell"""
                sys.exit()


        self.reaper_sprites.update(self.block_sprites
                                   , self.skull_sprites
                                   , self.super_skull_sprites
                                   , self.exorcist_sprites)
        self.exorcist_sprites.update(self.block_sprites)




        textpos = 0          
        self.screen.blit(self.background, (0, 0))     
        if pygame.font:
            font = pygame.font.Font(None, 36)
            text = font.render("Skulls %s" % self.reaper.skulls
                                , 1, (255, 0, 0))
            textpos = text.get_rect(centerx=self.background.get_width()/2)
            self.screen.blit(text, textpos)

        reclist = [textpos]  
        reclist += self.skull_sprites.draw(self.screen)
        reclist += self.super_skull_sprites.draw(self.screen)
        reclist += self.reaper_sprites.draw(self.screen)
        reclist +=  self.exorcist_sprites.draw(self.screen)
        #reclist += (self.block_sprites.draw(self.screen))

        pygame.display.update(reclist)
       # pygame.display.flip()

def LoadSprites(self):

    x_offset = (BLOCK_SIZE/2)
    y_offset = (BLOCK_SIZE/2)

    level1 = level001.level()
    layout = level1.getLayout()
    img_list = level1.getSprites()


    self.skull_sprites = pygame.sprite.RenderUpdates()
    self.super_skull_sprites = pygame.sprite.RenderUpdates()

    self.block_sprites = pygame.sprite.RenderUpdates()
    self.exorcist_sprites = pygame.sprite.RenderUpdates()

    for y in xrange(len(layout)):
        for x in xrange(len(layout[y])):

            centerPoint = [(x*BLOCK_SIZE)+x_offset,(y*BLOCK_SIZE+y_offset)]
            if layout[y][x]==level1.BLOCK:
                block = basicSprite.Sprite(centerPoint, img_list[level1.BLOCK])
                self.block_sprites.add(block)
            elif layout[y][x]==level1.REAPER:
                self.reaper = Reaper(centerPoint,img_list[level1.REAPER])
            elif layout[y][x]==level1.SKULL:
                skull = basicSprite.Sprite(centerPoint, img_list[level1.SKULL])
                self.skull_sprites.add(skull) 
            elif layout[y][x]==level1.EXORCIST:
                exorcist = Exorcist(centerPoint, img_list[level1.EXORCIST]
                                   , img_list[level1.SCARED_EXORCIST])
                self.exorcist_sprites.add(exorcist) 

                skull = basicSprite.Sprite(centerPoint, img_list[level1.SKULL])
                self.skull_sprites.add(skull) 
            elif layout[y][x]==level1.SUPER_SKULL:
                super_skull = basicSprite.Sprite(centerPoint, img_list[level1.SUPER_SKULL])
                self.super_skull_sprites.add(super_skull) 


    self.reaper_sprites = pygame.sprite.RenderUpdates(self.reaper)                                  

if __name__ == "__main__":
    MainWindow = PyManMain(500,575)
    MainWindow.MainLoop()`

1 个答案:

答案 0 :(得分:1)

在代码中查看游戏状况:

elif event.type == REAPER_EATEN:
            """The reaper is dead!"""
            """He has become a human again for that his soul will burn in hell"""
            sys.exit()

三引号中的两行只是字符串,但是没有任何东西告诉程序输出它们,所以它们只是被忽略了。你需要输出它们。这可以像print()语句一样简单:

elif event.type == REAPER_EATEN:
                print("The reaper is dead!")
                print("He has become a human again for that his soul will burn in hell")
                sys.exit()

注意这只会输出到shell /终端。如果要在游戏窗口中显示文本,则必须进行一些文本渲染 - 有关详细信息,请参阅http://www.pygame.org/docs/ref/font.html