Exemplo n.º 1
0
 def __init__(self):
     pyxel.init(consts.SCREEN_WIDTH,
                consts.SCREEN_HEIGHT,
                caption='PyPong',
                scale=consts.SCALE)
     self.state = 'start'
     self.single_player = True
     self.table = Table()
     self.start_menu = Menus(consts.START_SCREEN)
     self.controls_menu = Controls(consts.CONTROLS_SCREEN_OPTIONS)
     self.bot_menu = Menus(consts.BOT_SCREEN_OPTIONS)
     self.pause_menu = Menus(consts.PAUSE_SCREEN_OPTIONS)
     self.win_screen = Win(consts.WIN_SCREEN)
     self.first_paddle = Paddle(player=1)
     self.second_paddle = Paddle(player=2)
     self.ball = Ball(x_direction_right=True)
     self.score_time = 0
     self.winner = None
     pyxel.run(self.update, self.draw)
Exemplo n.º 2
0
 def create_paddle(self):
     paddle = Paddle(x=(config.SCREEN_WIDTH - config.PADDLE_WIDTH) // 2,
                     y=config.SCREEN_HEIGHT - config.PADDLE_HEIGHT * 2,
                     w=config.PADDLE_WIDTH,
                     h=config.PADDLE_HEIGHT,
                     color=config.PADDLE_COLOR,
                     offset=config.PADDLE_SPEED)
     self.keydown_handlers[pygame.K_LEFT].append(paddle.handle)
     self.keydown_handlers[pygame.K_RIGHT].append(paddle.handle)
     self.keyup_handlers[pygame.K_LEFT].append(paddle.handle)
     self.keyup_handlers[pygame.K_RIGHT].append(paddle.handle)
     self.paddle = paddle
     self.objects.append(self.paddle)
Exemplo n.º 3
0
from game_objects.ball import Ball
from game_objects.paddle import Paddle
from game_utils.utils import add_to_sprites_list

if __name__ == '__main__':
    WHITE = (255,255,255)
    BLACK = (0,0,0)
    pygame.init()

    window_size = (700,500)
    ball_radius = 20
    display = pygame.display.set_mode(window_size)
    pygame.display.set_caption("neural pong")

    paddleA = Paddle(10, 100, WHITE, window_size[1])
    paddleA.rect.x = 20
    paddleA.rect.y = 200

    ball = Ball(ball_radius,ball_radius,WHITE)
    ball.rect.x = 100
    ball.rect.y = 100

    sprites_list = pygame.sprite.Group()
    add_to_sprites_list(sprites_list, [paddleA, ball])
    run = True

    timer = pygame.time.Clock()

    while run:
Exemplo n.º 4
0
def eval_genome(genome, config):
    net = neat.nn.FeedForwardNetwork.create(genome, config)

    miss = 0.0
    hits = 0.0
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    pygame.init()

    window_size = (700, 500)
    ball_radius = 20
    ball_velocity = 2
    display = pygame.display.set_mode(window_size)
    pygame.display.set_caption("neural pong")
    timer = pygame.time.Clock()

    for runs in range(runs_per_net):
        sim_time = 0.0
        paddle = Paddle(10, 100, WHITE, window_size[1])
        paddle.rect.x = 20
        paddle.rect.y = window_size[1] // 2

        ball = Ball(ball_radius,
                    ball_radius,
                    WHITE,
                    velocity=[ball_velocity, ball_velocity])
        ball.rect.x = window_size[0] // 2
        ball.rect.y = window_size[1] // 2

        sprites_list = pygame.sprite.Group()
        add_to_sprites_list(sprites_list, [paddle, ball])
        run = True

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        while sim_time < simulation_seconds and run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_x:
                        run = False

            inputs = [
                paddle.rect.y / window_size[1], ball.rect.y / window_size[1],
                (ball.velocity[1] + ball_velocity) / (2 * ball_velocity)
            ]
            action = net.activate(inputs)
            # print("inputs",inputs)
            keys = pygame.key.get_pressed()
            # if np.argmax(action) == 0:
            #     paddle.move_up(5)
            # if np.argmax(action) == 1:
            #     paddle.move_down(5)

            if action[0] > 0.5:
                paddle.move_up(5)
            if action[0] <= 0.5:
                paddle.move_down(5)

            sprites_list.update()

            if ball.rect.x >= window_size[0] - ball_radius:
                ball.velocity[0] = -ball.velocity[0]
            if ball.rect.x <= 0:
                ball.rect.x = window_size[0] // 2
                ball.rect.y = window_size[1] // 2
                paddle.rect.x = 20
                paddle.rect.y = window_size[1] // 2
                miss += 1.0
                break
            if ball.rect.y > window_size[1] - ball_radius:
                ball.velocity[1] = -ball.velocity[1]
            if ball.rect.y < 0:
                ball.velocity[1] = -ball.velocity[1]

            if pygame.sprite.collide_mask(ball, paddle):
                ball.bounce()
                hits += 1.0

            display.fill(BLACK)
            sprites_list.draw(display)
            pygame.display.flip()

            timer.tick(int(framerate))
            sim_time += 1.0 / framerate
        # print(fitness)
        # fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return hits / (hits + miss) + sim_time
Exemplo n.º 5
0
 def reset(self):
     ''' Method resets the game. '''
     self.state = 'start'
     self.first_paddle = Paddle(player=1)
     self.second_paddle = Paddle(player=2)
     self.ball = Ball(x_direction_right=True)
Exemplo n.º 6
0
class PyPong:
    ''' Main game class. '''
    def __init__(self):
        pyxel.init(consts.SCREEN_WIDTH,
                   consts.SCREEN_HEIGHT,
                   caption='PyPong',
                   scale=consts.SCALE)
        self.state = 'start'
        self.single_player = True
        self.table = Table()
        self.start_menu = Menus(consts.START_SCREEN)
        self.controls_menu = Controls(consts.CONTROLS_SCREEN_OPTIONS)
        self.bot_menu = Menus(consts.BOT_SCREEN_OPTIONS)
        self.pause_menu = Menus(consts.PAUSE_SCREEN_OPTIONS)
        self.win_screen = Win(consts.WIN_SCREEN)
        self.first_paddle = Paddle(player=1)
        self.second_paddle = Paddle(player=2)
        self.ball = Ball(x_direction_right=True)
        self.score_time = 0
        self.winner = None
        pyxel.run(self.update, self.draw)

    def reset(self):
        ''' Method resets the game. '''
        self.state = 'start'
        self.first_paddle = Paddle(player=1)
        self.second_paddle = Paddle(player=2)
        self.ball = Ball(x_direction_right=True)

    def update(self):
        ''' Main update function. '''

        menu_entrance_time = 0
        if self.state == 'start':
            selected_option = self.start_menu.update_selected_option()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 0:
                self.single_player = True
                menu_entrance_time = time()
                self.state = 'bot'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 1:
                self.single_player = False
                self.state = 'game'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 2:
                menu_entrance_time = time()
                self.state = 'controls'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 3:
                pyxel.quit()

        if self.state == 'controls' and time() - menu_entrance_time >= 1:
            selected_option = self.controls_menu.update_selected_option()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 0:
                self.state = 'start'

        if self.state == 'bot' and time() - menu_entrance_time >= 1:
            selected_option = self.bot_menu.update_selected_option()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 0:
                self.bot = Bot(1)
                self.state = 'game'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 1:
                self.bot = Bot(2)
                self.state = 'game'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 2:
                self.bot = Bot(3)
                self.state = 'game'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 3:
                self.state = 'start'

        if self.state == 'game':
            self.ball.update_position()
            self.ball.edge_bounce()
            if not self.ball.x_direction_right:
                self.ball.paddle_bounce(self.first_paddle)
            if self.ball.x_direction_right:
                self.ball.paddle_bounce(self.second_paddle)
            self.first_paddle.update_position()
            if self.single_player:
                self.bot.update_paddle_position(self.ball, self.second_paddle)
            else:
                self.second_paddle.update_position()
            self.score_time = self.ball.score_event(self)
            self.winner = self.check_scores()
            if pyxel.btnp(pyxel.KEY_P) or pyxel.btnp(pyxel.KEY_Q):
                menu_entrance_time = time()
                self.state = 'pause'

        if self.state == 'score':
            self.ball.edge_bounce()
            self.first_paddle.update_position()
            if self.single_player:
                self.bot.update_paddle_position(self.ball, self.second_paddle)
            else:
                self.second_paddle.update_position()
            if time() - self.score_time >= 1:
                self.state = 'game'

        if self.state == 'pause':
            selected_option = self.pause_menu.update_selected_option()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 0:
                self.state = 'game'
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 1:
                self.reset()

        if self.state == 'win':
            selected_option = self.win_screen.update_selected_option()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 0:
                self.reset()
            if pyxel.btnp(pyxel.KEY_ENTER) and selected_option == 1:
                pyxel.quit()

    def draw(self):
        ''' Main method that draws game's objects. '''
        if self.state in consts.MENU_SCREENS:
            self.table.draw(middle_line=False)

        if self.state in ['start']:
            self.start_menu.draw()

        if self.state == 'controls':
            self.controls_menu.draw()
            self.controls_menu.display()

        if self.state == 'bot':
            self.bot_menu.draw()

        if self.state == 'pause':
            self.pause_menu.draw()

        if self.state in ['game', 'score']:
            self.table.draw()
            self.table.draw_scores(self.first_paddle.score,
                                   self.second_paddle.score)
            self.first_paddle.draw()
            self.second_paddle.draw()
            self.ball.draw()

        if self.state == 'win':
            self.win_screen.draw()
            self.win_screen.display(self.winner)

    def check_scores(self):
        ''' Checks score of both players. Ends game when one of the players
            has 11 points and has an advantage of at least 2 points. Returns
            the winner. '''
        winner = None
        if self.first_paddle.score >= 11 and (
                self.first_paddle.score - self.second_paddle.score >= 2):
            self.state = 'win'
            winner = 'Player 1'
        if self.second_paddle.score >= 11 and (
                self.second_paddle.score - self.first_paddle.score >= 2):
            self.state = 'win'
            winner = 'Player 2'
        return winner