2

This is my code (It's a pacman game type)

It runs but when the character in this case the reaper dies when he collides with an exorcist the game will crash instead of showing the game over sign

    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()`
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • i don't see any `game over sign` in your code. I only see `sys.exit()` to immediately exit game when reaper is dead – furas Dec 14 '15 at 04:04

1 Answers1

1

Looking at the game over condition in your code:

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

The two lines in the triple quotes are just strings, but there's nothing telling the program to output them, so they're just being ignored. You need to output them. This could be as simple as a print() statement:

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

Note this will only output to the shell/terminal. If you want to display text in the game window, you have to do some text rendering - see http://www.pygame.org/docs/ref/font.html for details.

Chris
  • 898
  • 1
  • 5
  • 8