Merge lp:~alecu/quickly/desktopcouch-scores into lp:quickly

Proposed by Alejandro J. Cura
Status: Merged
Merge reported by: Rick Spencer
Merged at revision: not available
Proposed branch: lp:~alecu/quickly/desktopcouch-scores
Merge into: lp:quickly
Diff against target: 206 lines (+103/-9)
6 files modified
data/templates/ubuntu-pygame/help/tutorial.xml (+2/-2)
data/templates/ubuntu-pygame/project_root/bin/project_name (+15/-7)
data/templates/ubuntu-pygame/project_root/python/game.py (+2/-0)
data/templates/ubuntu-pygame/project_root/python/hiscores.py (+81/-0)
data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py (+2/-0)
data/templates/ubuntu-pygame/test/filelist.sh (+1/-0)
To merge this branch: bzr merge lp:~alecu/quickly/desktopcouch-scores
Reviewer Review Type Date Requested Status
Rick Spencer Approve
Review via email: mp+50552@code.launchpad.net

Commit message

Save hiscores in desktopcouch, and show them while waiting for the game to start.

Description of the change

Save hiscores in desktopcouch, and show them while waiting for the game to start.

To post a comment you must log in.
Revision history for this message
Rick Spencer (rick-rickspencer3) wrote :

Code looked good and worked well. Thanks for the excellent contribution!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/templates/ubuntu-pygame/help/tutorial.xml'
2--- data/templates/ubuntu-pygame/help/tutorial.xml 2010-10-06 10:03:59 +0000
3+++ data/templates/ubuntu-pygame/help/tutorial.xml 2011-02-21 06:26:17 +0000
4@@ -529,8 +529,8 @@
5 <listitem>The default explosions are crayon drawings, they don't fit in much with the look of the game. Perhaps some new images for the explosion stages would be good.</listitem>
6 <listitem>There are no power ups! You could create power up classes the derive from BaseSprite to give the guy extra powers, like bigger bullets, more bullets, or extra lives.</listitem>
7 <listitem>More special enemies, for example you could use the HomingMissle class to create harder to kill enemies.</listitem>
8-<listitem>There is no high scores list. You could use desktopcouch to store high scores and use the code in view_tick to see how to present the scores.</listitem>
9+<listitem>The high score list is stored in desktopcouch, so it will sync with your other computers. But it will have to be adjusted to the size we set for the window, and perhaps you can also let the player choose the name that's stored.</listitem>
10 </itemizedlist>
11 </para>
12 </chapter>
13-</book>
14\ No newline at end of file
15+</book>
16
17=== modified file 'data/templates/ubuntu-pygame/project_root/bin/project_name'
18--- data/templates/ubuntu-pygame/project_root/bin/project_name 2010-12-04 17:25:06 +0000
19+++ data/templates/ubuntu-pygame/project_root/bin/project_name 2011-02-21 06:26:17 +0000
20@@ -17,6 +17,7 @@
21 from python_name.enemy import Enemy
22 from python_name.homingmissle import HomingMissle
23 from python_name.game import Game
24+from python_name import hiscores
25 import python_name.python_nameconfig
26
27 #set up translations
28@@ -50,11 +51,12 @@
29 g = Guy(bullets)
30 guys.add(g)
31
32-#set up the game over screen, center is by default
33-gov = BaseSprite(python_name.python_nameconfig.game_over_background)
34-gov.x = (sw - gov.rect.width)/2
35-gov.y = (sh - gov.rect.height)/2
36-gov.update()
37+def update_hiscore_screen():
38+ """Update the hiscore screen."""
39+ hiscores_size = python_name.python_nameconfig.hiscores_size
40+ hiscores.screen = hiscores.hiscores_screen(hiscores_size)
41+ screen_center = screen.get_rect().center
42+ hiscores.pos = hiscores.screen.get_rect(center=screen_center)
43
44 def next_level():
45 """next_level - go to the next game level
46@@ -94,6 +96,10 @@
47 #handle starting the game or quiting the game if the game
48 #is over or has not started
49 if game.lives < 1:
50+ if game.playing:
51+ game.playing = False
52+ hiscores.save_score(game.score, game.level)
53+ update_hiscore_screen()
54 for event in pygame.event.get():
55 if event.type == pygame.KEYDOWN:
56 if event.key == pygame.K_ESCAPE:
57@@ -185,8 +191,9 @@
58 screen.blit(scoretxt, scorepos)
59 screen.blit(leveltxt, levelpos)
60 screen.blit(livestxt, livespos)
61- if game.lives < 1:
62- screen.blit(gov.image,gov.rect)
63+ if not game.playing:
64+ screen.blit(hiscores.screen, hiscores.pos)
65+
66
67 #now show the new drawing
68 pygame.display.flip()
69@@ -223,6 +230,7 @@
70
71 """
72
73+ update_hiscore_screen()
74 while 1:
75 #set the clock to tick 15 times per second
76 clock.tick(15)
77
78=== modified file 'data/templates/ubuntu-pygame/project_root/python/game.py'
79--- data/templates/ubuntu-pygame/project_root/python/game.py 2010-12-04 17:25:06 +0000
80+++ data/templates/ubuntu-pygame/project_root/python/game.py 2011-02-21 06:26:17 +0000
81@@ -26,6 +26,7 @@
82 self.free_guy_at = 10
83 self.free_guy_sound = pygame.mixer.Sound(python_nameconfig.free_guy_sound)
84 self.paused = False
85+ self.playing = False
86
87 def add_free_guy(self):
88 """add_free_guy - increments the game's lives by 1 and plays a sound.
89@@ -52,6 +53,7 @@
90
91 def reset(self):
92 """reset - reset or start the game"""
93+ self.playing = True
94 self.level = 0
95 self.lives = 5
96 self.score = 0
97
98=== added file 'data/templates/ubuntu-pygame/project_root/python/hiscores.py'
99--- data/templates/ubuntu-pygame/project_root/python/hiscores.py 1970-01-01 00:00:00 +0000
100+++ data/templates/ubuntu-pygame/project_root/python/hiscores.py 2011-02-21 06:26:17 +0000
101@@ -0,0 +1,81 @@
102+import getpass
103+import operator
104+import time
105+import pwd
106+import pygame
107+
108+from datetime import datetime
109+from desktopcouch.records.server import CouchDatabase
110+from desktopcouch.records.record import Record
111+
112+DARK_GRAY = (68, 47, 47)
113+LIGHT_GRAY = (200, 180, 180)
114+WHITE = (255, 255, 255)
115+SCORE_RECORD_TYPE = 'https://wiki.ubuntu.com/GamesIntegration/HighScore'
116+
117+pygame.font.init()
118+font = pygame.font.Font(None, 24)
119+couchdb = CouchDatabase("python_name", create=True)
120+
121+def get_user_fullname():
122+ """Return the name of the current user."""
123+ username = getpass.getuser()
124+ fullname = pwd.getpwnam(username).pw_gecos.split(",")[0]
125+ return fullname if len(fullname) > 0 else username
126+
127+def add_dummy_scores():
128+ """Add some dummy scores to the database."""
129+ names = "Vince Noir|Naboo the Enigma|Bollo was Here|Bob Fossil|Howard Moon"
130+ level = 4
131+ for name in reversed(names.split("|")):
132+ save_score(level ** 2, level, name)
133+ level *= 2
134+
135+def get_hiscores(top=10):
136+ """Return the sorted list of scores."""
137+ all_scores = couchdb.get_all_records(record_type=SCORE_RECORD_TYPE)
138+ if len(all_scores) == 0:
139+ add_dummy_scores()
140+ all_scores = couchdb.get_all_records(record_type=SCORE_RECORD_TYPE)
141+ sort_key = operator.itemgetter("score", "timestamp")
142+ sorted_scores = sorted(all_scores, key=sort_key, reverse=True)
143+ return sorted_scores[:top]
144+
145+def save_score(score, level, player=get_user_fullname()):
146+ """Save the score in the couch database."""
147+ data = {
148+ "player": player,
149+ "level": level,
150+ "score": score,
151+ "timestamp": time.time(),
152+ }
153+ record = Record(data, record_type=SCORE_RECORD_TYPE)
154+ couchdb.put_record(record)
155+
156+def hiscores_screen(screen_size, message="Press Enter to Start"):
157+ """A pygame surface with the hiscore board and start message."""
158+ screen = pygame.Surface(screen_size)
159+ screen.fill(DARK_GRAY)
160+ screen_rect = screen.get_rect()
161+ centerx = screen_rect.centerx
162+ line_size = font.get_linesize()
163+
164+ y = line_size
165+ msg = font.render("Highest Scores", 1, WHITE)
166+ msg_top = screen_rect.bottom - line_size
167+ screen.blit(msg, msg.get_rect(centerx=centerx, top=y))
168+ y += line_size * 2
169+
170+ hiscores = get_hiscores(top=10)
171+ scoreboard_centerx = centerx * 0.83
172+ for score in hiscores:
173+ msg = font.render(str(score["score"]) + " - ", 1, LIGHT_GRAY)
174+ screen.blit(msg, msg.get_rect(right=scoreboard_centerx, top=y))
175+ msg = font.render(score["player"], 1, LIGHT_GRAY)
176+ screen.blit(msg, msg.get_rect(left=scoreboard_centerx, top=y))
177+ y += line_size
178+
179+ msg = font.render(message, 1, WHITE)
180+ msg_bottom = screen_rect.bottom - line_size
181+ screen.blit(msg, msg.get_rect(centerx=centerx, bottom=msg_bottom))
182+ return screen
183
184=== modified file 'data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py'
185--- data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py 2010-12-04 17:25:06 +0000
186+++ data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py 2011-02-21 06:26:17 +0000
187@@ -31,6 +31,8 @@
188 screen_width = 800
189 screen_height = 480
190
191+hiscores_size = (620, 300)
192+
193 #images
194 image_path = os.path.join(getdatapath(), "media/")
195
196
197=== modified file 'data/templates/ubuntu-pygame/test/filelist.sh'
198--- data/templates/ubuntu-pygame/test/filelist.sh 2010-12-04 17:25:06 +0000
199+++ data/templates/ubuntu-pygame/test/filelist.sh 2011-02-21 06:26:17 +0000
200@@ -51,5 +51,6 @@
201 # test_project/enemy.py
202 # test_project/game.py
203 # test_project/guy.py
204+# test_project/hiscores.py
205 # test_project/homingmissle.py
206 # test_project/test_projectconfig.py

Subscribers

People subscribed via source and target branches