def rotateRight(self):
     """Rotates..."""
     self.draw(Vector2(20, self.height * 32 + 30), "", False,
               Vector2(1000, 20))
     self.drawMsg("rotateRight()",
                  Vector2(20, self.height * 32 + 30),
                  size=20)
     time.sleep(self.waitTime)
     if self.facing == 'left':
         self.facing = 'up'
     elif self.facing == 'down':
         self.facing = 'left'
     elif self.facing == 'right':
         self.facing = 'down'
     elif self.facing == 'up':
         self.facing = 'right'
     self.drawPlayer()
Beispiel #2
0
 def drawObject(self,
                pos=Vector2(),
                img="grass tile.png",
                image=True,
                size=Vector2()):
     """Draws an image from dictionary of images. 
       Takes in agument pos as Vector2 type.(position is with tileset, not pixelwise) 
       img argument is the image name. If left blank, default is grass tile.
       if image is False, img will be drawn as text, in the posisiton of vector2 on pixel coordinates, not tile coordinates."""
     if image:
         self.screenSurface.blit(self.sprites.images[img],
                                 (pos.X * 32, pos.Y * 32))
     else:
         pygame.draw.rect(self.screenSurface, (0, 0, 0),
                          (pos.X, pos.Y, size.X, size.Y))
         self.drawMsg(
             img, pos)  #Vector2(10, self.currentLevel.height * 32 + 10))
Beispiel #3
0
    def drawMsg(self, msg='', pos=Vector2(), colour=(255, 255, 255), size=12):
        """Creates a msg on screen at Vector2 coordinates from top left corner.
          
          colour in RGB touple (0,0,0 is black, 255,255,255 white)
          size is font zize"""
        for i, line in enumerate(msg.splitlines()):
            msg_image = pygame.font.Font(pygame.font.get_default_font(),
                                         size).render(line, False, colour)

            self.screenSurface.blit(msg_image, (pos.X, pos.Y + i * (size + 5)))
 def __init__(self, level, draw, msgDrawer):
     """Player class.
       Only one stance should exist, as more than 1 might interfear. Use from main.py only
       
       Existing commands are:
            move() -> moves the plaver one tile to the front, if whater is non existant
            rotateLeft() -> rotates the player left
            rotateRight() -> rotates the player right
            """
     self.pos = Vector2()
     self.flagPos = Vector2()
     self.facing = 'down'
     self.totalCrystalCount = 0
     self.crystalsCollected = 0
     self.level = level.level
     self.width = level.width
     self.height = level.height
     self.draw = draw  #draw object para que pueda dibujar cosas el jugador
     self.drawMsg = msgDrawer  # allows text to be written
     self.completed = False
     self.waitTime = 1
    def move(self, spaces=1):
        """Moves the player on the direction its facing."""

        self.draw(Vector2(20, self.height * 32 + 30), "", False,
                  Vector2(1000, 20))

        self.drawMsg("move()", Vector2(20, self.height * 32 + 30), size=20)

        time.sleep(self.waitTime)

        if self.facing == 'down':
            try:
                if self.level[self.pos.Y + 1][self.pos.X] != 'W':
                    self.draw(self.pos)
                    self.pos.Y += 1
                    self.drawPlayer()
                    if self.level[self.pos.Y + 1][self.pos.X] != 'M':
                        if self.crystalsCollected == self.totalCrystalCount:
                            self.draw(self.flagPos, "flag-1.png")
                        else:
                            self.draw(self.flagPos, "flag-2.png")
                else:
                    print('there is water there, you will get drowned')
                    return False
            except:
                print('out of bounds')
        elif self.facing == 'right':
            try:
                if self.level[self.pos.Y][self.pos.X + 1] != 'W':
                    self.draw(self.pos)
                    self.pos.X += 1
                    self.drawPlayer()
                    if self.level[self.pos.Y + 1][self.pos.X] != 'M':
                        if self.crystalsCollected == self.totalCrystalCount:
                            self.draw(self.flagPos, "flag-1.png")
                        else:
                            self.draw(self.flagPos, "flag-2.png")
                else:
                    print('there is water there, you will get drowned')
                    return False
            except:
                print('out of bounds')
        elif self.facing == 'left':
            try:
                if self.level[self.pos.Y][self.pos.X - 1] != 'W':
                    self.draw(self.pos)
                    self.pos.X -= 1
                    self.drawPlayer()
                    if self.level[self.pos.Y + 1][self.pos.X] != 'M':
                        if self.crystalsCollected == self.totalCrystalCount:
                            self.draw(self.flagPos, "flag-1.png")
                        else:
                            self.draw(self.flagPos, "flag-2.png")
                else:
                    print('there is water there, you will get drowned')
                    return False
            except:
                print('out of bounds')
        elif self.facing == 'up':
            try:
                if self.level[self.pos.Y - 1][self.pos.X] != 'W':
                    self.draw(self.pos)
                    self.pos.Y -= 1
                    self.drawPlayer()
                    if self.level[self.pos.Y + 1][self.pos.X] != 'M':
                        if self.crystalsCollected == self.totalCrystalCount:
                            self.draw(self.flagPos, "flag-1.png")
                        else:
                            self.draw(self.flagPos, "flag-2.png")
                else:
                    print('there is water there, you will get drowned')
                    return False
            except:
                print('out of bounds')
        else:
            print('error, not facing an accepted direction')
        try:
            if self.level[self.pos.Y][self.pos.X] == 'S':
                self.crystalsCollected += 1
                self.level[self.pos.Y] = self.level[
                    self.pos.Y][:self.pos.X] + 'G' + self.level[self.pos.Y][
                        self.pos.X +
                        1:]  # Used to replace a character in a string, to delete the crystal and not having doubles.

                self.draw(Vector2(self.width * 32 + 7, 101),
                          str(self.crystalsCollected) + ' / ' +
                          str(self.totalCrystalCount), False, Vector2(
                              64,
                              12))  # updates the amount of crystals collected

                if self.crystalsCollected == self.totalCrystalCount:
                    self.draw(self.flagPos, "flag-1.png")

            elif self.level[self.pos.Y][
                    self.pos.
                    X] == 'M' and self.crystalsCollected == self.totalCrystalCount:
                self.draw(Vector2(20, self.height * 32 + 30), "", False,
                          Vector2(1000, 20))
                self.drawMsg("level completed, congratulations!",
                             Vector2(20, self.height * 32 + 30),
                             size=20)
                print('level finished, congratulations')
                self.completed = True
                raise NameError('Completed')

            return True

        except NameError as e:
            raise NameError('Completed')
        except:
            print('somehow youve done it, and player is out of bounds')
Beispiel #6
0
    def drawLevel(self, level=Level()):
        """ draws a level of type level.
          Usage:
               drawLevel(Level('nameOfLevel.txt'))
               -> if 'level' argument is left blank, default level will be loaded
               -> level takes in a Level() class
               """

        self.currentLevel = level

        # cooses at random place of flag, also can choose special crystals before drawing anything
        possibleLocations = []

        for x in range(0, level.width):
            for y in range(0, level.height):
                try:
                    if self.currentLevel.level[y][x] == 'A':
                        if random.randint(0, 100) > 60:
                            s = list(self.currentLevel.level[y])
                            s[x] = 'S'
                            self.currentLevel.level[y] = ''.join(s)
                        else:
                            s = list(self.currentLevel.level[y])
                            s[x] = 'G'
                            self.currentLevel.level[y] = ''.join(s)
                    elif self.currentLevel.level[y][x] == 'N':
                        possibleLocations.append([x, y])

                except Exception as e:
                    print('error in file cleaning: ', e)

        if possibleLocations:
            print('randomizing flag pos')
            chosen = random.randint(0, possibleLocations.__len__() - 1)

            xy = possibleLocations[chosen]

            y = possibleLocations[chosen][1]
            x = possibleLocations[chosen][0]

            s = list(self.currentLevel.level[y])
            s[x] = 'M'
            self.currentLevel.level[y] = ''.join(s)

            del possibleLocations[chosen]

            for i in possibleLocations:
                y = i[1]
                x = i[0]

                s = list(self.currentLevel.level[y])
                s[x] = 'G'
                self.currentLevel.level[y] = ''.join(s)

        self.player = Player(self.currentLevel, self.drawObject, self.drawMsg)

        # rezizes the screen zize to fit the level and extra stuff
        self.screenSize = (self.currentLevel.width * 32 + 64,
                           self.currentLevel.height * 32 + 64)
        self.screenSurface = pygame.display.set_mode(self.screenSize)

        for x in range(0, level.width):
            for y in range(0, level.height):
                try:
                    if self.currentLevel.level[y][x] == 'G':
                        self.screenSurface.blit(
                            self.sprites.images["grass tile.png"],
                            (x * 32, y * 32))

                    elif self.currentLevel.level[y][x] == 'W':
                        self.screenSurface.blit(
                            self.sprites.images["water tile.png"],
                            (x * 32, y * 32))

                    elif self.currentLevel.level[y][x] == 'M':
                        self.screenSurface.blit(
                            self.sprites.images["grass tile.png"],
                            (x * 32, y * 32))

                        self.screenSurface.blit(
                            self.sprites.images["flag-2.png"],
                            (x * 32, y * 32))
                        self.player.flagPos = Vector2(x, y)

                    elif self.currentLevel.level[y][x] == 'P':
                        self.screenSurface.blit(
                            self.sprites.images["grass tile.png"],
                            (x * 32, y * 32))
                        self.screenSurface.blit(
                            self.sprites.images["Player-1.png"],
                            (x * 32, y * 32))
                        self.player.pos = Vector2(x, y)

                    elif self.currentLevel.level[y][x] == 'S':
                        self.screenSurface.blit(
                            self.sprites.images["grass tile.png"],
                            (x * 32, y * 32))
                        self.screenSurface.blit(
                            self.sprites.images["Crystal.png"],
                            (x * 32, y * 32))
                        self.player.totalCrystalCount += 1

                except Exception as e:
                    print('error in', x, y, e)

        if self.player.totalCrystalCount == 0:
            self.screenSurface.blit(
                self.sprites.images["flag-1.png"],
                (self.player.flagPos.X * 32, self.player.flagPos.Y * 32))

        self.screenSurface.blit(self.sprites.images["Crystal.png"],
                                (self.currentLevel.width * 32 + 16, 16))
        self.drawMsg(
            "Crystals\nobtained:\n\n" + str(self.player.crystalsCollected) +
            ' / ' + str(self.player.totalCrystalCount),
            Vector2(self.currentLevel.width * 32 + 7, 50))
        self.drawMsg("Currently running:",
                     Vector2(10, self.currentLevel.height * 32 + 10))
        pygame.display.update()