My update open source code to Killworld, a text based python game. This is a quick and easy battle arena RPG game. You can compile the source code, modify and use freely as you want. It’s great for learning the basics of Python too.
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | class Player: def __init__(self, newName, newMoney): self.name = newName self.money = newMoney # Player has no weapon intiaially self.weapon = None self.health = 100 self.power = 20 def buyTrain(self): self.money = self.money - 25 def gainPower(self, gain): self.power = self.power + gain def restoreHealth(self): self.money = self.money - 25 self.health = 100 def buyWeapon(self, weapon, price): self.money = self.money - price self.weapon = weapon # Provide a string representing the player def __repr__(self): result = "Player Name: " + self.name + " | " " Money: " + str(self.money) + " | " " Health: " + str(self.health) + " | Power: " + str(self.power) print("") # "\n" simply produces a newline when it is printed return result + "\n" + "Has Weapon: " + str(self.weapon) return "" class Killworld: def Startup(): # This is the startup to the game, will also include story print("") print("> Welcome to KillWorld!") print("> You will fight in a sacred battle arena, a brutal fight for life!") input("Press enter to continue...") print("") while game <= True: print("") print("> Type what you wish to do from the list below...") print("battle | train | shop | medic | stats | end game") print("") select = input("What do you wish to do? : ") print("") if select == "battle": Killworld.battle() elif select == "train": Killworld.train() elif select == "shop": Killworld.shop() elif select == "medic": Killworld.medic() elif select == "stats": Killworld.stats() elif select == "end game": print("") print("##################") print("# GAME OVER #") print("##################") print("") break else: print("Select an option") def run(): print(" __ __ __ ") print("|__/ | | | | | / \ |__) | | \ ") print("| \ | |___ |___ |/\| \__/ | \ |___ |__/ ") print("") print("by Ronald A. Richardson") print("www.ronaldarichardson.com") print("Copyright Killworld 2010") print("") print("#####################################################################") print("") def gameover(): if player.health <= 0: print("Weak, need to heal") if player.money <= 0: gameover = 1 def battle(): print("> The battle arena is split into five realms of fatal fury!") print("") print("> Type what realm of the battle arena you wish to start from") print("earth-realm | dragon-realm | matrix | dienasty-realm | underworld") print("") slevel = input("What realm do you wish to battle in? : ") print("") print("You have selected to fight in the", slevel) if slevel == "underworld": if thedevil.health <= 0: print("You have already defeated "+ thedevil.name +"") else: Realms.underworld() elif slevel == "dienasty-realm": if davidlopan.health <= 0: print("You have already defeated "+ davidlopan.name +"") else: Realms.dienasty() elif slevel == "matrix": if smith.health <= 0: print("You have already defeated "+ smith.name +"") else: Realms.matrix() elif slevel == "earth-realm": if sweettooth.health <= 0: print("You have already defeated "+ sweettooth.name +"") else: Realms.earthrealm() elif slevel == "dragon-realm": if liukang.health <= 0: print("You have already defeated "+ liukang.name +"") else: Realms.dragonrealm() else: print("Select an option") def shop(): print("> Welcome to the shop, here you can buy a weapon") print("") print("> malice | nunchucks | sword | staff") wbuy = input("Type the weapon you wish to purchase? : ") print("") if wbuy == "malice": print("The malice costs 10 moneys") print("You have "+ str(player.money) +"") fbuy = input("Do you wish to buy? y/n: ") if fbuy == "y": if player.money >= 10: Player.buyWeapon(player, "Malice", 10) print("") print("> You have just purchased and equiped "+ player.weapon +"") elif player.money < 10: print("You do not have enough moneys") elif fbuy == "n": print("You exit the shop") if wbuy == "nunchucks": print("The nunchucks costs 15 moneys") print("You have "+ str(player.money) +"") fbuy = input("Do you wish to buy? y/n: ") if fbuy == "y": if player.money >= 15: Player.buyWeapon(player, "Nun Chucks", 15) print("") print("> You have just purchased and equiped "+ player.weapon +"") elif player.money < 15: print("You do not have enough moneys") elif fbuy == "n": print("You exit the shop") if wbuy == "sword": print("The sword costs 25 moneys") print("You have "+ str(player.money) +"") fbuy = input("Do you wish to buy? y/n: ") if fbuy == "y": if player.money >= 25: Player.buyWeapon(player, "Sword", 25) print("") print("> You have just purchased and equiped "+ player.weapon +"") elif player.money < 25: print("You do not have enough moneys") elif fbuy == "n": print("You exit the shop") if wbuy == "staff": print("The staff costs 5 moneys") print("You have "+ str(player.money) +"") fbuy = input("Do you wish to buy? y/n: ") if fbuy == "y": if player.money >= 5: Player.buyWeapon(player, "Staff", 5) print("") print("> You have just purchased and equiped "+ player.weapon +"") elif player.money < 5: print("You do not have enough moneys") elif fbuy == "n": print("You exit the shop") else: print("Select a weapon to buy first.") def stats(): print(player) def train(): print("> Welcome to the training dome, here you can increase in power points") print("> You currently have "+ str(player.money) +" moneys") print("") train = input("It cost 25 moneys to train your player, do wish to train your player? y/n: ") if train >= "y": if player.money >= 25: print(" - 25 moneys lost") print("You train for 20 days, and gain + 5 power points") Player.buyTrain(player) Player.gainPower(player, 5) elif player.money < 25: print("You do not have enough moneys") elif train >= "n": print("Very well.") def medic(): print("> Welcome to the medic, here you can heal your player") print("> You currently have "+ str(player.money) +" moneys") print("") med = input("It cost 25 moneys to heal your player, do you want to heal? y/n: ") if med >= "y": if player.money >= 25: print(" - 25 moneys lost") print("Your player is attended to, and is at full health") Player.restoreHealth(player) elif player.money < 25: print("You do not have enough moneys") else: print("Very well.") def inventory(): print("> This is your inventory, here you can equip different weapons") if underworldUnlock == 1: print(""+ thedevil.weapon +"") elif dienastyUnlock == 1: print(""+ davidlopan.weapon +"") elif matrixUnlock == 1: print(""+ smith.weapon +"") elif earthUnlock == 1: print(""+ sweettooth.weapon +"") elif dragonUnlock == 1: print(""+ liukang.weapon +"") else: print("Inventory Empty") class Enemy: def __init__(self, enName, enWeapon, enHealth, enPower): self.name = enName self.weapon = enWeapon self.health = enHealth self.power = enPower # Provide a string representing the enemy def __repr__(self): result = "Enemy Name: " + self.name + " | " " Health: " + str(self.health) + " | Power: " + str(self.power) print("") # "\n" simply produces a newline when it is printed return result + "\n" + "Has Weapon: " + str(self.weapon) return "" sweettooth = Enemy("Sweet Tooth", "Bare Fist", 110, 10) liukang = Enemy("Liu Kang", "Dragon Fire", 120, 30) smith = Enemy("Agent Smith", "Computer Hacking Skills", 130, 40) davidlopan = Enemy("David Lo Pan", "Ancient Chinese Secrete", 140, 49) thedevil = Enemy("The Devil", "Hell Triton", 150, 80) class Realms: def __init__(self, rName, rEnemy): self.name = rName def underworld(): print("> Welcome to the Underworld, prepare to meet your oponent.") print("") print("Your enemy is "+ thedevil.name +"") print("") print("> Welcome to HELL PUSSY!!! MUAHAHAHAHAHAHA HAAAA!!") print("") viewstats = input("Would you like to view your oponents stats? y/n: ") if viewstats == "y": print(thedevil) print("") elif viewstats == "n": print("") input("> Press enter to continue...") print("") battleon = input("The battle is about begin, do you wish to battle? y/n: ") if battleon == "y": while thedevil.health > 0: print("") print("You square up to your opponent "+ thedevil.name +", what will you do?") print("") pturn = input(">> Attack or Defend? a/d: ") if pturn == "a": print("") print("#####################################################################") print("#") print("# "+ player.name +" attacks "+ thedevil.name +" for "+ str(player.power) +" ammount of damage using his "+ str(player.weapon) +"") thedevil.health = thedevil.health - player.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ thedevil.name +" : "+ str(thedevil.health) +" HP") print("#") print("#####################################################################") print("#") print("# "+ thedevil.name +" then attacks you for "+ str(thedevil.power) +" ammount of damage using his "+ thedevil.weapon +"") player.health = player.health - thedevil.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ thedevil.name +" : "+ str(thedevil.health) +" HP") print("#") print("#####################################################################") if pturn == "d": print("") print("#####################################################################") print("#") print("# "+ player.name +" defends against "+ thedevil.name +" and his "+ thedevil.weapon +"") player.health = player.health - (thedevil.power - 15) print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ thedevil.name +" : "+ str(thedevil.health) +" HP") print("#") print("#####################################################################") if player.health <= 0: print("") print("> You died, BATTLE OVER!") print("") print("> You can restore your health by going to the medic") thedevil.health = -10 elif thedevil.health <= 0: print("") print("> You have defeated "+ thedevil.name +" for this you gain 10 power points and obtain the "+ thedevil.weapon +"") player.power = player.power + 10 player.weapon = thedevil.weapon player.money = player.money + 10 underworldUnlock = 1 player.health = player.health + 20 elif battleon == "n": print("") input("> Press enter to continue...") else: print("Select an option.") def dienasty(): print("> Welcome to the Dienasty Realm , prepare to meet your oponent.") print("") print("Your enemy is the uber-powerful "+ davidlopan.name +"") print("") print(""+ davidlopan.name +": Shut up, Mr. Burton! You are not brought upon this world to get it! ") print("") viewstats = input("Would you like to view your oponents stats? y/n: ") if viewstats == "y": print(davidlopan) print("") elif viewstats == "n": print("") input("> Press enter to continue...") print("") battleon = input("The battle is about begin, do you wish to battle? y/n: ") if battleon == "y": while davidlopan.health > 0: print("") print("You square up to your opponent "+ davidlopan.name +", what will you do?") print("") pturn = input(">> Attack or Defend? a/d: ") if pturn == "a": print("") print("#####################################################################") print("#") print("# "+ player.name +" attacks "+ davidlopan.name +" for "+ str(player.power) +" ammount of damage using his "+ str(player.weapon) +"") davidlopan.health = davidlopan.health - player.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ davidlopan.name +" : "+ str(davidlopan.health) +" HP") print("#") print("#####################################################################") print("#") print("# "+ davidlopan.name +" then attacks you for "+ str(davidlopan.power) +" ammount of damage using his "+ davidlopan.weapon +"") player.health = player.health - davidlopan.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ davidlopan.name +" : "+ str(davidlopan.health) +" HP") print("#") print("#####################################################################") if pturn == "d": print("") print("#####################################################################") print("#") print("# "+ player.name +" defends against "+ davidlopan.name +" and his "+ davidlopan.weapon +"") player.health = player.health - (davidlopan.power - 15) print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ davidlopan.name +" : "+ str(davidlopan.health) +" HP") print("#") print("#####################################################################") if player.health <= 0: print("") print("> You died, BATTLE OVER!") print("") print("> You can restore your health by going to the medic") davidlopan.health = -10 elif davidlopan.health <= 0: print("") print("> You have defeated "+ davidlopan.name +" for this you gain 10 power points and obtain the "+ davidlopan.weapon +"") player.power = player.power + 10 player.weapon = davidlopan.weapon player.money = player.money + 200 underworldUnlock = 1 player.health = player.health + 20 elif battleon == "n": print("") input("> Press enter to continue...") else: print("Select an option.") def matrix(): print("> Welcome to the Matrix, prepare to meet your oponent.") print("") print("Your enemy is "+ smith.name +"") print("") print(""+ smith.name +": It seems that you've been living two lives. One life, you're Thomas A. Anderson, program writer for a respectable software company.") print("You have a social security number, pay your taxes, and you... help your landlady carry out her garbage.") print("The other life is lived in computers, where you go by the hacker alias Neo and are guilty of virtually every computer crime we have a law for.") print("One of these lives has a future, and one of them does not. ") print("") print(""+ smith.name +": We can't all be the one "+ player.name +"") print("") viewstats = input("Would you like to view your oponents stats? y/n: ") if viewstats == "y": print(smith) print("") elif viewstats == "n": print("") input("> Press enter to continue...") print("") battleon = input("The battle is about begin, do you wish to battle? y/n: ") if battleon == "y": while smith.health > 0: print("") print("You square up to your opponent "+ smith.name +", what will you do?") print("") pturn = input(">> Attack or Defend? a/d: ") if pturn == "a": print("") print("#####################################################################") print("#") print("# "+ player.name +" attacks "+ smith.name +" for "+ str(player.power) +" ammount of damage using his "+ str(player.weapon) +"") smith.health = smith.health - player.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ smith.name +" : "+ str(smith.health) +" HP") print("#") print("#####################################################################") print("#") print("# "+ smith.name +" then attacks you for "+ str(smith.power) +" ammount of damage using his "+ smith.weapon +"") player.health = player.health - smith.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ smith.name +" : "+ str(smith.health) +" HP") print("#") print("#####################################################################") if pturn == "d": print("") print("#####################################################################") print("#") print("# "+ player.name +" defends against "+ smith.name +" and his "+ smith.weapon +"") player.health = player.health - (smith.power - 15) print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ smith.name +" : "+ str(smith.health) +" HP") print("#") print("#####################################################################") if player.health <= 0: print("") print("> You died, BATTLE OVER!") print("") print("> You can restore your health by going to the medic") smith.health = -10 elif smith.health <= 0: print("") print("> You have defeated "+ smith.name +" for this you gain 10 power points and obtain the "+ smith.weapon +"") player.power = player.power + 10 player.weapon = smith.weapon player.money = player.money + 110 underworldUnlock = 1 player.health = player.health + 20 elif battleon == "n": print("") input("> Press enter to continue...") else: print("Select an option.") def earthrealm(): print("> Welcome to the Earth Realm, prepare to meet your oponent.") print("") print("Your enemy is "+ sweettooth.name +"") print("") print(""+ sweettooth.name +": When they captured me, all I could think was, What a waste! All those people I hadn't killed yet.") print("Come the night of my execution, there must have been over a thousand people gathered outside to watch me fry. I was upset about that.") print("There should have been more. ") print("") print(""+ sweettooth.name +": Now... Shut up and BLEED, you motherfucker! ") print("") viewstats = input("Would you like to view your oponents stats? y/n: ") if viewstats == "y": print(sweettooth) print("") elif viewstats == "n": print("") input("> Press enter to continue...") print("") battleon = input("The battle is about begin, do you wish to battle? y/n: ") if battleon == "y": while sweettooth.health > 0: print("") print("You square up to your opponent "+ sweettooth.name +", what will you do?") print("") pturn = input(">> Attack or Defend? a/d: ") if pturn == "a": print("") print("#####################################################################") print("#") print("# "+ player.name +" attacks "+ sweettooth.name +" for "+ str(player.power) +" ammount of damage using his "+ str(player.weapon) +"") sweettooth.health = sweettooth.health - player.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ sweettooth.name +" : "+ str(sweettooth.health) +" HP") print("#") print("#####################################################################") print("#") print("# "+ sweettooth.name +" then attacks you for "+ str(sweettooth.power) +" ammount of damage using his "+ sweettooth.weapon +"") player.health = player.health - sweettooth.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ sweettooth.name +" : "+ str(sweettooth.health) +" HP") print("#") print("#####################################################################") if pturn == "d": print("") print("#####################################################################") print("#") print("# "+ player.name +" defends against "+ sweettooth.name +" and his "+ sweettooth.weapon +"") player.health = player.health - (sweettooth.power - 15) print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ sweettooth.name +" : "+ str(sweettooth.health) +" HP") print("#") print("#####################################################################") if player.health <= 0: print("") print("> You died, BATTLE OVER!") print("") print("> You can restore your health by going to the medic") sweettooth.health = -10 elif sweettooth.health <= 0: print("") print("> You have defeated "+ sweettooth.name +" for this you gain 10 power points and obtain the "+ sweettooth.weapon +"") player.power = player.power + 10 player.weapon = sweettooth.weapon player.money = player.money + 80 underworldUnlock = 1 player.health = player.health + 20 elif battleon == "n": print("") input("> Press enter to continue...") else: print("Select an option.") def dragonrealm(): print("> Welcome to the Dragon Realm, prepare to meet your oponent.") print("") print("Your enemy is "+ liukang.name +"") print("") print(""+ liukang.name +": No! You'll fight me. I am Liu Kang, descendent of Kung Lao. I challenge you to Mortal Kombat! ") print("") viewstats = input("Would you like to view your oponents stats? y/n: ") if viewstats == "y": print(liukang) print("") elif viewstats == "n": print("") input("> Press enter to continue...") print("") battleon = input("The battle is about begin, do you wish to battle? y/n: ") if battleon == "y": while liukang.health > 0: print("") print("You square up to your opponent "+ liukang.name +", what will you do?") print("") pturn = input(">> Attack or Defend? a/d: ") if pturn == "a": print("") print("#####################################################################") print("#") print("# "+ player.name +" attacks "+ liukang.name +" for "+ str(player.power) +" ammount of damage using his "+ str(player.weapon) +"") liukang.health = liukang.health - player.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ liukang.name +" : "+ str(liukang.health) +" HP") print("#") print("#####################################################################") print("#") print("# "+ liukang.name +" then attacks you for "+ str(liukang.power) +" ammount of damage using his "+ liukang.weapon +"") player.health = player.health - liukang.power print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ liukang.name +" : "+ str(liukang.health) +" HP") print("#") print("#####################################################################") if pturn == "d": print("") print("#####################################################################") print("#") print("# "+ player.name +" defends against "+ liukang.name +" and his "+ liukang.weapon +"") player.health = player.health - (liukang.power - 15) print("#") print("# "+ player.name +" : "+ str(player.health) +" HP / "+ liukang.name +" : "+ str(liukang.health) +" HP") print("#") print("#####################################################################") if player.health <= 0: print("") print("> You died, BATTLE OVER!") print("") print("> You can restore your health by going to the medic") liukang.health = -10 elif liukang.health <= 0: print("") print("> You have defeated "+ liukang.name +" for this you gain 10 power points and obtain the "+ liukang.weapon +"") player.power = player.power + 10 player.weapon = liukang.weapon player.money = player.money + 100 underworldUnlock = 1 player.health = player.health + 20 elif battleon == "n": print("") input("> Press enter to continue...") else: print("Select an option.") Killworld.run() player = Player(input("Enter a name for your character: "), 100) print(player) game = True Killworld.Startup() |
Leave a Reply