def start_new_game(self): next_lvl = self.game_state.advance_level() # if we finished all the levels restart_game = False if not next_lvl: self.game_state.reset() next_lvl = self.game_state.advance_level() restart_game = True # create new game game_logic = GameLogic() game_logic.game_state = self.game_state game_logic.event_manager = self.event_manager self.add_graphic_state(game_logic) game_logic.build_level(next_lvl) self.game_logic = game_logic # Display message at the begining of each level msg = 'Level %d' % self.game_state.current_level self.add_graphic_state(TemporaryMessage(msg)) if restart_game: self.add_graphic_state(StartMenu()) msg = 'Credit goes here' self.add_graphic_state(TemporaryMessage(msg)) else: self.add_graphic_state(EndLevel(True))
def __init__(self): # AI contains the Game Logic self.ai_logic = GameLogic() # discount factor in the Q-learning algorithm self.discount = 0.4 # k value for exploration self.k = 2 self.adult_last_move = "first" self.change = 0 self.t_count = 0 self.u_count = 0 self.v_count = 0 self.w_count = 0 # setup and initialize ordinal matrix self.ord_matrix = [] self.ord_t = 0 self.ord_u = 0 self.ord_v = 0 self.ord_w = 0 self.update_ord_matrix() self.save_ord_matrix() # setup and initialize Q-value matrix self.q_t = self.ai_logic.t[1] self.q_u = self.ai_logic.u[1] self.q_v = self.ai_logic.v[1] self.q_w = self.ai_logic.w[1] self.q_matrix = [] self.update_q_matrix() # setup and initialize probability matrix self.probability_matrix = [] self.update_probability_matrix()
def test_getplayer(self, mock_input): captured_output = StringIO() sys.stdout = captured_output game = GameLogic() self.assertIsInstance(game.player_1, Player) self.assertIsInstance(game.player_2, Player) self.assertEqual(game.player_1.name, 'ScaryTerry') self.assertEqual(game.player_2.name, 'Holt') sys.stdout = sys.__stdout__
def test_playerturn(self, mock_input): captured_output = StringIO() sys.stdout = captured_output game = GameLogic() game.player_turn() self.assertEqual(game.moves.state['mid'],{'l':0,'m':1, 'r':0}) game.player_turn() self.assertEqual(game.moves.state['mid'],{'l':2,'m':1, 'r':0}) sys.stdout = sys.__stdout__
def __init__(self): self.game_map = GameMap() self.game_logic = GameLogic(self.game_map) self.view = View() self.level_start() self.enemy_actions() self.player_actions() self.check_game_status()
def setUp(self): pygame.font.init() self.display = pygame.display.set_mode((1024, 640)) self.gamelogic = GameLogic() self.colors = Colors() self.startview = StartView(self.display, self.colors) self.save = SaveGame(self.gamelogic) self.menu = Menu(self.display, self.colors) self.shop = Shop(self.display, self.colors, self.gamelogic) pygame.init()
def play_game(player_one, player_two): player_mapping = {player_one: "A", player_two: "B"} players_turn = player_one next_player = player_two playing = True game_logic = GameLogic(player_mapping) while playing: game_logic.show_board() choosen_pit = input(f"{players_turn}, choose a pit: ") if choosen_pit and game_logic.valid_pit(players_turn, choosen_pit): go_again = game_logic.move(players_turn, choosen_pit) if game_logic.game_ended(player_one, player_two): playing = False elif not go_again: players_turn, next_player = next_player, players_turn else: print("Invalid choice, try again.")
def main(): """Main function of the game. Creates all the classes that GameLoop-class needs to run the game, and then starts the game by calling gameloop.start(). """ display = pygame.display.set_mode((1024, 640)) pygame.display.set_caption("Clicker") pygame.font.init() colors = Colors() event_queue = EventQueue() startview = StartView(display, colors) menu = Menu(display, colors) logic = GameLogic() shop = Shop(display, colors, logic) save = SaveGame(logic) game_loop = GameLoop(logic, display, colors, startview, event_queue, menu, shop, save) pygame.init() game_loop.start()
def test_checkwinner(self, mock_input): captured_output = StringIO() sys.stdout = captured_output game = GameLogic() self.assertEqual(game.check_winner(), None) game.moves.state['mid']['l'] = 1 game.moves.state['mid']['m'] = 1 game.moves.state['mid']['r'] = 1 self.assertEqual(game.check_winner(), 1) game.moves.state['mid']['m'] = 2 self.assertEqual(game.check_winner(), None) game.moves.state['top']['r'] = 2 game.moves.state['bot']['l'] = 2 self.assertEqual(game.check_winner(), 2) game.moves.state['top']['l'] = 1 game.moves.state['bot']['l'] = 1 self.assertEqual(game.check_winner(), 1) sys.stdout = sys.__stdout__
def __init__(self, username): """Initiates the game and takes as a parameter the users name""" pygame.init() self._screen = pygame.display.set_mode((1380, 800)) self._screen.fill((54, 48, 33)) self._username = username self._board_font = pygame.font.SysFont('Arial', 15, bold=pygame.font.Font.bold) # What stage of the game we are self._game_status = 'PRE-SETUP' # Board setup files self._running = True # Creates map and board graphics self._create_sea_map() self._vertical_horizontal_lines() self._game_logic = GameLogic(self._username) self.mark_active_boats([1100, 30]) self.mark_active_boats([1100, 200], 'human') self._game_logic.setup_game() while self._running: for event in pygame.event.get(): # Populate ai boat dictionary self._event_handler(event) pygame.display.update()
def __init__(self, manager, white_player='human', black_player='human'): # game state members self.game_manager = manager self.game_running = False self.needs_redraw = True self.screen = None self.clock = pygame.time.Clock() # game components self.game_logic = GameLogic(self) self.game_grid = GameGrid(self) self.game_board = None self.white_player = None self.black_player = None self.initialize_players(white_player, black_player) self.turn_manager = TurnManager(self) self.highlighter = Highlighter(self) self.logger = Logger(self) self.buttons = {}
# game setup # init pygame pygame.init() # init display screen = pygame.display.set_mode(cfg['game']['screen_size']) # level creation levels = [LevelMic(screen, cfg['game']['screen_size'], mic)] # choose level level = levels[0] # game logic with dependencies game_logic = GameLogic() # add clock clock = pygame.time.Clock() # mic stream and update with mic.stream: # game loop while game_logic.run_loop: for event in pygame.event.get(): # input handling game_logic.event_update(event) level.event_update(event)
def setUp(self): self.gamelogic = GameLogic() self.save = SaveGame(self.gamelogic)
def __init__(self): self._rand_sel = ComputerSelection() self._game_logic = GameLogic() self.mainFunction()
def setUp(self): self.logic = GameLogic()
def __init__(self): # AI contains the Game Logic self.ai_logic = GameLogic() # discount factor in the Q-learning algorithm self.discount = 0.4 # k value for exploration self.k = 2 # Set up for Parent self.p_change = 0 self.t_count = 0 self.u_count = 0 self.v_count = 0 self.w_count = 0 # setup and initialize ordinal matrix self.p_ord_matrix = [] self.ord_t = 0 self.ord_u = 0 self.ord_v = 0 self.ord_w = 0 self.update_p_ord_matrix() self.save_p_ord_matrix() # setup and initialize Q-value matrix self.q_t = self.ai_logic.t[1] self.q_u = self.ai_logic.u[1] self.q_v = self.ai_logic.v[1] self.q_w = self.ai_logic.w[1] self.p_q_matrix = [] self.update_p_q_matrix() # setup and initialize probability matrix self.p_probability_matrix = [] self.update_p_probability_matrix() # Set up for Child self.c_change = 0 self.o_count = 0 self.p_count = 0 self.q_count = 0 self.y_count = 0 # setup and initialize ordinal matrix self.c_ord_matrix = [] self.ord_o = 0 self.ord_p = 0 self.ord_q = 0 self.ord_y = 0 self.update_c_ord_matrix() self.save_c_ord_matrix() # setup and initialize Q-value matrix self.q_o = self.ai_logic.t[0] self.q_p = self.ai_logic.u[0] self.q_q = self.ai_logic.v[0] self.q_y = self.ai_logic.w[0] self.c_q_matrix = [] self.update_c_q_matrix() # setup and initialize probability matrix self.c_probability_matrix = [] self.update_c_probability_matrix()
import time from game_logic import GameLogic from game_ui import GameUI gl = GameLogic() gui = GameUI() gui.focusOnEmulatorScreen() #print('test') while 1: print("Updating Game State") gui.UpdateGameState() print("Calculating Action") next_action = gl.GetNextAction(gui.gs, gui.gv) #print("Action: " + next_action.name) #print(next_action.cards) #inpstr = input('Continue?') #if inpstr != "y": # exit(0) gui.ProcessAction(next_action) #time.sleep(1) #exit(0)
def main(width, height): # Initialize window root = Tk() root.title("Game of War") # Initialize frame main_frame = ttk.Frame(root) main_frame.grid(column=1, row=3) #Create game runner game = GameLogic() # set game board size screen_width = width screen_height = height square_pixel_size = 10 # if width or heigh isn't given, specify via screen size calculation if (width is None or height is None): screen_width = root.winfo_screenwidth( ) * 1 / 2 # game board covers 1/2 of screen screen_height = root.winfo_screenheight() * 1 / 2 game_board_len = int(screen_width / square_pixel_size) game_board_height = int(screen_height / square_pixel_size) my_graph = GraphWidget(main_frame, game, game_board_height, game_board_len, square_pixel_size) #Initialize game runner with canvas game.initialize(my_graph) # Add label group label_group = ttk.Frame(main_frame, padding="10 0 10 20") label_group.grid(column=1, row=2) # Add color selection label ttk.Label(label_group, text="Place Civilization: ").grid(column=1, row=1) # Create variable to allow green to be default selected v = IntVar() v.set(1) # 1 is green radio button # Add green color radio button green_button = ttk.Radiobutton( label_group, text="Green", variable=v, command=lambda: my_graph.set_color(GraphColor.alive_green), value=1) green_button.grid(column=2, row=1, padx=5, pady=5) # Add blue color radio button blue_button = ttk.Radiobutton( label_group, text="Blue", variable=v, command=lambda: my_graph.set_color(GraphColor.alive_blue), value=2) blue_button.grid(column=3, row=1, padx=5, pady=5) # Add start button start_button = ttk.Button( label_group, text="Start", command=lambda: start(start_button, blue_button, green_button, my_graph, game)) start_button.grid(column=2, row=2, columnspan=2, padx=5, pady=5) # Add reset button reset_button = ttk.Button( label_group, text="Reset", command=lambda: reset(start_button, blue_button, green_button, my_graph, game)) reset_button.grid(column=2, row=3, columnspan=2, padx=5, pady=5) # Add clear button clear_button = ttk.Button( label_group, text="Clear", command=lambda: clear(start_button, blue_button, green_button, my_graph, game)) clear_button.grid(column=2, row=4, columnspan=2, padx=5, pady=5) while True: # update game logic game.update() try: # update canvas root.update() except TclError: # application no longer active exit()
from game_logic import GameLogic if __name__ == '__main__': game = GameLogic() game.start() File 2: game_logic.py import random from string import ascii_letters from phrase import Phrase class GameLogic: def __init__(self): self.missed = 0 self.phrases = [ Phrase("simplicity becomes elegance"), Phrase("i do not fear computers"), Phrase("programming is fun"), Phrase("imagination is more important than knowledge"), ] self.active_phrase = self.random_phrase() #this is the list that holds the letters that user has guessed self.guesses = [" "] def random_phrase(self): # choose a phrase from the list above return random.choice(self.phrases)
import pygame from entities import Hero from game_logic import GameLogic canvasWidth = 600 canvasHeight = 600 mapWidth = 500 mapHeight = 500 pygame.init() logic = GameLogic(canvasWidth, canvasHeight, mapWidth, mapHeight) logic.populate_entities(Hero()) black = (0, 0, 0) gameDisplay = pygame.display.set_mode((canvasWidth, canvasHeight)) pygame.display.set_caption('Wanderer the RPG Game') clock = pygame.time.Clock() def load_image(filePath): return pygame.transform.scale( pygame.image.load(filePath), (logic.game_ent_width(), logic.game_ent_height())) heroImgDown = load_image('src/images/hero-down.gif') heroImgUp = load_image('src/images/hero-up.gif') heroImgLeft = load_image('src/images/hero-left.gif') heroImgRight = load_image('src/images/hero-right.gif')
def __init__(self): self.instances = [] # create instances self.game_logic = GameLogic() self.instances.append(self.game_logic) self.instances.append(PlayerView(self.game_logic))