我需要在游戏中使用这个秒表

时间:2014-04-29 14:05:01

标签: python python-2.7 pygame maze

我正在为一个班级制作这个游戏,它是关于一个收集宝藏的人,这是游戏的代码:

    # -*- coding: cp1252 -*-
    import os, sys
    import pygame
    from pygame.locals import *
    from ayuda import *
    pygame.init
    from menu import *
    from Tkinter import *
    root = Tk()
    tiempo = 5




    class Juego:     

        def __init__(self, width=600,height=600):

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

        def MainLoop(self):

            self.LoadSprites(); 
            pygame.key.set_repeat(500, 30)
            self.background = pygame.Surface(self.screen.get_size())
            self.background = self.background.convert()
            self.background.fill((50,85,40))

            while 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT: 
                        sys.exit()
                    elif event.type == KEYDOWN:
                        if ((event.key == K_LEFT)
                        or (event.key == K_RIGHT)
                        or (event.key == K_DOWN)
                        or (event.key == K_UP)):
                            self.snake.move(event.key)

                lstCols = pygame.sprite.spritecollide(self.snake
                                                     , self.pellet_sprites
                                                     , True)

                self.snake.pellets = self.snake.pellets + len(lstCols)

                self.screen.blit(self.background, (0, 0))

                def contador():
                    if pygame.font:
                        tipo = pygame.font.Font(None, 30)
                        contador = tipo.render("Cofres %s" % self.snake.pellets
                                    , 1, (0, 0, 0))
                textpos = contador.get_rect(centerx=self.background.get_width()/8)
                        self.screen.blit(contador, textpos)                    
                contador()
                self.pellet_sprites.draw(self.screen)
                self.snake_sprites.draw(self.screen)
                pygame.display.flip()


        def LoadSprites(self): 
            self.snake = Snake()
            self.snake_sprites = pygame.sprite.RenderPlain((self.snake))

            nNumHorizontal = int(self.width/62)
            nNumVertical = int(self.height/62)       
            self.pellet_sprites = pygame.sprite.Group()

            for x in range(nNumHorizontal):
                for y in range(nNumVertical):
                    self.pellet_sprites.add(Pellet(pygame.Rect(x*100, y*100, 1, 1)))        

    class Snake(pygame.sprite.Sprite):


        def __init__(self):
            pygame.sprite.Sprite.__init__(self) 
            self.image, self.rect = load_image('personaje.png',-1)
            self.pellets = 0
            self.x_dist = 100
            self.y_dist = 100

        def move(self, key):

            xMove = 0;
            yMove = 0;



            if (key == K_RIGHT)and self.rect.left - self.x_dist >= 0:
                xMove = -self.x_dist
            elif (key == K_LEFT)and self.rect.right + self.x_dist <= 600:
                xMove = self.x_dist
            elif (key == K_UP)and self.rect.bottom + self.y_dist <=600:
                yMove = self.y_dist
            elif (key == K_DOWN)and self.rect.top - self.y_dist >=0:
                yMove = -self.y_dist
            self.rect.move_ip(xMove,yMove);

    class Pellet(pygame.sprite.Sprite):

        def __init__(self, rect=None):
            pygame.sprite.Sprite.__init__(self) 
            self.image, self.rect = load_image('cofrefront.png',-1)
            if rect != None:
                self.rect = rect
    def comojuego():
        screen = pygame.display.set_mode((600, 600))
        done = False

        font = pygame.font.SysFont("comicsansms", 18)

        text = font.render("agarrar todos los tesoros", True, (250, 0, 0))

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    done = True

            screen.fill((0, 0, 0))
            screen.blit(text,
                (320 - text.get_width() // 2, 240 - text.get_height() // 2))

            pygame.display.flip()




    def main():
       pygame.init()

       screen = pygame.display.set_mode((600, 600))

       pygame.display.set_caption("Juego de habilidad mental MML")


       menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
                   [('Empezar Juego', 1, None),
                    ('Opciones',    2, None),
                    ('Como Jugar',   3, None),
                    ('Salir',       4, None)])

       menu.set_center(True, True)

       menu.set_alignment('top', 'center')

       state = 0
       prev_state = 1

       rect_list = []

       pygame.event.set_blocked(pygame.MOUSEMOTION)

       while 1:
          if prev_state != state:
             pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
             prev_state = state

          e = pygame.event.wait()

          if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
             if state == 0:
                rect_list, state = menu.update(e, state)
             elif state == 1:
                MainWindow = Juego()
                MainWindow.MainLoop()
                state = 0
             elif state == 2:
                print 'Opciones!'
                state = 0
             elif state == 3:
                comojuego()
                state = 0
             else:
                print 'Salir!'
                pygame.quit()
                sys.exit()
          if e.type == pygame.QUIT:
             pygame.quit()
             sys.exit()

          pygame.display.update(rect_list)            

    if __name__ == "__main__":
        main() 

我无法弄清楚的是:我需要在与contador()相同的屏幕上添加一个秒表,它会计算角色收集的宝藏数量。因此,我可以计算我收集了多少宝藏,同时记下倒计时以了解游戏何时结束。我在创建秒表的单独窗口上有这个代码:

    from Tkinter import *
    root = Tk()
    canvas = Canvas(root)
    canvas.pack()
    time = 5
    def do_something():
        canvas.create_text("Se acabo el tiempo")

    def tick():

        canvas.delete(ALL)
        global time
        time -= 1
        canvas.create_text(10, 10, text=time)
        if time == 0:
            do_something()
        else:
            canvas.after(1000, tick)
    canvas.after(1, tick)
    root.mainloop()

0 个答案:

没有答案