Permalink
Browse files

Add a leaderboard using SQLite.

  • Loading branch information...
1 parent af12a22 commit 58fff19f90bcd0529406482410c4074cb03a0e52 @anthonydb committed Jul 12, 2012
Showing with 58 additions and 2 deletions.
  1. +10 −2 catchthecat.py
  2. +48 −0 db.py
  3. BIN leaderboard.db
View
@@ -5,8 +5,10 @@
import os
import random
import time
+from datetime import datetime
from randevents import general_events
from classes import cat, player
+from db import make_db, insert_db, results_db
from art import splash_text, win_text
calories = 0
@@ -18,6 +20,7 @@
# Set up the player and kitty. Player in foyer and kitty somewhere random.
def start():
+ make_db()
cls()
print splash_text
print 'Welcome to Catch the Cat! In this game, the objective is to'
@@ -276,7 +279,12 @@ def catch_test():
# FTW!
def win():
- print '\n\nYou caught the cat in ' + str(move_count) + ' moves!'
- print win_text
+ global move_count
+ print '\n\nYou caught the cat in ' + str(move_count) + ' moves!'
+ print win_text
+ insert_db(p.name, move_count, datetime.now())
+ results_db()
+
+# Start the game
start()
View
@@ -0,0 +1,48 @@
+import sqlite3
+import os.path
+
+def make_db():
+ if not os.path.exists('leaderboard.db'):
+ conn = sqlite3.connect('leaderboard.db')
+ cur = conn.cursor()
+ cur.execute('''CREATE TABLE leaders
+ (Name Text, Score Integer, GameDate Date)
+ ''')
+ conn.commit()
+ conn.close()
+ else: pass
+
+def insert_db(player, score, game_date):
+ conn = sqlite3.connect('leaderboard.db')
+ cur = conn.cursor()
+ cur.execute('''
+ INSERT INTO leaders VALUES (?,?,?);
+ ''', (player, score, game_date))
+ conn.commit()
+ conn.close()
+
+def results_db():
+ conn = sqlite3.connect('leaderboard.db')
+ cur = conn.cursor()
+ cur.execute('''
+ SELECT SUBSTR(name,1,20) AS name, score, strftime('%m/%d/%Y', gamedate)
+ FROM leaders
+ ORDER BY score ASC
+ LIMIT 20;
+ ''')
+ rows = cur.fetchall()
+ print '''\n\n
+ _ _
+| | | |
+| | ___ __ _ __| | ___ _ __ ___
+| | / _ \/ _` |/ _` |/ _ \ '__/ __|
+| |___| __/ (_| | (_| | __/ | \__ \\
+\_____/\___|\__,_|\__,_|\___|_| |___/
+ '''
+ print '\n\nNAME MOVES DATE'
+ print '____________________________________________________\n'
+ for row in rows:
+ print '%-20s %-10s %-30s' % (row[0], str(row[1]), row[2])
+ print '\n\n\n'
+
+ conn.close()
View
Binary file not shown.

0 comments on commit 58fff19

Please sign in to comment.