forked from ewindisch/pydance
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pydance.py
executable file
·214 lines (181 loc) · 7.11 KB
/
pydance.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# pydance - a dancing game written in Python
import os
import sys
from getopt import getopt
VERSION = "1.1.0"
from i18n import *
# fuck you, Python.
def print_help():
print
print _("Usage: %s [options]") % sys.argv[0]
print _(" -h, --help display this help text and exit")
print _(" -v, --version display the version and exit")
print _(" -f, --filename load and play a step file")
print _(" -m, --mode the mode to play the file in (default SINGLE)")
print _(" -d, --difficulty the difficult to play the file (default BASIC)")
raise SystemExit
def print_version():
print _("pydance %s by Joe Wreschnig, Brendan Becker, and others") % VERSION
print "[email protected] - http://icculus.org/pyddr"
raise SystemExit
if len(sys.argv) < 2: pass
elif sys.argv[1] in ["--help", "-h"]: print_help()
elif sys.argv[1] in ["--version", "-v"]: print_version()
# Don't import anything that initializes the joysticks or config until
# after we're (reasonably) sure no one wants --help or --version.
from constants import * # This needs to be here to set sys.path.
import util
import games
import dance
import pygame
import courses
import colors
import records
import menudriver
from fileparsers import SongItem
from pygame.mixer import music
from fontfx import TextProgress
from error import ErrorMessage
from fonttheme import FontTheme
import ui
# Set our required display paramters. Currently, this is nothing
# strange on any platforms, but in the past and likely in the future
# some platforms need other flags.
def set_display_mode():
try:
flags = 0
if mainconfig["fullscreen"]: flags |= FULLSCREEN
screen = pygame.display.set_mode([640, 480], flags, 16)
except:
raise SystemExit(_("E: Can't get a 16 bit display! pydance doesn't work in 8 bit mode."))
return screen
# Load a single song (given the filename) and then play it on the
# given difficulty.
def play_and_quit(fn, mode, difficulty):
print _("Entering debug (play-and-quit) mode.")
screen = set_display_mode()
pygame.display.set_caption("pydance " + VERSION)
pygame.mouse.set_visible(0)
pc = games.GAMES[mode].players
dance.play(screen, [(fn, [difficulty] * pc)],
[player_config] * pc, game_config, mode)
raise SystemExit
# Pass a list of files to a constructor (Ctr) that takes the filename
# as the first argument, and the args tuple as the rest.
def load_files(screen, files, type, Ctr, args):
if len(files) == 0: return []
screen.fill(colors.BLACK)
pct = 0
inc = 100.0 / len(files)
# Remove duplicates
files = list(dict(map(None, files, [])).keys())
objects = []
message = _("Found %d %s. Loading...") % (len(files), _(type))
pbar = TextProgress(FontTheme.loading_screen, message, colors.WHITE, colors.BLACK)
r = pbar.render(0).get_rect()
r.center = [320, 240]
for f in files:
try: objects.append(Ctr(*((f,) + args)))
except RuntimeError, message:
print _("E:"), f
print _("E:"), message
print
except Exception, message:
print _("E: Unknown error loading"), f
print _("E:"), message
print _("E: Please contact the developers ([email protected]).")
print
pct += inc
img = pbar.render(pct)
pygame.display.update(screen.blit(img, r))
return objects
# Support fullscreen on Win32 / OS X?
if osname != "posix": pygame.display.toggle_fullscreen = set_display_mode
else: pass
# Actually start the program running.
def main():
print "pydance", VERSION, "<[email protected]> - irc.freenode.net/#pyddr"
if mainconfig["usepsyco"]:
try:
import psyco
print _("Psyco optimizing compiler found. Using psyco.full().")
psyco.full()
except ImportError: print _("W: Psyco optimizing compiler not found.")
# default settings for play_and_quit.
mode = "SINGLE"
difficulty = "BASIC"
test_file = None
for opt, arg in getopt(sys.argv[1:],
"hvf:d:m:", ["help", "version", "filename=",
"difficulty=", "mode="])[0]:
if opt in ["-h", _("--help")]: print_help()
elif opt in ["-v", _("--version")]: print_version()
elif opt in ["-f", _("--filename")]: test_file = arg
elif opt in ["-m", _("--mode")]: mode = arg
elif opt in ["-d", _("--difficulty")]: difficulty = arg
if test_file: play_and_quit(test_file, mode, difficulty)
song_list = []
course_list = []
for dir in mainconfig["songdir"].split(os.pathsep):
print _("Searching for songs in"), dir
# The order of patterns is significant. Deduplication will remove songs that
# match a later pattern if they are in the same directory as a song with the
# same basename but a different extension that matches an earlier pattern.
# E.g. many ZIP files contain both .sm and .dwi. The .dwi will be ignored in
# this case.
song_list.extend(util.find(dir, ['*.dance', '*.sm', '*.dwi', '*/song.*'], 1))
for dir in mainconfig["coursedir"].split(os.pathsep):
print _("Searching for courses in"), dir
course_list.extend(util.find(dir, ['*.crs'], 0))
screen = set_display_mode()
pygame.display.set_caption("pydance " + VERSION)
pygame.mouse.set_visible(False)
try:
if os.path.exists("/usr/share/pixmaps/pydance.png"):
icon = pygame.image.load("/usr/share/pixmaps/pydance.png")
else: icon = pygame.image.load(os.path.join(pydance_path, "icon.png"))
pygame.display.set_icon(icon)
except: pass
music.load(os.path.join(sound_path, "menu.ogg"))
music.play(4, 0.0)
songs = load_files(screen, song_list, _("songs"), SongItem, (False,))
# Construct the song and record dictionaries for courses. These are
# necessary because courses identify songs by title and mix, rather
# than filename. The recordkey dictionary is needed for player's
# picks courses.
song_dict = {}
record_dict = {}
for song in songs:
mix = song.info["mix"].lower()
title = song.info["title"].lower()
if song.info["subtitle"]: title += " " + song.info["subtitle"].lower()
if not song_dict.has_key(mix): song_dict[mix] = {}
song_dict[mix][title] = song
record_dict[song.info["recordkey"]] = song
crs = load_files(screen, course_list, _("courses"), courses.CourseFile,
(song_dict, record_dict))
crs.extend(courses.make_players(song_dict, record_dict))
records.verify(record_dict)
# Let the GC clean these up if it needs to.
song_list = None
course_list = None
record_dict = None
ui.ui.clear()
if len(songs) < 1:
ErrorMessage(screen,
(_("You don't have any songs or step files. Check out "
"http://icculus.org/pyddr/get.php#songs "
"and download some free ones. "
"If you already have some, make sure they're in ")) +
mainconfig["songdir"])
raise SystemExit(_("You don't have any songs. Check http://icculus.org/pyddr/get.php#songs ."))
menudriver.do(screen, (songs, crs, screen))
# Clean up shit.
music.stop()
pygame.quit()
mainconfig.update(game_config)
mainconfig.update(player_config)
mainconfig.write(os.path.join(rc_path, "pydance.cfg"))
records.write()
if __name__ == '__main__': main()