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
=== modified file 'data/templates/ubuntu-pygame/help/tutorial.xml'
--- data/templates/ubuntu-pygame/help/tutorial.xml 2010-10-06 10:03:59 +0000
+++ data/templates/ubuntu-pygame/help/tutorial.xml 2011-02-21 06:26:17 +0000
@@ -529,8 +529,8 @@
529<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>529<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>
530<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>530<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>
531<listitem>More special enemies, for example you could use the HomingMissle class to create harder to kill enemies.</listitem>531<listitem>More special enemies, for example you could use the HomingMissle class to create harder to kill enemies.</listitem>
532<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>532<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>
533</itemizedlist>533</itemizedlist>
534</para>534</para>
535</chapter>535</chapter>
536</book>
537\ No newline at end of file536\ No newline at end of file
537</book>
538538
=== modified file 'data/templates/ubuntu-pygame/project_root/bin/project_name'
--- data/templates/ubuntu-pygame/project_root/bin/project_name 2010-12-04 17:25:06 +0000
+++ data/templates/ubuntu-pygame/project_root/bin/project_name 2011-02-21 06:26:17 +0000
@@ -17,6 +17,7 @@
17from python_name.enemy import Enemy17from python_name.enemy import Enemy
18from python_name.homingmissle import HomingMissle18from python_name.homingmissle import HomingMissle
19from python_name.game import Game19from python_name.game import Game
20from python_name import hiscores
20import python_name.python_nameconfig21import python_name.python_nameconfig
2122
22#set up translations23#set up translations
@@ -50,11 +51,12 @@
50g = Guy(bullets)51g = Guy(bullets)
51guys.add(g)52guys.add(g)
5253
53#set up the game over screen, center is by default54def update_hiscore_screen():
54gov = BaseSprite(python_name.python_nameconfig.game_over_background)55 """Update the hiscore screen."""
55gov.x = (sw - gov.rect.width)/256 hiscores_size = python_name.python_nameconfig.hiscores_size
56gov.y = (sh - gov.rect.height)/257 hiscores.screen = hiscores.hiscores_screen(hiscores_size)
57gov.update()58 screen_center = screen.get_rect().center
59 hiscores.pos = hiscores.screen.get_rect(center=screen_center)
5860
59def next_level():61def next_level():
60 """next_level - go to the next game level62 """next_level - go to the next game level
@@ -94,6 +96,10 @@
94 #handle starting the game or quiting the game if the game96 #handle starting the game or quiting the game if the game
95 #is over or has not started97 #is over or has not started
96 if game.lives < 1:98 if game.lives < 1:
99 if game.playing:
100 game.playing = False
101 hiscores.save_score(game.score, game.level)
102 update_hiscore_screen()
97 for event in pygame.event.get():103 for event in pygame.event.get():
98 if event.type == pygame.KEYDOWN:104 if event.type == pygame.KEYDOWN:
99 if event.key == pygame.K_ESCAPE:105 if event.key == pygame.K_ESCAPE:
@@ -185,8 +191,9 @@
185 screen.blit(scoretxt, scorepos)191 screen.blit(scoretxt, scorepos)
186 screen.blit(leveltxt, levelpos)192 screen.blit(leveltxt, levelpos)
187 screen.blit(livestxt, livespos)193 screen.blit(livestxt, livespos)
188 if game.lives < 1:194 if not game.playing:
189 screen.blit(gov.image,gov.rect)195 screen.blit(hiscores.screen, hiscores.pos)
196
190197
191 #now show the new drawing198 #now show the new drawing
192 pygame.display.flip()199 pygame.display.flip()
@@ -223,6 +230,7 @@
223230
224 """231 """
225232
233 update_hiscore_screen()
226 while 1:234 while 1:
227 #set the clock to tick 15 times per second235 #set the clock to tick 15 times per second
228 clock.tick(15)236 clock.tick(15)
229237
=== modified file 'data/templates/ubuntu-pygame/project_root/python/game.py'
--- data/templates/ubuntu-pygame/project_root/python/game.py 2010-12-04 17:25:06 +0000
+++ data/templates/ubuntu-pygame/project_root/python/game.py 2011-02-21 06:26:17 +0000
@@ -26,6 +26,7 @@
26 self.free_guy_at = 1026 self.free_guy_at = 10
27 self.free_guy_sound = pygame.mixer.Sound(python_nameconfig.free_guy_sound)27 self.free_guy_sound = pygame.mixer.Sound(python_nameconfig.free_guy_sound)
28 self.paused = False28 self.paused = False
29 self.playing = False
2930
30 def add_free_guy(self):31 def add_free_guy(self):
31 """add_free_guy - increments the game's lives by 1 and plays a sound.32 """add_free_guy - increments the game's lives by 1 and plays a sound.
@@ -52,6 +53,7 @@
5253
53 def reset(self):54 def reset(self):
54 """reset - reset or start the game"""55 """reset - reset or start the game"""
56 self.playing = True
55 self.level = 057 self.level = 0
56 self.lives = 558 self.lives = 5
57 self.score = 059 self.score = 0
5860
=== added file 'data/templates/ubuntu-pygame/project_root/python/hiscores.py'
--- data/templates/ubuntu-pygame/project_root/python/hiscores.py 1970-01-01 00:00:00 +0000
+++ data/templates/ubuntu-pygame/project_root/python/hiscores.py 2011-02-21 06:26:17 +0000
@@ -0,0 +1,81 @@
1import getpass
2import operator
3import time
4import pwd
5import pygame
6
7from datetime import datetime
8from desktopcouch.records.server import CouchDatabase
9from desktopcouch.records.record import Record
10
11DARK_GRAY = (68, 47, 47)
12LIGHT_GRAY = (200, 180, 180)
13WHITE = (255, 255, 255)
14SCORE_RECORD_TYPE = 'https://wiki.ubuntu.com/GamesIntegration/HighScore'
15
16pygame.font.init()
17font = pygame.font.Font(None, 24)
18couchdb = CouchDatabase("python_name", create=True)
19
20def get_user_fullname():
21 """Return the name of the current user."""
22 username = getpass.getuser()
23 fullname = pwd.getpwnam(username).pw_gecos.split(",")[0]
24 return fullname if len(fullname) > 0 else username
25
26def add_dummy_scores():
27 """Add some dummy scores to the database."""
28 names = "Vince Noir|Naboo the Enigma|Bollo was Here|Bob Fossil|Howard Moon"
29 level = 4
30 for name in reversed(names.split("|")):
31 save_score(level ** 2, level, name)
32 level *= 2
33
34def get_hiscores(top=10):
35 """Return the sorted list of scores."""
36 all_scores = couchdb.get_all_records(record_type=SCORE_RECORD_TYPE)
37 if len(all_scores) == 0:
38 add_dummy_scores()
39 all_scores = couchdb.get_all_records(record_type=SCORE_RECORD_TYPE)
40 sort_key = operator.itemgetter("score", "timestamp")
41 sorted_scores = sorted(all_scores, key=sort_key, reverse=True)
42 return sorted_scores[:top]
43
44def save_score(score, level, player=get_user_fullname()):
45 """Save the score in the couch database."""
46 data = {
47 "player": player,
48 "level": level,
49 "score": score,
50 "timestamp": time.time(),
51 }
52 record = Record(data, record_type=SCORE_RECORD_TYPE)
53 couchdb.put_record(record)
54
55def hiscores_screen(screen_size, message="Press Enter to Start"):
56 """A pygame surface with the hiscore board and start message."""
57 screen = pygame.Surface(screen_size)
58 screen.fill(DARK_GRAY)
59 screen_rect = screen.get_rect()
60 centerx = screen_rect.centerx
61 line_size = font.get_linesize()
62
63 y = line_size
64 msg = font.render("Highest Scores", 1, WHITE)
65 msg_top = screen_rect.bottom - line_size
66 screen.blit(msg, msg.get_rect(centerx=centerx, top=y))
67 y += line_size * 2
68
69 hiscores = get_hiscores(top=10)
70 scoreboard_centerx = centerx * 0.83
71 for score in hiscores:
72 msg = font.render(str(score["score"]) + " - ", 1, LIGHT_GRAY)
73 screen.blit(msg, msg.get_rect(right=scoreboard_centerx, top=y))
74 msg = font.render(score["player"], 1, LIGHT_GRAY)
75 screen.blit(msg, msg.get_rect(left=scoreboard_centerx, top=y))
76 y += line_size
77
78 msg = font.render(message, 1, WHITE)
79 msg_bottom = screen_rect.bottom - line_size
80 screen.blit(msg, msg.get_rect(centerx=centerx, bottom=msg_bottom))
81 return screen
082
=== modified file 'data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py'
--- data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py 2010-12-04 17:25:06 +0000
+++ data/templates/ubuntu-pygame/project_root/python/python_nameconfig.py 2011-02-21 06:26:17 +0000
@@ -31,6 +31,8 @@
31screen_width = 80031screen_width = 800
32screen_height = 48032screen_height = 480
3333
34hiscores_size = (620, 300)
35
34#images36#images
35image_path = os.path.join(getdatapath(), "media/")37image_path = os.path.join(getdatapath(), "media/")
3638
3739
=== modified file 'data/templates/ubuntu-pygame/test/filelist.sh'
--- data/templates/ubuntu-pygame/test/filelist.sh 2010-12-04 17:25:06 +0000
+++ data/templates/ubuntu-pygame/test/filelist.sh 2011-02-21 06:26:17 +0000
@@ -51,5 +51,6 @@
51# test_project/enemy.py51# test_project/enemy.py
52# test_project/game.py52# test_project/game.py
53# test_project/guy.py53# test_project/guy.py
54# test_project/hiscores.py
54# test_project/homingmissle.py55# test_project/homingmissle.py
55# test_project/test_projectconfig.py56# test_project/test_projectconfig.py

Subscribers

People subscribed via source and target branches