Exemple #1
0
class HighlightToken(object):
    def __init__(self, _x, _y, level):
        self.highlighted = False
        self.level_name = level
        self.location = Location(_x, _y)
        self.size = Location(80, 80)

        self.tokensprite = pygame.sprite.Sprite()
        self.tokensprite.image = pygame.image.load(
            "highlight_token.png").convert_alpha()
        self.tokensprite.image = pygame.transform.scale(
            self.tokensprite.image, (20, 20))
        self.tokensprite.rect = self.tokensprite.image.get_rect()

    def draw(self, screen):
        if self.highlighted:
            screen.blit(self.tokensprite.image, self.location.get_loc())

    def hit_check(self, x, y):
        if x > self.location.x - (self.size.x / 2) and x < self.location.x + (
                self.size.x / 2):
            if y > self.location.y - (
                    self.size.y / 2) and y < self.location.y + (self.size.y /
                                                                2):
                self.highlighted = True
                return True
        self.highlighted = False
        return False
class Player(object):
    def __init__(self):
        self.location = Location(100, 100)
        self.size = Location(100, 50)
        self.countdown = 5
        self.cur_offset = 0
        self.reversing = False
        self.sprites = []
        for i in range(0, 6):
            self.sprites.append(pygame.sprite.Sprite())
            self.sprites[i].image = pygame.image.load(
                "lewis_character_5.png").convert_alpha()
            self.sprites[i].rect = self.sprites[i].image.get_rect()

    def update(self):
        # TODO: Move through the sprites
        self.countdown -= 1
        if self.countdown < 1:
            self.countdown = 5
            if self.reversing is False:
                self.cur_offset += 50
                if self.cur_offset >= 250:
                    self.cur_offset = 150
                    self.reversing = True
            else:
                self.cur_offset -= 50
                if self.cur_offset < 0:
                    self.cur_offset = 50
                    self.reversing = False

    def draw(self, screen):
        screen.blit(self.sprites[0].image, self.location.get_loc(),
                    (self.cur_offset, 0, 50, 100))
class Background(object):
    def __init__(self, width, height, message_pump, tent):
        self.tent = tent
        self.message_pump = message_pump
        self.message_pump.register(self)
        self.curlevel = 1
        self.location = Location(0, 0)
        self.reset()  #Randomly pick a starting location
        self.player_location = Location(0, 0)
        #        self.size = Location(40,40)
        self.forestsprite = pygame.sprite.Sprite()
        self.forestsprite.image = pygame.image.load(
            "start location.jpg").convert()
        self.forestsprite.image = pygame.transform.scale(
            self.forestsprite.image, (width, height))
        self.forestsprite.rect = self.forestsprite.image.get_rect()

        self.forestsprite2 = pygame.sprite.Sprite()
        self.forestsprite2.image = pygame.image.load(
            "location 2.jpg").convert()
        self.forestsprite2.image = pygame.transform.scale(
            self.forestsprite2.image, (width, height))
        self.forestsprite2.rect = self.forestsprite2.image.get_rect()

        self.forestsprite3 = pygame.sprite.Sprite()
        self.forestsprite3.image = pygame.image.load(
            "location 3.jpg").convert()
        self.forestsprite3.image = pygame.transform.scale(
            self.forestsprite3.image, (width, height))
        self.forestsprite3.rect = self.forestsprite3.image.get_rect()

        self.forestsprite4 = pygame.sprite.Sprite()
        self.forestsprite4.image = pygame.image.load(
            "location 4.jpg").convert()
        self.forestsprite4.image = pygame.transform.scale(
            self.forestsprite4.image, (width, height))
        self.forestsprite4.rect = self.forestsprite4.image.get_rect()

        self.forestsprite5 = pygame.sprite.Sprite()
        self.forestsprite5.image = pygame.image.load(
            "location 5.jpg").convert()
        self.forestsprite5.image = pygame.transform.scale(
            self.forestsprite5.image, (width, height))
        self.forestsprite5.rect = self.forestsprite5.image.get_rect()

        self.forestsprite6 = pygame.sprite.Sprite()
        self.forestsprite6.image = pygame.image.load(
            "location 6.jpg").convert()
        self.forestsprite6.image = pygame.transform.scale(
            self.forestsprite6.image, (width, height))
        self.forestsprite6.rect = self.forestsprite6.image.get_rect()

        self.yurtsprite = pygame.sprite.Sprite()
        self.yurtsprite.image = pygame.image.load("yurt inside.jpg").convert()
        self.yurtsprite.image = pygame.transform.scale(self.yurtsprite.image,
                                                       (width, height))
        self.yurtsprite.rect = self.yurtsprite.image.get_rect()

        self.mapsprite = pygame.sprite.Sprite()
        self.mapsprite.image = pygame.image.load("map.jpg").convert()
        self.mapsprite.image = pygame.transform.scale(self.mapsprite.image,
                                                      (100, 45))
        self.mapsprite.rect = self.mapsprite.image.get_rect()

        self.map = MRFMap(width, height, message_pump)
        self.changed_level = False

    def update(self):
        self.changed_level = False
        if self.curlevel == 2:
            if self.player_location.x > 380 and self.player_location.x < 470:
                if self.player_location.y >= 230 and self.player_location.y < 250:

                    if pygame.key.get_pressed()[pygame.K_e] != 0:
                        print("press e")
                        self.message_pump.send_message("change level", "3")
        elif self.curlevel == 3:
            #            print("update map...")
            self.map.update()
#            self.facing = "e"
#            # We need to move to the LEFT
#            self.location.x -= 2

    def message(self, msg_type, msg):
        if msg_type == "change level" and self.changed_level is False:
            self.changed_level = True
            self.curlevel = int(msg)
            if msg == "1":
                #                self.curlevel = 1
                print("loading first location")
            elif msg == "2":
                #                self.curlevel = 2
                print("loading tent")
            elif msg == "3":
                #                self.curlevel = 3
                print("loading map in background.py")
            elif msg == "4":
                #                self.curlevel = 4
                pass

            else:
                print("ERROR: Unkown level in background.py {}".format(msg))

        elif msg_type == "player location" and self.curlevel == 2:
            x = msg.split(" ")[0]
            x = int(x)
            y = msg.split(" ")[1]
            y = int(y)
            self.player_location.x = x
            self.player_location.y = y
            if y > 420:
                print("Going to level 1")
                self.message_pump.send_message("change level",
                                               self.tent.return_level)

            print("{} : {} , [{}] background.py".format(x, y, type(x)))

    def reset(self):
        # We have reached the bottom of the screen so reset
        #        self.location.y = 0
        #        self.location.x = random.randint(0, 560)
        pass

    def draw(self, screen):
        #        screen.blit(self.forestsprite.image, self.location.get_loc())
        if self.curlevel == 1:
            screen.blit(self.forestsprite.image, self.location.get_loc())
        elif self.curlevel == 2:
            screen.blit(self.yurtsprite.image, self.location.get_loc())
            screen.blit(self.mapsprite.image, (410, 220))
        elif self.curlevel == 3:
            self.map.draw(screen)
        elif self.curlevel == 4:
            screen.blit(self.forestsprite2.image, self.location.get_loc())
        elif self.curlevel == 5:
            screen.blit(self.forestsprite3.image, self.location.get_loc())
        elif self.curlevel == 6:
            screen.blit(self.forestsprite4.image, self.location.get_loc())
        elif self.curlevel == 7:
            screen.blit(self.forestsprite5.image, self.location.get_loc())
        elif self.curlevel == 8:
            screen.blit(self.forestsprite6.image, self.location.get_loc())

    def check_collision(self, obj):
        # Check if the two objects are touching
        diffx = self.location.x - obj.location.x
        diffy = obj.location.y - self.location.y
        if diffx < self.size.x and diffx > (self.size.x * -1):
            if diffy < self.size.y and diffy > (self.size.y * -1):
                return True
        return False
Exemple #4
0
class Tent(object):
    def __init__(self, message_pump):
        self.message_pump = message_pump
        self.message_pump.register(self)
        self.visible = False
        self.location = Location(10,150)
#        self.reset() #Randomly pick a starting location
        self.return_level = "1"
#        self.size = Location(40,40)
        self.sprite = pygame.sprite.Sprite()
        self.sprite.image = pygame.image.load("yurt.png").convert_alpha()
        self.sprite.image= pygame.transform.scale(self.sprite.image,(300, 250))
#        self.sprite.image = self.sprite.image.convert_alpha()
        self.sprite.rect = self.sprite.image.get_rect()

    def update(self):
        pass
#        self.location.y += 10
#        if self.location.y > 500:
#            self.reset()
    def message(self, msg_type, msg):
        if msg_type == "change level":
            if msg != "2":
                self.return_level = msg
                self.curlevel = int(msg)
                
            if msg == "1":
                self.location.x = 10
                self.location.y = 150
            elif msg == "4":
                self.location.x = 250
                self.location.y = 150
            elif msg == "5":
                self.location.x = 250
                self.location.y = 130
            elif msg == "6":
                self.location.x = 250
                self.location.y = 130
            elif msg == "7":
                self.location.x = 250
                self.location.y = 130
            elif msg == "8":
                self.location.x = 250
                self.location.y = 130
                
            tent_visible_levels = ["1", "4", "5", "6", "7", "8"]
            if msg in tent_visible_levels:
                self.visible = True
            else:
                self.visible = False
                
        elif msg_type == "player location" and self.visible:
            x = msg.split(" ")[0]
            x = int(x)
            y = msg.split(" ")[1]
            y = int(y)
            if x> 94 and x < 138:
                if y > 180 and y < 290:
                    print("Going to level 2")
                    self.message_pump.send_message("change level","2")
            print("{} : {} , [{}]tent.py".format(x, y, type(x)))

    def reset(self):
        # We have reached the bottom of the screen so reset
#        self.location.y = 0
#        self.location.x = random.randint(0, 560)
        pass

    def draw(self, screen):
        if self.visible:
            screen.blit(self.sprite.image, self.location.get_loc())

    def check_collision(self, obj):
        # Check if the two objects are touching
        diffx = self.location.x - obj.location.x
        diffy = obj.location.y - self.location.y
        if diffx < self.size.x and diffx > (self.size.x * -1):
            if diffy < self.size.y and diffy > (self.size.y * -1):
                return True
        return False
class Player(object):
    def __init__(self, message_pump):
        self.curlevel = 0
        self.message_pump = message_pump
        self.message_pump.register(self)

        self.location = Location(50, 440)
        self.size = Location(40, 40)

        self.countdown = 10
        self.cur_offset = 0
        self.reversing = False
        self.COOLDOWN = 5
        self.sprites = []

        self.sprites.append(pygame.sprite.Sprite())
        self.sprites[0].image = pygame.image.load(
            "lewis_character_5.png").convert_alpha()
        self.sprites[0].rect = self.sprites[0].image.get_rect()
        self.sprites.append(pygame.sprite.Sprite())
        self.sprites[1].image = pygame.image.load(
            "lewis_character_5_left.png").convert_alpha()
        self.sprites[1].rect = self.sprites[1].image.get_rect()

        self.sprites.append(pygame.sprite.Sprite())
        self.sprites[2].image = pygame.image.load(
            "lewis_character_5.png").convert_alpha()
        self.sprites[2].image = pygame.transform.scale(self.sprites[0].image,
                                                       (125, 50))
        self.sprites[2].rect = self.sprites[2].image.get_rect()

        self.sprites.append(pygame.sprite.Sprite())
        self.sprites[3].image = pygame.image.load(
            "lewis_character_5.png").convert_alpha()
        self.sprites[3].image = pygame.transform.scale(self.sprites[1].image,
                                                       (125, 50))
        self.sprites[3].rect = self.sprites[3].image.get_rect()

        self.levels = []
        level0 = []
        self.levels.append(level0)
        level1 = []
        self.levels.append(level1)
        level2 = []
        bed_rect = Rect(185, 170, 195, 100)
        cushion_rect = Rect(0, 0, 110, 250)
        cushion_rect_2 = Rect(0, 0, 90, 280)
        stove_rect = Rect(0, 0, 66, 340)
        level2.append(bed_rect)
        level2.append(cushion_rect)
        level2.append(cushion_rect_2)
        level2.append(stove_rect)
        self.levels.append(level2)

        self.facing = "right"

    def message(self, msg_type, msg):
        if msg_type == "change level":
            self.curlevel = int(msg)
            if msg == "1":
                self.location.x = 110
                self.location.y = 320
#                self.curlevel = 1
            if msg == "2":
                self.location.x = 110
                self.location.y = 350
#                self.curlevel = 2
            if msg == "3":
                self.location.x = 110
                self.location.y = 350
#                self.curlevel = 3
            if msg == "4":
                self.location.x = 110
                self.location.y = 350
                print("Player is loading level 4")
        if msg_type == "start_game":
            self.location.x = 40
            self.location.y = 420

    def update_walking_sprite(self):
        self.countdown -= 1
        if self.countdown < 1:
            self.countdown = self.COOLDOWN
            if self.reversing is False:
                self.cur_offset += 50
                if self.cur_offset >= 250:
                    self.cur_offset = 150
                    self.reversing = True
            else:
                self.cur_offset -= 50
                if self.cur_offset < 0:
                    self.cur_offset = 50
                    self.reversing = False

    def update(self):
        # To track whether we have actually moved or not
        old_x = self.location.x
        old_y = self.location.y

        if pygame.key.get_pressed()[pygame.K_LEFT] != 0:
            self.facing = "left"
            # We need to move to the LEFT
            self.location.x -= 2

        if pygame.key.get_pressed()[pygame.K_RIGHT] != 0:
            self.facing = "right"
            # We need to move to the RIGHT
            self.location.x += 2

        if pygame.key.get_pressed()[pygame.K_UP] != 0:
            # We need to move UP
            self.location.y -= 2

        if pygame.key.get_pressed()[pygame.K_DOWN] != 0:
            # We need to move to the DOWN
            self.location.y += 2

        # Make sure we can't leave the screen
        if self.location.x > 500:
            self.location.x = 500
#            386
        if self.location.x < 0:
            self.location.x = 0

        if self.location.y > 440:
            self.location.y = 440

        if self.location.y < 0:
            self.location = 0

        #Level specific boundary checks
        if self.curlevel == 1:
            if self.location.y < 272:
                self.location.y = 272
            if self.location.x > 386:
                self.location.x = 386
        elif self.curlevel == 2:
            bHit = False
            for r in self.levels[2]:
                if r.hit_test(self.location.x, self.location.y):
                    self.location.x = old_x
                    self.location.y = old_y
                    bHit = True

            # Now check the boundaries
            if self.location.y < 230:
                self.location.y = 230
            if self.location.x > 500:
                self.location.x = 500

        if self.location.x != old_x or self.location.y != old_y:
            self.update_walking_sprite()
            # Only update everyone on our location if we have actually moved - otherwise this is a waste
            self.message_pump.send_message(
                "player location", "{} {}".format(self.location.x,
                                                  self.location.y))

#            print("{} : {} , [{}] player.py".format(x, y, type(x)))
        else:
            self.cur_offset = 100
            self.countdown = self.COOLDOWN

    def draw(self, screen):
        #        small_levels = [0, 1, 3, 4, 5, 6, ]

        #        if self.curlevel in small_levels:
        if self.curlevel != 2:
            act_offset = self.cur_offset / 2

            if self.facing == "left":
                screen.blit(self.sprites[3].image, self.location.get_loc(),
                            (act_offset, 0, 25, 50))
            else:
                screen.blit(self.sprites[2].image, self.location.get_loc(),
                            (act_offset, 0, 25, 50))
        else:
            if self.facing == "left":
                screen.blit(self.sprites[1].image, self.location.get_loc(),
                            (self.cur_offset, 0, 50, 100))
            else:
                screen.blit(self.sprites[0].image, self.location.get_loc(),
                            (self.cur_offset, 0, 50, 100))

    def reset(self):
        self.destroy()

    def destroy(self):
        # Reset back to our location
        self.location.x = 300
        self.location.y = 430
class MRFMap(object):
    def __init__(self, width, height, message_pump):
        self.message_pump = message_pump
        self.message_pump.register(self)
        self.curlevel = 1
        self.player_location = Location(0, 0)

        self.changed_level = False

        self.mapsprite = pygame.sprite.Sprite()
        self.mapsprite.image = pygame.image.load("map.jpg").convert()
        self.mapsprite.image = pygame.transform.scale(self.mapsprite.image,
                                                      (width, height))
        self.mapsprite.rect = self.mapsprite.image.get_rect()
        self.location = Location(0, 0)
        self.tokens = []
        self.tokens.append(HighlightToken(112, 183, "1"))
        self.tokens.append(HighlightToken(180, 243, "4"))
        self.tokens.append(HighlightToken(256, 267, "5"))
        self.tokens.append(HighlightToken(316, 310, "6"))
        self.tokens.append(HighlightToken(378, 275, "7"))
        self.tokens.append(HighlightToken(436, 250, "8"))

    def update(self):
        if pygame.key.get_pressed()[pygame.K_e] != 0:
            if self.changed_level is not False:
                print("duplicate e pressed on map")
                return
            for t in self.tokens:
                if t.hit_check(self.player_location.x,
                               self.player_location.y) is True:
                    print(
                        "MRF_map is changing level to {} player {},{}".format(
                            t.level_name, self.player_location.x,
                            self.player_location.y))
                    self.message_pump.send_message("change level",
                                                   t.level_name)
        else:
            self.changed_level = False

    def reset(self):
        for t in self.tokens:
            t.highlighted = False
        self.changed_level = False

    def message(self, msg_type, msg):
        if msg_type == "change level":
            print("Got a message {} {}".format(msg, self.changed_level))

        if msg_type == "change level" and self.changed_level is False:
            if self.curlevel == 3:
                print("reset")
                self.reset()

            if msg == "3":
                self.changed_level = True
                self.curlevel = 3
                self.player_location.x = 0
                self.player_location.y = 0
#                print("loading map on map.py")
            else:
                self.curlevel = 1

        elif msg_type == "player location" and self.curlevel == 3:

            x = msg.split(" ")[0]
            x = int(x)
            y = msg.split(" ")[1]
            y = int(y)
            self.player_location.x = x
            self.player_location.y = y
            for t in self.tokens:
                t.hit_check(x, y)

#            print("{} : {} , [{}] mrf_map.py".format(x, y, type(x)))

    def draw(self, screen):
        #        if self.curlevel != 3:
        #            print("ERROR: Should not be able to call this when not on level 3")
        #draw map stuff - ie. all the map bits
        screen.blit(self.mapsprite.image, self.location.get_loc())

        for t in self.tokens:
            t.draw(screen)