File: misc.py

package info (click to toggle)
pyracerz 0.2-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 5,148 kB
  • ctags: 108
  • sloc: python: 2,657; makefile: 49; sh: 36
file content (263 lines) | stat: -rw-r--r-- 7,341 bytes parent folder | download | duplicates (3)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Copyright (C) 2005  Jujucece <jujucece@gmail.com>
#
# This file is part of pyRacerz.
#
# pyRacerz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# pyRacerz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyRacerz; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import pygame
from pygame.locals import *

import random
import os
import sys
import ConfigParser
import sha

VERSION = "0.2"

lightColor = (230, 230, 255)
darkColor = (118, 118, 151)

background = None
screen = None

popUpFont = None
titleFont = None
itemFont = None
smallItemFont = None
bigFont = None

music = 1

zoom = 1

def init():
  global popUpFont
  global titleFont
  global itemFont
  global smallItemFont
  global bigFont
  global background

  try:
    popUpFont = pygame.font.Font(os.path.join("fonts", "alba", "ALBA____.TTF"), int(16*zoom))
    titleFont = pygame.font.Font(os.path.join("fonts", "alba", "ALBA____.TTF"), int(52*zoom))
    itemFont = pygame.font.Font(os.path.join("fonts", "alba", "ALBA____.TTF"), int(34*zoom))
    smallItemFont = pygame.font.Font(os.path.join("fonts", "alba", "ALBA____.TTF"), int(30*zoom))
    bigFont = pygame.font.Font(os.path.join("fonts", "alba", "ALBA____.TTF"), int(66*zoom))
  except Exception, e:
    print "Cannot initialize fonts:"
    print e
    sys.exit(-1)

  background = pygame.transform.scale(pygame.image.load(os.path.join("sprites", "background.png")).convert(), (int(1024*zoom), int(768*zoom)))

def chrono2Str(chrono):
  return str(chrono/100.0).replace(".", "''")

def wait4Key():
  # Clear event queue
  pygame.event.clear()

  # Wait for key Input
  ok = 0
  while ok == 0:
    for event in pygame.event.get():
      if event.type == QUIT:
        sys.exit(0)
      if event.type == KEYDOWN:
        ok = 1
        break

  # Clear event queue
  pygame.event.clear()  

def startRandomMusic():
  global music

  stopMusic()

  if music == 1:
    # Randomly choose the Music among .ogg files
    musics = []
    listFiles = os.listdir("musics")
    for fileMusic in listFiles:
      if fileMusic.endswith(".ogg") or fileMusic.endswith(".OGG"):
        musics.append(fileMusic)

    if len(musics) > 0:
      rand = random.randint(0, len(musics)-1)
      try:
        pygame.mixer.music.load(os.path.join("musics", musics[rand]))
        pygame.mixer.music.play()
      except Exception, e:
        print "Music: %s unable to play..." % musics[rand] 
        print e

def stopMusic():
  pygame.mixer.music.fadeout(1000)

class PopUp:
  def __init__(self, track):
    self.track = track
    self.listElement = []
    self.rect = pygame.Rect(0, 688*zoom, 260*zoom, 80*zoom)

  def addElement(self, car, text):
    self.listElement.append([car, text, 0])

  def display(self):

    #Erase PopUp Area
    screen.blit(self.track.track, self.rect, self.rect)
        
    #If useful, display PopUp Area
    if self.listElement != []:

      y = 740*zoom

      for elem in self.listElement:
        x = 0
        carMini = elem[0].miniCar
        carMiniRect = carMini.get_rect()
        carMiniRect.x = x
        x = x + carMiniRect.width

        text = popUpFont.render(elem[1], 1, lightColor, (0, 0, 0))
        textRect = text.get_rect()
        textRect.x = x
        textRect.y = y
        screen.blit(text, textRect)

        carMiniRect.centery = textRect.centery 
        screen.blit(carMini, carMiniRect)

        # Remove an old element
        if elem[2] == 400:
          self.listElement.remove(elem)
        else:
          elem[2] = elem[2] + 1
        y = y - textRect.height

def addHiScore(track, player):

  fileExist = 1

  confFile=ConfigParser.SafeConfigParser() 
  try:
    confFile.readfp(file(".pyRacerz.conf", "r")) 
  except Exception:
    fileExist = 0
  
  # If the track is not represented, create it
  if fileExist == 0 or not confFile.has_section("hi " + track.name):
    fwrite = file(".pyRacerz.conf", "w+")
    confFile.add_section("hi " + track.name)
    confFile.write(fwrite)
    confFile.readfp(file(".pyRacerz.conf", "r")) 

  # For the Inverse
  if track.reverse == 0:
    level = player.level
  else:
    level = -player.level

  # If the Level is not represented create it and put the Hi-scores
  if not confFile.has_option("hi " + track.name, "level" + str(level)):
    h = sha.new(str(track.name))
    h.update(str("level" + str(level)))
    h.update(player.name)
    h.update(str(player.bestChrono))
    fwrite = file(".pyRacerz.conf", "w+")
    confFile.set("hi " + track.name, "level" + str(level), player.name + " " + str(player.bestChrono) + " " + h.hexdigest())
    confFile.write(fwrite)
    return 1
  else:
    hi = confFile.get("hi " + track.name, "level" + str(level)).split()
    h = sha.new(str(track.name))
    h.update(str("level" + str(level)))
    h.update(hi[0])
    h.update(hi[1])
    if hi[2] == h.hexdigest():
      if int(hi[1]) > player.bestChrono:
        h = sha.new(str(track.name))
        h.update(str("level" + str(level)))
        h.update(player.name)
        h.update(str(player.bestChrono))
        fwrite = file(".pyRacerz.conf", "w+")
        confFile.set("hi " + track.name, "level" + str(level), player.name + " " + str(player.bestChrono) + " " + h.hexdigest())
        confFile.write(fwrite)
        return 1
      else:
        return 0
    else:
      # If the HiScore is Corrupted, erase it
      h = sha.new(str(track.name))
      h.update(str("level" + str(level)))
      h.update(player.name)
      h.update(str(player.bestChrono))
      fwrite = file(".pyRacerz.conf", "w+")
      confFile.set("hi " + track.name, "level" + str(level), player.name + " " + str(player.bestChrono) + " " + h.hexdigest())
      confFile.write(fwrite)
      return 1

def getUnlockLevel():

  confFile=ConfigParser.SafeConfigParser() 
  try:
    confFile.readfp(file(".pyRacerz.conf", "r")) 
  except Exception:
    return 0

  if not confFile.has_section("unlockLevel"):
    return 0
  if not confFile.has_option("unlockLevel", "key"):
    return 0

  key = confFile.get("unlockLevel", "key").split()
  h = sha.new("pyRacerz")
  h.update(str(key[0]))
  if h.hexdigest() == key[1]:
    return key[0]
  else:
    return 0

def setUnlockLevel(lck):

  # Only change the unlock level if it's better than the actual one
  if getUnlockLevel() >= lck:
    return

  fileExist = 1

  confFile=ConfigParser.SafeConfigParser() 
  try:
    confFile.readfp(file(".pyRacerz.conf", "r")) 
  except Exception:
    fileExist = 0

  if fileExist == 0 or not confFile.has_section("unlockLevel"):
    fwrite = file(".pyRacerz.conf", "w+")
    confFile.add_section("unlockLevel")
    confFile.write(fwrite)
    confFile.readfp(file(".pyRacerz.conf", "r"))

  h = sha.new("pyRacerz")
  h.update(str(lck))
  fwrite = file(".pyRacerz.conf", "w+")
  confFile.set("unlockLevel", "key", str(lck) + " " + h.hexdigest())
  confFile.write(fwrite)