Example #1
0
    def __init__(self):
        # state
        self.w_close = False
        self.w_pause = False

        # objects
        self.size = read_size(self.file_map)
        self.after_wait = 0
        self.field_map = read_map(self.file_map)
        self.field_size = (self.size[0] * part, self.size[1] * part)
        self.screen_size = (self.field_size[0], self.field_size[1] + 70)
        self.screen = pygame.display.set_mode(self.screen_size)
        self.my_font = pygame.font.Font("images/111.ttf", 30)
        self.pacman_life = pygame.image.load("images/pacman.png")
        self.pacman_life = pygame.transform.scale(self.pacman_life, (50, 50))
        self.geom_life = self.pacman_life.get_rect()
        self.bg = pygame.image.load(self.bg_name)
        self.bg = pygame.transform.scale(self.bg, self.screen_size)
        self.bg_geom = self.bg.get_rect()

        self.button_pause = Button((10, self.field_size[1] + 10, 100, 50),
                                   BLACK,
                                   self.button_action_pause,
                                   text='Pause',
                                   **BUTTON_STYLE)
        self.button_exit = Button((120, self.field_size[1] + 10, 100, 50),
                                  BLACK,
                                  self.button_action_exit,
                                  text='Exit',
                                  **BUTTON_STYLE)

        self.field = Field(self.field_map, self.size)
        self.food = Food(self.field)
        self.hero = PacMan(start, self.size)
        self.ghost1 = Ghost(start_ghost1, 1)
        self.ghost2 = Ghost(start_ghost2, 2)
        self.ghost3 = Ghost(start_ghost3, 3)
        self.ghost4 = Ghost(start_ghost4, 4)

        self.level = 1
        self.life = 3
        self.score = 0
Example #2
0
    def spawnFood(self):
        if len(self.snake.pieces) == self.grid_size_x * self.grid_size_y:
            self.reset()
            return

        x = random.randint(0, self.grid_size_x - 1)
        y = random.randint(0, self.grid_size_y - 1)
        if self.snake.contains(x, y):
            self.spawnFood()
            return

        food = Food(x, y, self.block_size_x, self.block_size_y)
        self.food = food
Example #3
0
class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((GAME_HEIGHT, GAME_WIDTH))
        self.clock = pygame.time.Clock()
        self.food = Food(FOOD_COLOR)
        self.snake = Snake(SNAKE_COLOR)
        self.game_over = False

    def run(self):
        # update core game components
        self.clock.tick(100 * (1 + len(self.snake.body) / 50))
        self.screen.fill(pygame.Color(0,0,0))
	    
        # render the snake and food for the game
        self.food.render(self.screen)
        self.snake.render(self.screen)

        # update the snake and check for lose conditions
        self.snake.snake_controls(self.food)
        self.game_over = self.snake.check_lose()

        return self.game_over
Example #4
0
def parse_tick(data: dict) -> Tick:
    me = data.get('Mine')

    food = []
    virus = {}
    opponents = {}
    for item in data.get('Objects'):
        type_ = item.get('T')
        if type_ == 'F':
            food.append(Food(item))

        if type_ == 'P':
            id_, pid = Player.get_id(item.get('Id'))
            opponents.setdefault(id_, []).append(item)

        if type_ == 'V':
            id_ = item.get('Id')
            virus[id_] = Virus(item)

    return Tick(me, opponents, food, virus, None)
Example #5
0
def _simulate_environment(settings):
    foods = []
    for _ in range(0, settings['food_num']):
        foods.append(Food(settings))

    organisms = []
    for org in range(0, settings['org_num']):
        wih_init = np.random.uniform(
            -1, 1, (settings['hnodes'],
                    settings['inodes']))  # mlp weights (input -> hidden)
        who_init = np.random.uniform(
            -1, 1, (settings['onodes'],
                    settings['hnodes']))  # mlp weights (hidden -> output)

        organisms.append(
            Organism(settings,
                     wih_init,
                     who_init,
                     name=first_gen_org_name_template(org)))

    predators = []
    if settings['pred_create']:
        for pred in range(0, settings['pred_num']):
            wih_init = np.random.uniform(
                -1, 1, (settings['hnodes'],
                        settings['inodes']))  # mlp weights (input -> hidden)
            who_init = np.random.uniform(
                -1, 1, (settings['onodes'],
                        settings['hnodes']))  # mlp weights (hidden -> output)

            predators.append(
                Predator(settings,
                         wih_init,
                         who_init,
                         name=first_gen_org_name_template(pred)))

    return predators, organisms, foods
Example #6
0
def create_food(food, game_surface):
	while len(food) < 5:
		food.append(Food(game_surface))

	return food
Example #7
0
        head = {'x': x_pos, 'y': y_pos}
        snake_size.append(head)

        if len(snake_size) > snake_length:
            del snake_size[0]

        for x in snake_size[:-1]:
            if x == head:
                close_game = True

        SNAKE.draw(snake_size, snake_block)
        WINDOW.display_score(str(snake_length - 1))
        WINDOW.refresh()

        if x_pos == Food.x_pos and y_pos == Food.y_pos:
            FOOD.random_position(snake_block)
            snake_length += 1

        WINDOW.snake_speed(Snake.SPEED)

    Windows.quit()
    sys.exit()


WINDOW = setup(Window.MEDIUM_SIZE, Window.TITLE)
BLOCK = Block(WINDOW)
FOOD = Food(copy(BLOCK), Colors.GREEN)
SNAKE = Python(copy(BLOCK), Colors.GRAY)

game_loop()
Example #8
0
def main():
    # initializing the pygame module
    pygame.init()

    # general information
    logo = pygame.image.load(ICON_PATH)
    pygame.display.set_icon(logo)
    pygame.display.set_caption('Snake')

    # creating screen
    screen = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT))
    screen.fill(BACKGROUND_COLOR)
    pygame.display.flip()

    # creating snake2 object
    snake = Snake(screen, SNAKE_START_LOCATION)

    # creating food object
    food = Food(screen)
    food.update(snake.last_locations)

    # creating highscore object
    highscore = Highscore()

    # creating highscore object

    # set game to running
    running = True
    clock = pygame.time.Clock()

    # initializing game_over variable
    game_over = False

    # mainloop
    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if event.type == pygame.KEYDOWN:
                # other controls
                # restart
                if event.key == pygame.K_r:
                    running = False
                    main()

                # quit
                if event.key == pygame.K_ESCAPE:
                    running = False

                # movement keys
                if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    snake.change_direction('r')

                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    snake.change_direction('l')

                if event.key == pygame.K_UP or event.key == pygame.K_w:
                    snake.change_direction('u')

                if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    snake.change_direction('d')

        if not game_over:

            # 'clears' the background
            screen.fill(BACKGROUND_COLOR)

            # moves snake2
            snake.update()

            # checks if snake2 is on the screen. if not = Game Over
            if not snake.is_on_screen():
                game_over = True

            # checks if the snake2 hit its tail
            if snake.hit_tail():
                game_over = True

            # draws snake2
            snake.draw()

            # check if snake2 hits food
            if snake.x == food.x and snake.y == food.y:
                snake.length += 1
                food.update(snake.last_locations)

                # check if score is over the highscore
                if snake.length - 1 > highscore.get():
                    highscore.score = snake.length - 1

            # draw food rectangle
            food.draw()

            # update score
            font = pygame.font.SysFont('bahnschrift', 20)
            tag = font.render(f'Score: {snake.length - 1}', True, WHITE)
            screen.blit(tag, (25, 25))

            # update highscore
            font = pygame.font.SysFont('bahnschrift', 20)
            tag = font.render(f'Highscore: {highscore.get()}', True, WHITE)
            screen.blit(tag, (250, 25))

        if game_over:
            screen.fill((194, 61, 128))

            highscore.update()

            go_font = pygame.font.SysFont('times new roman', 40)
            go_tag = go_font.render('GAME OVER', True, WHITE)

            score_font = pygame.font.SysFont('bahnschrift', 30)
            score_tag = score_font.render(f'your score: {snake.length -1}',
                                          True, WHITE)

            ctrl_font = pygame.font.SysFont('bahnschrift', 30)
            ctrl_tag = ctrl_font.render('r: restart       esc: quit', True,
                                        WHITE)

            screen.blits(((go_tag, GO_TAG_LOCATION),
                          (score_tag, (GAME_WIDTH / 4, GAME_HEIGHT * 0.75)),
                          (ctrl_tag, (GAME_WIDTH / 8, GAME_HEIGHT - 50))))

        # update display
        pygame.display.flip()
Example #9
0
class Game:
    file_records = 'text_files/records.txt'
    file_map = 'text_files/map.txt'
    name = 'Artem '
    bg_name = 'images/images.jpg'

    def __init__(self):
        # state
        self.w_close = False
        self.w_pause = False

        # objects
        self.size = read_size(self.file_map)
        self.after_wait = 0
        self.field_map = read_map(self.file_map)
        self.field_size = (self.size[0] * part, self.size[1] * part)
        self.screen_size = (self.field_size[0], self.field_size[1] + 70)
        self.screen = pygame.display.set_mode(self.screen_size)
        self.my_font = pygame.font.Font("images/111.ttf", 30)
        self.pacman_life = pygame.image.load("images/pacman.png")
        self.pacman_life = pygame.transform.scale(self.pacman_life, (50, 50))
        self.geom_life = self.pacman_life.get_rect()
        self.bg = pygame.image.load(self.bg_name)
        self.bg = pygame.transform.scale(self.bg, self.screen_size)
        self.bg_geom = self.bg.get_rect()

        self.button_pause = Button((10, self.field_size[1] + 10, 100, 50),
                                   BLACK,
                                   self.button_action_pause,
                                   text='Pause',
                                   **BUTTON_STYLE)
        self.button_exit = Button((120, self.field_size[1] + 10, 100, 50),
                                  BLACK,
                                  self.button_action_exit,
                                  text='Exit',
                                  **BUTTON_STYLE)

        self.field = Field(self.field_map, self.size)
        self.food = Food(self.field)
        self.hero = PacMan(start, self.size)
        self.ghost1 = Ghost(start_ghost1, 1)
        self.ghost2 = Ghost(start_ghost2, 2)
        self.ghost3 = Ghost(start_ghost3, 3)
        self.ghost4 = Ghost(start_ghost4, 4)

        self.level = 1
        self.life = 3
        self.score = 0

    def main_loop(self):
        while not self.w_close:
            self.process_events()
            self.game_logic()
            self.draw()
            self.after_game()
            pygame.time.wait(20)
        self.write_score()

    def after_game(self):
        if self.after_wait != 0:
            pygame.time.wait(500)

    def write_score(self):
        file_score = open(self.file_records, 'a')
        score_line = self.name + str(self.score) + '\n'
        file_score.write(score_line)
        file_score.close()

    def button_action_pause(self):
        if self.w_pause:
            self.w_pause = False
        else:
            self.w_pause = True

    def button_action_exit(self):
        self.w_close = True

    def process_events(self):
        events = pygame.event.get()
        for event in events:
            self.button_pause.check_event(event)  # обновляем кнопки
            self.button_exit.check_event(event)
            if event.type == pygame.QUIT:
                self.w_close = True
            if event.type == pygame.KEYDOWN and not self.w_pause:
                self.hero.change_direct(event.key)

    def game_logic(self):
        if not self.w_pause:
            self.hero.shift(self.field, self.food, self.ghost1, self.ghost2,
                            self.ghost3, self.ghost4)
            self.ghost1.shift(self.field, self.hero, self.ghost2, self.ghost3,
                              self.ghost4)
            self.ghost2.shift(self.field, self.hero, self.ghost1, self.ghost3,
                              self.ghost4)
            self.ghost3.shift(self.field, self.hero, self.ghost1, self.ghost2,
                              self.ghost4)
            self.ghost4.shift(self.field, self.hero, self.ghost2, self.ghost3,
                              self.ghost1)
            self.score = self.hero.score

        if self.food.check_last_level():
            if not self.food.is_food():
                self.food.new_level()
                self.level += 1
                self.food.level += 1
        else:
            self.w_close = True

        if self.life > self.hero.death:
            self.life = self.hero.death
        if self.life == 0:
            self.after_wait = 1
            self.w_close = True

    def draw(self):
        self.screen.fill(BLACK)
        # self.screen.blit(self.bg, self.bg_geom)
        self.button_exit.update(self.screen)
        self.button_pause.update(self.screen)
        self.draw_elements()
        self.field.show(self.screen, self.size)
        self.food.show(self.screen)
        self.hero.draw(self.screen)
        self.draw_life()
        self.screen.blit(self.ghost1.ghost, self.ghost1.geometry)
        self.screen.blit(self.ghost2.ghost, self.ghost2.geometry)
        self.screen.blit(self.ghost3.ghost, self.ghost3.geometry)
        self.screen.blit(self.ghost4.ghost, self.ghost4.geometry)
        pygame.display.flip()

    def draw_elements(self):
        pygame.draw.rect(self.screen, BLACK,
                         (230, self.field_size[1] + 10, 50,
                          50))  # выводим на экран данный уровень
        text_pole1 = self.my_font.render(str(self.level), True,
                                         (255, 255, 255))
        self.screen.blit(text_pole1, (250, self.field_size[1] + 24))
        pygame.draw.rect(
            self.screen, BLACK,
            (290, self.field_size[1] + 10, 70, 50))  # выводим на экран очки
        text_pole2 = self.my_font.render(str(self.score), True,
                                         (255, 255, 255))
        self.screen.blit(text_pole2, (300, self.field_size[1] + 24))

    def draw_life(self):
        if self.life == 1:
            self.geom_life.x = 400
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
        if self.life == 2:
            self.geom_life.x = 400
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
            self.geom_life.x = 460
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
        if self.life == 3:
            self.geom_life.x = 400
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
            self.geom_life.x = 460
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
            self.geom_life.x = 520
            self.geom_life.y = self.field_size[1] + 10
            self.screen.blit(self.pacman_life, self.geom_life)
Example #10
0
 def __init__(self):
     self.screen = pygame.display.set_mode((GAME_HEIGHT, GAME_WIDTH))
     self.clock = pygame.time.Clock()
     self.food = Food(FOOD_COLOR)
     self.snake = Snake(SNAKE_COLOR)
     self.game_over = False