コード例 #1
0
ファイル: pirate.py プロジェクト: Durgal/StarCrusader
class Pirate(pygame.sprite.Sprite):
    def __init__(self, x, y):

        super().__init__()
        self.type = "enemy"
        self.sprite = Sprite("Sprites/Pirate_Sheet.png", 44, 44)
        self.initial_x = x
        self.initial_y = y
        self.angle = 0
        self.direction = "R"
        self.speed = .0005

        self.image = self.sprite.get_image(0, 0)
        self.center_x = x - self.image.get_size()[0] / 2
        self.center_y = y - self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.rect.x = self.center_x
        self.rect.y = self.center_y

    def get_pos(self):
        return (self.rect.x, self.rect.y)

    def animate(self, time, direction):
        self.image = self.sprite.get_image(0, 0)
        self.sprite.animate(time)
        self.direction = direction

        self.x = self.rect.x
        self.y = self.rect.y

    def change_direction(self):
        if self.direction == "R":
            self.direction = "L"
            self.speed = -.0005
        else:
            self.direction = "R"
            self.speed = .0005

        self.sprite.flip_image()

    def collision_check(self, laser_list, entity_list):
        for laser in laser_list:
            if self.sprite.get_collision(self.rect.x, self.rect.y,
                                         self.sprite.width, self.sprite.height,
                                         laser.rect.x, laser.rect.y,
                                         laser.sprite.width,
                                         laser.sprite.height):
                laser.kill()
                self.die(entity_list)

    def die(self, entity_list):
        self.kill()
        item_list = [
            Fuel(self.x, self.y),
            Health(self.x, self.y),
            Energy(self.x, self.y),
            Treasure(self.x, self.y)
        ]
        entity_list.add(random.choice(item_list))
コード例 #2
0
ファイル: laser.py プロジェクト: Durgal/StarCrusader
class Laser(pygame.sprite.Sprite):
    def __init__(self, x, y):

        super().__init__()
        self.type = "laser"
        self.sprite = Sprite("Sprites/Hero_Laser.png", 4, 44)
        self.initial_x = x
        self.initial_y = y
        self.speed = 0
        self.angle = 0

        self.image = self.sprite.get_image(0, 0)
        self.center_x = x - self.image.get_size()[0] / 2
        self.center_y = y - self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.rect.x = self.center_x
        self.rect.y = self.center_y

    def get_pos(self):
        return (self.rect.x, self.rect.y)

    def set_direction(self, direction):
        if direction == "R":
            self.speed = .005
        else:
            self.speed = -.005
コード例 #3
0
class Energy(Item):
    def __init__(self, x, y):

        super().__init__(x, y)
        self.sub_type = "energy"
        self.sprite = Sprite("Sprites/Item_Energy.png", 36, 36)
        self.image = self.sprite.get_image(0, 0)
コード例 #4
0
class Treasure(Item):
    def __init__(self, x, y):

        super().__init__(x, y)
        self.sub_type = "treasure"
        self.sprite = Sprite("Sprites/Item_Treasure.png", 36, 36)
        self.image = self.sprite.get_image(0, 0)
コード例 #5
0
class Health(Item):
    def __init__(self, x, y):

        super().__init__(x, y)
        self.sub_type = "health"
        self.sprite = Sprite("Sprites/Item_Health.png", 36, 36)
        self.image = self.sprite.get_image(0, 0)
コード例 #6
0
class Fuel(Item):
    def __init__(self, x, y):

        super().__init__(x, y)
        self.sub_type = "fuel"
        self.sprite = Sprite("Sprites/Item_Fuel.png", 36, 36)
        self.image = self.sprite.get_image(0, 0)
コード例 #7
0
class Ship_Landed(pygame.sprite.Sprite):

    def __init__(self, x, y):

        super().__init__()
        self.type = "ship"
        self.sprite = Sprite("Sprites/Spaceship_Planet.png", 270, 270)
        self.initial_x = x
        self.initial_y = y
        self.speed = 0
        self.angle = 0

        self.image = self.sprite.get_image(0, 0)
        self.center_x = x - self.image.get_size()[0] / 2
        self.center_y = y - self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.center = self.rect.center
        self.rect.x = self.center_x
        self.rect.y = self.center_y

        self.orig_center = self.rect.center
        self.orig_img = self.image
        self.rotation = 0

    def get_pos(self):
        return(self.rect.x,self.rect.y)

    def rotate(self, angle):
        self.rotation = angle
        self.image = pygame.transform.rotate(self.orig_img, self.rotation)
        self.rect = self.image.get_rect(center=self.rect.center)
コード例 #8
0
class Ground(pygame.sprite.Sprite):

    def __init__(self, x, y):

        super().__init__()
        self.type = "ground"
        self.sprite = Sprite("Sprites/Ground.png", 44, 44)
        self.initial_x = x
        self.initial_y = y
        self.angle = 0

        self.image = self.sprite.get_image(0, 0)
        self.center_x = x - self.image.get_size()[0] / 2
        self.center_y = y - self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.rect.x = self.center_x
        self.rect.y = self.center_y

    def get_pos(self):
        return(self.rect.x,self.rect.y)
コード例 #9
0
class Item(pygame.sprite.Sprite):
    def __init__(self, x, y):

        super().__init__()
        self.type = "item"
        self.sprite = Sprite("Sprites/Item_Parent.png", 36, 36)
        self.initial_x = x
        self.initial_y = y
        self.speed = 0
        self.angle = 0

        self.image = self.sprite.get_image(0, 0)
        self.center_x = x + self.image.get_size()[0] / 2
        self.center_y = y + self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.rect.x = self.center_x
        self.rect.y = self.center_y

    def get_pos(self):
        return (self.rect.x, self.rect.y)
コード例 #10
0
class Hero(pygame.sprite.Sprite):

    def __init__(self):

        super().__init__()
        self.sprite = Sprite("Sprites/Hero_Sheet.png", 44, 44)
        self.sprite_stopped = Sprite("Sprites/Hero.png", 44, 44)
        self.sprite_shoot_l = Sprite("Sprites/Hero_Shoot_L.png", 44, 44)
        self.sprite_shoot_r = Sprite("Sprites/Hero_Shoot_R.png", 44, 44)
        self.level = "none"
        self.angle = 0
        self.velocity = 0
        self.gravity = -.25
        self.collision_entity = None
        self.collision = False
        self.on_ground = False
        self.falling = False
        self.laser_timer = 0
        self.laser_cooldown = 500
        self.move_speed = 0
        self.direction = "R"

        self.fuel = 100
        self.health = 100
        self.energy = 100
        self.treasure = 0

        self.image = self.sprite.get_image(0, 0)
        self.center_x = STARTING_POS_X - self.image.get_size()[0] / 2
        self.center_y = STARTING_POS_Y - self.image.get_size()[1] / 2
        self.rect = self.image.get_rect()
        self.rect.x = self.center_x
        self.rect.y = self.center_y

        self.snd_jump = Audio(snd_jump)
        self.snd_item = Audio(snd_item)
        self.snd_laser = Audio(snd_laser)

    def get_pos(self):
        return(self.rect.x,self.rect.y)

    def get_x(self):
        return(self.rect.x + self.image.get_size()[0] / 2)

    def get_y(self):
        return(self.rect.y + self.image.get_size()[1] / 2)

    def get_level(self):
        return self.level

    def move(self, time, direction):
        old_direction = self.direction

        if old_direction != direction:
            self.sprite.flip_image()

        self.image = self.sprite.get_image(0, 0)
        self.sprite.animate(time)
        self.direction = direction

        if (direction == "R"):
            self.move_speed = -PLAYER_SPEED
        else:
            self.move_speed = PLAYER_SPEED

    def shoot(self, direction, laser_list):
        if self.move_speed == 0:
            if direction == "R":
                self.image = self.sprite_shoot_r.get_image(0, 0)
            else:
                self.image = self.sprite_shoot_l.get_image(0, 0)

        # create pulse for lasers
        time = pygame.time.get_ticks()
        if time - self.laser_timer >= self.laser_cooldown and self.energy > 0:
            self.energy -= 2
            self.laser_timer = time
            laser = Laser(self.get_x(), self.get_y())
            laser.set_direction(self.direction)
            laser_list.add(laser)
            self.snd_laser.play()

    def jump(self):
        if self.on_ground == True:
            if self.velocity == 0:
                self.snd_jump.play()
                self.velocity = 6

    def stop(self ,time):
        self.move_speed = 0
        if self.sprite.current_sprite != 0:
            self.sprite.animate(time)
        else:
            self.image = self.sprite_stopped.get_image(0, 0)

    def update(self, object, ground):
        if self.velocity < 0:
            self.falling = True

        self.collision = self.sprite.get_collision(self.rect.x, self.rect.y, self.sprite.width, self.sprite.height, ground.rect.x, ground.rect.y, ground.sprite.width, ground.sprite.height)

        if not self.collision:
            self.on_ground = False

        # if player touches ground stop
        if self.collision:
            if self.falling == True:
                self.falling = False
                self.on_ground = True
                self.velocity = 0

        # fall if not on ground, else snap to position
        if not self.on_ground:
            self.velocity += self.gravity
        else:
            self.rect.y = self.center_y

        self.rect.y -= self.velocity

    def collision_check(self, object):
        if self.sprite.get_collision(self.rect.x, self.rect.y, self.sprite.width, self.sprite.height, object.rect.x, object.rect.y, object.sprite.width, object.sprite.height):
            self.collision_entity = object.type
        else:
            self.collision_entity = None

        if self.collision_entity == "item":
            object.kill()
            self.snd_item.play()

            if object.sub_type == "fuel":
                self.fuel += 5

            if object.sub_type == "energy":
                self.energy += 5

            if object.sub_type == "health":
                self.health += 5

            if object.sub_type == "treasure":
                self.treasure += 5

        if self.collision_entity == "enemy":
            object.kill()
            self.health -= 20

        if self.collision_entity == "ship":
            self.level = "universe"
コード例 #11
0
ファイル: spaceship.py プロジェクト: Durgal/StarCrusader
class Spaceship(pygame.sprite.Sprite):
    def __init__(self,
                 sprite_group,
                 laser_group,
                 x=STARTING_POS_X,
                 y=STARTING_POS_Y):
        self.group = sprite_group
        self.laser_group = laser_group
        pygame.sprite.Sprite.__init__(self, self.group)
        self.sprite = Sprite('Sprites/spaceship_sheet.png', SPRITE_W, SPRITE_H)
        self.sprite_stopped = Sprite('Sprites/spaceship.png', SPRITE_W,
                                     SPRITE_H)

        self.level = "none"
        self.position = vec(x, y)
        self.velocity = vec(INITIALIZE, INITIALIZE)
        self.acceleration = vec(INITIALIZE, INITIALIZE)
        self.rotation = INITIALIZE
        self.rot_speed = INITIALIZE
        self.dt = INITIALIZE
        self.dead = False
        self.last_shot = INITIALIZE
        self.is_accel = False

        self.fuel = FUEL_CAPACITY
        self.health = HEALTH_CAPACITY
        self.energy = ENERGY_CAPACITY
        self.treasure = INITIALIZE

        self.image = self.sprite.get_image(0, 0)
        self.rect = self.image.get_rect()
        self.rect.center = self.position
        self.orig_img = self.image
        self.orig_center = self.rect.center

        self.player_chunk = pygame.Rect(INITIALIZE, INITIALIZE, WIDTH, HEIGHT)
        self.player_chunk.center = self.position

        self.snd_laser = Audio(snd_laser)
        self.snd_thrust = Audio(snd_thrust)

    def get_event(self, event):
        self.rot_speed = INITIALIZE
        if MIN <= self.fuel and not self.dead:
            if ACCELERATE == event:
                self.acceleration = vec(MIN, -ACCELERATION_RATE)
                self.deplete_fuel()
                self.image = self.sprite.get_image(0, 0)
                self.sprite.animate(pygame.time.get_ticks() *
                                    FUEL_ANIMATION_TIME)
                self.is_accel = True
                self.snd_thrust.play()

        if ROTATE_LEFT == event:
            self.rot_speed = ROTATION_RATE
            self.rotate_sprite()
        if ROTATE_RIGHT == event:
            self.rot_speed = -ROTATION_RATE
            self.rotate_sprite()

    def shoot_laser(self):
        if MIN < self.energy and not self.dead:
            now = pygame.time.get_ticks()
            if now - self.last_shot > FIRE_RATE:
                self.last_shot = now
                velocity = self.velocity + LASER_SPEED.rotate(-self.rotation)
                position = self.position
                rotation = self.rotation
                Laser(self.laser_group, position, velocity, rotation)
                self.energy -= ENERGY_DEPLETION_RATE
                self.snd_laser.play()

    def deplete_fuel(self):
        self.fuel -= (FUEL_DEPLETION_RATE * self.dt)

    def collided(self, amount):
        self.health -= amount

    def check_death(self):
        if MIN >= self.health:
            self.kill()
            self.dead = True

        if self.dead:
            self.velocity = vec(INITIALIZE, INITIALIZE)

    def set_dt(self, dt):
        self.dt = dt

    def rotate_sprite(self):
        self.image = pygame.transform.rotate(self.orig_img, self.rotation)
        self.rect = self.image.get_rect(center=self.orig_center)

    def debugging(self, screen, fps):
        if pygame.font:
            font = pygame.font.Font("courbd.ttf", 12)

            rot_angle = font.render(str(int(self.rotation)), 1,
                                    (255, 255, 255))
            rot_angle1 = font.render('Angle: ', 1, (255, 255, 255))
            position = font.render(str(self.position), 1, (255, 255, 255))
            position1 = font.render('Position [x,y]: ', 1, (255, 255, 255))
            velocity = font.render(str(self.velocity), 1, (255, 255, 255))
            velocity1 = font.render('Velocity [x,y]: ', 1, (255, 255, 255))
            frames = font.render(str(int(fps)), 1, (255, 255, 255))
            fuel = font.render('Fuel: ', 1, (255, 255, 255))
            fuel1 = font.render(str(int(self.fuel)), 1, (255, 255, 255))
            health = font.render('Health: ', 1, (255, 255, 255))
            health1 = font.render(str(self.health), 1, (255, 255, 255))
            energy = font.render('Energy: ', 1, (255, 255, 255))
            energy1 = font.render(str(self.energy), 1, (255, 255, 255))

            textpos1 = rot_angle.get_rect().move(120, 25)
            textpos2 = position.get_rect().move(120, 40)
            textpos3 = velocity.get_rect().move(120, 55)
            text_fuel1 = fuel1.get_rect().move(120, 70)
            text_health1 = health1.get_rect().move(120, 85)
            text_energy1 = energy1.get_rect().move(120, 100)
            textpos4 = rot_angle1.get_rect().move(10, 25)
            textpos5 = position1.get_rect().move(10, 40)
            textpos6 = velocity1.get_rect().move(10, 55)
            text_fuel = fuel.get_rect().move(10, 70)
            text_health = health.get_rect().move(10, 85)
            text_energy = energy.get_rect().move(10, 100)
            textpos7 = frames.get_rect().move(10, 10)

            screen.blit(rot_angle, textpos1)
            screen.blit(position, textpos2)
            screen.blit(velocity, textpos3)
            screen.blit(rot_angle1, textpos4)
            screen.blit(position1, textpos5)
            screen.blit(velocity1, textpos6)
            screen.blit(fuel, text_fuel)
            screen.blit(fuel1, text_fuel1)
            screen.blit(health, text_health)
            screen.blit(health1, text_health1)
            screen.blit(energy, text_energy)
            screen.blit(energy1, text_energy1)
            screen.blit(frames, textpos7)

    def update(self):
        self.rotation = (self.rotation +
                         self.rot_speed * self.dt) % FULL_ROTATION
        self.velocity += self.acceleration.rotate(-self.rotation) * self.dt
        self.position += self.velocity
        self.acceleration = vec(INITIALIZE, INITIALIZE)
        self.rect.center = self.position
        self.rot_speed = INITIALIZE

        if self.is_accel is not True:
            self.image = self.sprite_stopped.get_image(0, 0)
            self.rect = self.image.get_rect()
            self.rect.center = self.position
            self.orig_img = self.image
            self.orig_center = self.rect.center
            self.rotate_sprite()
        else:
            self.image = self.sprite.get_image(0, 0)
            self.rect = self.image.get_rect()
            self.rect.center = self.position
            self.orig_img = self.image
            self.orig_center = self.rect.center
            self.rotate_sprite()

        self.check_death()
        self.player_chunk.centerx = self.position[POSITION_X]
        self.player_chunk.centery = self.position[POSITION_Y]
        self.is_accel = False