~quickly-committers/quickly/trunk

« back to all changes in this revision

Viewing changes to data/templates/ubuntu-pygame/project_root/python/__init__.py

  • Committer: Rick Spencer
  • Date: 2012-04-19 11:13:31 UTC
  • Revision ID: rick.spencer@canonical.com-20120419111331-b6thksokdaxys4s0
deleted pygame template, depends on desktopcouch, users should rather embed pygame into a ubuntu-application

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
import pygame, random
3
 
 
4
 
#import classes from the python_name library
5
 
from python_name.base_sprite import BaseSprite
6
 
from python_name.guy import Guy
7
 
from python_name.enemy import Enemy
8
 
from python_name.homingmissle import HomingMissle
9
 
from python_name.game import Game
10
 
from python_name import hiscores
11
 
import python_name.python_nameconfig
12
 
 
13
 
#set up translations
14
 
import gettext
15
 
from gettext import gettext as _
16
 
gettext.textdomain('project_name')
17
 
 
18
 
#initialize pygame and ther libraries
19
 
pygame.font.init()
20
 
pygame.mixer.init()
21
 
random.seed()
22
 
 
23
 
sw = python_name.python_nameconfig.screen_width
24
 
sh = python_name.python_nameconfig.screen_height
25
 
 
26
 
#setup the display
27
 
screen = pygame.display.set_mode((sw, sh))
28
 
background = pygame.image.load(python_name.python_nameconfig.background_image)
29
 
pygame.mouse.set_visible(False)
30
 
 
31
 
#set up some objects and models
32
 
clock = pygame.time.Clock() 
33
 
font = pygame.font.Font(None, 24)
34
 
game = Game()
35
 
game.level = 0
36
 
 
37
 
#create the player's guy and some enemies
38
 
bullets = pygame.sprite.RenderUpdates()
39
 
guys = pygame.sprite.RenderUpdates()
40
 
enemies = pygame.sprite.RenderUpdates()
41
 
g = Guy(bullets)
42
 
guys.add(g)
43
 
 
44
 
def update_hiscore_screen():
45
 
    """Update the hiscore screen."""
46
 
    hiscores_size = python_name.python_nameconfig.hiscores_size
47
 
    hiscores.screen = hiscores.hiscores_screen(hiscores_size)
48
 
    screen_center = screen.get_rect().center
49
 
    hiscores.pos = hiscores.screen.get_rect(center=screen_center)
50
 
 
51
 
def next_level():
52
 
    """next_level - go to the next game level
53
 
    after completing the previous level
54
 
 
55
 
    """
56
 
 
57
 
    game.level += 1
58
 
    enemies.empty()
59
 
 
60
 
    #TODO: set up enemies here
61
 
    #You can add more enemies or use different enemies
62
 
    #depening on the level
63
 
    for i in xrange(0,1):
64
 
        enemies.add(Enemy())
65
 
    enemies.add(HomingMissle(g,python_name.python_nameconfig.homing_missle_image))
66
 
 
67
 
def reset_level():
68
 
    """reset_level - reset the current level after the player's 
69
 
    guy has died, and is done exploding
70
 
 
71
 
    """
72
 
 
73
 
    #The guy is done exploding, so decrement the level and 
74
 
    #and reset the guy
75
 
    game.lives -= 1
76
 
    g.explodestage = 0 #he's done exploding
77
 
    guys.add(g) #make him live again
78
 
    g.visible = True
79
 
    g.init_position()
80
 
 
81
 
def controller_tick():
82
 
    """controller_tick - collect user input and update data
83
 
 
84
 
    """
85
 
    
86
 
    #handle starting the game or quiting the game if the game
87
 
    #is over or has not started
88
 
    if game.lives < 1:
89
 
        if game.playing:
90
 
            game.playing = False
91
 
            hiscores.save_score(game.score, game.level)
92
 
            update_hiscore_screen()
93
 
        for event in pygame.event.get():
94
 
            if event.type == pygame.KEYDOWN:
95
 
                if event.key == pygame.K_ESCAPE:
96
 
                    return 0
97
 
                if event.key == 13:
98
 
                    game.reset()
99
 
                    next_level()
100
 
                    return 1
101
 
                if event.key == pygame.K_F11:
102
 
                    pygame.display.toggle_fullscreen() 
103
 
                    return 1
104
 
 
105
 
    #if the guy has been killed and the explosion is over
106
 
    #reset the level
107
 
    if(not g.alive() and not g.exploding):
108
 
        reset_level()
109
 
 
110
 
    #if all the enemies are kill and there are more lives,
111
 
    #go to the next level
112
 
    if len(enemies) == 0 and not game.lives < 1:
113
 
        next_level()
114
 
 
115
 
    #respond to user input if the game is not over
116
 
    for event in pygame.event.get():
117
 
        if event.type == pygame.QUIT:
118
 
            return 0
119
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
120
 
        #key down events, typically initiate actions
121
 
        if event.type == pygame.KEYDOWN:
122
 
            if event.key == pygame.K_ESCAPE:
123
 
                return 0
124
 
   
125
 
            #pause or unpause
126
 
            if event.key == pygame.K_p:
127
 
                game.paused = not game.paused
128
 
   
129
 
            #control the guy
130
 
            if not game.paused: 
131
 
                if event.key == pygame.K_f:
132
 
                    g.start_rotation_right()
133
 
                if event.key == pygame.K_s:
134
 
                    g.start_rotation_left()
135
 
                if event.key == pygame.K_l:
136
 
                    g.accelerate()
137
 
                if event.key == pygame.K_j:
138
 
                    g.shoot()
139
 
                if event.key == pygame.K_SPACE:
140
 
                    g.hyperspace()
141
 
     
142
 
        #key up events, typically stop actions
143
 
        if event.type == pygame.KEYUP:
144
 
            if event.key == pygame.K_F11:
145
 
                pygame.display.toggle_fullscreen() 
146
 
            if event.key == pygame.K_f:
147
 
                g.stop_rotating_right()
148
 
            if event.key == pygame.K_s:
149
 
                g.stop_rotating_left()
150
 
            if event.key == pygame.K_l:
151
 
                g.stop_acceleration()    
152
 
 
153
 
def update_sprites():
154
 
    """update_sprites - call update() for all sprites"""
155
 
    guys.update()
156
 
    bullets.update()
157
 
    enemies.update() 
158
 
 
159
 
def view_tick():
160
 
    """view_tick - visually update the screen"""
161
 
 
162
 
    #draw the background
163
 
    screen.blit(background, [0,0])
164
 
    
165
 
    #draw the player's guy and bullets
166
 
    if g.visible:
167
 
        screen.blit(g.image, g.rect)
168
 
    bullets.draw(screen)
169
 
 
170
 
    #draw enemies
171
 
    enemies.draw(screen)
172
 
  
173
 
    #update the scoreboard
174
 
    scoretxt = font.render("score: " + str(game.score), 1, (100, 100, 100))
175
 
    scorepos = pygame.Rect(35,15,scoretxt.get_rect().height,scoretxt.get_rect().width)
176
 
    leveltxt = font.render("level: " + str(game.level), 1, (100, 100, 100))
177
 
    levelpos = pygame.Rect(35,35,leveltxt.get_rect().height,leveltxt.get_rect().width)
178
 
    livestxt = font.render("lives: " + str(game.lives), 1, (100, 100, 100))
179
 
    livespos = pygame.Rect(35,55,livestxt.get_rect().height,livestxt.get_rect().width)
180
 
 
181
 
    screen.blit(scoretxt, scorepos)
182
 
    screen.blit(leveltxt, levelpos)
183
 
    screen.blit(livestxt, livespos)
184
 
    if not game.playing:
185
 
        screen.blit(hiscores.screen, hiscores.pos)
186
 
 
187
 
 
188
 
    #now show the new drawing
189
 
    pygame.display.flip()
190
 
 
191
 
def check_collisions():
192
 
    """check_collisions - check for sprite collisions and update
193
 
    as necessary.
194
 
 
195
 
    """
196
 
 
197
 
    #if the player's guy is not alive, don't let it collide
198
 
    #this occurs if he is exploding
199
 
    #otherwise, if he collides with enemies, explode them both
200
 
    if g.alive():
201
 
        e = pygame.sprite.spritecollideany(g, enemies)
202
 
        if e != None:
203
 
            if e.alive():
204
 
                g.explode()
205
 
                e.explode()
206
 
  
207
 
    #check if any enemies got hit by any bullets
208
 
    hits_dict = pygame.sprite.groupcollide(bullets, enemies, False, False)
209
 
    if len(hits_dict) != 0:
210
 
        for b in hits_dict:
211
 
            for e in hits_dict[b]:
212
 
                if e.alive():
213
 
                    e.explode()
214
 
                    game.increase_score(e.points)
215
 
                    b.explode()
216
 
 
217
 
def main():
218
 
    """main - function to start the main loop. Program ends
219
 
    when main() returns.
220
 
 
221
 
    """
222
 
 
223
 
    update_hiscore_screen()
224
 
    while 1:
225
 
        #set the clock to tick 15 times per second
226
 
        clock.tick(15)
227
 
 
228
 
        #check for user input, quit if necessary
229
 
        if controller_tick() == 0:
230
 
            return
231
 
 
232
 
        #let the game run for a frame
233
 
        if not game.paused:
234
 
            update_sprites()
235
 
            view_tick()
236
 
            check_collisions()
237