def update(self): if not self.door_open: self.image = gf.load_image('door_open.png') self.door_open = True else: self.image = gf.load_image('door_closed.png') self.door_open = False self.image = pygame.transform.scale( self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5)))
def update_image(self, pos): if pos == 'down': if abs(self.post_y - self.rect.y) > 40 or self.new_down: self.new_down = False self.post_y = self.rect.y if self.post_down: self.image = gf.load_image('orc_warrior2.png', -1) self.post_down = False else: self.post_down = True self.image = gf.load_image('orc_warrior1.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5)))
def __init__(self): super(Traider, self).__init__() self.image = gf.load_image('traider.png', -1) self.image = pygame.transform.scale( self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) self.rect = self.image.get_rect()
def __init__(self, Everything, profil, screen, pos=(0, 0)): pygame.sprite.Sprite.__init__(self) self.profil = profil self.image, self.rect = gf.load_image(profil['image_name']) self.area = pygame.display.get_surface().get_rect() self.rect.topleft = pos interval = st.Game_Attr.INTERVAL.value Pos_x_grid = (pos[0]//interval)*interval Pos_y_grid = (pos[1]//interval)*interval self.rect.topleft = (Pos_x_grid,Pos_y_grid) self.pos = (Pos_x_grid,Pos_y_grid) self.screen = screen self.selected = 0 self.moved = 0 self.finished = 0 self.Everything = Everything self.attackable = 0 self.zoneMovable = None ################################################### self.attack_value = profil['attack_value'] self.damage_value = profil['damage_value'] self.defense_value = profil['defense_value'] self.life_value = profil['life_value'] self.exp_value = profil['exp_value'] self.level = profil['level'] self.move_distance = profil['Move_distance_default'] self.attack_distance = profil['Attack_distance_default'] self.abilites = profil['abilites']
def __init__(self, screen, x, y): """Инициализирует корабль и задает его начальную позицию.""" self.screen = screen super(Player, self).__init__() self.image = gf.load_image('hero.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) self.screen_rect = screen.get_rect() self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False self.position_up = True self.position_on_ladder = False self.post_x = self.rect.x self.post_y = self.rect.y self.post_left = False self.post_right = False self.new_left = True self.new_right = True self.post_up = False self.new_up = True self.post_down = False self.new_down = True
def __init__(self, in_or_out_wall): super(Ground, self).__init__() self.in_or_out_wall = in_or_out_wall if in_or_out_wall == 1: self.image = gf.load_image('grass.png') elif in_or_out_wall == 0: self.image = gf.load_image('ground.png') elif in_or_out_wall == 2: self.image = gf.load_image('ground_wall.png') self.image = pygame.transform.scale( self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) self.rect = self.image.get_rect() self.first_y = False self.end_y = False self.first_x = False self.end_x = False
def update_image(self, pos): if pos == 'left': if abs(self.post_x - self.rect.x) > 40 or self.new_left: self.new_left = False self.post_x = self.rect.x if self.post_left: self.image = gf.load_image('hero_left2.png', -1) self.post_left = False else: self.post_left = True self.image = gf.load_image('hero_left.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) elif pos == 'right': if abs(self.post_x - self.rect.x) > 40 or self.new_right: self.new_right = False self.post_x = self.rect.x if self.post_right: self.image = gf.load_image('hero_right.png', -1) self.post_right = False else: self.post_right = True self.image = gf.load_image('hero_right2.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) elif pos == 'up': if abs(self.post_y - self.rect.y) > 40 or self.new_up: self.new_up = False self.post_y = self.rect.y if self.post_up: self.image = gf.load_image('hero_up.png', -1) self.post_up = False else: self.post_up = True self.image = gf.load_image('hero_up2.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) elif pos == 'down': if abs(self.post_y - self.rect.y) > 40 or self.new_down: self.new_down = False self.post_y = self.rect.y if self.post_down: self.image = gf.load_image('hero_down.png', -1) self.post_down = False else: self.post_down = True self.image = gf.load_image('hero_down2.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5)))
def __init__(self, wall_up, door=False): super(Wall, self).__init__() self.wall_up = wall_up self.door = door self.door_open = False if self.wall_up: self.image = gf.load_image('wall_up.png') else: self.image = gf.load_image('wall_down.png') if self.door and not self.door_open: self.image = gf.load_image('door_closed.png') elif self.door and self.door_open: self.image = gf.load_image('door_open.png') self.image = pygame.transform.scale( self.image, (int(float(self.image.get_width()) * 1.5), int(float(self.image.get_height()) * 1.5))) self.rect = self.image.get_rect() self.first = False self.end = False self.health = 100
def __init__(self, screen, player): """Создает объект пули в текущей позиции корабля.""" super(Bullet, self).__init__() self.screen = screen # Создание пули в позиции (0,0) и назначение правильной позиции. self.image = gf.load_image('arrow.png', -1) self.rect = self.image.get_rect() self.rect.centerx = player.rect.centerx self.rect.top = player.rect.top # Позиция пули хранится в вещественном формате. self.y = float(self.rect.y) self.speed_factor = 3
def run_game(): # Initialize game and create a screen object pygame.init() ai_s = Settings( ) # instance of class Settings, which store all settings parameters screen = pygame.display.set_mode( (ai_s.screen_width, ai_s.screen_height)) # Get a Surface of screen pygame.display.set_caption('ALIEN INVASION') background, background_rect = gf.load_image("bg1.jpg") # Create an instance to store game statistics and create a scoreboard stats = GameStats(ai_s) sb = Scoreboard(ai_s, screen, stats) # Make a ship, a group of bullets, and a group of aliens ship = Ship(ai_s, screen) bullets = Group() # sprites-group of Bullet instances aliens = Group() # Create the fleet of aliens gf.create_fleet(ai_s, screen, ship, aliens) # Make the PLAY button, Pause Button play_button = Button(ai_s, screen, 'PLAY') pause_button = Button(ai_s, screen, "PAUSE") # Load Sound losing_sound = gf.load_music("boom1.wav") level_up_sound = gf.load_music('level_up1.wav') bg_music = gf.load_music("bg_music.wav") # start background music bg_music.set_volume(0.09) bg_music.play(-1) # Start the main loop for the game running = True while running: gf.check_events(ai_s, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active and not stats.game_pause: ship.update() # ship position update gf.update_bullets(ai_s, screen, stats, sb, ship, bullets, aliens, level_up_sound) gf.update_aliens(ai_s, stats, screen, sb, ship, aliens, bullets, losing_sound) gf.update_screen(ai_s, screen, background, stats, sb, ship, aliens, bullets, play_button, pause_button)
def __init__(self, ai_s, screen): """Initialize the alien and set its starting position""" super().__init__() self.screen = screen self.ai_s = ai_s # Load the alien image and set its rect attribute self.image, self.rect = gf.load_image("ufo.png", None, 90, 50) # Start each new alien near the top left of the screen self.rect.x = self.rect.width//3 self.rect.y = self.rect.height//2 # Store the aliens's exact position self.x = float(self.rect.x)
def __init__(self, elementName, pos=(0, 0)): pygame.sprite.Sprite.__init__(self) self.elementName = elementName self.image_name = elementName + '.png' self.image, self.rect = gf.load_image(self.image_name) self.rect.topleft = pos if elementName == Map_Elements.LAWN: self.reachable = 1 self.attackable = 1 self.crossable = 1 if elementName == Map_Elements.MOUNTAIN: self.reachable = 0 self.attackable = 0 self.crossable = 0
def __init__(self, screen, stats): self.stats = stats self.screen = screen self.screen_rect = self.screen.get_rect() self.font = pygame.font.SysFont('Brush Script MT', 350) pygame.sprite.Sprite.__init__(self) # call Sprite initializer self.image = gf.load_image('intro.jpg') self.image = pygame.transform.scale( self.image, (self.screen_rect.w, self.screen_rect.h)) self.start_game_button = Button(screen, "Play", self.screen_rect.centerx - 100, self.screen_rect.centery, (100, 150, 50)) self.quit_button = Button(screen, "quit", self.screen_rect.centerx - 100, self.screen_rect.centery + 70, (100, 150, 50))
def __init__(self, screen): """Инициализирует пришельца и задает его начальную позицию.""" super(Enemy, self).__init__() self.screen = screen self.screen_rect = screen.get_rect() # Загрузка изображения пришельца и назначение атрибута rect. self.image = gf.load_image('orc_warrior.png', -1) self.image = pygame.transform.scale(self.image, (int(float(self.image.get_width()) * 2), int(float(self.image.get_height()) * 2))) self.rect = self.image.get_rect() # Каждый новый пришелец появляется в левом верхнем углу экрана. self.rect.x = self.rect.width self.rect.y = self.rect.height self.post_x = self.rect.x self.post_y = self.rect.y self.post_down = False self.new_down = True
def smooth_attack(self,target): print('EntityOfPlayer.smooth_attack') if target.attackable == 1: #增加一个攻击方向的判断,以此决定加载那个图片 am_attack = AM.Animation(self,'attack') images_am_attack = am_attack.images_attack nb_frames = am_attack.nb_frames for i in range(nb_frames): self.image = images_am_attack[i] self.refresh_image() time.sleep(0.2) self.image, self.rect = gf.load_image(self.profil['image_name']) self.rect.topleft = self.pos target.life_value = target.life_value - self.attack_value - self.damage_value self.exp_value, self.level = Entity.changeExpAndLevel(self,[target]) Entity.attack(self,target) if target.life_value <= 0: target.playerDie()
def __init__(self, ai_settings, screen): """Initialize the ship and set its starting position""" super().__init__() self.screen = screen self.ai_s = ai_settings self.screen_rect = screen.get_rect() # Rect object for screen # Load the ship image and get its rect. self.image, self.rect = load_image("ship1.1.jpg", (255, 255, 255), 75, 85) # Start each new ship at the bottom center of the screen self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom # Store a decimal value for the ship's center self.center = float(self.rect.centerx) # Movement flags self.moving_right = False self.moving_left = False
gf.create_fleet(width_background, height_background, wall_group_up, wall_group_down, ground_group, ladder_group, door_group, end_group, traider_group, Wall(False).rect.h) for sprite in wall_group_up: nn = sprite.rect.y break player = Player(screen, screen_rect.centerx, nn) player_group.add(player) play_button = Button(screen, "Play", screen_rect.centerx - 100, screen_rect.centery, (100, 150, 50)) game_over_button = Button(screen, "New", screen_rect.centerx - 100, screen_rect.centery, (100, 150, 50)) quit_button = Button(screen, "quit", screen_rect.centerx - 100, screen_rect.centery + 70, (100, 150, 50)) return_intro_button = Button(screen, "return intro", screen_rect.centerx - 100, screen_rect.centery + 130, (100, 150, 50)) camera = Camera() intro = Intro(screen, stats) shop = Shop(screen, stats) help_image = gf.load_image('help.png') while stats.game_active: # действия с квавиатуры и мыши gf.key_evens(screen, player, bullets, ladder_group, door_group, play_button, game_over_button, camera, stats, intro, background, end_group, enemy_group, wall_group_down, wall_group_up, ground_group, quit_button, return_intro_button, traider_group, shop) if stats.game_intro: # интро игры intro.blit() else: if stats.game_return: # переход через интро gf.new_fleet(screen, background, end_group, enemy_group, wall_group_down, wall_group_up, ladder_group, ground_group, door_group, stats, player, camera, traider_group) stats.game_return = False # обновление останавлиывается если пауза или магазин или окончание игры
def __init__(self, imageName, Everything, screen, pos=(0, 0)): pygame.sprite.Sprite.__init__(self) self.image, self.rect = gf.load_image(imageName) self.Everything = Everything self.screen = screen self.pos = pos