Advertisement
Guest User

Untitled

a guest
Jun 13th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.60 KB | None | 0 0
  1. import cgi
  2. import webapp2
  3. import random
  4. import time
  5.  
  6.  
  7. from google.appengine.api import users
  8.  
  9.  
  10. ##BEGIN POKEMON CLASSES & FUNCTIONS
  11. class pokemon(object):
  12. def __init__(self, name, pokemonType, health, moves, attack, defense, speed):
  13. self.name = name
  14. self.type = pokemonType
  15. self.health = health
  16. self.moves = moves
  17. self.attack = attack
  18. self.defense = defense
  19. self.speed = speed
  20. self.accuracy = 1
  21.  
  22. def isAlive(self):
  23. if self.health > 0:
  24. return True
  25. else:
  26. return False
  27.  
  28. def useMove(self, opponent, move):
  29. chance = random.random()
  30. chance *= self.accuracy
  31. if chance > move.accuracy:
  32. return ['missed']
  33. elif chance <= move.accuracy:
  34. #hit
  35. if type(move.effect) == list:
  36. if move.effect[0] == 'self':
  37. stat = move.effect[1]
  38. if stat == 'attack':
  39. self.attack *= move.effect[2]
  40. return ['hit', 'status', 'self', 'attack']
  41. if stat == 'defense':
  42. self.defense *= move.effect[2]
  43. return ['hit', 'status', 'self', 'defense']
  44. if stat == 'speed':
  45. self.speed *= move.effect[2]
  46. return ['hit', 'status', 'self', 'speed']
  47. if stat == 'accuracy':
  48. self.accuracy *= move.effect[2]
  49. return ['hit', 'status', 'self', 'accuracy']
  50. elif move.effect[0] == 'other':
  51. stat = move.effect[1]
  52. if stat == 'attack':
  53. opponent.active.attack *= move.effect[2]
  54. return ['hit', 'status', 'other', 'attack']
  55. if stat == 'defense':
  56. opponent.active.attack *= move.effect[2]
  57. return ['hit', 'status', 'other', 'defense']
  58. if stat == 'speed':
  59. opponent.active.attack *= move.effect[2]
  60. return ['hit', 'status', 'other', 'speed']
  61. if stat == 'accuracy':
  62. return ['hit', 'status', 'other', 'accuracy']
  63. elif move.effect == False:
  64. toReturn = ['hit', 'physical']
  65. #apply attack and def stats
  66. dmgDone = ((float(move.damage) / opponent.active.defense) * self.attack)
  67. #critical hits
  68. critRate = self.speed * 100.0 / 512
  69. critRate = 100 - critRate
  70. critRoll = random.random()
  71. if critRoll > critRate:
  72. toReturn.append('crit')
  73. dmgDone *= 2
  74. elif critRoll < critRate:
  75. toReturn.append('noCrit')
  76. #check for super-effectiveness
  77. if isWeakTo(opponent.active, move):
  78. toReturn.append('super')
  79. dmgDone *= 2
  80. if isStrongTo(opponent.active, move):
  81. toReturn.append('ineffective')
  82. dmgDone *= .5
  83. else:
  84. toReturn.append('normal')
  85. opponent.active.health -= dmgDone
  86. toReturn.append(dmgDone)
  87. return toReturn
  88.  
  89.  
  90. class move(object):
  91. def __init__(self, name, moveType, damage, accuracy, effect=False):
  92. self.name = name
  93. self.type = moveType
  94. self.damage = damage
  95. self.accuracy = accuracy
  96. self.effect = effect
  97.  
  98. class character(object):
  99. def __init__(self, name, roster):
  100. self.name = name
  101. self.roster = roster
  102. self.active = 'nothing'
  103. self.living = self.listLiving()
  104. self.alive = len(self.living)
  105.  
  106. def checkLiving(self):
  107. self.alive = len(self.roster)
  108. for pokemon in self.roster:
  109. if not pokemon.isAlive():
  110. self.alive -= 1
  111. self.living = self.listLiving()
  112. if self.active != 'nothing':
  113. if self.active.health <= 0:
  114. print self.active.name, 'has died!'
  115. self.active = 'nothing'
  116.  
  117. def hasActive(self):
  118. if self.active != 'nothing':
  119. return True
  120. else:
  121. return False
  122.  
  123. def listLiving(self):
  124. living = []
  125. for pokemon in self.roster:
  126. if pokemon.health > 0:
  127. living.append(pokemon)
  128. return living
  129.  
  130. def printLiving(self):
  131. index = 0
  132. for pokemon in self.living:
  133. print index, pokemon.name
  134. index += 1
  135.  
  136.  
  137. def isWeakTo(pkmn, move):
  138. if pkmn.type in weakTo.keys():
  139. if move.type in weakTo[pkmn.type]:
  140. return True
  141. else:
  142. return False
  143.  
  144. def isStrongTo(pkmn, move):
  145. if pkmn.type in strongTo.keys():
  146. if move.type in strongTo[pkmn.type]:
  147. return True
  148. else:
  149. return False
  150.  
  151. def getHeroChoice(character):
  152. global gamePlaying
  153. character.checkLiving()
  154. if character.alive <= 0:
  155. print
  156. print
  157. print 'You have no more pokemon!'
  158. print 'You lose.'
  159. print
  160. print
  161. gamePlaying = False
  162. else:
  163. if character.hasActive():
  164. while True:
  165. choice = raw_input('Do you want to (l)ist pokemon, (c)hange pokemon, or (u)se a move?')
  166. if choice == 'l':
  167. character.printLiving()
  168. elif choice == 'c':
  169. #SELECT A POKEMON
  170. index = 0
  171. for pokemon in character.living:
  172. print index, ':', pokemon.name
  173. index += 1
  174. while True:
  175. choice = raw_input('Choose a pokemon to play.')
  176. try:
  177. choice = int(choice)
  178. except ValueError:
  179. print 'Not a valid choice.'
  180. if choice in range(0, len(character.living)):
  181. print character.name, 'chose', character.living[choice].name
  182. return ['c', character.living[choice]]
  183. elif choice == 'u':
  184. #SELECT A MOVE
  185. index = 0
  186. for move in character.active.moves:
  187. print index, move.name
  188. index += 1
  189. while True:
  190. choice = raw_input('Choose a move to use. (1, 2, 3, or 4)')
  191. choice = int(choice)
  192. if choice in range(0, 4):
  193. return ['u', character.active.moves[choice]]
  194. else:
  195. print 'Not a valid choice'
  196. elif not character.hasActive():
  197. #SELECT A POKEMON
  198. index = 0
  199. for pokemon in character.living:
  200. print index, ':', pokemon.name
  201. index += 1
  202. while True:
  203. choice = raw_input('Choose a pokemon to play.')
  204. try:
  205. choice = int(choice)
  206. except ValueError:
  207. print 'Not a valid choice'
  208. if choice in range(0, len(character.living)):
  209. print character.name, 'chose', character.living[choice].name
  210. return ['c', character.living[choice]]
  211.  
  212. def getAntagChoice(character):
  213. global gamePlaying
  214. character.checkLiving()
  215. if character.alive <= 0:
  216. print
  217. print
  218. print 'Your opponent has no more pokemon!'
  219. print 'You win.'
  220. print
  221. print
  222. gamePlaying = False
  223. else:
  224. if character.hasActive():
  225. moveRoll = random.randint(0, len(character.active.moves) - 1)
  226. return ['u', character.active.moves[moveRoll]]
  227. else:
  228. monsterRoll = random.randint(0, (len(character.living) - 1))
  229. return ['c', character.living[monsterRoll]]
  230.  
  231. ### END POKEMON CLASSES AND FUNCTIONS
  232.  
  233. # BEGIN POKEMON DATA AND CLASS INSTANTIATIONS
  234. weakTo = {'fire': ['water'], 'water': ['grass'], 'grass': ['fire', 'flying'], 'normal': ['none']}
  235. strongTo = {'fire': ['grass'], 'water': ['fire'], 'grass': ['water', 'flying'], 'normal': ['none']}
  236.  
  237.  
  238. #Moves: Name, type, power, accuracy, effect[self/other, stat, amount]=False
  239. Tackle = move('tackle', 'normal', 50, .85)
  240. Ember = move('ember', 'fire', 40, 1)
  241. Bubble = move('bubble', 'water', 20, 1)
  242. Scratch = move('scratch', 'normal', 40, 1)
  243. Gust = move('gust', 'flying', 40, 1)
  244. Growl = move('growl', 'normal', 0, 1, ['other', 'attack', .5])
  245. TailWhip = move('tail whip', 'normal', 0, 1, ['other', 'defense', .5])
  246. SandAttack = move('sand attack', 'normal', 0, 1, ['other', 'accuracy', .5])
  247.  
  248. #Pokemon: Name, type, health, moves(list), attack, defense, speed
  249. AshPidgey = pokemon('Pidgey', 'flying', 40, [Tackle, Gust, SandAttack], 45, 40, 56)
  250. AshRattata = pokemon('Rattata', 'normal', 30, [Tackle, TailWhip], 56, 35, 72)
  251. AshCharmander = pokemon('Charmander', 'fire', 39, [Ember, Scratch, Growl], 52, 43, 65)
  252. ashRoster = [AshCharmander, AshPidgey, AshRattata]
  253.  
  254. GaryPidgey = pokemon('Pidgey', 'flying', 40, [Tackle, Gust, SandAttack], 45, 40, 56)
  255. GaryRattata = pokemon('Rattata', 'normal', 30, [Tackle, TailWhip], 56, 35, 72)
  256. GarySquirtle = pokemon('Squirtle', 'water', 44, [Bubble, Tackle, TailWhip], 48, 65, 43)
  257. garyRoster = [GaryPidgey, GaryRattata, GarySquirtle]
  258.  
  259.  
  260. Ash = character('Ash', ashRoster)
  261. Gary = character('Gary', garyRoster)
  262.  
  263.  
  264. hero = Ash
  265. antag = Gary
  266.  
  267. pokeList = [AshPidgey, AshRattata]
  268.  
  269. ### END POKEMONE DATA AND CLASS INSTANTIATIONS
  270.  
  271. ### BEGIN GOOGLE APP ENGINE HANDLER CLASSES
  272.  
  273. #Selection variable determines which request is used. Can be 'main', 'change', 'use', or 'buffer'
  274. selection = ''
  275.  
  276.  
  277. class MainPage(webapp2.RequestHandler):
  278. def get(self):
  279. global selection
  280. hero.checkLiving()
  281. pokeIndex = 0
  282. self.response.out.write("""<html><body>""")
  283. self.response.out.write('Hello ')
  284. self.response.out.write(hero.name)
  285. self.response.out.write('<br>')
  286. self.response.out.write('Living pokemon: ')
  287. self.response.out.write(hero.alive)
  288. self.response.out.write('<br>')
  289. self.response.out.write('Your available pokemon:')
  290. self.response.out.write('<br>')
  291. selection = 'main'
  292. for pkmn in hero.living:
  293. self.response.out.write(pokeIndex)
  294. self.response.out.write(' ')
  295. self.response.out.write(pkmn.name)
  296. self.response.out.write('<br>')
  297. pokeIndex += 1
  298. self.response.out.write('Choose a pokemon to play. (1, 2, or 3)')
  299. self.response.out.write("""
  300. <form action="/change" method="post">
  301. <div>
  302. <textarea name="mainChoice" rows="3" cols="60"></textarea>
  303. </div>
  304. <div>
  305. <input type="submit" value="Enter selection">
  306. </div>
  307. </form>
  308. </body>
  309. </html>""")
  310.  
  311.  
  312.  
  313. class ChangePage(webapp2.RequestHandler):
  314. def doMove(self, char, opponent, move):
  315. moveData = char.active.useMove(opponent, move)
  316. if moveData[0] == 'missed':
  317. self.response.out.write(char.active.name)
  318. self.response.out.write(' tried to use ')
  319. self.response.out.write(move.name)
  320. self.response.out.write(' but it missed.')
  321. self.response.out.write('<br>')
  322. if moveData[0] == 'hit':
  323. self.response.out.write(char.active.name)
  324. self.response.out.write(' used ')
  325. self.response.out.write(move.name)
  326. if moveData[1] == 'status':
  327. if moveData[2] == 'self':
  328. self.response.out.write("It's ")
  329. self.response.out.write(moveData[3])
  330. self.response.out.write(' increases.')
  331. self.response.out.write('<br>')
  332. if moveData[2] == 'other':
  333. self.response.out.write('It lowered the ')
  334. self.response.out.write(moveData[3])
  335. self.response.out.write(' of ')
  336. self.response.out.write(opponent.active.name)
  337. self.response.out.write('<br>')
  338. if moveData[1] == 'physical':
  339. if moveData[2] == 'crit':
  340. self.response.out.write('Crital hit!')
  341. self.response.out.write('<br>')
  342. if moveData[3] == 'super':
  343. self.response.out.write("It's super-effective!")
  344. self.response.out.write('<br>')
  345. if moveData[3] == 'ineffective':
  346. self.response.out.write("It's not very effective...")
  347. self.response.out.write('<br>')
  348. self.response.out.write('It does ')
  349. self.response.out.write(moveData[4])
  350. self.response.out.write(' damage.')
  351.  
  352. def post(self):
  353. global selction
  354. if selection == 'main':
  355. heroChoice = str(self.request.get('mainChoice'))
  356. if selection == 'buffer':
  357. heroChoice = str(self.request.get('bufferChoice'))
  358. hero.checkLiving()
  359. self.response.out.write('<html><body>')
  360. self.response.out.write(hero.name)
  361. self.response.out.write('<br>')
  362. if hero.active != 'nothing':
  363. if hero.active.isAlive():
  364. self.response.out.write('Your active pokemon is ')
  365. self.response.out.write(hero.active.name)
  366. self.response.out.write('<br>')
  367. self.response.out.write("It's health is: ")
  368. self.response.out.write(hero.active.health)
  369. self.response.out.write('<br>')
  370. if hero.alive <= 0:
  371. self.response.out.write('You lose!')
  372. self.response.out.write('<br>')
  373. elif antag.alive <= 0:
  374. self.response.out.write('You win!')
  375. self.response.out.write('<br>')
  376. else:
  377. EM = getAntagChoice(antag)
  378. if EM[0] == 'c':
  379. hero.active = hero.living[int(heroChoice)]
  380. self.response.out.write('You chose ')
  381. self.response.out.write(hero.active.name)
  382. self.response.out.write('<br>')
  383. antag.active = EM[1]
  384. self.response.out.write(antag.name)
  385. self.response.out.write(' chose ')
  386. self.response.out.write(antag.active.name)
  387. self.response.out.write('<br>')
  388. if EM[0] == 'u':
  389. hero.active = hero.living[int(heroChoice)]
  390. self.response.out.write('You chose ')
  391. self.response.out.write(hero.active.name)
  392. self.response.out.write('<br>')
  393. #antag does move EM[1] == a randomly selected move object
  394. self.doMove(antag, hero, EM[1])
  395. hero.checkLiving()
  396. self.response.out.write('Your living pokemon: ')
  397. self.response.out.write(hero.alive)
  398. self.response.out.write('<br>')
  399. if hero.active != 'nothing':
  400. if hero.active.isAlive():
  401. self.response.out.write('Your active pokemon is ')
  402. self.response.out.write(hero.active.name)
  403. self.response.out.write('<br>')
  404. self.response.out.write("It's health is: ")
  405. self.response.out.write(hero.active.health)
  406. self.response.out.write('<br>')
  407. if hero.alive <= 0:
  408. self.response.out.write('You lose!')
  409. self.response.out.write('<br>')
  410. elif antag.alive <= 0:
  411. self.response.out.write('You win!')
  412. self.response.out.write('<br>')
  413. else:
  414. #Ask to change pkmn or use move
  415. selection = 'change'
  416. self.response.out.write('Do you want to (c)hange pokemon or do you want to (u)se a move?')
  417. self.response.out.write("""
  418. <form action="/buffer" method="post">
  419. <div>
  420. <textarea name="chooseChoice" rows="3" cols="60"></textarea>
  421. </div>
  422. <div>
  423. <input type="submit" value="Enter selection">
  424. </div>
  425. </form>
  426. </body>
  427. </html>""")
  428.  
  429.  
  430. class BufferPage(webapp2.RequestHandler):
  431. def post(self):
  432. global selection
  433. if selection == 'change':
  434. chooseChoice = self.request.get('chooseChoice')
  435. if chooseChoice == 'c':
  436. self.response.out.write('<html><body>')
  437. pokeIndex = 0
  438. self.response.out.write('Select a pokemon.')
  439. for pkmn in hero.living:
  440. self.response.out.write('<br>')
  441. self.response.out.write(pokeIndex)
  442. self.response.out.write(' ')
  443. self.response.out.write(pkmn.name)
  444. self.response.out.write('<br>')
  445. pokeIndex += 1
  446. selection = 'buffer'
  447. self.response.out.write("""
  448. <form action="/change" method="post">
  449. <div>
  450. <textarea name="bufferChoice" rows="3" cols="60"></textarea>
  451. </div>
  452. <div>
  453. <input type="submit" value="Enter selection">
  454. </div>
  455. </form>
  456. </body>
  457. </html>""")
  458. elif chooseChoice == 'u':
  459. self.response.out.write('<html><body>')
  460. pokeIndex = 0
  461. self.response.out.write('Select a move.')
  462. for move in hero.active.moves:
  463. self.response.out.write('<br>')
  464. self.response.out.write(pokeIndex)
  465. self.response.out.write(' ')
  466. self.response.out.write(move.name)
  467. self.response.out.write('<br>')
  468. pokeIndex += 1
  469. selection = 'buffer'
  470. self.response.out.write("""
  471. <form action="/use" method="post">
  472. <div>
  473. <textarea name="bufferChoice" rows="3" cols="60"></textarea>
  474. </div>
  475. <div>
  476. <input type="submit" value="Enter selection">
  477. </div>
  478. </form>
  479. </body>
  480. </html>""")
  481.  
  482.  
  483.  
  484. if selection == 'use':
  485. useChoice = self.request.get('useChoice')
  486. if useChoice == 'c':
  487. self.response.out.write('<html><body>')
  488. pokeIndex = 0
  489. self.response.out.write('Select a pokemon.')
  490. for pkmn in hero.living:
  491. self.response.out.write('<br>')
  492. self.response.out.write(pokeIndex)
  493. self.response.out.write(' ')
  494. self.response.out.write(pkmn.name)
  495. self.response.out.write('<br>')
  496. pokeIndex += 1
  497. selection = 'buffer'
  498. self.response.out.write("""
  499. <form action="/change" method="post">
  500. <div>
  501. <textarea name="bufferChoice" rows="3" cols="60"></textarea>
  502. </div>
  503. <div>
  504. <input type="submit" value="Enter selection">
  505. </div>
  506. </form>
  507. </body>
  508. </html>""")
  509. elif useChoice == 'u':
  510. self.response.out.write('<html><body>')
  511. pokeIndex = 0
  512. self.response.out.write('Select a move.')
  513. for move in hero.active.moves:
  514. self.response.out.write('<br>')
  515. self.response.out.write(pokeIndex)
  516. self.response.out.write(' ')
  517. self.response.out.write(move.name)
  518. self.response.out.write('<br>')
  519. pokeIndex += 1
  520. selection = 'buffer'
  521. self.response.out.write("""
  522. <form action="/use" method="post">
  523. <div>
  524. <textarea name="bufferChoice" rows="3" cols="60"></textarea>
  525. </div>
  526. <div>
  527. <input type="submit" value="Enter selection">
  528. </div>
  529. </form>
  530. </body>
  531. </html>""")
  532.  
  533.  
  534.  
  535. class UsePage(webapp2.RequestHandler):
  536. def doMove(self, char, opponent, move):
  537. moveData = char.active.useMove(opponent, move)
  538. if moveData[0] == 'missed':
  539. self.response.out.write(char.active.name)
  540. self.response.out.write(' tried to use ')
  541. self.response.out.write(move.name)
  542. self.response.out.write(' but it missed.')
  543. self.response.out.write('<br>')
  544. if moveData[0] == 'hit':
  545. self.response.out.write(char.active.name)
  546. self.response.out.write(' used ')
  547. self.response.out.write(move.name)
  548. if moveData[1] == 'status':
  549. if moveData[2] == 'self':
  550. self.response.out.write("It's ")
  551. self.response.out.write(moveData[3])
  552. self.response.out.write(' increases.')
  553. self.response.out.write('<br>')
  554. if moveData[2] == 'other':
  555. self.response.out.write('It lowered the ')
  556. self.response.out.write(moveData[3])
  557. self.response.out.write(' of ')
  558. self.response.out.write(opponent.active.name)
  559. self.response.out.write('<br>')
  560. if moveData[1] == 'physical':
  561. if moveData[2] == 'crit':
  562. self.response.out.write('Crital hit!')
  563. self.response.out.write('<br>')
  564. if moveData[3] == 'super':
  565. self.response.out.write("It's super-effective!")
  566. self.response.out.write('<br>')
  567. if moveData[3] == 'ineffective':
  568. self.response.out.write("It's not very effective...")
  569. self.response.out.write('<br>')
  570. self.response.out.write('It does ')
  571. self.response.out.write(moveData[4])
  572. self.response.out.write(' damage.')
  573.  
  574.  
  575. def post(self):
  576. global selection
  577. choice = str(self.request.get('bufferChoice'))
  578. choice = int(choice)
  579. moveChoice = hero.active.moves[choice]
  580. hero.checkLiving()
  581. self.response.out.write('<html><body>')
  582. self.response.out.write(hero.name)
  583. self.response.out.write('<br>')
  584. if hero.active != 'nothing':
  585. if hero.active.isAlive():
  586. self.response.out.write('Your active pokemon is ')
  587. self.response.out.write(hero.active.name)
  588. self.response.out.write('<br>')
  589. self.response.out.write("It's health is: ")
  590. self.response.out.write(hero.active.health)
  591. self.response.out.write('<br>')
  592. if hero.alive <= 0:
  593. self.response.out.write('You lose!')
  594. self.response.out.write('<br>')
  595. elif antag.alive <= 0:
  596. self.response.out.write('You win!')
  597. self.response.out.write('<br>')
  598. EM = getAntagChoice(antag)
  599. if EM[0] == 'c':
  600. antag.active = EM[1]
  601. self.response.out.write(antag.name)
  602. self.response.out.write(' chose ')
  603. self.response.out.write(antag.active.name)
  604. self.response.out.write('<br>')
  605. self.doMove(hero, antag, moveChoice)
  606. if EM[0] == 'u':
  607. if antag.active.speed > hero.active.speed:
  608. self.doMove(antag, hero, EM[1])
  609. hero.checkLiving
  610. if hero.active.isAlive():
  611. self.doMove(hero, antag, moveChoice)
  612. antag.checkLiving
  613. # if not antag.active.isAlive():
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620. ### END GOOGLE APP ENGING HANDLER CLASSES
  621.  
  622. app = webapp2.WSGIApplication([
  623. ('/', MainPage),
  624. ('/buffer', BufferPage),
  625. ('/change', ChangePage),
  626. ('/use', UsePage)
  627. ],
  628. debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement