Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_keys_pressed does not get fired #1

Open
oneandonlyonebutyou opened this issue May 12, 2020 · 2 comments
Open

get_keys_pressed does not get fired #1

oneandonlyonebutyou opened this issue May 12, 2020 · 2 comments

Comments

@oneandonlyonebutyou
Copy link

Thank you for your wonderful tutorial
For some reason, the function "get_keys_pressed" never gets fired..

Before I start programming the ML, I want the play randomly (Up down, left,right ) ...

from pygame.constants import K_DOWN, K_UP, K_LEFT, K_RIGHT
from pygame_player import PyGamePlayer
import random
from Game.Snake import UP, DOWN, LEFT, RIGHT


class SnakePlayer(PyGamePlayer):
    def __init__(self):
        """  
    Example class for playing Pong  
    """
        super(SnakePlayer, self).__init__(
            force_game_fps=10
        )  # we want to run at 10 frames per second
        self.last_score = 0.0

    def get_keys_pressed(self, screen_array, feedback):
        # The code for running the actual learning agent would go here with the screen_array and feeback as the inputs
        # and an output for each key in the game, activating them if they are over some threshold.
        self._last_action = self._choose_next_action()
        return SnakePlayer._key_presses_from_action(self._last_action)

    def get_feedback(self):
        # import must be done here because otherwise importing would cause the game to start playing
        from Game.Snake import GAME_SCORE

        # get the difference in score between this and the last run
        score_change = GAME_SCORE - self.last_score
        self.last_score = GAME_SCORE
        return score_change, score_change != 0

    def _choose_next_action(self):
        **return random.choice([UP, DOWN, LEFT, RIGHT])**


if __name__ == "__main__":
    player = SnakePlayer()
    player.start()
    # importing will start the game playing
    import Game.Snake
@oneandonlyonebutyou
Copy link
Author

Btw, the Snake game is written by me

@luckymouse0
Copy link
Owner

@joseph-vedadi The exploration part is already done, you don't need to do it manually. Without the full code can't see why it isn't working... try to isolate where is the problem using breakpoints or prints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants