Example #1
0
    def __init__(self, fps=10, caption='Snake'):
        """
        Constructor
        :param fps: in-game clock tick rate (in ticks/sec)
        """
        self._running = False
        self._display_surf = None
        self._player_surf = None
        self._food_surf = None
        self.fps = fps
        self.player = Player()
        self.food = (
            20 * random.randint(low_bound[0] / 20, up_bound[1] / 20 -
                                1),  # initially spawning food
            20 * random.randint(low_bound[1] / 20, up_bound[1] / 20 - 1))
        self.score = 0

        pygame.init()
        self.clock = pygame.time.Clock()
        self._display_surf = pygame.display.set_mode((win_width, win_height),
                                                     pygame.HWSURFACE)
        self._running = True
        self._player_surf = pygame.image.load("player.png").convert()
        self._food_surf = pygame.image.load("food.png").convert()
        self.buffer = queue.PriorityQueue(maxsize=2)
        self.caption = caption
Example #2
0
def create_players():
    first_player = choose_first()
    second_player = 3 - first_player

    print(f'Player {first_player}, you go first')
    first_player_marker, second_player_marker = player_input()

    return [
        Player(f'Player {first_player}', first_player_marker),
        Player(f'Player {second_player}', second_player_marker)
    ]
Example #3
0
    def __init__(self, pid, color, direction, piece_count):
        Player.__init__(self, pid, color, direction, ai=True)
        self.piece_count = piece_count  # Number of pieces assigned initially
        self.best_move = None  # best move found in current
        self.infinity = 99999  # infinity used in alpha-beta
        self.time_limit = 15  # time limit
        self.starting_depth = 9  # minimum depth to search
        self.depth_limit = 30  # maximum depth to search

        # storage location of statistics for each move
        self.number_of_nodes = 0
        self.number_of_alpha_prunes = 0
        self.number_of_beta_prunes = 0
        self.temp_list = []
Example #4
0
class Game:
    """
    Game object that runs the entirety of Snake, including Food placement.
    TODO: make a start screen and death screen
    """
    def __init__(self, fps=10, caption='Snake'):
        """
        Constructor
        :param fps: in-game clock tick rate (in ticks/sec)
        """
        self._running = False
        self._display_surf = None
        self._player_surf = None
        self._food_surf = None
        self.fps = fps
        self.player = Player()
        self.food = (
            20 * random.randint(low_bound[0] / 20, up_bound[1] / 20 -
                                1),  # initially spawning food
            20 * random.randint(low_bound[1] / 20, up_bound[1] / 20 - 1))
        self.score = 0

        pygame.init()
        self.clock = pygame.time.Clock()
        self._display_surf = pygame.display.set_mode((win_width, win_height),
                                                     pygame.HWSURFACE)
        self._running = True
        self._player_surf = pygame.image.load("player.png").convert()
        self._food_surf = pygame.image.load("food.png").convert()
        self.buffer = queue.PriorityQueue(maxsize=2)
        self.caption = caption

    def render(self):
        """
        Helper method to store all the pygame rendering objects/processes
        :return:
        """
        pygame.display.set_caption(self.caption)
        self._display_surf.fill((0, 0, 0))
        rect = pygame.Rect(low_bound[0], low_bound[1],
                           up_bound[0] - low_bound[0],
                           up_bound[1] - low_bound[1])
        pygame.draw.rect(self._display_surf, (255, 255, 255), rect, 1)
        self.player.draw(self._display_surf, self._player_surf)
        self._display_surf.blit(self._food_surf, self.food)
        my_font = pygame.font.SysFont("monospace", 16)
        score_text = my_font.render("Score = " + str(self.score), 1,
                                    (255, 255, 255))
        self._display_surf.blit(score_text, (5, 10))

        pygame.display.flip()
        pygame.display.update()

    def respawn_food(self):
        """
        Helper method to respawn the food in a valid place
        :return:
        """
        valid = False
        while not valid:
            x = 20 * random.randint(low_bound[0] / 20, up_bound[0] / 20 - 1)
            y = 20 * random.randint(low_bound[1] / 20, up_bound[1] / 20 - 1)
            valid = (x not in self.player.x) or (y not in self.player.y)
        self.food = (x, y)

    def check_eaten(self):
        """
        Checks if player has eaten the food, respawning food if the player has
        :return: True if food is eaten, else False
        """
        if (self.food[0] == self.player.x[0]) and (self.food[1]
                                                   == self.player.y[0]):
            self.respawn_food()
            self.score += 1
            return True
        else:
            return False

    def check_death(self):
        """
        Checks if the player has died
        :return: True if the player has satisfied any death condition, else False
        """
        if (self.player.x[0], self.player.y[0]) in list(
                zip(self.player.x[1:], self.player.y[1:])):
            print('death by self')
            return True
        elif self.player.x[0] < low_bound[0] or self.player.x[
                0] > up_bound[0] - 20:
            print('death by x-wall')
            return True
        elif self.player.y[0] < low_bound[1] or self.player.y[
                0] > up_bound[1] - 20:
            print('death by y-wall')
            return True
        else:
            return False

    def valid_move(self, move):
        """
        Checks if the inputted move will actually do something
        :param move: input move
        :return: True if valid, False otherwise
        """
        if np.array_equal(-1 * move, self.player.direction) or np.array_equal(
                move, self.player.direction):
            return 1
        else:
            return 0

    def run(self, ai=None):
        self._running = True
        while self._running:
            if ai is not None:
                pygame.event.pump()
                keys = pygame.key.get_pressed()
                self.player.change_direction(ai.next_move())
                if keys[K_ESCAPE]:
                    pygame.quit()
                    system.exit()
            else:
                key_events = pygame.event.get(KEYDOWN)
                # print(len(key_events))
                for ev in key_events:
                    if ev.key in moves:
                        if not self.buffer.full():
                            self.buffer.put((self.valid_move(moves[ev.key]),
                                             next(tiebreaker), moves[ev.key]),
                                            block=False)
                        else:
                            print('full')

                    if ev.key is K_ESCAPE:
                        # exit the game if esc is pressed
                        pygame.quit()
                        return

                # print(self.buffer.qsize())
                if not self.buffer.empty(
                ):  # take the moves one at a time, dequeuing one per frame
                    x = self.buffer.get(block=False)[2]
                    # print('Moving', x)
                    self.player.change_direction(x)

            eat = self.check_eaten()
            self.player.move(eat)
            self._running = not self.check_death()

            self.render()
            self.clock.tick(self.fps)

        pygame.quit()
        return
Example #5
0
 def buildGamePlayer(self):
     """ Build the Game Player for this player in the Lobby """
     self.player = Player(self.name, self.character,
                          self.startingDeckSetting.loadDeck())
     return self.player
Example #6
0
import tkinter as tk

from Domain.domain import Sentence
from Game.player import Player
from Repository.repo_files import RepositoryFiles
from Service.service import ServiceSentences

repository = RepositoryFiles()
service = ServiceSentences(repository)
string = service.get_random_sentence()
sentence = Sentence(string)
player = Player(sentence)


def game_page():
    tki = tk.Tk()
    page = GamePage(tki, player, sentence)
    page.create_title()
    page.create_board()
    page.create_button()
    page.create_input()
    tki.mainloop()


class GamePage:
    def __init__(self, tk, player, sentence):
        # i ll have the photo up
        # the text will be in the center
        # the input will be down
        self.tk = tk
        self.player = player
Example #7
0
from Game.player import Player
from Game.enemy import Enemy, Troll, Vampyre, VampyreKing

tim = Player("Tim")

random_monster = Enemy("Basic enemy", 12, 1)
print(random_monster)

random_monster.take_damage(4)
print(random_monster)

ugly_troll = Troll("Pug")
print("Ugly troll - {}".format(ugly_troll))

another_troll = Troll("Ug")
print("Another troll - {}".format(another_troll))
another_troll.take_damage(18)

brother = Troll("Urg")
print(brother)

ugly_troll.grunt()
another_troll.grunt()
brother.grunt()

dracula = Vampyre("Dracula")
print(dracula)
dracula.take_damage(12)
print(dracula)

viago = VampyreKing("Viago")
Example #8
0
if __name__ == '__main__':
    score_calc = Score_Calculator()
    bowling_game = Bowling_Game(score_calc)

    print(f"Welcome to the Bowling Alley !!")

    try:
        no_of_players = int(input('Enter the number of Players: '))
    except:
        print('No of players must be an integer. Please rerun the CLI')
        quit()
    players = []

    for idx in range(no_of_players):
        player_name = input('Enter player name: ')
        players.append(Player(player_name))

    print()
    print(
        f"** Use {STRIKE_SYMBOL} for STRIKE, {SPARE_SYMBOL} for SPARE , {MISS_SYMBOL} for a MISS **"
    )
    print()
    for player in players:
        player_frame = input(f"Enter {player.get_name()} bowling frame: ")
        if not player_frame:
            print('Blank Player Frame is not Allowed')
            quit()
        player_score = bowling_game.score_of_game(player_frame)
        player.set_final_score(player_score)

    players.sort(key=lambda player: player.get_final_score(), reverse=True)
Example #9
0
from Game.tic_tac_toe_game import TicTacToeGame
from Game.game_manager import GameManager
from Game.player import Player

p1 = Player("One")
p2 = Player("Two")
1
t = GameManager(p1, p2)

t.start_game_series()
Example #10
0
def BuildPlayer(character=None):
    """ Build a Player for testing purposes """
    if character is None:
        character = BuildCharacter()
    return Player("Test", character)