#player ship class import pygame from pygame.locals import * import game, gfx shipimages = [] # states ship_canshoot = 0 ship_canbomb = 1 def load_game_resources(): #load ship graphics global shipimages for loop in range(0,2): shipimages.append(gfx.load('ship%d.gif'%loop)) class Ship: def __init__(self): self.images = shipimages self.rect = self.images[ship_canbomb].get_rect() self.lastrect = self.rect self.xthrust = game.ship_xthrust self.ythrust = game.ship_ythrust self.thrust_timer = 0 # for laser blasts self.blastdelay = game.ship_blast_delay self.blastspeed = game.ship_blast_speed self.blastoffset = [56,11] self.bombdelay = game.ship_bomb_delay self.lastbomb = 0 # where we should place bombs relative to topleft corner [x,y] self.bomboffset = [8,7] self.flame = None self.change_state(ship_canshoot) def start(self, pos): """initialize variables at the start of each life""" self.x, self.y = pos self.rect.topleft = pos self.dx = 0 self.dy = 0 self.active = 0 self.dead = 0 self.move = [0, 0] self.lastblast = pygame.time.get_ticks() self.blasting = 0 self.bombing = 0 self.fuel_time = 0 self.exploding = 0 self.change_state(ship_canshoot) def erase(self, background): background(self.lastrect) if self.dead: gfx.dirty(self.lastrect) def draw(self, gfx): r = gfx.surface.blit(self.image, self.rect) gfx.dirty2(r, self.lastrect) self.lastrect = r def tick(self, speedadjust = 1.0): if self.active == 0: # we're under program control return if game.fuel > 0: # thrust timer if self.thrust_timer <= 0: self.move = [0,0] else: self.thrust_timer -= 1 # player controls xaccel = self.move[0] * self.xthrust yaccel = self.move[1] * self.ythrust self.dx += xaccel self.dy += yaccel # physics self.dx *= game.ship_friction self.dy *= game.ship_friction if self.dy > game.ship_maxvel: self.dy = game.ship_maxvel elif self.dy < game.ship_minvel: self.dy = game.ship_minvel else: # out of fuel self.dx = ((self.dx + 1) * game.ship_friction) - 1 self.dy += game.gravity if self.dx > game.ship_maxvel: self.dx = game.ship_maxvel elif self.dx < game.ship_minvel: self.dx = game.ship_minvel self.x += self.dx self.y += self.dy self.check_pos() if self.y > game.arena.bottom and game.fuel > 0: self.y = game.arena.bottom self.dy = 0 self.rect.topleft = [self.x,self.y] self.change_state() now = pygame.time.get_ticks() if now - self.fuel_time > game.ship_fuel_tick_time: self.use_fuel(game.ship_fuel_tick_cost) self.fuel_time = now def check_pos(self): if self.y < game.arena.top: self.y = game.arena.top self.dy = 0 elif self.y > game.groundarena.bottom - self.rect.height: self.y = game.groundarena.bottom - self.rect.height self.dy = 0 if self.x < game.arena.left: self.x = game.arena.left self.dx = 0 elif self.x > game.arena.right - self.rect.width: self.x = game.arena.right - self.rect.width self.dx = 0 def cmd_left(self): self.move = [-1, 0] self.thrust_timer = game.ship_thrust_time def cmd_up(self): self.move = [0, -1] self.thrust_timer = game.ship_thrust_time def cmd_right(self): self.move = [1, 0] self.thrust_timer = game.ship_thrust_time def cmd_down(self): self.move = [0, 1] self.thrust_timer = game.ship_thrust_time def cmd_turbo(self, onoff): self.turbo = onoff def cmd_fire(self): now = pygame.time.get_ticks() if self.shipstate == ship_canshoot: if now - self.lastblast > self.blastdelay and game.fuel > game.ship_blast_cost: self.blasting = 1 self.lastblast = now self.use_fuel(game.ship_blast_cost) elif self.shipstate == ship_canbomb: if now - self.lastbomb > self.bombdelay and game.bombs > 0: self.bombing = 1 self.lastbomb = now # check for cheat mode if game.infinite_bombs == 0: game.bombs -= 1 def change_state(self,state=None): if state == None: if self.rect.top > game.ship_bomb_height: self.shipstate = ship_canbomb else: self.shipstate = ship_canshoot else: self.shipstate = state self.image = self.images[self.shipstate] def use_fuel(self,fuel): # check for cheat mode if game.infinite_fuel == 0: game.fuel -= fuel if game.fuel <= 0: game.fuel = 0 if self.flame != None: self.flame.dead = 1 def die(self): self.dead = 1 if self.flame != None: self.flame.dead = 1 self.exploding = 1 self.bombing = 0 self.blasting = 0 # off the display (mainly to avoid rockets while we're dead) self.x = -1000000 self.y = -1000000