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
15
from gettext import gettext as _
16
gettext.textdomain('project_name')
18
#initialize pygame and ther libraries
23
sw = python_name.python_nameconfig.screen_width
24
sh = python_name.python_nameconfig.screen_height
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)
31
#set up some objects and models
32
clock = pygame.time.Clock()
33
font = pygame.font.Font(None, 24)
37
#create the player's guy and some enemies
38
bullets = pygame.sprite.RenderUpdates()
39
guys = pygame.sprite.RenderUpdates()
40
enemies = pygame.sprite.RenderUpdates()
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)
52
"""next_level - go to the next game level
53
after completing the previous level
60
#TODO: set up enemies here
61
#You can add more enemies or use different enemies
62
#depening on the level
65
enemies.add(HomingMissle(g,python_name.python_nameconfig.homing_missle_image))
68
"""reset_level - reset the current level after the player's
69
guy has died, and is done exploding
73
#The guy is done exploding, so decrement the level and
76
g.explodestage = 0 #he's done exploding
77
guys.add(g) #make him live again
81
def controller_tick():
82
"""controller_tick - collect user input and update data
86
#handle starting the game or quiting the game if the game
87
#is over or has not started
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:
101
if event.key == pygame.K_F11:
102
pygame.display.toggle_fullscreen()
105
#if the guy has been killed and the explosion is over
107
if(not g.alive() and not g.exploding):
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:
115
#respond to user input if the game is not over
116
for event in pygame.event.get():
117
if event.type == pygame.QUIT:
120
#key down events, typically initiate actions
121
if event.type == pygame.KEYDOWN:
122
if event.key == pygame.K_ESCAPE:
126
if event.key == pygame.K_p:
127
game.paused = 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:
137
if event.key == pygame.K_j:
139
if event.key == pygame.K_SPACE:
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()
153
def update_sprites():
154
"""update_sprites - call update() for all sprites"""
160
"""view_tick - visually update the screen"""
163
screen.blit(background, [0,0])
165
#draw the player's guy and bullets
167
screen.blit(g.image, g.rect)
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)
181
screen.blit(scoretxt, scorepos)
182
screen.blit(leveltxt, levelpos)
183
screen.blit(livestxt, livespos)
185
screen.blit(hiscores.screen, hiscores.pos)
188
#now show the new drawing
189
pygame.display.flip()
191
def check_collisions():
192
"""check_collisions - check for sprite collisions and update
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
201
e = pygame.sprite.spritecollideany(g, enemies)
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:
211
for e in hits_dict[b]:
214
game.increase_score(e.points)
218
"""main - function to start the main loop. Program ends
223
update_hiscore_screen()
225
#set the clock to tick 15 times per second
228
#check for user input, quit if necessary
229
if controller_tick() == 0:
232
#let the game run for a frame