Since we are on the subject of Rock, Paper, Scissors. I've recntly watched this one video on how to make a .deb out of .py files and the tutor was using a rock, paper scissors game.<br><br>Not sure how this may come of use to you but, I'll post it anyway for you to look at. May help somehow.<br>
<br><a href="http://www.google.com/search?hl=en&q=filetype%3Apy+%22rock+paper+scissors%22&btnG=Search">http://www.google.com/search?hl=en&q=filetype%3Apy+%22rock+paper+scissors%22&btnG=Search</a><br><br><a href="http://www.showmedo.com/videos/video?name=linuxJensMakingDeb&fromSeriesID=37">http://www.showmedo.com/videos/video?name=linuxJensMakingDeb&fromSeriesID=37</a><br>
<br><br>Hope this help you in your journey for multiplayer game somehow<br><br><div class="gmail_quote">On Sun, Jan 4, 2009 at 3:58 AM, <a href="mailto:greywine@gmail.com">greywine@gmail.com</a> <span dir="ltr"><<a href="mailto:greywine@gmail.com">greywine@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi everyone,<br>
<br>
I'm learning python to get a multiplayer roleplaying game up and<br>
running.<br>
<br>
I didn't see any simple examples of multiplayer games on the web so I<br>
thought I'd post mine here. I choose Rock, Paper, Scissors as a first<br>
game to experiment with as the game logic/options are easy to<br>
implement and understand.<br>
<br>
Initially, I tried to get the socketserver.TCPServer example in the<br>
Python Docs to work, but couldn't get my game variables into the<br>
handle method of the class MyTCPHandler. And I didn't know how else<br>
to do it. I ended up creating my own server & client out of simple<br>
sockets based on the simple echo server & client examples in the<br>
Python Docs.<br>
<br>
I also wanted to send chat messages OR game variables back & forth,<br>
but I couldn't figure out how to do the OR, so the basic idea in this<br>
implementation is to send the dictionary 'game' back and forth. game<br>
dict contains all the gaming variables as well as any chat messages.<br>
The program processes results depending on whether the game is<br>
starting, there's a chat message, or there's a game move.<br>
<br>
Finally, in testing, I ran the server in IDLE, but I had to load a<br>
command prompt and switch to c:\python30; then type 'python<br>
rpsmulti.py' for the client every time. Anyone know how to test<br>
server/client code strictly in IDLE?<br>
<br>
Anyway, try it out and let me know how you would improve it.<br>
<br>
John R.<br>
<br>
# NAME: rpsmulti.py<br>
# DESCRIPTION: rock, paper, scissors game multiplayer game<br>
# AUTHOR: John Robinson<br>
# DATE: 1/3/09<br>
# VERSION: Python 3.0<br>
# TO DO:<br>
# .server_address instead of HOST, PORT?<br>
<br>
import socket<br>
from random import choice<br>
from pickle import dumps, loads<br>
from pprint import pprint<br>
<br>
HOST, PORT = "localhost", 9999 # defined for now<br>
BUFFSIZE = 1024 # for socket.send & recv commands<br>
BACKLOG = 2 # number of clients supported by<br>
server<br>
SECONDS = 3 # seconds until socket.timeout (not<br>
implemented)<br>
# moves dict to translate 'rps' choice<br>
MOVES = {'r':'Rock',<br>
'p':'Paper',<br>
's':'Scissors'}<br>
# outcome dict stores result for all possible scenarios<br>
OUTCOME = {('p','r'): 'win',<br>
('r','s'): 'win',<br>
('s','p'): 'win',<br>
('p','p'): 'draw',<br>
('r','r'): 'draw',<br>
('s','s'): 'draw',<br>
('r','p'): 'lose',<br>
('s','r'): 'lose',<br>
('p','s'): 'lose'}<br>
<br>
def main_menu(game):<br>
""" initialize game dict variables & opening screen of the game<br>
"""<br>
game['total'] = 0 # total number of games played<br>
game['won'] = 0 # total games won by player<br>
game['lost'] = 0 # total games lost by player<br>
game['drew'] = 0 # total games drew by player<br>
game['move'] = '' # player's move (rps)<br>
game['message'] = '' # player's chat message<br>
game['sentmessage'] = '' # player's previous message<br>
game['start'] = True # setting up the game boolean<br>
<br>
print("\tROCK PAPER SCISSORS\n")<br>
if game['name']=='': # if returning to menu don't display<br>
the following<br>
game['name'] = get_name()<br>
print("Welcome "+game['name']+". Remember...")<br>
print("Rock smashes scissors! Paper covers Rock! Scissors cuts<br>
paper!\n")<br>
print("1. Play single player game")<br>
print("2. Start two player game")<br>
print("3. Join two player game")<br>
print("4. Quit")<br>
print()<br>
c = safe_input("Your choice? ", "1234")<br>
c = int(c)<br>
if c==1:<br>
one_player(game)<br>
elif c==2:<br>
start_two_player(game)<br>
elif c==3:<br>
two_player_join(game)<br>
else:<br>
print('Play again soon.')<br>
<br>
def safe_input(prompt, values='abcdefghijklmnopqrstuvwxyz'):<br>
""" gives prompt, checks first char of input, assures it meets<br>
given values<br>
default is anything goes """<br>
while True:<br>
i = input(prompt)<br>
try:<br>
c = i[0].lower()<br>
except IndexError: # the only possible error?!<br>
if c=='':<br>
print("Try again.")<br>
else:<br>
if c not in values: # some other character<br>
print("Try again.")<br>
else: # looks good. continue.<br>
break<br>
return i<br>
<br>
def get_name():<br>
""" returns input as name """<br>
while True:<br>
name = input("What is your name? ")<br>
check = input(name + ". Correct (y/n)? ")<br>
if check[0] in 'yY':<br>
break<br>
return name<br>
<br>
def get_result(player, opponent):<br>
""" reports opponent's choice;<br>
checks player and opponent dicts ['move'] against OUTCOME<br>
dict;<br>
reports result<br>
returns player dict with updated values """<br>
print(opponent['name'], 'chose %s.' % (MOVES[opponent['move']]))<br>
# check lookout dict (OUTCOME dictionary)<br>
result = OUTCOME[(player['move'], opponent['move'])]<br>
# update game variables<br>
player['total'] += 1<br>
if result=='win':<br>
print('%s beats %s. You win.' % (MOVES[player['move']], MOVES<br>
[opponent['move']]))<br>
player['won'] += 1<br>
elif result=='draw':<br>
print('%s - %s: no one wins. You draw.' % (MOVES[player<br>
['move']], MOVES[opponent['move']]))<br>
player['drew'] += 1<br>
else:<br>
print('%s loses to %s. You lose.' % (MOVES[player['move']],<br>
MOVES[opponent['move']]))<br>
player['lost'] += 1<br>
return player<br>
<br>
def one_player(game):<br>
""" implements one player game with minimal opponent dict """<br>
print("\nType (R)ock, (P)aper or (S)cissors to play. (q)uit to<br>
return to \<br>
main menu.")<br>
opponent = {}<br>
opponent['name'] = 'Computer'<br>
<br>
# gaming loop<br>
while True:<br>
# gets player's choice<br>
game['move'] = safe_input('1, 2, 3, GO! ','rpsq')<br>
if game['move']=='q':<br>
break<br>
# computer chooses via random.choice<br>
opponent['move'] = choice('rps')<br>
# check game outcome dict<br>
game = get_result(game, opponent)<br>
<br>
# exit loop<br>
print('\nYou won %s, lost %s, drew %s (%s total)\n' % \<br>
(game['won'],game['lost'],game['drew'],game['total']))<br>
main_menu(game)<br>
<br>
def start_two_player(game):<br>
""" starts tcp server and implements two player game<br>
game dict = player 1"""<br>
# Create a socket (SOCK_STREAM means a TCP socket)<br>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>
sock.bind((HOST, PORT))<br>
sock.listen(BACKLOG)<br>
serverip, serverport = sock.getsockname()<br>
print("Running at %s, %s" % (serverip, serverport))<br>
print("\nType (R)ock, (P)aper or (S)cissors to play. (q)uit to \<br>
return to main menu.")<br>
print("Waiting for player...")<br>
<br>
client, address = sock.accept()<br>
clientip, clientport = address<br>
# server/game loop<br>
while True:<br>
try:<br>
P2game = loads(client.recv(BUFFSIZE)) # receive other<br>
game variables<br>
except EOFError: # if available<br>
print(P2game['name'], "left the game.")<br>
break<br>
client.send(dumps(game)) # send our<br>
variables<br>
# it's either the start...<br>
if P2game['start']:<br>
print(P2game['name'],"logged on at", clientip, clientport)<br>
game['start'] = False<br>
# or there's a message<br>
if P2game['message']!='' and P2game['message']!=game<br>
['sentmessage']:<br>
print(P2game['name']+': '+P2game['message'])<br>
game['sentmessage'] = P2game['message'] # to avoid many<br>
print calls<br>
game['move'] = '' # message always takes<br>
priority<br>
# or there's a move<br>
if game['move']=='':<br>
game['move'] = safe_input('1, 2, 3, GO! ')<br>
if game['move']=='q':<br>
break<br>
elif game['move'] not in 'rps':<br>
game['message'] = game['move']<br>
game['move'] = ''<br>
# only check result if P2game also made a move<br>
if P2game['move']!='':<br>
# check game outcome dict<br>
game=get_result(game, P2game)<br>
game['move']=''<br>
# exit loop<br>
client.close()<br>
print('\nYou won %s, lost %s, drew %s (%s total)\n' % \<br>
(game['won'],game['lost'],game['drew'],game['total']))<br>
main_menu(game)<br>
<br>
def two_player_join(game):<br>
""" joins a tcp server two player game<br>
game dict = player 2"""<br>
# Create a socket (SOCK_STREAM means a TCP socket)<br>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>
sock.connect((HOST, PORT))<br>
<br>
print("\nType (R)ock, (P)aper or (S)cissors to play. (q)uit to \<br>
return to main menu.")<br>
<br>
# client/game loop<br>
while True:<br>
sock.send(dumps(game))<br>
try:<br>
P1game = loads(sock.recv(BUFFSIZE))<br>
except EOFError:<br>
print(P1game['name'], "left the game.")<br>
break<br>
if P1game['start']:<br>
print("You're connected to "+P1game['name']+"'s game.")<br>
game['start'] = False<br>
if P1game['message']!='' and P1game['message']!=game<br>
['sentmessage']:<br>
print(P1game['name']+': '+P1game['message'])<br>
game['sentmessage'] = P1game['message']<br>
game['move'] = ''<br>
if game['move']=='':<br>
game['move'] = safe_input('1, 2, 3, GO! ')<br>
if game['move']=='q':<br>
break<br>
elif game['move'] not in 'rps':<br>
game['message'] = game['move']<br>
game['move'] = ''<br>
if P1game['move']!='':<br>
# check game outcome dict<br>
game=get_result(game, P1game)<br>
game['move']=''<br>
# exit loop<br>
sock.close()<br>
print('\nYou won %s, lost %s, drew %s (%s total)\n' % \<br>
(game['won'],game['lost'],game['drew'],game['total']))<br>
main_menu(game)<br>
<br>
if __name__=='__main__':<br>
game = {} # initialize game dict to store all game<br>
variables<br>
game['name'] = '' # player's name initially<br>
main_menu(game)<br>
<font color="#888888"><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я<br>а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я<br>