[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Game not running



Are your image files always located in the folder where your script is? If not, you're probably getting errors because the script can't find your images.

Also, can you run your script via the commandline when it's outside the Python32 folder and tell us what error you're getting?

Op 11 maart 2012 17:51 schreef Avirup Kundu <avirupkundu@xxxxxxxxx> het volgende:
import pygame, os.path, sys
from pygame.locals import *
from random import randint

def load_image(name, colorkey=None):
    try:
        image = pygame.image.load(name)
    except pygame.error:
        print ('Cannot load image:', name)
        raise SystemExit(str(geterror()))
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image

class IMG:
    pass

class NPC:
    #A container class for the NPCs
    def __init__(self, image):
        self.image = image
        self.pos = [(38,38),(119,63),(181,21),(35,189),(119,157),(169,219),(15,323),(95,303),(183,319)]
    
    def update(self):
        pass
    
    def draw(self, screen, k):
        self.rect = self.pos[k]
        screen.blit(self.image, self.rect)

class Mice(NPC):
    #The bad guy!
    def __init__(self):
        NPC.__init__(self, IMG.MICE)

class Dog(NPC):
    #The pet dog!
    def __init__(self):
        NPC.__init__(self, IMG.DOG)

class Cat(NPC):
    #The neighbour's cat!
    def __init__(self):
        NPC.__init__(self, IMG.CAT)

    
def main():
    pygame.init()
    screen = pygame.display.set_mode((240,380))
    pygame.display.set_caption("Hit The Mice!")
    
    background = "">
    screen.blit(background, (0,0))
    pygame.display.flip()
    
    flag = False
    score = 0
    allowed_time = 600 #change this variable to control the speed of the game. In miliseconds.
    time_to_run = 200   #change this variable to play longer.
    hit = False
    life = 3
    score = 0
    time_elapsed = 0
    
    IMG.MICE = load_image('mice.png', -1)
    IMG.MICE=pygame.transform.scale(IMG.MICE, (50,50))
    IMG.DOG = load_image('dog.png', -1)
    IMG.DOG=pygame.transform.scale(IMG.DOG, (50,50))
    IMG.CAT = load_image('cat.png', -1)
    IMG.CAT=pygame.transform.scale(IMG.CAT, (50,50))
    
    actors = [Mice(), Mice(), Mice(), Mice(), Dog(), Cat()]
    
    while True:
        screen.blit(background, (0,0))
        r = randint(0,5)
        k = randint(0,8)
        actors[r].draw(screen,k)
        pygame.display.update()
        
        time = pygame.time.Clock()
        time_passed = 0
        
        while True:
            time_passed = time.tick()+time_passed
            time_elapsed = time.tick()+time_elapsed
            if time_passed > allowed_time:
                break
            
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        flag = True
                        break
            
            pressed_key = pygame.key.get_pressed()
            
            if pressed_key[K_q] and k==0:
                hit = True
                break
            if pressed_key[K_w] and k==1:
                hit = True
                break
            if pressed_key[K_e] and k==2:
                hit = True
                break
            if pressed_key[K_a] and k==3:
                hit = True
                break
            if pressed_key[K_s] and k==4:
                hit = True
                break
            if pressed_key[K_d] and k==5:
                hit = True
                break
            if pressed_key[K_z] and k==6:
                hit = True
                break
            if pressed_key[K_x] and k==7:
                hit = True
                break
            if pressed_key[K_c] and k==8:
                hit = True
                break
        if life < 0:
            break
        if flag is True:
            break
        if hit is True:
            if r==4 or r==5:
                life = life-1
            else:
                score = score+1
                print(score)
        
        if time_elapsed > time_to_run:
            break
        hit = False
    
    background = "">
    background.fill((255,255,0))
    score = str(score)
    score = "Your score is "+ score
    print(score)
    while True:
        
        for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        sys.exit()
        
        screen.blit(background, (0,0))
        my_font = pygame.font.SysFont("arial", 28)
        score_surface = my_font.render(score, True, (0,0,0))
        screen.blit(score_surface, (34, 24))
        
        my_font_1 = pygame.font.SysFont("arial", 16)
        quit_surface = my_font_1.render("Press esc to Quit", True, (0,0,0))
        screen.blit(quit_surface, (64, 300))
        pygame.display.flip()
        

if __name__ == '__main__':main()