0

I'm making this game for a class and it is about a guy who collects treasures, this is the code for the game:

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

What I can't figure out is this: I need to add a stopwatch in the same screen as contador(), which counts how many treasures the character collects. So I can count how many treasures I collect and at the same time keep a countdown to know when the game will end. I have this code on a separate window that creates the stopwatch:

    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()
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
  • I have removed your subsidiary question, which is much too broad. What have you done so far to try to include the stopwatch? Your code structure is extremely odd, and having both a function and a variable called `contador` inside a method is going to mean no end of trouble. – jonrsharpe Apr 29 '14 at 14:10
  • My stopwatch will run and dislpay if i run it separately, but i can't figure out a way to display the stopwatch and the treasure counter in the same screen @jonrsharpe – ProgrammingNewbie Apr 29 '14 at 15:46
  • 1
    I would never use tkinker with pygame - it looks strange or even stupid. Use pygame to create stopwach and draw it on pygame `screen` in `mainloop`. – furas May 05 '14 at 03:00

0 Answers0