# Python Win 7
RPG Systems
Simple Battle System 1
Brief:
_______________________________________________________________
A very short series made to help classmates in CS100 back in 2013. This is a
revamped version with classes instead of dictionaries. And a few additions to
the code.
Functionality :
_______________________________________________________________
Improvements:
- N/A
Issues:
- N/A
Demo:
_______________________________________________________________
Code snippet:
_______________________________________________________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
from random import randint class character(): def __init__(self, **keywargs): self.name = keywargs['name'] if 'name' in keywargs else "Hero" self.lvl = keywargs['lvl'] if 'lvl' in keywargs else 1 self.xp = keywargs['xp'] if 'xp' in keywargs else 0 self.lvl_next = keywargs['lvl_next'] if 'lvl_next' in keywargs else 25 self.str = keywargs['str'] if 'str' in keywargs else 1 self.dex = keywargs['dex'] if 'dex' in keywargs else 1 self.int = keywargs['int'] if 'int' in keywargs else 1 self.hp = keywargs['hp'] if 'hp' in keywargs else 30 self.atk = keywargs['atk'] if 'atk' in keywargs else [5, 12] self.xp_reward = keywargs['xp_reward'] if 'xp_reward' in keywargs else 25 self.loot_reward = keywargs['loot_reward'] if 'loot_reward' in keywargs else ("gold", 3) def update_level(self): if self.xp < self.lvl_next: return None new_str, new_dex, new_int = 0, 0, 0 init_lvl = self.lvl # Updated code: # Differences in accessing values # char is now self # Class vs Dictionary # self.xp vs. char['xp'] # V V # 25+ 25+ while self.xp >= self.lvl_next: self.lvl += 1 self.xp = self.xp - self.lvl_next self.lvl_next = round(self.lvl_next * 1.5) new_str += 1 new_dex += 1 new_int += 1 print() lvl_gained = self.lvl - init_lvl plural = "level" if 1 < lvl_gained: plural = "levels" print(f"{self.name} has gained {lvl_gained} {plural}!") print("====================") print(f"{self.name} reached level {self.lvl}!") # The assignment as I gave it last lesson: # str, dex, and int are now accessed through self print(f"STR {self.str} +{new_str}", f"DEX {self.dex} +{new_dex}", f"INT {self.int} +{new_int}" ) print("====================") print() self.str += new_str self.dex += new_dex self.int += new_int def attack(self): # *atk unppacks the list # atk = [5, 12] # *atk = 5, 12. the brackets are # dropped leaving a sequence of # integers instead of a list # so instead of randint(attacker.atk[0], attacker.atk[1]) return randint(*self.atk) def take_damage(self, damage): self.hp = self.hp - damage # 0 is immutable so if we type # >= instead of = python will # give an error instead of # silently assigning health to zero if 0 >= self.hp: print(f"{self.name} has been slain") # attacker.xp += self.xp_reward # update_level(attacker) # input("-Press any Key to quit-") return self.xp_reward else: print(f"{self.name} takes {damage}!") return 0 def is_alive(self): return 0 < self.hp def combat(attacker, defender): xp_yield = defender.take_damage(attacker.attack()) if xp_yield: # 0 is False attacker.xp += xp_yield attacker.update_level() def game_loop(player, enemy): playing = True while playing: print('--------------------') cmd = input("Do you want to attack? yes/no: ").lower() if cmd in 'yes': combat(player, enemy) elif cmd in 'no': print(f"{enemy.name} takes the opportunity to attack!") combat(enemy, player) else: pass if False == enemy.is_alive(): playing = False if False == player.is_alive(): playing = False hero = character( name="hero", lvl=1, xp=0, lvl_next=25, str=1, dex=1, int=1, hp=30, atk=[5, 12], xp_reward=25, loot_reward=("gold", 2) ) imp = character( name="imp", lvl=1, xp=0, lvl_next=25, str=1, dex=1, int=1, hp=30, atk=[5, 12], xp_reward=25, loot_reward=("gold", 6) ) game_loop(hero, imp) |
Credits & Resources:
- CS 100 - Tm R.
- Code formatted via: http://hilite.me/ : Styling: Python, monokai
No comments:
Post a Comment
Conduct: Be nice and don't advertise or plug without adding value to the conversation.