示例#1
0
    def load(self):
        self.boundary = Boundary(self.screen, 0, 0)

        snake_position_x, snake_position_y = self.random_position()
        self.snake = Snake(self.screen, snake_position_x, snake_position_y)

        apple_position_x, apple_position_y = self.random_position()
        self.apple = Apple(self.screen, apple_position_x, apple_position_y)
        self.collision = Collision(self.boundary.get_boundaries())
示例#2
0
    def __init__(self, screen, screen_x, info_x, rocket, world):
        self.screen = screen
        self.info_x = info_x
        self.rocket = rocket
        self.world = world

        self.factory = NumberFactory()
        self.basic_unit = 25

        #Setup boundary
        self.info_width = screen_x - self.info_x
        self.boundary = Boundary(self.info_width, 0, 3, screen.get_height(),
                                 screen)
示例#3
0
    def __init__(self, name):
        Component.__init__(self, name, facility="exchanger")

        self.sink = None
        self.source = None

        self.mesh = None
        self.rank = None
        self.communicator = None

        from Boundary import Boundary
        self.boundary = Boundary()

        import journal
        self._info = journal.debug("exchanger")
        return
示例#4
0
    def start_battle(self, clock):
        pygame.display.set_caption("Fight!!")

        # ENTITIES
        # Background
        background = pygame.Surface(self.screen.get_size())
        background.fill((0, 0, 0))
        self.screen.blit(background, (0, 0))
        # Sprites
        top_widget_size = (self.screen.get_size()[0],
                           round(self.screen.get_size()[1] / 10))
        top_widget = Boundary((self.screen.get_size()[0] / 2,
                               round(self.screen.get_size()[1] / 20)),
                              color=(255, 255, 0),
                              size=top_widget_size)
        # Grids
        grid_sprites, safe_grid, grid_position = self.make_grid(
            (0, round(self.screen.get_size()[1] / 10)))
        curr_map = Map(grid_position)
        obsticles = curr_map.make_map(OBSTACLE_GRID_TILE)
        potential_box = curr_map.get_box_position()
        obsticles = pygame.sprite.Group(obsticles)

        # Boundary
        top_boundary = Boundary((600, 69), (255, 255, 255), (1200, 20))
        left_boundary = Boundary((-4, 350), (255, 255, 255), (20, 700))
        right_boundary = Boundary((1204, 350), (255, 255, 255), (20, 700))
        bottom_boundary = Boundary((600, 701), (255, 255, 255), (1200, 20))
        top_boundary.make_invisible()
        left_boundary.make_invisible()
        right_boundary.make_invisible()
        bottom_boundary.make_invisible()
        obsticles.add(top_boundary, left_boundary, right_boundary,
                      bottom_boundary)

        # Player
        player_1 = Player(grid_position[7][32])
        player_2 = Player(grid_position[7][0])
        player_1_attacks = pygame.sprite.Group()
        player_2_attacks = pygame.sprite.Group()

        # Player score zone
        p1_score_zone = Boundary((0, 0), (0, 0, 0), (72, 144))
        p1_score_zone.rect.topleft = (grid_position[6][0][0] - 18,
                                      grid_position[6][0][1] - 18)
        p1_score_zone.make_invisible()
        p2_score_zone = Boundary((0, 0), (0, 0, 0), (72, 144))
        p2_score_zone.rect.topleft = (grid_position[6][31][0] - 18,
                                      grid_position[6][31][1] - 18)
        p2_score_zone.make_invisible()
        score_zone = [p1_score_zone, p2_score_zone]

        # Zombies
        zombie_group = pygame.sprite.Group()
        zombie_group.add(Zombie(player_1, player_2, (500, -5), safe_grid))
        zombie_group.add(Zombie(player_1, player_2, (700, -5), safe_grid))
        zombie_group.add(Zombie(player_1, player_2, (600, 350), safe_grid))
        zombie_group.add(Zombie(player_1, player_2, (500, 705), safe_grid))
        zombie_group.add(Zombie(player_1, player_2, (700, 705), safe_grid))

        # HP Bar
        player_2_bar = StatBar((200, 35), (0, 255, 0), (300, 25), MAX_HP)
        player_1_bar = StatBar((1000, 35), (0, 255, 0), (300, 25), MAX_HP)
        player_1_bar.set_reversed_direction()
        bars = [
            player_2_bar.get_full_bar((255, 0, 0)),
            player_1_bar.get_full_bar((255, 0, 0))
        ]

        # Box

        # Item Box
        box_sprites = pygame.sprite.Group()

        # Goals
        flag = Flag((600, 350))
        goal = CaptureFlag([player_1, player_2], score_zone, flag, num_round=3)
        # Label color
        p1_label_color = (0, 0, 255)
        p2_label_color = (255, 0, 0)
        # Flag
        flag_p2 = Label(44, (400, 35))
        flag_p2.set_color(p2_label_color)
        flag_p1 = Label(44, (780, 35))
        flag_p1.set_color(p1_label_color)

        # timer
        timer = Timer(30, (560, 35), 4, 20, FRAME_RATE)
        # Labels
        p2_label = Label(20, (20, 35))
        p2_label.set_color(p2_label_color)
        p2_label.set_msg("P2  ")
        p1_label = Label(20, (1160, 35))
        p1_label.set_color(p1_label_color)
        p1_label.set_msg("P1")

        p1_head = Label(15, (player_1.rect.centerx - 8, player_1.rect.top - 5))
        p1_head.set_color(p1_label_color)
        p1_head.set_msg("P1")
        p2_head = Label(15, (player_2.rect.centerx - 8, player_2.rect.top - 5))
        p2_head.set_color(p2_label_color)
        p2_head.set_msg("P2")

        p1_weapon = Label(20, (990, 56))
        p1_weapon.set_msg("sada")
        p2_weapon = Label(20, (170, 56))
        p2_weapon.set_msg("sada")

        labels = [
            p1_label, p2_label, p1_head, p2_head, p1_weapon, p2_weapon,
            flag_p1, flag_p2
        ]

        keep_going = True
        box_flag = True
        all_sprites = pygame.sprite.OrderedUpdates(grid_sprites, obsticles,
                                                   flag, player_1, player_2,
                                                   zombie_group, top_widget,
                                                   timer, labels, bars,
                                                   player_1_bar, player_2_bar)
        # LOOP
        while keep_going:
            if goal.screen_pause:
                pygame.time.wait(1500)
                goal.screen_pause = False
            # TIME
            clock.tick(FRAME_RATE)

            if timer.get_real_second() % 5 == 0:
                # Box
                if box_flag:
                    box_flag = False
                    box = self.generate_box(potential_box)
                    box_sprites.add(box)
                    all_sprites.add(box)
            else:
                box_flag = True

            if timer.get_real_second() % 20 == 0 and len(zombie_group) <= 1:
                zombie_group.add(
                    Zombie(player_1, player_2, (500, -5), safe_grid))
                zombie_group.add(
                    Zombie(player_1, player_2, (700, -5), safe_grid))
                zombie_group.add(
                    Zombie(player_1, player_2, (600, 350), safe_grid))
                zombie_group.add(
                    Zombie(player_1, player_2, (500, 705), safe_grid))
                zombie_group.add(
                    Zombie(player_1, player_2, (700, 705), safe_grid))
                all_sprites.add(zombie_group)

            for z in zombie_group:
                z.move(zombie_group)

            # Check for Goal
            if goal.check_for_win():
                keep_going = False
            if timer.time_up():
                keep_going = False

            flag_p1.set_msg(str(goal.score[0]))
            flag_p2.set_msg(str(goal.score[1]))
            # EVENT HANDLING
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return 3
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        keep_going = False

            #Player 1 event handle
            if pygame.key.get_pressed()[pygame.K_RIGHT]:
                temp_block = TempBlock(player_1.rect.center,
                                       player_1.rect.size)
                temp_block.rect.x += player_1.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    # Check the previous directoin
                    if player_1.direction == 1 or player_1.prev_direction == 1:
                        i = 1
                        # Check if the we continue with previous direction for more frames will be able to pass through
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_down()
                                break
                    elif player_1.direction == 3 or player_1.prev_direction == 3:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_up()
                                break
                    else:
                        pass
                else:
                    player_1.go_right()

            elif pygame.key.get_pressed()[pygame.K_DOWN]:
                temp_block = TempBlock(player_1.rect.center,
                                       player_1.rect.size)
                temp_block.rect.y += player_1.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_1.direction == 0 or player_1.prev_direction == 0:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_right()
                                break
                    elif player_1.direction == 2 or player_1.prev_direction == 2:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_left()
                                break
                    else:
                        pass
                else:
                    player_1.go_down()

            elif pygame.key.get_pressed()[pygame.K_UP]:
                temp_block = TempBlock(player_1.rect.center,
                                       player_1.rect.size)
                temp_block.rect.y -= player_1.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_1.direction == 0 or player_1.prev_direction == 0:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_right()
                                break
                    elif player_1.direction == 2 or player_1.prev_direction == 2:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_left()
                                break
                    else:
                        pass
                else:
                    player_1.go_up()

            elif pygame.key.get_pressed()[pygame.K_LEFT]:
                temp_block = TempBlock(player_1.rect.center,
                                       player_1.rect.size)
                temp_block.rect.x -= player_1.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_1.direction == 1 or player_1.prev_direction == 1:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_down()
                                break
                    elif player_1.direction == 3 or player_1.prev_direction == 3:
                        i = 1
                        while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_1.go_up()
                                break
                    else:
                        pass
                else:
                    player_1.go_left()

            if pygame.key.get_pressed()[pygame.K_RCTRL]:
                lst = player_1.attack()
                player_1_attacks.add(lst)
                all_sprites.add(lst)
            # Player 2 event handle
            if pygame.key.get_pressed()[pygame.K_d]:
                temp_block = TempBlock(player_2.rect.center,
                                       player_2.rect.size)
                temp_block.rect.x += player_2.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    # Check the previous directoin
                    if player_2.direction == 1 or player_2.prev_direction == 1:
                        i = 1
                        # Check if the we continue with previous direction for more frames will be able to pass through
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_down()
                                break
                    elif player_2.direction == 3 or player_2.prev_direction == 3:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_up()
                                break
                    else:
                        pass
                else:
                    player_2.go_right()

            elif pygame.key.get_pressed()[pygame.K_s]:
                temp_block = TempBlock(player_2.rect.center,
                                       player_2.rect.size)
                temp_block.rect.y += player_2.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_2.direction == 0 or player_2.prev_direction == 0:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_right()
                                break
                    elif player_2.direction == 2 or player_2.prev_direction == 2:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_left()
                                break
                    else:
                        pass
                else:
                    player_2.go_down()

            elif pygame.key.get_pressed()[pygame.K_w]:
                temp_block = TempBlock(player_2.rect.center,
                                       player_2.rect.size)
                temp_block.rect.y -= player_2.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_2.direction == 0 or player_2.prev_direction == 0:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_right()
                                break
                    elif player_2.direction == 2 or player_2.prev_direction == 2:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.x -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_left()
                                break
                    else:
                        pass
                else:
                    player_2.go_up()

            elif pygame.key.get_pressed()[pygame.K_a]:
                temp_block = TempBlock(player_2.rect.center,
                                       player_2.rect.size)
                temp_block.rect.x -= player_2.velocity
                if pygame.sprite.spritecollide(temp_block, obsticles, False):
                    if player_2.direction == 1 or player_2.prev_direction == 1:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y += 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_down()
                                break
                    elif player_2.direction == 3 or player_2.prev_direction == 3:
                        i = 1
                        while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY:
                            i += 1
                            temp_block.rect.y -= 1
                            if not pygame.sprite.spritecollide(
                                    temp_block, obsticles, False):
                                player_2.go_up()
                                break
                    else:
                        pass
                else:
                    player_2.go_left()

            if pygame.key.get_pressed()[pygame.K_SPACE]:
                lst2 = player_2.attack()
                player_2_attacks.add(lst2)
                all_sprites.add(lst2)

            # Collisions handleling
            for box1 in pygame.sprite.spritecollide(player_1, box_sprites,
                                                    False):
                temp_label = TempLabel(
                    15, (player_1.rect.centerx - 8, player_1.rect.top - 5), 3,
                    FRAME_RATE)
                temp_label.set_msg(box1.__str__())
                all_sprites.add(temp_label)
                box1.collide(player_1)
            for box2 in pygame.sprite.spritecollide(player_2, box_sprites,
                                                    False):
                box2.collide(player_2)
                temp_label = TempLabel(
                    15, (player_2.rect.centerx - 8, player_2.rect.top - 5), 3,
                    FRAME_RATE)
                temp_label.set_msg(box2.__str__())
                all_sprites.add(temp_label)

            # Zombie hit player
            for zombie in pygame.sprite.spritecollide(player_1, zombie_group,
                                                      False):
                zombie.collide(player_1)
            for zombie in pygame.sprite.spritecollide(player_2, zombie_group,
                                                      False):
                zombie.collide(player_2)

            # Players hit zombies
            for zombie in zombie_group:
                for bullet in pygame.sprite.spritecollide(
                        zombie, player_2_attacks, False):
                    bullet.collide(zombie)
                for bullet in pygame.sprite.spritecollide(
                        zombie, player_1_attacks, False):
                    bullet.collide(zombie)

            # bullet_hit_walls
            for wall in obsticles:
                for bullet in pygame.sprite.spritecollide(
                        wall, player_1_attacks, False):
                    bullet.end()
                for bullet in pygame.sprite.spritecollide(
                        wall, player_2_attacks, False):
                    bullet.end()
            # Player_1 attacks
            for hit in pygame.sprite.spritecollide(player_2, player_1_attacks,
                                                   False):
                hit.collide(player_2)

            # Player_2 attacks
            for hit in pygame.sprite.spritecollide(player_1, player_2_attacks,
                                                   False):
                hit.collide(player_1)

            # Player hp bar
            player_2_bar.set_curr_stat(player_2.health)
            player_1_bar.set_curr_stat(player_1.health)

            # Player head
            p1_head.set_position(
                (player_1.rect.centerx - 8, player_1.rect.top - 5))
            p2_head.set_position(
                (player_2.rect.centerx - 8, player_2.rect.top - 5))

            p1_weapon.set_msg(str(player_1.weapon))
            p2_weapon.set_msg(str(player_2.weapon))

            if player_1.is_dead():
                if flag.get_player() == player_1:
                    flag.drop()
                player_1.respawn()

            if player_2.is_dead():
                if flag.get_player() == player_2:
                    flag.drop()
                player_2.respawn()
            for zombie in zombie_group:
                if zombie.is_dead(): zombie.kill()

            all_sprites.clear(self.screen, background)
            all_sprites.update()
            all_sprites.draw(self.screen)
            pygame.display.flip()

            lst = player_1.weapon.update()
            player_1_attacks.add(lst)
            all_sprites.add(lst)

            lst = player_2.weapon.update()
            player_2_attacks.add(lst)
            all_sprites.add(lst)

        end_msg = Label(60, (500, 350))
        end_msg.set_color((255, 255, 0))
        end_msg.set_msg(goal.get_wining_msg())
        self.screen.blit(end_msg.image, end_msg)
        pygame.display.flip()
        pygame.time.wait(2000)
        return 2
示例#5
0
    winningPlayer = myfont.render(winnerText, False, WHITE)

    while (run):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
                quit()

        screen.fill(BLACK)

        screen.blit(header, (350, 100))
        screen.blit(winningPlayer, (350, 300))
        pygame.display.update()


boundaryArray.append(Boundary((0, 0), (0, WINDOW_HEIGHT), BLACK))
boundaryArray.append(Boundary((0, 0), (WINDOW_WIDTH, 0), BLACK))
boundaryArray.append(
    Boundary((0, WINDOW_HEIGHT), (WINDOW_WIDTH, WINDOW_HEIGHT), BLACK))
boundaryArray.append(
    Boundary((WINDOW_WIDTH, 0), (WINDOW_WIDTH, WINDOW_HEIGHT), BLACK))
boundaryArray.append(
    Boundary((WINDOW_WIDTH / 2, 0), (WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2),
             BLACK))

playerArray.append(Player(100, 100, GREEN, 45, 1))
playerArray.append(Player(700, 100, RED, 135, 2))

game1 = Game(boundaryArray, playerArray, screen, [0, 0])

game_menu()
示例#6
0
#pygame configurations
pygame.init()
pygame.display.set_caption("2D Raymarching")
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
fps = 60
screen_offset = 50

object_count = 10
angle = 0
objects = []

for i in range(object_count):
    obj = Boundary(randint(screen_offset, width - screen_offset),
                   randint(screen_offset, height - screen_offset),
                   randint(20, 100))
    objects.append(obj)

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    ray = Ray(width // 2, height // 2, 0, screen, white)
    ray.angle = angle

    screen.fill(black)
    for object in objects: