示例#1
0
def client_main():
    """ connects to server, logs in / register, and starts game """
    print('Connecting to server...')
    game_session = GameSession()
    game_session.connect()

    # Login / Register loop
    while True:
        choice = input('login or register? l / r -> ')
        username, password = input('Enter username and password: '******'l' or choice == 'login':
            if game_session.login(username, password):
                break
            print('Failed to login')
        elif choice == 'r' or choice == 'register':
            if game_session.register(username, password):
                break
            print('Failed to register')
        else:
            print('Please enter l,r,login or register')

    # Wait to start game (blocking call)
    game_session.wait_for_game_start()

    while True:
        print("game started")
        game = Game(game_session)
        game.start()
示例#2
0
async def start(ctx) -> None:  #Starts the game in the current channel
    '''
    Parameters:
        - ctx: Discord context object for the command call.\n
    Attempts to start the game for the user of the command in the channel the command was called in.\n
    If the game cannot be started, the reason why is sent the channel.\n
    If the game can be started, the game object is created from the lobby in the channel the command was called from, and the lobby is closed.\n
    Returns: None
    '''
    if channelInGame(ctx.channel.id):
        await ctx.send(
            "The game in this channel is already in progress. Switch to a different channel or wait for this game to end."
        )
        return
    elif playerInGame(ctx.author.id):
        await ctx.send(
            f"You are already in a game in <#{playersInGame[ctx.author.id]}>. Type `/leave` to leave that game to join a new one."
        )
        return
    elif not channelHasLobby(ctx.channel.id):
        await ctx.send(
            f"There is not a lobby in this channel. Join one with `/join`.")
        return
    elif not playerInLobby(ctx.author.id):
        await ctx.send(
            f"You are not in the lobby. Type `/join` to join the lobby.")
        return
    elif playersInLobby[
            ctx.author.
            id] != ctx.channel.id:  #The above if shows that the player is in a lobby, so no check for that is needed
        await ctx.send(
            f"You are already in a lobby in <#{playersInLobby[ctx.author.id]}>. Type `/leave` to leave that lobby to join a new one."
        )
        return
    elif len(
            openLobbies[ctx.channel.id].players
    ) < 1:  #The above ifs prove that there is a lobby in this channel with only one player, the command user, so no checks are needed
        await ctx.send(
            f"There are not enough players in this lobby. Get at least one more to join with `/join`."
        )
        return
    else:
        game = Game(openLobbies[ctx.channel.id])  #Create new game object
        await game.startGame()  #Starts the newly created game
        del (openLobbies[ctx.channel.id]
             )  #Deletes the lobby from the dictionary
示例#3
0
from random import random

import pyglet
pyglet.options['debug_gl'] = False

from client.game import Game
from client.game_client import GameClient
from client.config import HOST, PORT

client = GameClient(HOST, PORT)
client.auth('123')
id = client.add_character(name=str(random()), cls='warrior')
client.enter_game(id)

window = pyglet.window.Window(fullscreen=False, width=1024, height=700)
game = Game(window, client)

pyglet.app.run()
示例#4
0
def main(argv=None):
    game = Game()
    game.start()

    return 0
示例#5
0
from client.game import Game

if __name__ == "__main__":
    Game().mainMenu()
示例#6
0
    def run(self):
        print("Client running")
        self.display = Display()
        self.networker = Networker()
        self.game = Game()
        #self.display.set_up()

        self.connected = False
        while not self.connected:
            self.connected = self.networker.init_connection()

        self.connected_cf = False
        while not self.connected_cf:
            self.connected_cf = self.networker.check_connection()

        self.connection_made.emit(True)

        # We are now looking for a lobby
        while self.nickname is None:
            # Will be update by the display
            pass

        print("Found nickname {}".format(self.nickname))
        self.networker.join_lobby(self.nickname)

        name_allowed = False
        recheck = False
        while not name_allowed:
            if not recheck:
                allowed = self.networker.check_name_allowed()
                if allowed is False:  # As opposed to allowed is None
                    recheck = True
                    self.name_allowed.emit(False)
                elif allowed:
                    name_allowed = True
                    self.name_allowed.emit(True)
            else:
                self.nickname = None
                while self.nickname is None:
                    pass
                self.networker.join_lobby(self.nickname)
                recheck = False

        # We have an allowed name now
        joined_lobby = False
        while not joined_lobby:
            joined, other_names, self.lobby_id = self.networker.check_join_lobby(
            )
            if joined:
                joined_lobby = True
        self.lobby_joined.emit(self.lobby_id, other_names)
        self.display.join_lobby(other_names)
        # We are now in a lobby, so we wait for the game to start
        game_started = False
        self.starting_soon = False
        self.players = other_names
        self.time_to_start = 60
        while not game_started:
            if not self.starting_soon:
                updated = self.lobby_update()
                if updated:
                    print(
                        f"Emitting values {self.time_to_start} {self.players}")
                    self.lobby_update_info.emit(self.time_to_start,
                                                self.players)
                    self.display.update_lobby(self.time_to_start, self.players,
                                              self.starting_soon)
            else:
                start_info = self.networker.game_start()
                if start_info:
                    game_started = True
        print("Game started")
        print("Game info of " + str(start_info))
示例#7
0
#!/usr/bin/env python3
import sys
import asyncio

from client.game import Game

if len(sys.argv) != 1 + 3:
    print("Wrong arguments count. Expected <address> <port> <playername>")
    raise SystemExit(1)

*addr, name = sys.argv[1:]

game = Game(name)

asyncio.run(game.connect(addr))
示例#8
0
    server_state = channel.read_new_state()

    scene = Lobby(screen, channel, chat_channel, server_state, name)
    game_started = False

    state_fetch_executor = ThreadPoolExecutor(max_workers=1)
    state_fetch_future = state_fetch_executor.submit(channel.read_new_state)

    running = True
    while running:
        if state_fetch_future.done():
            server_state = state_fetch_future.result()
            state_fetch_future = state_fetch_executor.submit(
                channel.read_new_state)

        if server_state.is_game_mode and not game_started:
            scene = Game(screen, channel, server_state)
            game_started = True

        events = pygame.event.get()
        scene.update(server_state, events)
        scene.draw()

        for event in events:
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
示例#9
0
import sys
import asyncio
import websockets
from client.game import Game
import server.server as server

if __name__ == '__main__':
    if sys.argv[1] == "s":
        server.game = server.Game()
        start_server = websockets.serve(server.handler, 'localhost', 8080)

        asyncio.get_event_loop().run_until_complete(start_server)
        asyncio.get_event_loop().run_forever()
    elif sys.argv[1] == "c":
        ip = sys.argv[2]
        username = sys.argv[3]
        game = Game(ip, username)
        asyncio.get_event_loop().run_until_complete(game.start())