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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import random
import pygame
from pygame.locals import *
DIRECTORIOBASE = os.path.dirname(__file__)
IMAGEN_NAVE = os.path.join(DIRECTORIOBASE,
'Imagenes', 'Naves', 'Nave.png')
IMAGEN_NAVE_ENEMIGA = os.path.join(DIRECTORIOBASE,
'Imagenes', 'Naves', 'enemy02.png')
IMAGEN_BALA = os.path.join(DIRECTORIOBASE,
'Imagenes', 'Bala.png')
class Nave(pygame.sprite.Sprite):
"""Nave."""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(IMAGEN_NAVE)
self.rect = self.image.get_rect()
w = pygame.display.Info().current_w
h = pygame.display.Info().current_h
self.rect.center = (w/2, h/2)
def update(self):
"""Actualiza la nave, segĂșn eventos del teclado."""
tecla = pygame.key.get_pressed()
if tecla[pygame.K_UP]:
self.rect.y -= 5
if tecla[pygame.K_DOWN]:
self.rect.y += 5
if tecla[pygame.K_RIGHT]:
self.rect.x += 5
if tecla[pygame.K_LEFT]:
self.rect.x -= 5
if tecla[pygame.K_SPACE]:
print "Disparar"
class Enemigo(Nave):
"""Nave."""
def __init__(self):
Nave.__init__(self)
self.image = pygame.image.load(IMAGEN_NAVE_ENEMIGA)
self.rect = self.image.get_rect()
random.seed()
self.rect.center = (random.randrange(0,
pygame.display.Info().current_w), 0)
def update(self):
w = pygame.display.Info().current_w
random.seed()
mov = random.choice([-5, 0, 5])
x = self.rect.x + mov
if x < w and x > 0:
self.rect.x += mov
class Bala(pygame.sprite.Sprite):
def __init__(self, propietario):
pygame.sprite.Sprite.__init__(self)
self.propietario = propietario
self.image = pygame.image.load(IMAGEN_BALA)
self.rect = self.image.get_rect()
xnave = self.propietario.rect.x
ynave = self.propietario.rect.y
wnave = self.propietario.rect.w
hnave = self.propietario.rect.h
self.rect.x = xnave + wnave / 2 - self.rect.w / 2
self.rect.y = ynave - self.rect.h/2
def update(self):
if self.rect.y <= 0:
self.kill()
else:
self.rect.y -= 10
|