1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# SpaceWar
# Copyright (C) 2007
# Copyright (C) 2013, Alan Aguiar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Alan Aguiar <alanjas@gmail.com>
import os
import gtk
import itertools
import pygame
import random
from gettext import gettext as _
POINTS_ENEMY = 1000
POINTS_SHOT = 100
class SpaceWar():
"""Top-level application class."""
def __init__(self, parent=None):
self.parent = parent
self.count = 0
self.level = 1
self.points = 0
self.old_points = 0
self.running = True
self.screen = None
self.load_stats()
def load_stats(self):
if self.parent is not None:
try:
folder = self.parent.get_activity_root()
path = os.path.join(folder, 'data', 'stats.dat')
if os.path.exists(path):
f = open(path, 'r')
val = f.readline()
val = val.strip('\n')
if not(val == ''):
level = int(val)
self.level = level
val = f.readline()
val = val.strip('\n')
if not(val == ''):
score = int(val)
self.points = score
f.close()
except Exception, err:
print 'Cannot load score', err
def save_stats(self):
if self.parent is not None:
try:
folder = self.parent.get_activity_root()
path = os.path.join(folder, 'data', 'stats.dat')
f = open(path, 'w')
f.write(str(self.level) + '\n')
f.write(str(self.points) + '\n')
f.close()
except Exception, err:
print 'Error saving score', err
def load_all(self):
# Dun dun duuuuuuuun
pygame.display.init()
# Initialize the screen surface
self.screen = pygame.display.get_surface()
if not(self.screen):
info = pygame.display.Info()
size = (info.current_w, info.current_h)
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption(_('SpaceWar'))
self.rect = self.screen.get_rect()
# Core objects
self.clock = pygame.time.Clock()
# images
self.ship_img = pygame.image.load('img/Space/ship1.png').convert_alpha()
self.shot_img = pygame.image.load('img/Space/bullet4.png').convert_alpha()
self.enemy_img = pygame.image.load('img/OldSchool/skully.png').convert_alpha()
pygame.mixer.init()
self.shot_sound = pygame.mixer.Sound('sounds/shot.wav')
# game labels
pygame.font.init()
self._font = pygame.font.Font(None, 40)
self.level_msg = self._font.render(_('Level: %s') % self.level, 1, (255, 255, 255))
self.points_msg = self._font.render(_('Score: %s') % self.points, 1, (255, 255, 255))
# Game objects
self.ship = Ship(self)
self.ship.rect.midbottom = self.rect.midbottom
self.shots = pygame.sprite.Group()
self.enemies = pygame.sprite.Group()
# Add enemies
self.add_enemies(self.level)
def add_enemies(self, level):
for l in range(level):
enemy = Enemy(self)
x = random.randint(0, self.rect[2])
y = random.randint(50, self.rect[3] - 100)
enemy.rect.center = x, y
enemy.direction = random.choice((-1 , 1))
self.enemies.add(enemy)
def do_score_msg(self):
if self.points < 0:
p = str(abs(self.points))
c = (8 - len(p)) * '0'
msg = '-' + c + p
else:
p = str(self.points)
c = (8 - len(p)) * '0'
msg = c + p
self.points_msg = self._font.render(_('Score: %s') % msg, 1, (255, 255, 255))
def run(self):
self.load_all()
while self.running:
# Timing phase
delta = self.clock.tick(30)
while gtk.events_pending():
gtk.main_iteration()
# Event phase
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
self.running = False
self.save_stats()
elif evt.type == pygame.KEYDOWN:
if evt.key == pygame.K_ESCAPE:
self.running = False
if evt.key == pygame.K_LEFT:
self.ship.direction = -1
elif evt.key == pygame.K_RIGHT:
self.ship.direction = 1
elif evt.key == pygame.K_SPACE:
self.points = self.points - POINTS_SHOT
new_shot = Shot(self)
new_shot.rect.midbottom = self.ship.rect.midtop
self.shots.add(new_shot)
elif evt.type == pygame.KEYUP:
if evt.key == pygame.K_LEFT and self.ship.direction == -1:
self.ship.direction = 0
elif evt.key == pygame.K_RIGHT and self.ship.direction == 1:
self.ship.direction = 0
# Update phase
self.ship.update(delta)
self.shots.update(delta)
self.enemies.update(delta)
# Look for shot-enemy collisions
d = pygame.sprite.groupcollide(self.enemies, self.shots, True, True)
# add points for each enemy killed
for ship in d:
self.points = self.points + POINTS_ENEMY
# delay between killed all enemies and appears more
if len(self.enemies) == 0:
self.count = self.count + 1
if self.count > 10:
self.count = 0
self.level = self.level + 1
self.level_msg = self._font.render(_('Level: %s') % self.level, 1, (255, 255, 255))
self.add_enemies(self.level)
# update score msg
if not(self.points == self.old_points):
self.do_score_msg()
self.old_points = self.points
# Display phase
self.screen.fill((0,0,0))
self.screen.blit(self.points_msg, (self.rect[2] - 250, 10))
self.screen.blit(self.level_msg, (10, 10))
for spr in self.enemies:
self.screen.blit(spr.image, spr.rect, spr.source_rect)
self.shots.draw(self.screen)
self.screen.blit(self.ship.image, self.ship.rect)
pygame.display.flip()
class Ship(pygame.sprite.Sprite):
"""The player-controlled ship."""
def __init__(self, parent):
super(Ship, self).__init__()
self.parent = parent
self.image = parent.ship_img
self.rect = self.image.get_rect()
self.direction = 0
self.speed = 0.1
def update(self, delta):
self.rect.move_ip(delta*self.direction*self.speed, 0)
self.rect.clamp_ip(self.parent.rect)
class Shot(pygame.sprite.Sprite):
"""A single shot fired by the player."""
def __init__(self, parent):
super(Shot, self).__init__()
self.parent = parent
self.image = parent.shot_img
self.rect = self.image.get_rect()
self.speed = 0.2
# Load and play the shot sound
self.sound = parent.shot_sound
self.sound.play()
def update(self, delta):
self.rect.move_ip(0, -1*delta*self.speed)
if not self.rect.colliderect(self.parent.rect):
self.kill()
class Enemy(pygame.sprite.Sprite):
"""An enemy ship."""
def __init__(self, parent):
super(Enemy, self).__init__()
self.parent = parent
self.image = parent.enemy_img
self.rect = self.image.get_rect()
self.direction = 1
self.speed = 0.05
# Setup the animation frames
self.n_frames = 2
self.rect.width /= self.n_frames # Make self.rect to be the size of one frame
self.source_rects = itertools.cycle([self.rect.move(x*self.rect.width, 0) for x in xrange(self.n_frames)])
self.source_rect = self.source_rects.next() # Load the position of the first frame
# Setup the animation timing
self.time_per_frame = 200 # ms per frame in the animation
self.time_left = self.time_per_frame
def update(self, delta):
self.time_left -= delta
if self.time_left <= 0:
self.time_left += self.time_per_frame
self.source_rect = self.source_rects.next()
self.rect.move_ip(delta*self.direction*self.speed, 0)
if self.direction == 1 and self.rect.right >= self.parent.rect.right:
self.direction *= -1
self.rect.right = self.parent.rect.right
elif self.direction == -1 and self.rect.left <= self.parent.rect.left:
self.direction *= -1
self.rect.left = self.parent.rect.left
def main():
s = SpaceWar()
s.run()
if __name__ == '__main__':
main()
|