def pick_up(self): #add to the player's inventory and remove from the map if len(objects.inventory) >= 10: draw.message('Your pockets are full. You can\'t pick up the ' + self.owner.name + '.', libtcod.red) else: objects.inventory.append(self.owner) draw.message('You found ' + self.owner.name + '!', libtcod.green)
def create_player(x, y): global player, things hitz = mods.Fighter(hp=10, defense=2, power=2, on_death=mods.player_death) player = Object(x, y, '@', 'player', libtcod.white, blocks=True, fighter=hitz) player.is_player = True draw.message('You hear there are wondrous toys to be found in the Drakalor playground. You\'ve heard there are some formidable bullies, too.') things.append(player)
def find_at_loc(): global visible, player for thing in visible: if (thing.x == player.x and thing.y == player.y and not thing.is_player): if (thing.item): draw.message('You see ' + thing.name + ' lying here.') else: draw.message('You see a ' + thing.name + ' sitting here.')
def punish(self): if self.target.is_player: draw.message('The teacher pulls you off the playground with a threat to call your parents.') else: draw.message('The teacher pulls the ' + self.target.name + ' off the playground.') self.target.fighter.instakill() self.target = None self.owner.notify_visible('punish')
def find_at_loc(): global visible, player for thing in visible: if (thing.x == player.x and thing.y == player.y and not thing.is_player): if (thing.item): draw.message('You see ' + thing.name + ' lying here.') else: draw.message('You see a ' + thing.name + ' sitting here.')
def pick_up(self): #add to the player's inventory and remove from the map if len(objects.inventory) >= 10: draw.message( 'Your pockets are full. You can\'t pick up the ' + self.owner.name + '.', libtcod.red) else: objects.inventory.append(self.owner) draw.message('You found ' + self.owner.name + '!', libtcod.green)
def eat_candy(): global player, things, inventory for thing in inventory: if thing.item.type == 'candy': draw.message('You eat the ' + thing.name + '. You feel better.') player.fighter.hp += 1 inventory.remove(thing) return draw.message('You have no candy to eat.')
def player_death(player, leave_corpse=False): #the game ended! draw.message('You start to cry. Your mom appears and takes you away', libtcod.red) draw.message('You\'ve been pulled off the playground!', libtcod.red) player.alive = False #for added effect, transform the player into a corpse! if leave_corpse: player.char = '%' player.color = libtcod.dark_red
def eat_candy(): global player, things, inventory for thing in inventory: if thing.item.type == 'candy': draw.message('You eat the ' + thing.name + '. You feel better.') player.fighter.hp += 1 inventory.remove(thing) return draw.message('You have no candy to eat.')
def player_death(player, leave_corpse=False): #the game ended! draw.message('You start to cry. Your mom appears and takes you away', libtcod.red) draw.message('You\'ve been pulled off the playground!', libtcod.red) player.alive = False #for added effect, transform the player into a corpse! if leave_corpse: player.char = '%' player.color = libtcod.dark_red
def punish(self): if self.target.is_player: draw.message( 'The teacher pulls you off the playground with a threat to call your parents.' ) else: draw.message('The teacher pulls the ' + self.target.name + ' off the playground.') self.target.fighter.instakill() self.target = None self.owner.notify_visible('punish')
def attack(self, target): #a simple formula for attack damage hit_power = libtcod.random_get_int(0, 0, self.power) hit_defense = libtcod.random_get_int(0, 0, target.fighter.defense) damage = hit_power - hit_defense if (self.owner.is_player): attack_word = libtcod.random_get_int(0, 0, len(player_attacks) - 1) else: attack_word = libtcod.random_get_int(0, 0, len(enemy_attacks) - 1) if damage > 0: #make the target take some damage if (self.owner.is_player): draw.message('You ' + player_attacks[attack_word] + ' the ' + target.name + '.') else: draw.message('The ' + self.owner.name + ' ' + enemy_attacks[attack_word] + ' you. It hurts.') target.fighter.take_damage(damage) # notify other visible objects about attack if (self.owner.visible): self.owner.notify_visible('damage') else: if (self.owner.is_player): draw.message('You ' + player_attacks[attack_word] + ' the ' + target.name + ' but it has no effect!') else: draw.message('The ' + self.owner.name + ' ' + enemy_attacks[attack_word] + ' you but it has no effect!') # notify other visible objects about attack if (self.owner.visible): self.owner.notify_visible('attack')
def attack(self, target): #a simple formula for attack damage hit_power = libtcod.random_get_int(0, 0, self.power) hit_defense = libtcod.random_get_int(0, 0, target.fighter.defense) damage = hit_power - hit_defense if (self.owner.is_player): attack_word = libtcod.random_get_int(0, 0, len(player_attacks) - 1) else: attack_word = libtcod.random_get_int(0, 0, len(enemy_attacks) - 1) if damage > 0: #make the target take some damage if (self.owner.is_player): draw.message('You ' + player_attacks[attack_word] + ' the ' + target.name + '.') else: draw.message('The ' + self.owner.name + ' ' + enemy_attacks[attack_word] + ' you. It hurts.') target.fighter.take_damage(damage) # notify other visible objects about attack if (self.owner.visible): self.owner.notify_visible('damage') else: if (self.owner.is_player): draw.message('You ' + player_attacks[attack_word] + ' the ' + target.name + ' but it has no effect!') else: draw.message('The ' + self.owner.name + ' ' + enemy_attacks[attack_word] + ' you but it has no effect!') # notify other visible objects about attack if (self.owner.visible): self.owner.notify_visible('attack')
def create_player(x, y): global player, things hitz = mods.Fighter(hp=10, defense=2, power=2, on_death=mods.player_death) player = Object(x, y, '@', 'player', libtcod.white, blocks=True, fighter=hitz) player.is_player = True draw.message( 'You hear there are wondrous toys to be found in the Drakalor playground. You\'ve heard there are some formidable bullies, too.' ) things.append(player)
def take_turn(self, player): #a basic monster takes its turn. If you can see it, it can see you monster = self.owner if monster.visible and self.hostile: #move towards player if far away if monster.distance_to(player) >= 2: monster.move_towards(player.x, player.y) #close enough, attack! (if the player is still alive.) elif monster.fighter and player.fighter.hp > 0: #check for teachers if not self.check_teacher(): monster.fighter.attack(player) else: draw.message('The smart bully glances at the teacher and holds back.') else: monster.move_random()
def take_turn(self, player): #a basic monster takes its turn. If you can see it, it can see you monster = self.owner if monster.visible and self.hostile: #move towards player if far away if monster.distance_to(player) >= 2: monster.move_towards(player.x, player.y) #close enough, attack! (if the player is still alive.) elif monster.fighter and player.fighter.hp > 0: #check for teachers if not self.check_teacher(): monster.fighter.attack(player) else: draw.message( 'The smart bully glances at the teacher and holds back.' ) else: monster.move_random()
def monster_death(monster, leave_corpse=True): draw.message('the ' + monster.name.capitalize() + ' begins to cry.', libtcod.green) objects.score += (monster.fighter.power * 10) monster.fighter = None monster.ai = None # chance to leave a random item behind if libtcod.random_get_int(0, 0, 5) == 5: objects.generate_random_item(monster.x, monster.y) #transform it into a nasty corpse! it doesn't block, can't be #attacked and doesn't move if leave_corpse: monster.color = libtcod.dark_grey monster.blocks = False monster.fighter = None monster.ai = None monster.name = 'crying ' + monster.name monster.send_to_back() else: monster.kill()
def monster_death(monster, leave_corpse=True): draw.message('the ' + monster.name.capitalize() + ' begins to cry.', libtcod.green) objects.score += (monster.fighter.power * 10) monster.fighter = None monster.ai = None # chance to leave a random item behind if libtcod.random_get_int(0, 0, 5) == 5: objects.generate_random_item(monster.x, monster.y) #transform it into a nasty corpse! it doesn't block, can't be #attacked and doesn't move if leave_corpse: monster.color = libtcod.dark_grey monster.blocks = False monster.fighter = None monster.ai = None monster.name = 'crying ' + monster.name monster.send_to_back() else: monster.kill()
def on_attack(self, attacker): if self.target is None: draw.message('The teacher says to knock it off!')
def talk_mom(): global player draw.message('Your mom strokes your head.') draw.message( '\"Looks like you\'ve been having fun! Are you ready to leave (y/n)?') player.talking = True
def on_damage(self, attacker): if self.target is None: draw.message('The teacher says, \'that\'s enough.\'') self.target = attacker
def on_give(self): draw.message('The teacher says, \'I\'m so glad to see you sharing!\'', libtcod.green) objects.score += 10
def handle_keys(): global playerx, playery, game_state key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE or game_state == 'dead': return 'exit' #exit game #check for player death if (not objects.player.alive): game_state = 'dead' return 'died' if (objects.player.talking): game_state = 'talking' #test for other keys key_char = chr(key.c) if key_char == 'y' and libtcod.console_is_key_pressed(key): game_state = 'won' objects.add_toys_to_score() return 'won' elif key_char == 'n' and libtcod.console_is_key_pressed(key): game_state = 'playing' return 'didnt-take-turn' #movement keys if game_state == 'playing': if libtcod.console_is_key_pressed(libtcod.KEY_KP7): objects.player_actions(-1,-1) elif libtcod.console_is_key_pressed(libtcod.KEY_UP) or libtcod.console_is_key_pressed(libtcod.KEY_KP8): objects.player_actions(0,-1) elif libtcod.console_is_key_pressed(libtcod.KEY_KP9): objects.player_actions(1,-1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN) or libtcod.console_is_key_pressed(libtcod.KEY_KP2): objects.player_actions(0,1) elif libtcod.console_is_key_pressed(libtcod.KEY_KP1): objects.player_actions(-1,1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT) or libtcod.console_is_key_pressed(libtcod.KEY_KP4): objects.player_actions(-1,0) elif libtcod.console_is_key_pressed(libtcod.KEY_KP3): objects.player_actions(1,1) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT) or libtcod.console_is_key_pressed(libtcod.KEY_KP6): objects.player_actions(1,0) elif libtcod.console_is_key_pressed(libtcod.KEY_KP5): objects.player_actions(0,0) else: #test for other keys key_char = chr(key.c) if key_char == 'c' and libtcod.console_is_key_pressed(key): #pick up an item objects.collect_item() return 'collected-item' elif key_char == '.' and libtcod.console_is_key_pressed(key): objects.player_actions(0,0) elif key_char == 'e' and libtcod.console_is_key_pressed(key): objects.eat_candy() return 'ate-candy' elif key_char == 'g' and libtcod.console_is_key_pressed(key): draw.message('Press a direction key to give the toy:') game_state = 'giving-toy' return 'didnt-take-turn' elif key_char == 'h' and libtcod.console_is_key_pressed(key): draw.instructions('Key commands', ['','Press arrow keys or numpad to move.','','c: pick up candy and toys.','e: eat candy to regain health', 'g: give a toy to another child','esc: quit','','If the teacher sees you, you\'ll get in trouble.','','Giving away your toys calms your enemies down.'], 50) return 'didnt-take-turn' else: return 'didnt-take-turn' if game_state == 'giving-toy': if libtcod.console_is_key_pressed(libtcod.KEY_KP7): objects.give_toy(-1,-1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_UP) or libtcod.console_is_key_pressed(libtcod.KEY_KP8): objects.give_toy(0,-1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP9): objects.give_toy(1,-1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN) or libtcod.console_is_key_pressed(libtcod.KEY_KP2): objects.give_toy(0,1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP1): objects.give_toy(-1,1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT) or libtcod.console_is_key_pressed(libtcod.KEY_KP4): objects.give_toy(-1,0) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP3): objects.give_toy(1,1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT) or libtcod.console_is_key_pressed(libtcod.KEY_KP6): objects.give_toy(1,0) game_state = 'playing' else: return 'didnt-take-turn'
def give_toy(x,y): global inventory, score target = get_target(x,y) if target is not None: if not target.fighter: draw.message('The ' + target.name + ' doesn\'t want it.') else: for thing in inventory: if thing.item.type == 'toy': draw.message('You give the ' + thing.name + ' to the ' + target.name + '.') if target.ai.hostile: draw.message('The ' + target.name + ' calms down.') score += 5 target.ai.hostile = False else: draw.message('The ' + target.name + ' says \'thank you\'') inventory.remove(thing) return draw.message('You have no toys to give away.') else: draw.message('There\'s no one to give a toy to.')
def on_damage(self, attacker): if self.target is None: draw.message('The teacher says, \'that\'s enough.\'') self.target = attacker
def talk_mom(): global player draw.message('Your mom strokes your head.') draw.message('\"Looks like you\'ve been having fun! Are you ready to leave (y/n)?') player.talking = True
def on_punish(self): draw.message('The ' + self.owner.name + ' calms down.') self.hostile = False
def on_punish(self): draw.message('The ' + self.owner.name + ' calms down.') self.hostile = False
def handle_keys(): global playerx, playery, game_state key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE or game_state == 'dead': return 'exit' #exit game #check for player death if (not objects.player.alive): game_state = 'dead' return 'died' if (objects.player.talking): game_state = 'talking' #test for other keys key_char = chr(key.c) if key_char == 'y' and libtcod.console_is_key_pressed(key): game_state = 'won' objects.add_toys_to_score() return 'won' elif key_char == 'n' and libtcod.console_is_key_pressed(key): game_state = 'playing' return 'didnt-take-turn' #movement keys if game_state == 'playing': if libtcod.console_is_key_pressed(libtcod.KEY_KP7): objects.player_actions(-1, -1) elif libtcod.console_is_key_pressed( libtcod.KEY_UP) or libtcod.console_is_key_pressed( libtcod.KEY_KP8): objects.player_actions(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_KP9): objects.player_actions(1, -1) elif libtcod.console_is_key_pressed( libtcod.KEY_DOWN) or libtcod.console_is_key_pressed( libtcod.KEY_KP2): objects.player_actions(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_KP1): objects.player_actions(-1, 1) elif libtcod.console_is_key_pressed( libtcod.KEY_LEFT) or libtcod.console_is_key_pressed( libtcod.KEY_KP4): objects.player_actions(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_KP3): objects.player_actions(1, 1) elif libtcod.console_is_key_pressed( libtcod.KEY_RIGHT) or libtcod.console_is_key_pressed( libtcod.KEY_KP6): objects.player_actions(1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_KP5): objects.player_actions(0, 0) else: #test for other keys key_char = chr(key.c) if key_char == 'c' and libtcod.console_is_key_pressed(key): #pick up an item objects.collect_item() return 'collected-item' elif key_char == '.' and libtcod.console_is_key_pressed(key): objects.player_actions(0, 0) elif key_char == 'e' and libtcod.console_is_key_pressed(key): objects.eat_candy() return 'ate-candy' elif key_char == 'g' and libtcod.console_is_key_pressed(key): draw.message('Press a direction key to give the toy:') game_state = 'giving-toy' return 'didnt-take-turn' elif key_char == 'h' and libtcod.console_is_key_pressed(key): draw.instructions('Key commands', [ '', 'Press arrow keys or numpad to move.', '', 'c: pick up candy and toys.', 'e: eat candy to regain health', 'g: give a toy to another child', 'esc: quit', '', 'If the teacher sees you, you\'ll get in trouble.', '', 'Giving away your toys calms your enemies down.' ], 50) return 'didnt-take-turn' else: return 'didnt-take-turn' if game_state == 'giving-toy': if libtcod.console_is_key_pressed(libtcod.KEY_KP7): objects.give_toy(-1, -1) game_state = 'playing' elif libtcod.console_is_key_pressed( libtcod.KEY_UP) or libtcod.console_is_key_pressed( libtcod.KEY_KP8): objects.give_toy(0, -1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP9): objects.give_toy(1, -1) game_state = 'playing' elif libtcod.console_is_key_pressed( libtcod.KEY_DOWN) or libtcod.console_is_key_pressed( libtcod.KEY_KP2): objects.give_toy(0, 1) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP1): objects.give_toy(-1, 1) game_state = 'playing' elif libtcod.console_is_key_pressed( libtcod.KEY_LEFT) or libtcod.console_is_key_pressed( libtcod.KEY_KP4): objects.give_toy(-1, 0) game_state = 'playing' elif libtcod.console_is_key_pressed(libtcod.KEY_KP3): objects.give_toy(1, 1) game_state = 'playing' elif libtcod.console_is_key_pressed( libtcod.KEY_RIGHT) or libtcod.console_is_key_pressed( libtcod.KEY_KP6): objects.give_toy(1, 0) game_state = 'playing' else: return 'didnt-take-turn'
def on_attack(self, attacker): if self.target is None: draw.message('The teacher says to knock it off!')
def give_toy(x, y): global inventory, score target = get_target(x, y) if target is not None: if not target.fighter: draw.message('The ' + target.name + ' doesn\'t want it.') else: for thing in inventory: if thing.item.type == 'toy': draw.message('You give the ' + thing.name + ' to the ' + target.name + '.') if target.ai.hostile: draw.message('The ' + target.name + ' calms down.') score += 5 target.ai.hostile = False else: draw.message('The ' + target.name + ' says \'thank you\'') inventory.remove(thing) return draw.message('You have no toys to give away.') else: draw.message('There\'s no one to give a toy to.')
pygame.draw.rect(windowSurfaceObj, ORANGE, (int(WIDTH / 4), 2 * int(HEIGHT / 4) - 12, (int(WIDTH / 2)), (int(HEIGHT / 8))), 0) #(x,y,width,height) of rectangle pygame.draw.rect(windowSurfaceObj, RED, (int(WIDTH / 4), 3 * int(HEIGHT / 4) - 25, (int(WIDTH / 2)), (int(HEIGHT / 8))), 0) #(x,y,width,height) of rectangle menu_screen = False msg = 'Select a difficulty' msg2 = 'EZ MODE' msg3 = 'Meh' msg4 = 'INSANE' draw.message(msg, (WIDTH / 2), ((3 * HEIGHT) / 16), windowSurfaceObj, 32, BLACK) draw.message(msg2, (WIDTH / 2), int(HEIGHT / 3), windowSurfaceObj, 64, BLACK) draw.message(msg3, (WIDTH / 2), 2 * int(HEIGHT / 4) + 25, windowSurfaceObj, 64, BLACK) draw.message(msg4, (WIDTH / 2), 3 * int(HEIGHT / 4), windowSurfaceObj, 64, BLACK) # elif 8*int(WIDTH)/9 <= mouse_pos[0] <= (8*int(WIDTH)/9+int(WIDTH)/16) and 5*int(HEIGHT)/8 <= mouse_pos[1] <= (5*int(HEIGHT)/8 + int(HEIGHT)/8): # volume = volume + 0.1 # pygame.mixer.set_volume(volume) # print(pygame.mixer.music.get_volume()) # elif 8*int(WIDTH)/9 <= mouse_pos[0] <= (8*int(WIDTH)/9+int(WIDTH)/16) and (5*int(HEIGHT)/8+int(HEIGHT)/16) <= mouse_pos[1] <= (5*int(HEIGHT)/8 + int(HEIGHT)/8): # if volume != 0: # volume = volume - 0.1 # pygame.mixer.set_volume(volume) # print(pygame.mixer.music.get_volume())
def on_give(self): draw.message('The teacher says, \'I\'m so glad to see you sharing!\'', libtcod.green) objects.score += 10