Advertisement
Miziziziz

Untitled

Nov 2nd, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. xRows = range(8) # number of horizontal cells
  2. yRows = range(5) # number of vertical cells
  3. rocks = [[1,2],[3,4], [4,0],[4,3]] # spots that cannot be moved to
  4. enemies = [[1,1],[4,4]] # spots that chase you and end the game if touched
  5. allRows = [] # used to convert x and y rows into a list
  6. playerPos = [2,2] # the player's position
  7. finishLine = [7,0] # go here to end the game
  8.  
  9. def StartOver():
  10.     enemies[0] = [1,1] #is there a better way to reset these variables?
  11.     enemies[1] = [4,4]
  12.     playerPos[0] = 2
  13.     playerPos[1] = 2
  14.  
  15. def DrawScreen(xRows,yRows,playerPos):
  16.     for y in yRows:
  17.             for x in xRows:
  18.                 whatToPrint = '[ ]'
  19.                 if [x,y] == playerPos:
  20.                     whatToPrint = '[@]'
  21.                 if [x,y] in rocks:
  22.                     whatToPrint = '[#]'
  23.                 if [x,y] in enemies:
  24.                     whatToPrint = '[%]'
  25.                 if [x,y] == finishLine:
  26.                     whatToPrint = '[^]'
  27.                 print (whatToPrint, end=" ")
  28.             print()
  29.             print()
  30.  
  31.  
  32. def CheckCanWalk(allRows,xR,yR,potPlayerPos): # pot is short for potential
  33.     canWalk = False
  34.     if potPlayerPos in allRows:
  35.         canWalk = True
  36.     if potPlayerPos in rocks:
  37.         canWalk = False
  38.     return canWalk
  39.  
  40. def AIenemyTurn(E,playerPos):
  41.     startPos = list(E)
  42.     potEnemyPos = E
  43.    
  44.     if playerPos[0] > E[0]:
  45.         potEnemyPos[0] += 1
  46.     elif playerPos[0] < E[0]:
  47.         potEnemyPos[0] -= 1
  48.     elif playerPos[1] > E[1]:
  49.         potEnemyPos[1] += 1
  50.     elif playerPos[1] < E[1]:
  51.         potEnemyPos[1] -= 1
  52.     if potEnemyPos in rocks:
  53.         E[0] =  startPos[0] # is there a better way to do this?
  54.         E[1] =  startPos[1]
  55.  
  56.        
  57. print('''w + enter = move up, a = left, d = right, s = down
  58. @ = player
  59. # = rocks(cannot be walked on
  60. % = enemies (chase you, game over when touched)
  61. ^ = finish line, go here to win''')
  62. while True:
  63.     for y in yRows:
  64.         for x in xRows:
  65.             allRows.append([x,y])
  66.  
  67.     DrawScreen(xRows,yRows,playerPos)
  68.     potPlayerPos = playerPos
  69.     startPos = list(playerPos)
  70.     button = input()
  71.     if button == 'w':
  72.         potPlayerPos[1] -= 1
  73.     elif button == 'a':
  74.         potPlayerPos[0] -= 1
  75.     elif button == 's':
  76.         potPlayerPos[1] += 1
  77.     elif button == 'd':
  78.         potPlayerPos[0] += 1
  79.        
  80.     if CheckCanWalk(allRows,xRows,yRows,playerPos):
  81.         playerPos = potPlayerPos
  82.     else:
  83.         playerPos[0] =  startPos[0]
  84.         playerPos[1] =  startPos[1]
  85.     if button == 'b':
  86.         break
  87.     for enemy in enemies:
  88.         enemy = AIenemyTurn(enemy,playerPos)
  89.     if playerPos in enemies:
  90.         print('Game Over')
  91.         print('Play Again?(yes/no)')
  92.         n = input()
  93.         if n == 'yes':
  94.             StartOver()
  95.         else:
  96.             break
  97.     if playerPos == finishLine:
  98.         print('You Win')
  99.         print('Play Again?(yes/no)')
  100.         n = input()
  101.         if n == 'yes':
  102.             StartOver()
  103.         else:
  104.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement