spaceblaster: Use a fire interval rather than key repeating

Checking when keys are pressed or released is better than using the
key_repeat abilities of pygame.  We should use this, and then wait 50 ms
between each beam.
This commit is contained in:
Bryan Schumaker 2010-12-31 11:54:36 -05:00
parent ee59446eee
commit b73f73f400
4 changed files with 17 additions and 8 deletions

View File

@ -13,7 +13,6 @@ def init():
pygame.init()
print "Initializing engine"
screen.init()
pygame.key.set_repeat(50, 50)
def quit(event, ms):
screen.quit()

View File

@ -14,6 +14,7 @@ SPACE = pygame.K_SPACE
class ArcadeState(games.GameState):
def __init__(self, GAME):
self.ship = ship.CSE1670(GAME, (10, 10), (1, 1))
self.fire_interval = 0
self.beams = []
self.k_left = False
self.k_right = False
@ -40,6 +41,7 @@ class ArcadeState(games.GameState):
self.k_left = (self.k_left and not (key == LEFT))
self.k_up = (self.k_up and not (key == UP))
self.k_down = (self.k_down and not (key == DOWN))
self.k_space = (self.k_space and not (key == SPACE))
def logic(self, ms):
self.ship.move_x(self.k_right - self.k_left)
@ -51,5 +53,9 @@ class ArcadeState(games.GameState):
self.beams.remove(beam)
if self.k_space == True:
self.beams += self.ship.fire()
self.k_space = False
self.fire_interval += ms
if self.fire_interval >= 50:
self.beams += self.ship.fire()
self.fire_interval -= 50
else:
self.fire_interval = 0

View File

@ -6,10 +6,14 @@ load = image.load
GO = object.GameObject
class Beam(GO):
def __init__(self, GAME, type, pos):
GO.__init__(self, load(GAME, "beam-%s.png" % type), pos, (0, 1))
def __init__(self, GAME, type, pos, speed):
GO.__init__(self, load(GAME, "beam-%s.png" % type), pos, speed)
self.allow_offscreen = True
self.min_y -= self.h
def move(self):
self.move_y(-1)
self.move_y(1)
class CSEBeam(Beam):
def __init__(self, GAME, pos):
Beam.__init__(self, GAME, "blue", pos, (0, -1))

View File

@ -6,7 +6,7 @@ load = image.load
GO = object.GameObject
import beam
Beam = beam.Beam
CSEBeam = beam.CSEBeam
class ArcadeShip(GO):
def __init__(self, GAME, image, pos, speed):
@ -20,5 +20,5 @@ class CSE1670(ArcadeShip):
def fire(self):
beams = []
beams.append(Beam(self.game, "blue", (self.x, self.y)))
beams.append(CSEBeam(self.game, (self.x, self.y)))
return beams