def __init__(self, mock=False, run=True, **kwargs) -> None: super().__init__('gameplay', existing_loggers=['gameplay'], **kwargs) self.mock = mock # ConfigManager.set_value('game|global|gameplay status', 'disabled') self.config = ConfigManager.get_value('game') self.gameplay = Gameplay(self.config, Controller(), self.logger) self.strategy_publisher = messenger.Publisher('/strategy', messenger.Messages.string) self.settings_listener = messenger.Listener( '/settings_changed', messenger.Messages.string, callback=self.refresh_settings) self.recognition_listener = messenger.Listener( '/recognition', messenger.Messages.string, callback=self.callback) self.command_listener = messenger.Listener( '/command', messenger.Messages.string, callback=self.command_callback) self.kicker_listener = messenger.Listener( '/canbus_message', messenger.Messages.string, callback=self.kicker_callback) self.realsense_distance_listener = messenger.Listener( '/distance/realsense', messenger.Messages.float, callback=self.realsense_distance_callback) self.tfmini_distance_listener = messenger.Listener( '/distance/tfmini', messenger.Messages.float, callback=self.tfmini_distance_callback) self.realsense_active = time() self.logger.info("Start gameplay") if run: self.spin()
def __init__(self) -> None: self.gameplay = Gameplay() self.gravity = [ 1.00000, 0.79300, 0.61780, 0.47273, 0.35520, 0.26200, 0.18968, 0.13473, 0.09388, 0.06415, 0.04298, 0.02822, 0.01815, 0.01144, 0.00706, ] self.goal = 5 self.popups: List[Popup] = [] self.current_popup: Optional[Popup] = None self.end_screen = False self.finished = False self.pause = False
def __init__(self, width, height, window_title): super().__init__(width, height, window_title) self.stage = MENU self.all_check_cover() self.howto_page = 1 self.gameplay = Gameplay() self.sound_fx = Sound()
def load(self) -> bool: try: with open(filename, "rb") as f: self.game = pickle.load(f) return True except IOError: self.game = Gameplay(self.gui, self.gui.center) return False
def __init__(self) -> None: self.gameplay1 = Gameplay() self.gameplay2 = Gameplay() self.popups1: List[Popup] = [] self.popups2: List[Popup] = [] self.current_popup1: Optional[Popup] = None self.current_popup2: Optional[Popup] = None self.finished = False self.end_screen = False
def __init__(self): ShowBase.__init__(self) logging.basicConfig(level=logging.DEBUG) log.info("initializing...") # self.disableMouse() self.setFrameRateMeter(True) self.render.setShaderAuto() # gameplay=Gameplay(self) gameplay.start() # PStatClient.connect() # log.info("done initializing")
def main(): fenetre_joueur = Interface() fenetre_joueur.nombre_joueurs() fenetre_joueur.mainloop() liste_joueurs = Joueur.liste_joueurs Gameplay(liste_joueurs)
def main(): name = input("Please enter your name: ") new_deck = PlayerDeckCreation() deck = new_deck.deck_creation() player, computer = new_deck.deal_deck(deck) game = Gameplay(player, computer) while 0 < len(player) < 52: player, computer, play = game.flip_cards() if play == 'y': game.update_decks(player, computer) elif play == 'n': break print(f'\n{name}: {len(player)}\nComputer: {len(computer)}') print("\n---------------------------------") if len(player) == 52: print(f"{name} wins!") elif len(computer) == 52: print("Computer wins!")
def __init__(self) -> None: self.gameplay1 = Gameplay() self.gameplay2 = Gameplay() self.popups1: List[Popup] = [] self.popups2: List[Popup] = [] self.current_popup1: Optional[Popup] = None self.current_popup2: Optional[Popup] = None self.waiting = True self.waiting_cycle = 0 self.last_waiting_cycle = ctx.now self.buffer = b"" self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.settimeout(10.0) self.finished = False self.end_screen = False
def __init__(self): self.current_mouse_position = Vector(0, 0) self.playing = True self.frame_rate = 0.01 self.win = GraphWin("Bolinha Game", 800, 600) self.win.bind_all("<KeyRelease>", key_released) self.win.bind_all("<KeyPress>", key_pressed) self.win.bind('<Motion>', motion) self.highscore = HighScore() self.menu_scene = Menu(self.win, self.begin_gameplay, self.credits_switch, self.exit_game, self.credits_switch) self.gameplay_scene = Gameplay(self.win, self.pause, self.back_to_menu, self.exit_game, self.restart, self.highscore) self.menu_scene.draw() self.current_scene = self.menu_scene self.last_key = "" self.current_frame_delta = self.frame_rate
class Game: def __init__(self, scr: CursesWindow) -> None: self.delay = 0 self.gui = Gui(scr) self.game = Gameplay(self.gui, self.gui.center) def load(self) -> bool: try: with open(filename, "rb") as f: self.game = pickle.load(f) return True except IOError: self.game = Gameplay(self.gui, self.gui.center) return False def save(self) -> None: with open(filename, "wb") as f: self.game.gui = None # type: ignore pickle.dump(self.game, f) def pause(self) -> None: self.gui.pause() def loop(self, d: Optional[Pair]) -> int: self.delay = self.game.direct(d) if self.game.step(): self.gui.flash() self.pause() self.game.reset() return self.delay def play(self) -> None: self.init() self.gui.do_loop(self.loop) self.save() def init(self) -> None: self.delay = 0 self.gui = Gui(self.gui.scr) ok = self.load() self.game.gui = self.gui if not ok: self.game.reset() assert self.game.snake is not None
def show_available_maps(self, gameplay_mode): self.show_map = True self.cursor = 0 if not os.path.isdir(self.maps_folder): os.mkdir(self.maps_folder) if len(os.listdir(self.maps_folder)) == 0: lines = [("There is no map in 'maps' folder.", curses.color_pair(TEXT_COLOR)), ("", 0), ("You need to create maps to play.", curses.color_pair(TEXT_COLOR))] while self.show_map: self.window.clear() self.print_main_menu(0.25, False) self.print_menu(0.75, *lines) self.window.refresh() if (self.window.getch() == ord('\n')): self.show_map = False return title = ("Maps", curses.A_BOLD | curses.A_UNDERLINE | curses.color_pair(TITLE_COLOR)) option_list = [(file, lambda window=self.window, file=os.path.join( self.maps_folder, file), mode=gameplay_mode: Gameplay( window, file, mode).run()) for file in os.listdir(self.maps_folder)] option_list.append(("Return", lambda: True)) options = [[option[0], curses.color_pair(TEXT_COLOR)] for option in option_list] while self.show_map: self.window.clear() self.print_main_menu(0.25, False) y, x, h, w = self.print_menu(0.75, title, ("", 0), *options) for i, option in enumerate(options): if self.cursor == i: self.window.addstr(y + 2 + i, x + 3, ">", curses.color_pair(CURSOR_COLOR)) self.window.refresh() self.handle_event(self.window.getch(), option_list)
class FourElementsRunWindow(arcade.Window): def __init__(self, width, height, window_title): super().__init__(width, height, window_title) self.stage = MENU self.all_check_cover() self.howto_page = 1 self.gameplay = Gameplay() self.sound_fx = Sound() def all_check_cover(self): self.start_cover = False self.how_cover = False self.howto_left = False self.howto_right = False self.game_over_cover = False self.sound_onoff_cover = False self.howto_gui_cover() def howto_gui_cover(self): self.player = False self.range = False self.melee = False self.hp = False self.power = False self.floor = False self.score = False self.platform = False self.monster = False def all_howto_interface(self, x, y): self.howto_interface_player(x, y) self.howto_interface_range(x, y) self.howto_interface_melee(x, y) self.howto_interface_hp(x, y) self.howto_interface_power(x, y) self.howto_interface_floor(x, y) self.howto_interface_score(x, y) self.howto_interface_platform(x, y) self.howto_interface_monster(x, y) def howto_interface_player(self, x, y): if x in range(127, 178) and y in range(121, 187): self.player = True else: self.player = False def howto_interface_range(self, x, y): if x in range(206, 323) and y in range(159, 208): self.range = True else: self.range = False def howto_interface_melee(self, x, y): if x in range(206, 271) and y in range(102, 150): self.melee = True else: self.melee = False def howto_interface_hp(self, x, y): if x in range(166, 367) and y in range(462, 483): self.hp = True else: self.hp = False def howto_interface_power(self, x, y): if x in range(166, 347) and y in range(451, 461): self.power = True else: self.power = False def howto_interface_floor(self, x, y): if x in range(581, 640) and y in range(466, 483): self.floor = True else: self.floor = False def howto_interface_score(self, x, y): if x in range(581, 640) and y in range(448, 465): self.score = True else: self.score = False def howto_interface_platform(self, x, y): if x in range(501, 680) and y in range(332, 349): self.platform = True else: self.platform = False def howto_interface_monster(self, x, y): if x in range(561, 620) and y in range(192, 242): self.monster = True else: self.monster = False def menu_hover(self, x, y): self.game_over_cover = False if x in range(274, 526) and y in range(151, 204): self.start_cover = True else: self.start_cover = False if x in range(382, 418) and y in range(87, 124): self.how_cover = True else: self.how_cover = False if x in range(750, 785) and y in range(15, 50): self.sound_onoff_cover = True else: self.sound_onoff_cover = False def menu_press(self, x, y): if x in range(274, 526) and y in range(151, 204): self.stage = GAMEPLAY self.gameplay.set_up() elif x in range(382, 418) and y in range(87, 124): self.stage = HOWTOPLAY elif x in range(750, 785) and y in range(15, 50): self.gameplay.enable_sound = not self.gameplay.enable_sound def game_over_press(self, x, y): if self.gameplay.world.player.is_dead: if x in range(312, 489) and y in range(50, 100): self.stage = MENU def game_over_hover(self, x, y): if self.gameplay.world.player.is_dead: if x in range(312, 489) and y in range(50, 100): self.game_over_cover = True else: self.game_over_cover = False def howto_press(self, x, y): if 1 <= self.howto_page <= 3: if x in range(22, 96) and y in range(274, 325): self.howto_page -= 1 elif x in range(707, 781) and y in range(274, 325): self.howto_page += 1 def howto_arrow_hover(self, x, y): if x in range(22, 96) and y in range(274, 325): self.howto_left = True else: self.howto_left = False if x in range(707, 781) and y in range(274, 325): self.howto_right = True else: self.howto_right = False def draw_sprite(self, condition, location): if condition: sprite = arcade.Sprite(location, scale=0.24) sprite.set_position(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) sprite.draw() def draw_menu(self): arcade.draw_xywh_rectangle_textured( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, arcade.load_texture('images/menu/menu.png')) self.draw_sprite(self.gameplay.enable_sound, 'images/menu/sound_on.png') self.draw_sprite(not self.gameplay.enable_sound, 'images/menu/sound_off.png') self.draw_sprite(self.gameplay.enable_sound and self.sound_onoff_cover, 'images/menu/sound_on_cover.png') self.draw_sprite( not self.gameplay.enable_sound and self.sound_onoff_cover, 'images/menu/sound_off_cover.png') self.draw_sprite(self.start_cover, 'images/menu/start_cover.png') self.draw_sprite(self.how_cover, 'images/menu/how_cover.png') def draw_howto(self): arcade.draw_xywh_rectangle_textured( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, arcade.load_texture('images/how_to/menu_bg.png')) if 1 <= self.howto_page <= 3: arcade.draw_xywh_rectangle_textured( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, arcade.load_texture('images/how_to/howto_' + str(self.howto_page) + '.png')) self.draw_sprite(self.howto_left, 'images/how_to/arrow_left.png') self.draw_sprite(self.howto_right, 'images/how_to/arrow_right.png') if self.howto_page == 2: self.draw_howto_interface() else: self.stage = MENU self.howto_page = 1 def draw_game_over_hover(self): if self.game_over_cover: over = arcade.Sprite('images/game_over/game_over_cover.png', scale=0.24) over.set_position(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) over.draw() def draw_howto_interface(self): self.draw_sprite(self.player, 'images/how_to/howto_2-1.png') self.draw_sprite(self.range, 'images/how_to/howto_2-2.png') self.draw_sprite(self.melee, 'images/how_to/howto_2-3.png') self.draw_sprite(self.hp, 'images/how_to/howto_2-4.png') self.draw_sprite(self.floor, 'images/how_to/howto_2-5.png') self.draw_sprite(self.score, 'images/how_to/howto_2-6.png') self.draw_sprite(self.power, 'images/how_to/howto_2-7.png') self.draw_sprite(self.platform, 'images/how_to/howto_2-8.png') self.draw_sprite(self.monster, 'images/how_to/howto_2-9.png') def sound_on_mouse_motion(self, x, y, dx, dy): if self.stage == MENU: if x in range(274, 526) and y in range(151, 204): self.sound_fx.play_hover() elif x in range(382, 418) and y in range(87, 124): self.sound_fx.play_hover() else: self.sound_fx.reset_sound_play() elif self.stage == GAMEPLAY and self.gameplay.world.player.is_dead: if x in range(312, 489) and y in range(50, 100): self.sound_fx.play_hover() else: self.sound_fx.reset_sound_play() elif self.stage == HOWTOPLAY: if x in range(22, 96) and y in range(274, 325): self.sound_fx.play_hover() elif x in range(707, 781) and y in range(274, 325): self.sound_fx.play_hover() else: self.sound_fx.reset_sound_play() def sound_on_mouse_press(self, x, y): if self.stage == MENU: if x in range(274, 526) and y in range(151, 204): self.sound_fx.play_select() self.sound_fx.reset_sound_play() elif x in range(382, 418) and y in range(87, 124): self.sound_fx.play_select() self.sound_fx.reset_sound_play() elif self.stage == GAMEPLAY and self.gameplay.world.player.is_dead: if x in range(312, 489) and y in range(50, 100): self.sound_fx.play_main_menu() else: self.sound_fx.reset_sound_play() elif self.stage == HOWTOPLAY: if 1 <= self.howto_page <= 3: if x in range(22, 96) and y in range(274, 325): self.sound_fx.play_flip() elif x in range(707, 781) and y in range(274, 325): self.sound_fx.play_flip() else: self.sound_fx.reset_sound_play() else: if x in range(22, 96) and y in range(274, 325): self.sound_fx.play_close() elif x in range(707, 781) and y in range(274, 325): self.sound_fx.play_close() else: self.sound_fx.reset_sound_play() def on_draw(self): arcade.start_render() if self.stage == MENU: self.draw_menu() elif self.stage == GAMEPLAY: self.gameplay.on_draw() self.draw_game_over_hover() elif self.stage == HOWTOPLAY: self.draw_howto() def update(self, delta): # self.set_update_rate(1/70) if self.stage == GAMEPLAY: self.gameplay.update(delta) def on_key_press(self, key, key_modifiers): self.gameplay.on_key_press(key, key_modifiers) def on_key_release(self, key, key_modifiers): self.gameplay.on_key_release(key, key_modifiers) def on_mouse_motion(self, x, y, dx, dy): if self.gameplay.enable_sound: self.sound_on_mouse_motion(x, y, dx, dy) if self.stage == MENU: self.menu_hover(x, y) elif self.stage == GAMEPLAY: self.game_over_hover(x, y) elif self.stage == HOWTOPLAY: self.howto_arrow_hover(x, y) if self.howto_page == 2: self.all_howto_interface(x, y) def on_mouse_press(self, x, y, button, modifiers): if self.gameplay.enable_sound: self.sound_on_mouse_press(x, y) if self.stage == MENU: self.menu_press(x, y) if self.stage == GAMEPLAY: self.game_over_press(x, y) if self.stage == HOWTOPLAY: self.howto_press(x, y)
class BolinhaGame: def __init__(self): self.current_mouse_position = Vector(0, 0) self.playing = True self.frame_rate = 0.01 self.win = GraphWin("Bolinha Game", 800, 600) self.win.bind_all("<KeyRelease>", key_released) self.win.bind_all("<KeyPress>", key_pressed) self.win.bind('<Motion>', motion) self.highscore = HighScore() self.menu_scene = Menu(self.win, self.begin_gameplay, self.credits_switch, self.exit_game, self.credits_switch) self.gameplay_scene = Gameplay(self.win, self.pause, self.back_to_menu, self.exit_game, self.restart, self.highscore) self.menu_scene.draw() self.current_scene = self.menu_scene self.last_key = "" self.current_frame_delta = self.frame_rate def main(self): global current_key global held_key self.longest_frame = -1 while self.playing: if self.win.isClosed(): break self.start_frame = time.time() mouse_click = self.win.checkMouse() mouse_position = current_mouse_position if type(self.current_scene) is Menu: if current_key == "Escape": self.playing = False elif type(self.current_scene) is Gameplay: if current_key == "Escape": if not self.gameplay_scene.lost: self.pause() elif held_key == "Right": self.gameplay_scene.move_player("right", self.current_frame_delta) elif held_key == 'Left': self.gameplay_scene.move_player("left", self.current_frame_delta) elif current_key == 'space': self.gameplay_scene.start_game() current_key = "" self.current_scene.tick(mouse_position, mouse_click, self.current_frame_delta) current_time = time.time() if self.frame_rate > current_time - self.start_frame: time.sleep(self.frame_rate - (current_time - self.start_frame)) self.end_frame = time.time() self.current_frame_delta = self.end_frame - self.start_frame self.win.close() def begin_gameplay(self): self.change_scene("play") def credits_switch(self): self.menu_scene.credits() def exit_game(self): self.playing = False def back_to_menu(self): self.change_scene("menu") def pause(self): self.gameplay_scene.pause() def restart(self): self.gameplay_scene.restart() def key_released(self): print("released") def change_scene(self, next_scene: str): self.current_scene.undraw() if next_scene == "play": self.current_scene = self.gameplay_scene elif next_scene == "menu": self.current_scene = self.menu_scene self.current_scene.draw()
from gameplay import Gameplay game = Gameplay() game.play()
import courses from gameplay import Gameplay import sys import pygame from ending import ending from environment import * # INITIALIZE GAME BUTTON = pygame.USEREVENT + 1 init_courses = courses.populate() text_screen = initialize( [init_courses[label].description for label in list(init_courses)]) screen = text_screen[0] text = text_screen[1] control = Gameplay(init_courses) # LOOP THROUGH GRAPHICS AND CONTROLS, CHECKING FOR PASSING OF EVENTS done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(1) if event.type == BUTTON: done = control.add_order(event) courses = control.level_up() update_portfolio(courses, screen) mouseOver(text, screen) mouseClicks(control.order, BUTTON, screen) pygame.display.flip() pygame.event.pump()
from gameplay import Gameplay #w 'konstruktorze' na ta chwile mozna sobie ustawiac wartosci pol i tam tez wyswietlam plansze gameplay = Gameplay() gameplay.start()
class Marathon(State): def __init__(self) -> None: self.gameplay = Gameplay() self.gravity = [ 1.00000, 0.79300, 0.61780, 0.47273, 0.35520, 0.26200, 0.18968, 0.13473, 0.09388, 0.06415, 0.04298, 0.02822, 0.01815, 0.01144, 0.00706, ] self.goal = 5 self.popups: List[Popup] = [] self.current_popup: Optional[Popup] = None self.end_screen = False self.finished = False self.pause = False def is_finished(self) -> bool: return self.finished def initialize(self) -> None: self.gameplay.set_device(ctx.device1) self.gameplay.initialize() def update(self, switch_state: Callable) -> None: if self.gameplay.game_over and not self.end_screen: self.end_screen = True self.popups.append(Popup("Game over!", color="red")) if self.gameplay.game_over and self.gameplay.cancel: self.finished = True return if self.gameplay.cancel: if self.gameplay.countdown > 0: self.finished = True return else: self.gameplay.game_over = True self.gameplay.cancel = False self.gameplay.update() if self.gameplay.score.lines > 0: self.goal -= self.gameplay.score.lines self.goal = max(0, self.goal) self.gameplay.score.lines = 0 if self.goal == 0: if self.gameplay.level == 15 and not self.end_screen: self.end_screen = True self.gameplay.game_over = True self.popups.append( Popup("You won!", color="green", gcolor="yellow") ) else: self.gameplay.level += 1 self.goal = 5 * self.gameplay.level self.gameplay.fall_interval = self.gravity[ self.gameplay.level - 1 ] self.popups.extend(self.gameplay.get_popups()) self.gameplay.clear_popups() if not self.current_popup and self.popups: self.current_popup = self.popups.pop(0) self.current_popup.duration *= 2 elif self.current_popup: if not self.current_popup.update(): self.current_popup = None def draw(self) -> None: self.gameplay.draw(200, 80) Text().draw("Level", centerx=125, top=300) Text().draw( str(self.gameplay.level), centerx=125, top=340, size=4, color="gold", ) Text().draw("Goal", centerx=125, top=450) Text().draw( str(self.goal), centerx=125, top=490, size=4, color="green" ) if self.current_popup: self.current_popup.draw(650, 250, center=False)
class GameplayNode(messenger.Node): def __init__(self, mock=False, run=True, **kwargs) -> None: super().__init__('gameplay', existing_loggers=['gameplay'], **kwargs) self.mock = mock # ConfigManager.set_value('game|global|gameplay status', 'disabled') self.config = ConfigManager.get_value('game') self.gameplay = Gameplay(self.config, Controller(), self.logger) self.strategy_publisher = messenger.Publisher('/strategy', messenger.Messages.string) self.settings_listener = messenger.Listener( '/settings_changed', messenger.Messages.string, callback=self.refresh_settings) self.recognition_listener = messenger.Listener( '/recognition', messenger.Messages.string, callback=self.callback) self.command_listener = messenger.Listener( '/command', messenger.Messages.string, callback=self.command_callback) self.kicker_listener = messenger.Listener( '/canbus_message', messenger.Messages.string, callback=self.kicker_callback) self.realsense_distance_listener = messenger.Listener( '/distance/realsense', messenger.Messages.float, callback=self.realsense_distance_callback) self.tfmini_distance_listener = messenger.Listener( '/distance/tfmini', messenger.Messages.float, callback=self.tfmini_distance_callback) self.realsense_active = time() self.logger.info("Start gameplay") if run: self.spin() def refresh_settings(self, settings_topic): self.loginfo("Settings refreshed") self.config = ConfigManager.get_value('game') self.gameplay.config = Settings(self.config) def get_recognition(self): package = self.recognition_listener.package if package: return RecognitionState.from_dict(package) def kicker_callback(self, *_): package = self.kicker_listener.package if package: self.gameplay.kicker_speed = package.get('rpm', 0) def realsense_distance_callback(self, *_): self.realsense_active = time() distance = self.realsense_distance_listener.last_reading.data if not isnan(distance): self.gameplay.real_distance = distance def tfmini_distance_callback(self, *_): angle = self.gameplay.target_goal_angle distance = float(self.tfmini_distance_listener.last_reading.data) # if angle and abs(angle) <= 3.5: # timeout = time() - self.realsense_active > 1 # self.loginfo_throttle(1, f"TF-MINI is setting DISTANCE, RS:{not timeout} ANGLE:{angle}") # self.loginfo_throttle(1, f"TF-MINI is setting DISTANCE, RS:{distance:.0f} ANGLE:{angle:.1f} {self.gameplay.get_desired_kicker_speed()}") # if timeout: # self.gameplay.target_goal_distance = distance # self.gameplay.real_distance = distance pass def command_callback(self, *_): package = self.command_listener.package if package: self.gameplay.motors.reset() r_state = self.get_recognition() self.gameplay.recognition = r_state self.gameplay.update_recent_closest_balls() self.gameplay.set_target_goal_distance() self.gameplay.set_target_goal_angle_adjust() for function_name, arguments in package.items(): try: func = getattr(self.gameplay, function_name) func(**(arguments or {})) self.logger.info_throttle(1, 'command success') except Exception as e: self.logger.error('Gameplay command failed: %s %s\n %s', function_name, arguments, e) self.gameplay.motors.apply() def callback(self, *_): r_state = self.get_recognition() if r_state: self.gameplay.step(r_state) gp = self.gameplay self.strategy_publisher.command( is_enabled=gp.is_enabled, target_goal_angle=gp.target_goal_angle, goal=gp.config_goal, field=gp.field_id, robot=gp.robot_id, state=str(gp.state), dist=gp.target_goal_distance, angle=gp.target_goal_angle, average_closest_ball=gp.average_closest_ball and gp.average_closest_ball.serialize(), closest_ball=gp.closest_ball and gp.closest_ball.serialize(), id_balls=[bi.serialize() for bi in gp.sorted_id_balls], pwm=gp.get_desired_kicker_speed(), real_distance=gp.real_distance, )
class SplitScreen(State): def __init__(self) -> None: self.gameplay1 = Gameplay() self.gameplay2 = Gameplay() self.popups1: List[Popup] = [] self.popups2: List[Popup] = [] self.current_popup1: Optional[Popup] = None self.current_popup2: Optional[Popup] = None self.finished = False self.end_screen = False def is_finished(self) -> bool: return self.finished def initialize(self) -> None: self.gameplay1.set_device(ctx.device1) self.gameplay1.initialize() self.gameplay2.set_device(ctx.device2) self.gameplay2.initialize() def update(self, switch_state: Callable) -> None: if not self.end_screen: if self.gameplay1.game_over and not self.gameplay2.game_over: self.gameplay2.game_over = True self.end_screen = True self.popups1.append( Popup("You lost!", color="red", gcolor="orange")) self.popups2.append( Popup("You won!", color="green", gcolor="yellow")) elif self.gameplay2.game_over and not self.gameplay1.game_over: self.gameplay1.game_over = True self.end_screen = True self.popups1.append( Popup("You won!", color="green", gcolor="yellow")) self.popups2.append( Popup("You lost!", color="red", gcolor="orange")) elif self.gameplay1.game_over and self.gameplay2.game_over: self.end_screen = True self.popups1.append(Popup("Draw!", color="cyan")) self.popups2.append(Popup("Draw!", color="cyan")) if self.gameplay1.cancel or self.gameplay2.cancel: if self.end_screen or self.gameplay1.countdown > 0: self.finished = True return else: self.gameplay1.game_over = self.gameplay1.cancel self.gameplay2.game_over = self.gameplay2.cancel self.gameplay1.cancel = False self.gameplay2.cancel = False self.gameplay1.update() self.gameplay2.update() if self.gameplay1.score.duel_lines > 0: hole = randint(0, 9) self.gameplay2.add_garbage(hole, self.gameplay1.score.duel_lines) self.gameplay1.score.duel_lines = 0 if self.gameplay2.score.duel_lines > 0: hole = randint(0, 9) self.gameplay1.add_garbage(hole, self.gameplay2.score.duel_lines) self.gameplay2.score.duel_lines = 0 self.popups1.extend(self.gameplay1.get_popups()) self.gameplay1.clear_popups() self.popups2.extend(self.gameplay2.get_popups()) self.gameplay2.clear_popups() if not self.current_popup1 and self.popups1: self.current_popup1 = self.popups1.pop(0) elif self.current_popup1: if not self.current_popup1.update(): self.current_popup1 = None if not self.current_popup2 and self.popups2: self.current_popup2 = self.popups2.pop(0) elif self.current_popup2: if not self.current_popup2.update(): self.current_popup2 = None def draw(self) -> None: self.gameplay1.draw(130, 80) self.gameplay2.draw(880, 80) if self.current_popup1: self.current_popup1.draw(130 + 155, 80 + 220) if self.current_popup2: self.current_popup2.draw(880 + 155, 80 + 220)
def __init__(self, scr: CursesWindow) -> None: self.delay = 0 self.gui = Gui(scr) self.game = Gameplay(self.gui, self.gui.center)
menu.set_font_color(Settings.text_colour) menu.set_title_tab_color(Settings.menu_colour) except Exception as error: print(error) # add menu options to the Main Menu menu.add_option('Play', play_menu) menu.add_option('Game Rules', gamerules_menu.run) menu.add_option('About', about_menu) menu.add_option('Settings', settings_menu) menu.add_option('Exit', PYGAME_MENU_EXIT) #closes application # create object (different levels) from Gameplay class and pass level details game_level1 = Gameplay(screen, puck_speed=4, paddle_speed=2, paddle_length=80, level='Easy') game_level2 = Gameplay(screen, puck_speed=6, paddle_speed=2, paddle_length=80, level='Medium') game_level3 = Gameplay(screen, puck_speed=8, paddle_speed=2, paddle_length=60, level='Hard') game_level4 = Gameplay(screen, puck_speed=10, paddle_speed=4,
from gameplay import Gameplay from main_menu import MainMenu while True: MainMenu().run_menu() currentGameplay = Gameplay() currentGameplay.run_gameplay()
import socket from gameplay import Gameplay TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 1000 sockt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sockt.bind((TCP_IP, TCP_PORT)) sockt.listen(1) conn, addr = sockt.accept() print(addr) conn2, addr2 = sockt.accept() print(addr2) gameplay = Gameplay(conn, conn2) gameplay.start() conn2.send('closed'.encode()) conn2.recv(BUFFER_SIZE) conn2.close() conn.send('closed'.encode()) conn.recv(BUFFER_SIZE) conn.close()
from Player.player import PlayerField from Table.playerTable import TableGame from Computer.computer import ComputerField from gameplay import Gameplay _human_player = PlayerField() _human_table = TableGame(_human_player) _computer_player = ComputerField() allow_colors = True _game_brain = Gameplay(allow_colors, _human_table, _human_player, _computer_player)
class Online(State): def __init__(self) -> None: self.gameplay1 = Gameplay() self.gameplay2 = Gameplay() self.popups1: List[Popup] = [] self.popups2: List[Popup] = [] self.current_popup1: Optional[Popup] = None self.current_popup2: Optional[Popup] = None self.waiting = True self.waiting_cycle = 0 self.last_waiting_cycle = ctx.now self.buffer = b"" self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.settimeout(10.0) self.finished = False self.end_screen = False def is_finished(self) -> bool: return self.finished def initialize(self) -> None: self.gameplay1.set_device(ctx.device1) self.gameplay1.initialize() self.gameplay2.set_device(Device("dummy")) self.gameplay2.initialize() try: self.socket.connect(config.server) except (ConnectionRefusedError, socket.timeout): print("unable to connect to server") self.finished = True def update(self, switch_state: Callable) -> None: read, write, error = select.select([self.socket], [self.socket], [self.socket], 0) if self.waiting: self.gameplay1.cancel_input.update() if self.socket in read: buffer = self.socket.recv(BUFFER_SIZE) if buffer == b"go": self.waiting = False self.current_popup2 = None elif buffer == b"ping": self.socket.sendall(b"pong") elif buffer == b"": print("server disconnected") self.finished = True elif self.gameplay1.cancel: self.socket.close() self.finished = True return if not self.end_screen: if self.gameplay1.game_over and not self.gameplay2.game_over: self.gameplay2.game_over = True self.end_screen = True self.popups1.append( Popup("You lost!", color="red", gcolor="orange")) self.popups2.append( Popup("You won!", color="green", gcolor="yellow")) elif self.gameplay2.game_over and not self.gameplay1.game_over: self.gameplay1.game_over = True self.end_screen = True self.popups1.append( Popup("You won!", color="green", gcolor="yellow")) self.popups2.append( Popup("You lost!", color="red", gcolor="orange")) elif self.gameplay1.game_over and self.gameplay2.game_over: self.end_screen = True self.popups1.append(Popup("Draw!", color="cyan")) self.popups2.append(Popup("Draw!", color="cyan")) if self.gameplay1.cancel: self.gameplay1.send = True self.gameplay1.cancel = False self.gameplay1.game_over = True if self.end_screen or self.gameplay1.countdown > 0: self.finished = True self.gameplay1.update() if self.socket in read: try: self.gameplay2 = jsonpickle.decode( protocol.recv(self.socket).decode()) except (RuntimeError, ConnectionResetError): error = [self.socket] if self.socket in write and self.gameplay1.send: self.gameplay1.send = False try: protocol.send(self.socket, jsonpickle.encode(self.gameplay1).encode()) except (ConnectionResetError, ConnectionAbortedError): error = [self.socket] if self.socket in error: if not self.end_screen: print("communication error") self.finished = True if self.gameplay2.score.duel_lines > 0: hole = randint(0, 9) self.gameplay1.add_garbage(hole, self.gameplay2.score.duel_lines) self.gameplay2.score.duel_lines = 0 self.gameplay1.score.duel_lines = 0 self.popups1.extend(self.gameplay1.get_popups()) self.gameplay1.clear_popups() self.popups2.extend(self.gameplay2.get_popups()) self.gameplay2.clear_popups() if not self.current_popup1 and self.popups1: self.current_popup1 = self.popups1.pop(0) elif self.current_popup1: if not self.current_popup1.update(): self.current_popup1 = None if not self.current_popup2 and self.popups2: self.current_popup2 = self.popups2.pop(0) elif self.current_popup2: if not self.current_popup2.update(): self.current_popup2 = None def draw(self) -> None: self.gameplay1.draw(130, 80) self.gameplay2.draw(880, 80, draw_piece=not self.waiting) if self.waiting: Text.draw("Awaiting", size=4, centerx=880 + 155, top=220) Text.draw("opponent", size=4, gcolor="red", centerx=880 + 155, top=280) string = "." * self.waiting_cycle Text.draw(string, size=8, gcolor="black", centerx=880 + 155, top=350) if ctx.now - self.last_waiting_cycle > 0.5: self.last_waiting_cycle = ctx.now self.waiting_cycle = (self.waiting_cycle + 1) % 4 if self.current_popup1: self.current_popup1.draw(130 + 155, 80 + 220) if self.current_popup2: self.current_popup2.draw(880 + 155, 80 + 220)
def main(): """ Main Program """ pygame.mixer.pre_init(44100, -16, 2, 2048) pygame.init() # Initialize gameplay game = Gameplay() # Start game clock clock = pygame.time.Clock() # End loop condition loop = True # ======== Main Game Loop ======== while loop: for event in pygame.event.get(): if event.type == QUIT: loop = False # Check for level change game.change_level() # Limit to 60 FPS game.set_fps(FPS, clock) # Checks for input based on player type (ship vs hero) game.get_input() # Update each sprite groups in current level game.update() # Render level game.render_level(clock.get_fps()) # Update screen pygame.display.flip()
from gameplay import Gameplay from agents import RandomAgent, MarkovDecisionModelAgent, FilipkuvAgent import atari_py as ap from gym import envs #print sorted(ap.list_games()) #for e in sorted(envs.registry.env_specs.keys()): # print e #agent = MarkovDecisionModelAgent('riverraid.model') #agent = RandomAgent() agent = FilipkuvAgent() game = Gameplay('Riverraid-v0', agent) game.play() print game.get_score() print game.get_lives() # playback = game.get_playback() # # for i in xrange(100): # print "Playback len: %d" % len(playback._states) # game.play(playback) # print "Score: %d" % game.get_score() # #print game.get_lives() # playback = game.get_playback()
from action import PongAction from random import random def inverse_decay(N_sa, C=5.): return C / (C + N_sa) def best_action(s, Q, epsilon=0.05): actions = [PongAction.STILL, PongAction.DOWN, PongAction.UP] candidates = [(a_prime, Q[s][a_prime]) for a_prime in actions] return max(candidates, key=lambda x: x[1]) if __name__ == '__main__': import pickle from collections import defaultdict from gameplay import Gameplay with open('../models/onemillion/epoch.4000.model', 'rb') as f: qlAgent = pickle.load(f) n_games = 10000 wins = defaultdict(int) for n in range(n_games): gameplay = Gameplay(mdp_f=best_action, player_a=qlAgent) winner = gameplay.play_match() wins[winner] += 1 print('QLearningAgent win rate:', 1. * wins['QLearningAgent'] / n_games)
from player_commands import Player from gameplay import Gameplay from board import Board P = Player() B = Board() GP = Gameplay(P, B)