Beispiel #1
0
def main(mode, agent, modelpath):
	screen = initGame()
	# load the necessary game resources
	# --load the game sounds
	sounds = dict()
	for key, value in cfg.AUDIO_PATHS.items():
		sounds[key] = pygame.mixer.Sound(value)
	# --load the score digital images
	number_images = dict()
	for key, value in cfg.NUMBER_IMAGE_PATHS.items():
		number_images[key] = pygame.image.load(value).convert_alpha()
	# --the pipes
	pipe_images = dict()
	pipe_images['bottom'] = pygame.image.load(random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()
	pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)
	# --the bird images
	bird_images = dict()
	for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(list(cfg.BIRD_IMAGE_PATHS.keys()))].items():
		bird_images[key] = pygame.image.load(value).convert_alpha()
	# --the background images
	backgroud_image = pygame.image.load(random.choice(list(cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()
	# --other images
	other_images = dict()
	for key, value in cfg.OTHER_IMAGE_PATHS.items():
		other_images[key] = pygame.image.load(value).convert_alpha()
	# the start interface of our game
	game_start_info = startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg, mode)
	# enter the main game loop
	score = 0
	bird_pos, base_pos, bird_idx = list(game_start_info.values())
	base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
	clock = pygame.time.Clock()
	# --the instanced class of pipe
	pipe_sprites = pygame.sprite.Group()
	for i in range(2):
		pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
		pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('top')[-1]), type_='top'))
		pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('bottom')[-1]), type_='bottom'))
	# --the instanced class of bird
	bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
	# --whether add the pipe or not
	is_add_pipe = True
	# --whether the game is running or not
	is_game_running = True
	action = 1
	while is_game_running:
		screen.fill(0)
		for event in pygame.event.get():
			if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
				if mode == 'train': agent.saveModel(modelpath)
				pygame.quit()
				sys.exit()
		# --a general reward
		reward = 0.1
		# --use DQNAgent to play flappybird
		if action:
			bird.setFlapped()
			sounds['wing'].play()
		# --check the collide between bird and pipe
		for pipe in pipe_sprites:
			if pygame.sprite.collide_mask(bird, pipe):
				sounds['hit'].play()
				is_game_running = False
				reward = -1
		# --update the bird
		boundary_values = [0, base_pos[-1]]
		is_dead = bird.update(boundary_values)
		if is_dead:
			sounds['hit'].play()
			is_game_running = False
			reward = -1
		# --move the bases to the left to achieve the effect of bird flying forward
		base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
		# --move the pipes to the left to achieve the effect of bird flying forward
		flag = False
		for pipe in pipe_sprites:
			pipe.rect.left -= 4
			if pipe.rect.centerx <= bird.rect.centerx and not pipe.used_for_score:
				pipe.used_for_score = True
				score += 0.5
				reward = 1
				if '.5' in str(score):
					sounds['point'].play()
			if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
				pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
				pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=pipe_pos.get('top'), type_='top'))
				pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=pipe_pos.get('bottom'), type_='bottom'))
				is_add_pipe = False
			elif pipe.rect.right < 0:
				pipe_sprites.remove(pipe)
				flag = True
		if flag: is_add_pipe = True
		# --get image
		pipe_sprites.draw(screen)
		bird.draw(screen)
		image = pygame.surfarray.array3d(pygame.display.get_surface())
		image = image[:, :int(0.79*cfg.SCREENHEIGHT), :]
		# --blit the necessary game elements on the screen
		screen.blit(backgroud_image, (0, 0))
		pipe_sprites.draw(screen)
		screen.blit(other_images['base'], base_pos)
		showScore(screen, score, number_images)
		bird.draw(screen)
		# --record the action and corresponding reward
		agent.record(action, reward, score, is_game_running, image)
		# --make decision
		action = agent.nextAction(reward)
		# --update screen
		pygame.display.update()
		clock.tick(cfg.FPS)
	# the end interface of our game
	endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg, mode)
Beispiel #2
0
def main():
    screen = initGame()
    # 加载必要的游戏资源
    # --导入音频
    sounds = dict()
    for key, value in cfg.AUDIO_PATHS.items():
        sounds[key] = pygame.mixer.Sound(value)
    # --导入数字图片
    number_images = dict()
    for key, value in cfg.NUMBER_IMAGE_PATHS.items():
        number_images[key] = pygame.image.load(value).convert_alpha()
    # --管道
    pipe_images = dict()
    pipe_images['bottom'] = pygame.image.load(
        random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()
    pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)
    # --小鸟图片
    bird_images = dict()
    for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(
            list(cfg.BIRD_IMAGE_PATHS.keys()))].items():
        bird_images[key] = pygame.image.load(value).convert_alpha()
    # --背景图片
    backgroud_image = pygame.image.load(
        random.choice(list(
            cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()
    # --其他图片
    other_images = dict()
    for key, value in cfg.OTHER_IMAGE_PATHS.items():
        other_images[key] = pygame.image.load(value).convert_alpha()
    # 游戏开始界面
    game_start_info = startGame(screen, sounds, bird_images, other_images,
                                backgroud_image, cfg)
    # 进入主游戏
    score = 0
    bird_pos, base_pos, bird_idx = list(game_start_info.values())
    base_diff_bg = other_images['base'].get_width(
    ) - backgroud_image.get_width()
    clock = pygame.time.Clock()
    # --管道类
    pipe_sprites = pygame.sprite.Group()
    for i in range(2):
        pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
        pipe_sprites.add(
            Pipe(image=pipe_images.get('top'),
                 position=(cfg.SCREENWIDTH + 200 + i * cfg.SCREENWIDTH / 2,
                           pipe_pos.get('top')[-1])))
        pipe_sprites.add(
            Pipe(image=pipe_images.get('bottom'),
                 position=(cfg.SCREENWIDTH + 200 + i * cfg.SCREENWIDTH / 2,
                           pipe_pos.get('bottom')[-1])))
    # --bird类
    bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
    # --是否增加pipe
    is_add_pipe = True
    # --游戏是否进行中
    is_game_running = True
    while is_game_running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN
                                             and event.key == pygame.K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                    bird.setFlapped()
                    sounds['wing'].play()
        # --碰撞检测
        for pipe in pipe_sprites:
            if pygame.sprite.collide_mask(bird, pipe):
                sounds['hit'].play()
                is_game_running = False
        # --更新小鸟
        boundary_values = [0, base_pos[-1]]
        is_dead = bird.update(boundary_values,
                              float(clock.tick(cfg.FPS)) / 1000.)
        if is_dead:
            sounds['hit'].play()
            is_game_running = False
        # --移动base实现小鸟往前飞的效果
        base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
        # --移动pipe实现小鸟往前飞的效果
        flag = False
        for pipe in pipe_sprites:
            pipe.rect.left -= 4
            if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:
                pipe.used_for_score = True
                score += 0.5
                if '.5' in str(score):
                    sounds['point'].play()
            if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
                pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
                pipe_sprites.add(
                    Pipe(image=pipe_images.get('top'),
                         position=pipe_pos.get('top')))
                pipe_sprites.add(
                    Pipe(image=pipe_images.get('bottom'),
                         position=pipe_pos.get('bottom')))
                is_add_pipe = False
            elif pipe.rect.right < 0:
                pipe_sprites.remove(pipe)
                flag = True
        if flag: is_add_pipe = True
        # --绑定必要的元素在屏幕上
        screen.blit(backgroud_image, (0, 0))
        pipe_sprites.draw(screen)
        screen.blit(other_images['base'], base_pos)
        showScore(screen, score, number_images)
        bird.draw(screen)
        pygame.display.update()
        clock.tick(cfg.FPS)
    endGame(screen, sounds, showScore, score, number_images, bird,
            pipe_sprites, backgroud_image, other_images, base_pos, cfg)
Beispiel #3
0
def main(mode, policy, agent, modelpath):
    screen = initGame()
    # load the necessary game resources
    # --load the game sounds
    sounds = dict()
    for key, value in cfg.AUDIO_PATHS.items():
        sounds[key] = pygame.mixer.Sound(value)
    # --load the score digital images
    number_images = dict()
    for key, value in cfg.NUMBER_IMAGE_PATHS.items():
        number_images[key] = pygame.image.load(value).convert_alpha()
    # --the pipes
    pipe_images = dict()
    pipe_images['bottom'] = pygame.image.load(
        random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()
    pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)
    # --the bird images
    bird_images = dict()
    for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(
            list(cfg.BIRD_IMAGE_PATHS.keys()))].items():
        bird_images[key] = pygame.image.load(value).convert_alpha()
    # --the background images
    backgroud_image = pygame.image.load(
        random.choice(list(
            cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()
    # --other images
    other_images = dict()
    for key, value in cfg.OTHER_IMAGE_PATHS.items():
        other_images[key] = pygame.image.load(value).convert_alpha()
    # the start interface of our game
    game_start_info = startGame(screen, sounds, bird_images, other_images,
                                backgroud_image, cfg, mode)
    # enter the main game loop
    score = 0
    bird_pos, base_pos, bird_idx = list(game_start_info.values())
    base_diff_bg = other_images['base'].get_width(
    ) - backgroud_image.get_width()
    clock = pygame.time.Clock()
    # --the instanced class of pipe
    pipe_sprites = pygame.sprite.Group()
    for i in range(2):
        pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
        pipe_sprites.add(
            Pipe(image=pipe_images.get('top'),
                 position=(cfg.SCREENWIDTH + 200 + i * cfg.SCREENWIDTH / 2,
                           pipe_pos.get('top')[-1]),
                 type_='top'))
        pipe_sprites.add(
            Pipe(image=pipe_images.get('bottom'),
                 position=(cfg.SCREENWIDTH + 200 + i * cfg.SCREENWIDTH / 2,
                           pipe_pos.get('bottom')[-1]),
                 type_='bottom'))
    # --the instanced class of bird
    bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
    # --whether add the pipe or not
    is_add_pipe = True
    # --whether the game is running or not
    is_game_running = True
    while is_game_running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN
                                             and event.key == pygame.K_ESCAPE):
                if mode == 'train': agent.saveModel(modelpath)
                pygame.quit()
                sys.exit()
        # --use QLearningAgent to play flappybird
        delta_x = 10000
        delta_y = 10000
        for pipe in pipe_sprites:
            if pipe.type_ == 'bottom' and (pipe.rect.left - bird.rect.left +
                                           30) > 0:
                if pipe.rect.right - bird.rect.left < delta_x:
                    delta_x = pipe.rect.left - bird.rect.left
                    delta_y = pipe.rect.top - bird.rect.top
        delta_x = int((delta_x + 60) / 5)
        delta_y = int((delta_y + 225) / 5)
        if agent.act(delta_x, delta_y, int(bird.speed + 9)):
            bird.setFlapped()
            sounds['wing'].play()
        # --check the collide between bird and pipe
        for pipe in pipe_sprites:
            if pygame.sprite.collide_mask(bird, pipe):
                sounds['hit'].play()
                is_game_running = False
        # --update the bird
        boundary_values = [0, base_pos[-1]]
        is_dead = bird.update(boundary_values)
        if is_dead:
            sounds['hit'].play()
            is_game_running = False
        # --if game over, update qvalues_storage
        if not is_game_running:
            agent.update(score, True) if mode == 'train' else agent.update(
                score, False)
        # --move the bases to the left to achieve the effect of bird flying forward
        base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
        # --move the pipes to the left to achieve the effect of bird flying forward
        flag = False
        reward = 1
        for pipe in pipe_sprites:
            pipe.rect.left -= 4
            if pipe.rect.centerx <= bird.rect.centerx and not pipe.used_for_score:
                pipe.used_for_score = True
                score += 0.5
                reward = 5
                if '.5' in str(score):
                    sounds['point'].play()
            if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
                pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
                pipe_sprites.add(
                    Pipe(image=pipe_images.get('top'),
                         position=pipe_pos.get('top'),
                         type_='top'))
                pipe_sprites.add(
                    Pipe(image=pipe_images.get('bottom'),
                         position=pipe_pos.get('bottom'),
                         type_='bottom'))
                is_add_pipe = False
            elif pipe.rect.right < 0:
                pipe_sprites.remove(pipe)
                flag = True
        if flag: is_add_pipe = True
        # --set reward
        if mode == 'train' and is_game_running: agent.setReward(reward)
        # --blit the necessary game elements on the screen
        screen.blit(backgroud_image, (0, 0))
        pipe_sprites.draw(screen)
        screen.blit(other_images['base'], base_pos)
        showScore(screen, score, number_images)
        bird.draw(screen)
        pygame.display.update()
        clock.tick(cfg.FPS)
    # the end interface of our game
    endGame(screen, sounds, showScore, score, number_images, bird,
            pipe_sprites, backgroud_image, other_images, base_pos, cfg, mode)