forked from map0logo/pyspaceinvaders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyspaceinvaders_objects.py
152 lines (123 loc) · 5.21 KB
/
pyspaceinvaders_objects.py
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
# $LastChangedDate: 2009-08-19 15:06:10 -0500 (Wed, 19 Aug 2009) $
# Game object classes.
# Author: Jim Brooks http://www.jimbrooks.org
# Date: initial 2004/08, rewritten 2009/08
# License: GNU General Public License Version 2 (GPL2).
#===============================================================================
import random
import pygame
from pyspaceinvaders_exception import *
from pyspaceinvaders_conf import *
#-------------------------------------------------------------------------------
# Base class of drawable objects.
#-------------------------------------------------------------------------------
class Drawable(object):
""" Base class of drawable objects. """
def __init__( self ):
pass
def Draw( self, surface ):
pass
#-------------------------------------------------------------------------------
# Base class of game objects (player, aliens).
#-------------------------------------------------------------------------------
class Object(Drawable):
""" Base class of game objects (player, aliens). """
def __init__( self, game ):
Drawable.__init__( self )
self.game = game
self.valid = True
self.hit = 0
self.movement = ( 0, 0 )
#-------------------------------------------------------------------------------
# Player class.
#-------------------------------------------------------------------------------
class Player(Object):
def __init__( self, game ):
Object.__init__( self, game )
self.game = game
self.image = pygame.image.load( "img/player.png" ).convert()
self.image2 = pygame.image.load( "img/playerb.png" ).convert()
self.imageHit = pygame.image.load( "img/explosion.png" ).convert()
self.rect = self.image.get_rect()
self.imageFlip = False
self.step = 3 * self.game.stride
self.fire = False
self.fireLatency = 3
self.salvo = 5
self.livesReset = Conf.PLAYER_LIVES
self.lives = Conf.PLAYER_LIVES
# Reset() does the rest.
self.Reset( self.livesReset )
def Reset( self, lives ):
""" Let Game decide whether to reset lives or let lives decrease. """
self.lives = lives
self.valid = True
self.hit = 0 # counts down
self.rect.centerx = self.game.window.width // 2
self.rect.bottom = self.game.ground
def Hit( self ):
self.hit = 30
self.lives -= 1
if self.lives <= 0: # player out of lives?
self.game.GameOver()
def Draw( self, surface ):
if self.hit <= 0:
if self.imageFlip:
surface.blit( self.image, self.rect )
else:
surface.blit( self.image2, self.rect )
else:
surface.blit( self.imageHit, self.rect )
#-------------------------------------------------------------------------------
# Alien class.
#-------------------------------------------------------------------------------
class Alien(Object):
# Class constants:
POINTS = 50
COL_CNT = 10
ROW_CNT = 6
TOTAL_CNT = COL_CNT * ROW_CNT
# Class vars:
imageFlip = False
horzDir = -1
def __init__( self, game, fname, fname2 ):
""" Pass filenames of 2 images that animation will alternate. """
Object.__init__( self, game )
self.image = pygame.image.load( fname ).convert()
self.image2 = pygame.image.load( fname2 ).convert()
self.imageHit = pygame.image.load( "img/explosion.png" ).convert()
self.rect = self.image.get_rect()
def Hit( self ):
self.hit = 7
def Draw( self, surface ):
if self.hit <= 0:
if Alien.imageFlip:
surface.blit( self.image, self.rect )
else:
surface.blit( self.image2, self.rect )
else:
surface.blit( self.imageHit, self.rect )
#-------------------------------------------------------------------------------
# Player missile class.
#-------------------------------------------------------------------------------
class PlayerMissile(Object):
def __init__( self, game ):
Object.__init__( self, game )
self.image = pygame.image.load( "img/missile_player.png" ).convert()
self.rect = self.image.get_rect()
self.rect.centerx = self.game.player.rect.centerx
self.rect.centery = self.game.player.rect.centery - self.game.player.rect.height
def Draw( self, surface ):
surface.blit( self.image, self.rect )
#-------------------------------------------------------------------------------
# Alien missile class.
#-------------------------------------------------------------------------------
class AlienMissile(Object):
def __init__( self, game, alien ):
Object.__init__( self, game )
self.image = pygame.image.load( "img/missile_alien.png" ).convert()
self.rect = self.image.get_rect()
self.rect.centerx = alien.rect.centerx + random.randint( -8, 8 )
self.rect.centery = alien.rect.centery + alien.rect.height
def Draw( self, surface ):
surface.blit( self.image, self.rect )