class Game: EVENT_SOURCE = "../sources/event_data.json" #The path to the file that contains the event data DEFAULT_PLAYERLIST = [ Player.Player("Player1"), Player.Player("Player2"), Player.Player("Player3"), Player.Player("Player4") ] def __init__(self, players=DEFAULT_PLAYERLIST, sourcefile=EVENT_SOURCE): self.__players = players self.__events = self.__readEvents(sourcefile) def __readEvents(self, path): """Reads in the events from the provided file.""" events = [] datafile = open(path, "r") jsondata = datafile.read() data = json.loads(jsondata) for chunk in data: chunk = chunk.values() chunk.reverse() events.append(Event.Event(*chunk)) return events def getPlayers(self): """Returns the list of players participating in this game.""" return self.__players def randEvent(self): return self.__events[random.randrange(0, len(self.__events))]
def check2(p1): if User.check(question, key, p1) == False: Player.lives -= 1 User.Type(random.choice(responses[1])) Player.getInfo() else: User.Type(random.choice(responses[0]))
def level_1(): g = Game() g.add_player( Player(position=Vector2(0.2, 0.5), movement=Vector2(0, 0.002 * random()))) g.add_player( Player(position=Vector2(0.8, 0.5), movement=Vector2(0, 0.002 * random()))) return g
async def wshandler(request): resp = web.WebSocketResponse() available = resp.can_prepare(request) if not available: with open(WS_FILE, "rb") as fp: return web.Response(body=fp.read(), content_type="text/html") await resp.prepare(request) new_player = Player() new_player.ws = resp try: print("New player joined.") request.app["players"].append(new_player) await spawn(request, game_handler(request)) async for msg in new_player.ws: if msg.type == web.WSMsgType.TEXT: message_data = json.loads(msg.data) if message_data.get("action") == "choice": await new_player.game.play( Choice[message_data["message"].upper()], new_player) elif message_data.get("action") == "forfeit": await new_player.game.forfeit(new_player) elif message_data.get("action") == "player_name": new_player.name = message_data.get("message") else: for player in request.app["players"]: if player.ws is not new_player.ws: await player.ws.send_json({ "action": "message", "player": new_player.name, "message": msg.data, }) else: return new_player.ws return new_player.ws finally: if new_player in request.app["players"]: request.app["players"].remove(new_player) print("Someone disconnected.")
def mousePressed(): global names, buttonX, buttonY, game if mouseX in buttonX and mouseY in buttonY and len(names) >= 2: players = [] for name in names: players.append(Player.Player(name)) main.createGame(players) main.createController() main.gameController.startTurn(None) main.currentScene.pop() turn.refresh() main.currentScene.append(main.scenes.get("turn"))
def handle_room(socket): try: msg = json.loads(socket.receive()) LOG.debug('handle_room socket opened. message : {}'.format(msg)) if not message.MsgReservedField.type_field in msg or\ CSMessageTypes.enter_room != msg['type']: raise errors.ProtocolError(msg, 'Invalid enterroomreq') room_no = msg['room_no'] player = Player(msg['name'], socket) token = msg['token'] if (lobby_instance.process_enter_room(player, room_no, token)): while not socket.closed: msg = socket.receive() if None is msg: raise Exception('Socket closed') LOG.info('Message received. player : {} msg: {}'.format( player, msg)) msg = json.loads(msg) player.process_message(msg) else: raise Exception('Room is full') except Exception as e: import traceback traceback.print_exc() err_msg = 'Handling room msg Error : {}'.format(e) LOG.error(err_msg) LOG.info('{} is leaving room due to error...'.format(player)) try: player.exit_room() socket.close() except Exception as e: LOG.info('exit room error {}'.format(e)) pass return str(e)
def __init__(self, parent): super().__init__(parent) db = sqlite3.connect('source/data/playertable') cursor = db.cursor() cursor.execute('SELECT name, cash FROM players') data = cursor.fetchall() print(data) db.commit() db.close() self.playerlist = {} for item in data: player = Player(item[0], item[1]) self.playerlist[item[0]] = player print(self.playerlist) self.player1 = self.playerlist["Playerholder"] self.player2 = self.playerlist["Computer"] # Image Decleration self.blank = tk.PhotoImage(file = "source/img/placeholdergrey.gif") # Placeholder self.orange = tk.PhotoImage(file = "source/img/orangegrey.gif") # Orange Counter self.red = tk.PhotoImage(file = "source/img/redgray.gif") # Red Counter self.show = tk.PhotoImage(file = "source/img/show.gif") # Top Button self.showred = tk.PhotoImage(file = "source/img/showred.gif") # Red Top Button self.showorange = tk.PhotoImage(file = "source/img/showorange.gif") # Orange Top Button self.header = tk.PhotoImage(file = "source/img/header.gif") # Header canvas = tk.Canvas(width=800, highlightthickness=0, relief='ridge') canvas.create_image(0, 0, image = self.header, anchor="nw") # Add header image to canvas canvas.place(x=0, y=0) # Header canvas ## CREATE POOL ## self.pool = tk.Canvas(width = 170, height = 150) self.round_rectangle(self.pool, 4, 4, 170, 150, r=10, fill="black") self.round_rectangle(self.pool, 9, 9, 165, 145, r=10, fill="#777777") self.pool.place(x=600, y=170) self.poolframe = tk.Frame(bg = "#777777") tk.Label(self.poolframe, text="POOL", bg = "#777777", font=("Verdana", 25, "bold")).pack() self.poollabel = tk.Label(self.poolframe, bg = "#777777", text=("$0"), font=("Verdana", 12)) self.poollabel.pack(pady=20) self.pool.create_window((85, 25), window = self.poolframe, anchor = 'n') ## CREATE PLAYER 1 INFO ## self.player1name = tk.StringVar(parent) self.player1name.set(self.player1.name) self.player1name.trace('w', self.update_player_1) self.player1info = tk.Canvas(width = 170, height = 150) self.round_rectangle(self.player1info, 4, 4, 170, 100, r=10, fill="#7F2A00") self.round_rectangle(self.player1info, 7, 7, 167, 97, r=10, fill="#FF5300") self.player1info.place(x=600, y=330) self.player1frame = tk.Frame(bg = "#FF5300") #self.player1name = tk.Label(self.player1frame, bg="#FF5300", text=self.player1.name) self.player1namedown = tk.OptionMenu(self.player1frame, self.player1name, *self.playerlist.keys()) self.player1namedown.pack() self.player1cash = tk.Label(self.player1frame, bg="#FF5300", text="$" + str(self.player1.cash)) self.player1cash.pack() self.player1info.create_window((85, 25), window = self.player1frame, anchor = 'n') ## CREATE PLAYER 2 INFO ## self.player2name = tk.StringVar(parent) self.player2name.set(self.player2.name) self.player2name.trace('w', self.update_player_2) self.player2info = tk.Canvas(width = 170, height = 150) self.round_rectangle(self.player2info, 4, 4, 170, 100, r=10, fill="#650400") self.round_rectangle(self.player2info, 7, 7, 167, 97, r=10, fill="#FF0900") self.player2info.place(x=600, y=445) self.player2frame = tk.Frame(bg = "#FF0900") self.player2namedown = tk.OptionMenu(self.player2frame, self.player2name, *self.playerlist.keys()) self.player2namedown.pack() self.player2cash = tk.Label(self.player2frame, bg="#FF0900", text="$" + str(self.player2.cash)) self.player2cash.pack() self.player2info.create_window((85, 25), window = self.player2frame, anchor = 'n') board_back = tk.Canvas(width = 570, height = 480) # Back canvas behind playing board makes a black outline board_back.place(x = 10, y = 115) board_back.create_rectangle(0, 59, 570, 432, fill = "black") self.board = tk.Frame(board_back) # Playing area board_back.create_window((3, 0), window = self.board, anchor='nw') self.grid = [[],[],[],[],[],[],[]] # Each space on the board is held on this grid as a (y, x) value for y in range(7): for x in range(7): if y == 0: thick = 0 else: thick = 1 self.grid[y].append(tk.Canvas(self.board, width = 79, height = 60, highlightthickness=thick, highlightbackground="black", relief='sunken')) # Add a new space on board for current (y, x) if y == 0: self.grid[y][x].create_image(0, 0, image = self.show, anchor='nw') # Add top buttons self.grid[y][x].bind("<Button-1>", partial(self.play, x)) self.grid[y][x].bind("<Enter>", self.change_on_enter) # Change image on cursor hover enter self.grid[y][x].bind("<Leave>", self.change_on_leave) # Change image on cursor hover leave else: self.grid[y][x].create_image(0, 1, image = self.blank, anchor='nw') # Add placeholder self.grid[y][x].grid(row = y, column = x) ## END OF INITILISATION ## self.disable()
import tkinter as tk import tkinter.ttk as ttk from tkinter import END import rooms as rom import locations as loc import weapons as wep from logic import Player, Monster player = Player("Player", "Bob", "This is you", 10, 10, 5, 0, 1) player.location = rom.entry_gate player.inventory.add(wep.vorpal_blade, 1) class LootWindow(tk.Frame): def __init__(self, parent, container_obj): tk.Frame.__init__(self, parent) self.parent = parent self.container_obj = container_obj self.loot_frame = tk.Frame(self.parent) self.loot_frame.grid(column=0, row=0) self.left_frame = tk.Frame(self.loot_frame) self.left_frame.grid(column=0, row=0) self.center_frame = tk.Frame(self.loot_frame) self.center_frame.grid(column=1, row=0) self.right_frame = tk.Frame(self.loot_frame) self.right_frame.grid(column=2, row=0) tk.Label(self.left_frame,
from menu import Menu from logic import Player, Logic from lists import modes, responses from dicts import continents, easy, medium, hard, medium_bonus, hard_bonus import time import random # Creates player object Player = Player(3) User = Logic("Player") # Creates and prints starting menu begin = Menu(["Instructions", "Play"]) User.Type("Welcome to Runaway Trivia!") begin.display() selection = eval(input("Enter Choice Here: ")) if selection == 0: User.Rules() # Creates/prints mode menu difficulty = Menu(["Easy", "Medium", "Hard"]) print("Pick your difficulty.") difficulty.display() challenge = eval(input("Enter Choice Here: ")) mode_str = f"You selected {modes[challenge]} mode." User.Type(mode_str) # Creates menus for choosing destinations easy_picker = Menu(continents["easy"]) medium_picker = Menu(continents["medium"]) hard_picker = Menu(continents["hard"])