示例#1
1
class View:
    
    def __init__(self, camera_bounds, event_system, world_bounds):
        pygame.init()
        self.display = pygame.display.set_mode(camera_bounds)
        pygame.display.set_caption("VV Pilot")

        icon = pygame.Surface((32, 32))
        icon.fill(WHITE)
        pygame.gfxdraw.polygon(icon, ((3, 29), (29, 29), (16, 3)), BLACK)
        pygame.gfxdraw.polygon(icon, ((4, 28), (28, 28), (16, 3)), BLACK)
        pygame.gfxdraw.aapolygon(icon, ((2, 30), (30, 30), (15, 2)), BLACK)
        pygame.display.set_icon(icon)
        # initialize the lists
        self.ship_list = []
        self.bullet_list = []
        self.wall_list = []
        self.debris = []
        # make the camera
        self.camera = Camera((camera_bounds[0]/2, camera_bounds[1]/2), camera_bounds)
        self.world_bounds = world_bounds
        # add the view to the event system
        event_system.onShipDeath+=self.onShipDeath
        event_system.onBulletDeath+=self.onBulletDeath
        
    def onShipDeath(self, ship):
        Display.death_animation(ship, self.debris, BLACK)
        
    def onBulletDeath(self, bullet, wall_rect):
        Display.bullet_death(bullet, wall_rect, self.debris, BLUE)
        
    def draw_ship(self, ship):
        if not ship.isDead():
            Display.draw_triangle(self.display, self.camera, BLACK, ship.location, ship.bounds, ship.direction, 2)
            if ship.moved:
                Display.draw_triangle_offset(self.display, self.camera, RED, (ship.location[0], ship.location[1]+ship.bounds[1]*3/2), (ship.bounds[0]/2, ship.bounds[1]/2), ship.direction-180, ship.location, 2)
            if ship.shield_obj:
                Display.draw_rect(self.display, self.camera, GREEN, ship.shield_obj, 2)
                
    def draw_bullet(self, bullet):
        Display.draw_circle(self.display, self.camera, BLUE, bullet.location, bullet.bounds[0])
        
    def draw_wall(self, wall):
        Display.draw_rect(self.display, self.camera, GREEN, wall, 2)
        
    def set_camera_loc(self, location):
        self.camera.set_camera_loc(location)
        self.camera.bound_camera(self.world_bounds)
        
    def draw_everything(self):
        self.display.fill(WHITE)
        for ship in self.ship_list:
            self.draw_ship(ship)
        for bullet in self.bullet_list:
            self.draw_bullet(bullet)
        i = 0
        while i < len(self.debris):
            debra = self.debris[i]
            loc = (debra[0][0] + debra[1][0], debra[0][1] + debra[1][1])
            dur = debra[2] - 1
            self.debris[i] = (loc, debra[1], dur, debra[3], debra[4], debra[5], debra[6], debra[7])
            debra = self.debris[i]
            Display.draw_circle(self.display, self.camera, debra[7], debra[0], int((debra[6]-debra[2])*debra[3]) + debra[4], debra[5])
            if self.debris[i][2]<=0:
                self.debris.remove(self.debris[i])
            else:
                i+=1
        for wall in self.wall_list:
            self.draw_wall(wall)
        pygame.display.update()
示例#2
1
     if keys[K_SPACE] and player_ship.delay<=0:
         bullet = Bullet((x + dimy*sinD, y-dimy*cosD), (BULLET_SIZE, BULLET_SIZE), player_ship.direction, (BULLET_SPEED*sinD, -BULLET_SPEED*cosD), BULLET_DURATION)
         #bullet = (x + dimy*sinD, y-dimy*cosD, player_ship.direction, BULLET_DURATION)
         bulletList.append(bullet)
         player_ship.delay = SHOOT_DELAY
         player_ship.move_from_force_in_direction(player_ship.acceleration, player_ship.direction+180)
 
 player_ship.update()
 
 for n in wall_list:
     if player_ship.rect.colliderect(n) and not player_ship.isDead():
         player_ship.kill(DEATH_TIME)
         Display.death_animation(player_ship, debris, BLACK)
 
 camera.set_camera_loc((x, y))
 camera.bound_camera(map_dimensions)
     
 if not player_ship.isDead():
     Display.draw_triangle(display, camera, BLACK, player_ship.location, player_ship.bounds, player_ship.direction, 2)
     if moved:
         Display.draw_triangle_offset(display, camera, RED, (player_ship.location[0], player_ship.location[1]+player_ship.bounds[1]*3/2), (player_ship.bounds[0]/2, player_ship.bounds[1]/2), player_ship.direction-180, player_ship.location, 2)
     
 i = 0
 while i < len(debris):
     debra = debris[i]
     loc = (debra[0][0] + debra[1][0], debra[0][1] + debra[1][1])
     dur = debra[2] - 1
     debris[i] = (loc, debra[1], dur, debra[3], debra[4], debra[5], debra[6], debra[7])
     debra = debris[i]
     Display.draw_circle(display, camera, debra[7], debra[0], int((debra[6]-debra[2])*debra[3]) + debra[4], debra[5])
     if debra[2]<=0:
示例#3
0
class View:
    def __init__(self, camera_bounds, event_system, world_bounds):
        pygame.init()
        self.display = pygame.display.set_mode(camera_bounds)
        pygame.display.set_caption("VV Pilot")

        icon = pygame.Surface((32, 32))
        icon.fill(WHITE)
        pygame.gfxdraw.polygon(icon, ((3, 29), (29, 29), (16, 3)), BLACK)
        pygame.gfxdraw.polygon(icon, ((4, 28), (28, 28), (16, 3)), BLACK)
        pygame.gfxdraw.aapolygon(icon, ((2, 30), (30, 30), (15, 2)), BLACK)
        pygame.display.set_icon(icon)
        # initialize the lists
        self.ship_list = []
        self.bullet_list = []
        self.wall_list = []
        self.debris = []
        # make the camera
        self.camera = Camera((camera_bounds[0] / 2, camera_bounds[1] / 2),
                             camera_bounds)
        self.world_bounds = world_bounds
        # add the view to the event system
        event_system.onShipDeath += self.onShipDeath
        event_system.onBulletDeath += self.onBulletDeath

    def onShipDeath(self, ship):
        Display.death_animation(ship, self.debris, BLACK)

    def onBulletDeath(self, bullet, wall_rect):
        Display.bullet_death(bullet, wall_rect, self.debris, BLUE)

    def draw_ship(self, ship):
        if not ship.isDead():
            Display.draw_triangle(self.display, self.camera, BLACK,
                                  ship.location, ship.bounds, ship.direction,
                                  2)
            if ship.moved:
                Display.draw_triangle_offset(
                    self.display, self.camera, RED,
                    (ship.location[0],
                     ship.location[1] + ship.bounds[1] * 3 / 2),
                    (ship.bounds[0] / 2, ship.bounds[1] / 2),
                    ship.direction - 180, ship.location, 2)
            if ship.shield_obj:
                Display.draw_rect(self.display, self.camera, GREEN,
                                  ship.shield_obj, 2)

    def draw_bullet(self, bullet):
        Display.draw_circle(self.display, self.camera, BLUE, bullet.location,
                            bullet.bounds[0])

    def draw_wall(self, wall):
        Display.draw_rect(self.display, self.camera, GREEN, wall, 2)

    def set_camera_loc(self, location):
        self.camera.set_camera_loc(location)
        self.camera.bound_camera(self.world_bounds)

    def draw_everything(self):
        self.display.fill(WHITE)
        for ship in self.ship_list:
            self.draw_ship(ship)
        for bullet in self.bullet_list:
            self.draw_bullet(bullet)
        i = 0
        while i < len(self.debris):
            debra = self.debris[i]
            loc = (debra[0][0] + debra[1][0], debra[0][1] + debra[1][1])
            dur = debra[2] - 1
            self.debris[i] = (loc, debra[1], dur, debra[3], debra[4], debra[5],
                              debra[6], debra[7])
            debra = self.debris[i]
            Display.draw_circle(
                self.display, self.camera, debra[7], debra[0],
                int((debra[6] - debra[2]) * debra[3]) + debra[4], debra[5])
            if self.debris[i][2] <= 0:
                self.debris.remove(self.debris[i])
            else:
                i += 1
        for wall in self.wall_list:
            self.draw_wall(wall)
        pygame.display.update()