UPDATE: The original strings were in spanish, so I translated them to english for a better understanding.
I didn’t mean to post this, but the code of this mini game has grown (in 2 hours) so much that I thought someone would be interested. It is a “Guess the film” game that a friend asked me to do (while finishing the petition with: I’m not gonna pay you! hehe).
It’s a very basic version that works on terminal/windows command line. You must provide it with a file with film names which must be interpreted (mute) by the person who guessed the last film, and the program does the rest. It has a player system with points, and a very basic stats system with a list of player points and game time. Hope you’ll find it useful!.
And one more thing, I know the code can be improved, but what the hell, I wont get paid for this! :-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| # Copyright (c) 2010 Oscar Carballal Prego <info@oscarcp.com>
# Distributed under MIT license
# -*- coding: utf-8 -*-
# TODO:
# - Replace lists for dictionaries.
# - Solve the codification problem in Windows.
# - Improve the films list to have a director and description.
# Import python modules
import sys
import random
import datetime
# Open files and create empty lists
films_file = open('films.txt', 'r')
films = []
players = []
points = []
n_players = 0
def generate_random():
"""
This function picks a random film from the films list.
"""
print '\n %s' % (random.choice(films)[0])
def get_players():
"""
This function asks how many players and their names to store them and
make the stats after the game.
"""
i = 0
global n_players
try:
n_players = int(raw_input('* How many players? '))
except:
print 'ERROR: You must provide a number. Exiting...'
sys.exit(1)
if n_players == 1:
print '* Are you kidding? You can\'t play this alone!'
sys.exit(0)
else:
fake_players = n_players - 1
while fake_players >= 0:
points.append(0)
fake_players -= 1
while i < n_players:
player_name = raw_input('Player %s name: ' % (i))
players.append(player_name)
i += 1
def get_points():
winner = int(raw_input('* Who guessed? (player number): '))
points[winner] += 1
def endgame():
global n_players
films_file.close()
end = datetime.datetime.now()
game_time = str((end - start)).split(':')
print " "
print " "
print " In a game with %s players, the statistics are:" % (n_players)
print " "
print " Game time: %s hours, %s minutes, %s seconds." % (game_time[0],
game_time[1],
game_time[2].split('.')[0])
print " "
print " Points"
print " ----------"
print " "
players_counter = n_players - 1
while players_counter >= 0:
print " %s won %s points." % (players[players_counter],
points[players_counter])
players_counter -= 1
print " "
pos_winner = points.index(max(points))
print " ******* Winner: %s *******" % (players[pos_winner])
print " "
sys.exit(0)
# Set start time (due to the speed of execution we can put this here instead
# of calling it in the first game call, it just makes some microseconds more)
start = datetime.datetime.now()
# Fill the films list with film names
[films.append([line]) for line in films_file]
get_players()
while True:
generate = raw_input('* Generate film? (y/n) ')
[endgame() if generate == 'n' or generate == 'no' else generate_random()]
get_points() |