def __init__(self, game, character): pygame.sprite.Sprite.__init__(self) self.game = game self.character = character self.facing = 1 self.faces = textures.get_faces(self.character) self.image = self.faces[self.facing] self.rect = self.image.get_rect() self.pos = vect(0, 0) self.vel = vect(0, 0) self.acc = vect(0, 0) self.health = PLAYER_HEALTH self.dead = 0 self.explosion = {} for i in range(1, 25): e = "0" + str(i) e = e[-2:] + ".png" self.explosion[i] = pygame.transform.scale( textures.get_image( path.join("textures", self.character, "explosion", e)), (60, 60)) self.current_frame = 0 self.last_update = 0 self.health_bar = pygame.rect.Rect(self.rect.midtop + vect(-30, -10), vect(self.rect.width, 5))
def __init__(self, game, player): self.game = game self.group = game.grenades pygame.sprite.Sprite.__init__(self, self.group) self.player = player self.image = textures.get_image("textures/weapons/tome_" + self.player.character + ".png") self.pos = vect(self.player.rect.center) + vect(-30, -15) self.rect = pygame.rect.Rect(self.pos, (1, 1)) dir = pygame.mouse.get_pos() rel_x, rel_y = dir[0] - (self.pos.x + game.camera.x), dir[1] - ( self.pos.y + game.camera.y) rel_n = math.sqrt(rel_x * rel_x + rel_y * rel_y) self.vel = vect(rel_x / rel_n, rel_y / rel_n) * GRENADE_SPEED self.last_update = 0 self.explosion = {} for i in range(1, 25): e = "0" + str(i) e = e[-2:] + ".png" self.explosion[i] = pygame.transform.scale( textures.get_image(path.join("textures/weapons/explode", e)), (60, 60)) self.current_frame = 0 self.last_update = 0 self.health_bar = pygame.rect.Rect(self.rect.midtop + vect(-30, -10), vect(self.rect.width, 5))
def take_turn(self): now = pygame.time.get_ticks() - self.start_tick seconds = TURN_TIME - int((now - self.turn_time) / 1000) - 1 seconds_str = "0" + str(seconds) seconds_str = seconds_str[-2:] col = BLACK if now - self.turn_time > TURN_TIME * 1000 - 5000: col = RED scroll = pygame.transform.scale( textures.get_image("textures/scroll.png"), (60, 60)) self.screen.blit(scroll, (50, HEIGHT - 100)) self.screen.blit(FONT_TIME.render(seconds_str, 1, col), (65, HEIGHT - 82)) if now - self.turn_time > TURN_TIME * 1000 or ( self.teams[self.turn].left_bullets == 0 and self.teams[self.turn].left_grenades == 0 and len(self.grenades) == 0 and len(self.bullets) == 0): self.turn_time = now self.teams[self.turn].left_grenades = 1 self.teams[self.turn].left_bullets = 3 self.turn = 1 - self.turn self.teams[self.turn].active = 1 self.teams[1 - self.turn].active = 0 self.teams[1 - self.turn].players[self.teams[ 1 - self.turn].chosen_player].vel = vect(0, 0) self.teams[1 - self.turn].players[self.teams[ 1 - self.turn].chosen_player].acc = vect(0, 0)
def __init__(self, game, character, number, name): self.game = game pygame.sprite.Group.__init__(self) self.team_number = number self.team_name = name self.character = character self.player_1 = Player(game, self.character) self.player_2 = Player(game, self.character) self.player_3 = Player(game, self.character) self.player_4 = Player(game, self.character) self.add(self.player_1) self.add(self.player_2) self.add(self.player_3) self.add(self.player_4) self.players = { 0: self.player_1, 1: self.player_2, 2: self.player_3, 3: self.player_4 } self.chosen_player = 0 self.active = 1 # active or passive turn self.team_health = self.get_team_health() self.left_bullets = 3 self.left_grenades = 1 self.team_health_bar = pygame.rect.Rect(vect(10, 10), vect(100, 10))
def switch_character(self): for i in range(0, 4): self.players[i].vel = vect(0, 0) self.players[i].acc = vect(0, 0) self.chosen_player = (self.chosen_player + 1) % 4 while self.players[self.chosen_player].dead == 1: self.chosen_player = (self.chosen_player + 1) % 4
def __init__(self, game, player): self.game = game self.group = self.game.map.objects pygame.sprite.Sprite.__init__(self, self.group) self.image = pygame.transform.scale(textures.get_image("textures/grave.png"), (60, 60)) self.player = player self.rect = self.image.get_rect() self.pos = vect(self.player.rect.center) self.rect.center = self.player.rect.center - vect(0, -5)
def __init__(self, game, player): self.game = game self.group = game.bullets pygame.sprite.Sprite.__init__(self, self.group) self.player = player self.image = textures.get_image("textures/weapons/bullet_" + self.player.character + ".png") self.pos = vect(self.player.rect.center) + vect(-30, -15) self.rect = pygame.rect.Rect(self.pos, (1, 1)) self.vel = vect(self.player.facing, 0) * BULLET_SPEED self.spawn_time = pygame.time.get_ticks()
def draw_team_health(self): col = textures.get_team_color(self.players[0].character) if self.team_number == 1: pos = 30 else: pos = WIDTH - 230 width = self.get_team_health() / 400 self.team_health_bar = pygame.rect.Rect(vect(pos, 30), vect(width * 200, 20)) pygame.draw.rect(self.game.screen, col, self.team_health_bar) message = self.team_name + ": " + str(self.get_team_health()) + "/400" self.game.screen.blit(FONT_PLAYERS.render(message, 1, col), vect(pos, 30 + 20 + 10))
def text_to_button(game, message, buttonx, buttony, buttonwidth, buttonheight, font, active_col, inactive_col, action=None): cur = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if buttonx + buttonwidth > cur[ 0] > buttonx and buttony + buttonheight > cur[1] > buttony: col = active_col if click[0] == 1 and action != None: if action == "play": game.new() elif action == "options": game.options_screen() elif action == "instructions": game.instructions_screen() elif action == "back": game.show_start_screen() elif action == "quit": sys.exit(0) else: col = inactive_col text_surf = font.render(message, 1, col) text_rect = text_surf.get_rect() text_rect.center = vect(buttonx + (buttonwidth / 2), buttony + (buttonheight / 2)) game.screen.blit(text_surf, text_rect)
def draw_health(self): if self.health > 90: col = GREEN elif self.health > 60: col = LIGHTGREEN elif self.health > 30: col = YELLOW else: col = RED if self.health > 0: width = (self.rect.width * self.health / 100) height = 3 self.health_bar = pygame.rect.Rect( self.rect.midtop + vect(-30, -10), vect(width, height)) pygame.draw.rect(self.game.screen, col, self.game.camera.apply_rect(self.health_bar))
def _create_way(self, p1, p2): ''' Creating a random path between points p1 and p2. The coordinates are given in a list. ''' current_pos = vect(p1) # current position end_pos = vect(p2) # final position # The delta tuple contains normative vectors, respectively, to go to # the left, top, right, and bottom. These vectors allow to move in # self.map delta = (vect(0, -1), vect(-1, 0), vect(0, 1), vect(1, 0)) while current_pos != end_pos: # vector director between point current_pos and end_pos Dv = end_pos - current_pos freq = self._frequency(Dv) # mv contains a tuple list for each possible direction. Each # tuple contains the new current position and its frequency of # realization. mv = [(eval((current_pos + delta[k]).__str__()), freq[k]) for k in range(4) if freq[k]] tmp = [] # list containing all possible achievements for p, f in mv: # we check that the new position is well included in map. If # this condition is respected, we add f (frequency) times the # position p in tmp if 0 <= p[0] <= 14 and 0 <= p[1] <= 14: tmp.extend([p for _ in range(f)]) mv = tmp shuffle(mv) pos = choice(mv) self.map[pos[0]][pos[1]] = GROUND current_pos = vect(pos[0], pos[1])
def update(self, game): self.acc = vect(0, PLAYER_GRAVITY / 2) self.acc.x += self.vel.x * PLAYER_FRICTION / 10 self.vel += self.acc self.pos += self.vel + 0.5 * self.acc self.pos += self.vel self.rect.center = self.pos hit_bottom = pygame.sprite.spritecollide( self, self.game.map.bottomplatformsprite, False) if hit_bottom: self.vel = vect(0, 0) self.pos.y = hit_bottom[0].rect.top self.explode() if self.pos.y > HEIGHT: self.kill()
def move(self): self.acc = vect(0, PLAYER_GRAVITY) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.acc.x = -PLAYER_ACC self.facing = -1 self.image = self.faces[self.facing] if keys[pygame.K_RIGHT]: self.acc.x = PLAYER_ACC self.facing = 1 self.image = self.faces[self.facing]
def __init__(self): pygame.sprite.Group.__init__(self) # Platforms self.platforms = pygame.sprite.Group() self.bottomplatform = map.Platform(vect(0, HEIGHT - 50), vect(2560, 40)) self.castleplatform1 = map.Platform( vect(3 * WIDTH / 2 + 5, HEIGHT - 225), vect(512, 350)) self.castleplatform2 = map.Platform(vect(200 + 5, HEIGHT - 225), vect(512, 350)) self.tree1platform = map.Platform(vect(40, HEIGHT - 300), vect(150, 50)) self.tree2platform = map.Platform(vect(2 * WIDTH - 160, HEIGHT - 300), vect(150, 50)) self.tree3platform = map.Platform( vect(3 * WIDTH / 2 - 160, HEIGHT - 300), vect(150, 50)) self.tree4platform = map.Platform(vect(650, HEIGHT - 300), vect(150, 50)) self.shootingtowerplatform1 = map.Platform(vect(330, 300), vect(70, 22)) self.shootingtowerplatform2 = map.Platform(vect(530, 300), vect(70, 22)) self.shootingtowerplatform3 = map.Platform(vect(2050, 300), vect(70, 22)) self.shootingtowerplatform4 = map.Platform(vect(2250, 300), vect(70, 22)) self.platforms.add(self.bottomplatform) self.platforms.add(self.castleplatform1) self.platforms.add(self.castleplatform2) self.platforms.add(self.tree1platform) self.platforms.add(self.tree2platform) self.platforms.add(self.tree3platform) self.platforms.add(self.tree4platform) self.platforms.add(self.shootingtowerplatform1) self.platforms.add(self.shootingtowerplatform2) self.platforms.add(self.shootingtowerplatform3) self.platforms.add(self.shootingtowerplatform4) self.bottomplatformsprite = pygame.sprite.Group() self.bottomplatformsprite.add(self.bottomplatform) # Objects self.objects = pygame.sprite.Group() self.castle1 = map.Object(vect(3 * WIDTH / 2, 250), "textures/castle.png") self.castle2 = map.Object(vect(200, 250), "textures/castle.png") self.bottom = map.Object(vect(0, HEIGHT - 50), "textures/grass.png") self.tree1 = map.Object(vect(0, HEIGHT - 350), "textures/tree.png") self.tree2 = map.Object(vect(2 * WIDTH - 200, HEIGHT - 350), "textures/tree.png") self.tree3 = map.Object(vect(3 * WIDTH / 2 - 200, HEIGHT - 350), "textures/tree.png") self.tree4 = map.Object(vect(650, HEIGHT - 350), "textures/tree.png") self.objects.add(self.bottom) self.objects.add(self.castle1) self.objects.add(self.castle2) self.objects.add(self.tree1) self.objects.add(self.tree2) self.objects.add(self.tree3) self.objects.add(self.tree4) self.add(self.platforms) self.add(self.objects)
def options_screen(self): waiting = True team1_name = textures.Input_box(WIDTH / 4 - (186 / 2), 2 * HEIGHT / 10 - 10, 186, 40, self.teams[0].team_name, FONT_PLAYERS) team2_name = textures.Input_box(3 * WIDTH / 4 - (186 / 2), 2 * HEIGHT / 10 - 10, 186, 40, self.teams[1].team_name, FONT_PLAYERS) while waiting: self.tps_clock.tick(30) background = pygame.transform.scale( textures.get_image("textures/options.png"), (1280, 720)) topinfo = textures.get_image("textures/hang.png") sign = pygame.transform.scale( textures.get_image("textures/arrow_back.png"), (100, 100)) frame = pygame.transform.scale( textures.get_image("textures/frame.png"), (186, 238)) character_1 = textures.get_character( self.teams[0].player_1.character) character_2 = textures.get_character( self.teams[1].player_1.character) switch_l1 = pygame.transform.scale( textures.get_image("textures/switch_l_" + self.teams[0].player_1.character + ".png"), (25, 25)) switch_r1 = pygame.transform.scale( textures.get_image("textures/switch_r_" + self.teams[0].player_1.character + ".png"), (25, 25)) switch_l2 = pygame.transform.scale( textures.get_image("textures/switch_l_" + self.teams[1].player_1.character + ".png"), (25, 25)) switch_r2 = pygame.transform.scale( textures.get_image("textures/switch_r_" + self.teams[1].player_1.character + ".png"), (25, 25)) language = pygame.transform.scale( textures.get_image("textures/" + self.language + ".png"), (64, 64)) sound = pygame.transform.scale( textures.get_image("textures/megaphone" + str(self.sound) + ".png"), (64, 64)) save = pygame.transform.scale( textures.get_image("textures/save.png"), (64, 64)) versus = pygame.transform.scale( textures.get_image("textures/versus.png"), (120, 100)) self.screen.blit(background, (0, 0)) self.screen.blit(topinfo, (WIDTH / 2 - 200, 0)) self.screen.blit(sign, (50, HEIGHT - 100)) self.screen.blit(frame, (WIDTH / 4 - (186 / 2), HEIGHT / 4)) self.screen.blit(frame, (3 * WIDTH / 4 - (186 / 2), HEIGHT / 4)) self.screen.blit(character_1, (WIDTH / 4 - (186 / 2) + 25, HEIGHT / 4 + 30)) self.screen.blit(character_2, (3 * WIDTH / 4 - (186 / 2) + 25, HEIGHT / 4 + 30)) self.screen.blit(switch_l1, (WIDTH / 4 - (186 / 2) - 40, HEIGHT / 4 + 248)) self.screen.blit(switch_r1, (WIDTH / 4 + (186 / 2) + 10, HEIGHT / 4 + 248)) self.screen.blit(switch_l2, (3 * WIDTH / 4 - (186 / 2) - 40, HEIGHT / 4 + 248)) self.screen.blit(switch_r2, (3 * WIDTH / 4 + (186 / 2) + 10, HEIGHT / 4 + 248)) options = self.resources.loc[self.resources["K"] == "K_options"][ self.language].values[0] self.screen.blit(FONT_MENU.render(options, 1, WHITE), (WIDTH / 2 - 55, 30)) character_1_type = self.resources.loc[self.resources["K"] == "K_class"][self.language].values[0] + \ self.teams[0].player_1.character character_2_type = self.resources.loc[self.resources["K"] == "K_class"][self.language].values[0] + \ self.teams[1].player_1.character self.screen.blit( FONT_PLAYERS.render( character_1_type, 1, textures.get_team_color(self.teams[0].player_1.character)), (WIDTH / 4 - (186 / 2), HEIGHT / 4 + 250)) self.screen.blit( FONT_PLAYERS.render( character_2_type, 1, textures.get_team_color(self.teams[1].player_1.character)), (3 * WIDTH / 4 - (186 / 2), HEIGHT / 4 + 250)) self.screen.blit(language, (WIDTH / 2 - 32, 5 * HEIGHT / 7)) self.screen.blit(sound, (WIDTH / 2 - 32, 6 * HEIGHT / 7)) self.screen.blit(save, (WIDTH - 80, 6 * HEIGHT / 7 + 10)) self.screen.blit(versus, (WIDTH / 2 - 60, HEIGHT / 2 - 100)) self.screen.blit( FONT_OPTIONS.render( self.resources.loc[self.resources["K"] == "K_player"][ self.language].values[0] + " 1", 1, textures.get_team_color(self.teams[0].player_1.character)), (WIDTH / 4 - (186 / 2), HEIGHT / 10)) self.screen.blit( FONT_OPTIONS.render( self.resources.loc[self.resources["K"] == "K_player"][ self.language].values[0] + " 2", 1, textures.get_team_color(self.teams[1].player_1.character)), (3 * WIDTH / 4 - (186 / 2), HEIGHT / 10)) back_button = pygame.rect.Rect(vect(50, HEIGHT - 80), vect(100, 30)) textures.text_to_button( self, self.resources.loc[self.resources["K"] == "K_back"][ self.language].values[0], back_button.x, back_button.y, back_button.width, back_button.height, FONT_OPTIONS, BLACK, WHITE, "back") for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.MOUSEBUTTONDOWN: textures.switch_character_type_button( self, 0, WIDTH / 4 - (186 / 2) - 40, HEIGHT / 4 + 248, 25, 25, "backward") textures.switch_character_type_button( self, 0, WIDTH / 4 + (186 / 2) + 10, HEIGHT / 4 + 248, 25, 25, "forward") textures.switch_character_type_button( self, 1, 3 * WIDTH / 4 - (186 / 2) - 40, HEIGHT / 4 + 248, 25, 25, "backward") textures.switch_character_type_button( self, 1, 3 * WIDTH / 4 + (186 / 2) + 10, HEIGHT / 4 + 248, 25, 25, "forward") textures.switch_language_button(self, WIDTH / 2 - 32, 5 * HEIGHT / 7, 64, 64) textures.switch_sound_button(self, WIDTH / 2 - 32, 6 * HEIGHT / 7, 64, 64) textures.save_button(self, WIDTH - 80, 6 * HEIGHT / 7 + 10, 64, 64) team1_name.handle_event(event) team2_name.handle_event(event) team1_name.draw(self.screen) team2_name.draw(self.screen) self.teams[0].team_name = team1_name.text self.teams[1].team_name = team2_name.text pygame.display.flip()
def instructions_screen(self): waiting = True while waiting: self.tps_clock.tick(30) background = pygame.transform.scale( textures.get_image("textures/instructions.jpg"), (1280, 720)) topinfo = textures.get_image("textures/hang.png") sign = pygame.transform.scale( textures.get_image("textures/arrow_back.png"), (100, 100)) leftright = pygame.transform.scale( textures.get_image("textures/instructions/leftright.png"), (196, 50)) up = pygame.transform.scale( textures.get_image("textures/instructions/up.png"), (196, 50)) space = pygame.transform.scale( textures.get_image("textures/instructions/space.png"), (196, 50)) b = pygame.transform.scale( textures.get_image("textures/instructions/B.png"), (196, 50)) tab = pygame.transform.scale( textures.get_image("textures/instructions/tab.png"), (196, 50)) self.screen.blit(background, (0, 0)) self.screen.blit(sign, (50, HEIGHT - 100)) self.screen.blit(topinfo, (WIDTH / 2 - 200, 0)) self.screen.blit(leftright, (40, 150)) self.screen.blit(up, (40, 250)) self.screen.blit(space, (40, 350)) self.screen.blit(b, (40, 450)) self.screen.blit(tab, (40, 550)) leftright_info = self.resources.loc[ self.resources["K"] == "K_move"][self.language].values[0] up_info = self.resources.loc[self.resources["K"] == "K_up"][ self.language].values[0] space_info = self.resources.loc[self.resources["K"] == "K_space"][ self.language].values[0] b_info = self.resources.loc[self.resources["K"] == "K_b"][ self.language].values[0] tab_info = self.resources.loc[self.resources["K"] == "K_tab"][ self.language].values[0] self.screen.blit(FONT_OPTIONS.render(leftright_info, 1, WHITE), (340, 150)) self.screen.blit(FONT_OPTIONS.render(up_info, 1, WHITE), (340, 250)) self.screen.blit(FONT_OPTIONS.render(space_info, 1, WHITE), (340, 350)) self.screen.blit(FONT_OPTIONS.render(b_info, 1, WHITE), (340, 450)) self.screen.blit(FONT_OPTIONS.render(tab_info, 1, WHITE), (340, 550)) instructions = self.resources.loc[self.resources["K"] == "K_instructions"][ self.language].values[0] self.screen.blit(FONT_MENU.render(instructions, 1, WHITE), (WIDTH / 2 - 110, 30)) back_button = pygame.rect.Rect(vect(50, HEIGHT - 80), vect(100, 30)) textures.text_to_button( self, self.resources.loc[self.resources["K"] == "K_back"][ self.language].values[0], back_button.x, back_button.y, back_button.width, back_button.height, FONT_OPTIONS, BLACK, WHITE, "back") for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) pygame.display.flip()
def start_pos(self): for i in self.teams: for j in self.teams[i].players: self.teams[i].players[j].pos = vect( random.randint(0, 2 * WIDTH), 0)