Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- xRows = range(8) # number of horizontal cells
- yRows = range(5) # number of vertical cells
- rocks = [[1,2],[3,4], [4,0],[4,3]] # spots that cannot be moved to
- enemies = [[1,1],[4,4]] # spots that chase you and end the game if touched
- allRows = [] # used to convert x and y rows into a list
- playerPos = [2,2] # the player's position
- finishLine = [7,0] # go here to end the game
- def StartOver():
- enemies[0] = [1,1] #is there a better way to reset these variables?
- enemies[1] = [4,4]
- playerPos[0] = 2
- playerPos[1] = 2
- def DrawScreen(xRows,yRows,playerPos):
- for y in yRows:
- for x in xRows:
- whatToPrint = '[ ]'
- if [x,y] == playerPos:
- whatToPrint = '[@]'
- if [x,y] in rocks:
- whatToPrint = '[#]'
- if [x,y] in enemies:
- whatToPrint = '[%]'
- if [x,y] == finishLine:
- whatToPrint = '[^]'
- print (whatToPrint, end=" ")
- print()
- print()
- def CheckCanWalk(allRows,xR,yR,potPlayerPos): # pot is short for potential
- canWalk = False
- if potPlayerPos in allRows:
- canWalk = True
- if potPlayerPos in rocks:
- canWalk = False
- return canWalk
- def AIenemyTurn(E,playerPos):
- startPos = list(E)
- potEnemyPos = E
- if playerPos[0] > E[0]:
- potEnemyPos[0] += 1
- elif playerPos[0] < E[0]:
- potEnemyPos[0] -= 1
- elif playerPos[1] > E[1]:
- potEnemyPos[1] += 1
- elif playerPos[1] < E[1]:
- potEnemyPos[1] -= 1
- if potEnemyPos in rocks:
- E[0] = startPos[0] # is there a better way to do this?
- E[1] = startPos[1]
- print('''w + enter = move up, a = left, d = right, s = down
- @ = player
- # = rocks(cannot be walked on
- % = enemies (chase you, game over when touched)
- ^ = finish line, go here to win''')
- while True:
- for y in yRows:
- for x in xRows:
- allRows.append([x,y])
- DrawScreen(xRows,yRows,playerPos)
- potPlayerPos = playerPos
- startPos = list(playerPos)
- button = input()
- if button == 'w':
- potPlayerPos[1] -= 1
- elif button == 'a':
- potPlayerPos[0] -= 1
- elif button == 's':
- potPlayerPos[1] += 1
- elif button == 'd':
- potPlayerPos[0] += 1
- if CheckCanWalk(allRows,xRows,yRows,playerPos):
- playerPos = potPlayerPos
- else:
- playerPos[0] = startPos[0]
- playerPos[1] = startPos[1]
- if button == 'b':
- break
- for enemy in enemies:
- enemy = AIenemyTurn(enemy,playerPos)
- if playerPos in enemies:
- print('Game Over')
- print('Play Again?(yes/no)')
- n = input()
- if n == 'yes':
- StartOver()
- else:
- break
- if playerPos == finishLine:
- print('You Win')
- print('Play Again?(yes/no)')
- n = input()
- if n == 'yes':
- StartOver()
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement