Moving around a small grid: Difference between revisions
Mr. MacKenty (talk | contribs) |
|||
(13 intermediate revisions by 2 users not shown) | |||
Line 9: | Line 9: | ||
You have a simple 10 x 10 grid, with your character at the center (5,5). You can move up, down, left and right. Add: | You have a simple 10 x 10 grid, with your character at the center (5,5). You can move up, down, left and right. Add: | ||
* terrain | * terrain (add water, add hills, add town, add logical terrain (trees should be like a forest, water like a lake)). | ||
* a bad guy | * a bad guy | ||
* combat | * combat | ||
Line 15: | Line 15: | ||
* basic statistics for the bad guy (or you) like Hit Points, Armour, Power, etc... | * basic statistics for the bad guy (or you) like Hit Points, Armour, Power, etc... | ||
== | == The basic game == | ||
<syntaxhighlight lang="python" > | <syntaxhighlight lang="python" > | ||
import random | import random | ||
import prettytable | |||
import colorama | |||
from colorama import Fore, Back, Style | |||
grid = [] | grid = [] | ||
# let's make our map. We are making a 25 x 25 map | # let's make our map. We are making a 25 x 25 map | ||
# for our terrain, we are going to randomly insert a number from 1 to 4 | # for our terrain, we are going to randomly insert a number from 1 to 4 | ||
# | # | ||
# 1 is normal ground | # 1 is normal ground ... | ||
# 2 is normal ground | # 2 is normal ground ... | ||
# 3 is a tree | # 3 is a tree .*. | ||
# 4 is a mountain | # 4 is a mountain .^. | ||
# 5 is the player | # 5 is the player [T] | ||
for i in range(0,625): | for i in range(0,625): | ||
grid.append(random.randrange(1,5)) | grid.append(random.randrange(1,5)) | ||
# now we | # now we can manually change our terrain. This code puts a basic mountain range | ||
# at the very top of our map. | |||
grid.insert(0,4) | |||
grid.insert(1,4) | |||
grid.insert(2,4) | |||
grid.insert(3,4) | |||
grid.insert(4,4) | |||
grid.insert(5,4) | |||
# now we place the player | |||
grid.insert(317,5) | grid.insert(317,5) | ||
Line 46: | Line 56: | ||
current_player_location = 317 | current_player_location = 317 | ||
# remember we have a 25 x 25 grid, which we are representing in a simple list. | # remember we have a 25 x 25 grid, which we are representing in a simple list. | ||
# this means row 1 is 0 to 24, row 2 is 25 to 49, row 3 is 50 to 74, etc... | # this means row 1 is 0 to 24, row 2 is 25 to 49, row 3 is 50 to 74, etc... | ||
Line 56: | Line 66: | ||
for i in range(0,625): | for i in range(0,625): | ||
if grid[i] == 1: | if grid[i] == 1: | ||
print("...") | print(Style.DIM + Fore.YELLOW + "..." + Style.RESET_ALL ,end='') | ||
elif grid[i] == 2: | elif grid[i] == 2: | ||
print("...") | print(Style.DIM + Fore.YELLOW + "..." + Style.RESET_ALL ,end='') | ||
elif grid[i] == 3: | elif grid[i] == 3: | ||
print(".*.") | print("." + Fore.GREEN + "*" + Style.RESET_ALL + ".",end='') | ||
elif grid[i] == 4: | elif grid[i] == 4: | ||
print(".^.") | print("." + Style.DIM + Fore.WHITE + "^" + Style.RESET_ALL + ".",end='') | ||
elif grid[i] == 5: | elif grid[i] == 5: | ||
print("[T]") | print("[" + Style.BRIGHT + Fore.RED + "T" + Style.RESET_ALL + "]",end='') | ||
else: | else: | ||
print("ERR"), | print("ERR"), | ||
Line 75: | Line 85: | ||
def move(where_do_you_want_go): | def move(where_do_you_want_go): | ||
global current_player_location | global current_player_location | ||
current_player_position = grid.index(5) | |||
if where_do_you_want_go == "up": | if where_do_you_want_go == "up": | ||
# | # | ||
Line 81: | Line 92: | ||
if grid.index(5) <=25: | if grid.index(5) <=25: | ||
print("You can't go up anymore.") | print("You can't go up anymore.") | ||
elif grid[current_player_position - 25] == 3 or grid[current_player_position - 25] == 4: | |||
print("You cannot go up, as there is an obstacle in your path!") | |||
else: | else: | ||
print("The player is at position " + str(grid.index(5))) | print("The player is at position " + str(grid.index(5))) | ||
current_player_position = | grid[current_player_position - 25] = 5 | ||
grid | grid[current_player_position] = 1 | ||
print("The new player is at position " + str(grid.index(5))) | print("The new player is at position " + str(grid.index(5))) | ||
elif where_do_you_want_go == "down": | elif where_do_you_want_go == "down": | ||
current_player_position = grid.index(5) | |||
# | # | ||
# let's make sure we are not at the bottom row, which is 600 to 624 | # let's make sure we are not at the bottom row, which is 600 to 624 | ||
Line 93: | Line 107: | ||
if grid.index(5) >=600: | if grid.index(5) >=600: | ||
print("You can't go down anymore.") | print("You can't go down anymore.") | ||
elif grid[current_player_position + 25] == 3 or grid[current_player_position + 25] == 4: | |||
print("You cannot move down, as there is an obstacle in your path!") | |||
else: | else: | ||
print("The player is at position " + str(grid.index(5))) | print("The player is at position " + str(grid.index(5))) | ||
current_player_position = | grid[current_player_position + 25] = 5 | ||
grid | grid[current_player_position] = 1 | ||
print("The new player is at position " + str(grid.index(5))) | print("The new player is at position " + str(grid.index(5))) | ||
elif where_do_you_want_go == "right": | elif where_do_you_want_go == "right": | ||
current_player_position = grid.index(5) | |||
# | # | ||
# let's make sure we are not at the bottom row, which is 600 to 624 | # let's make sure we are not at the bottom row, which is 600 to 624 | ||
Line 105: | Line 122: | ||
if grid.index(5) % 25 == 24: | if grid.index(5) % 25 == 24: | ||
print("You can't go right anymore.") | print("You can't go right anymore.") | ||
elif grid[current_player_position + 1] == 3 or grid[current_player_position + 1] == 4: | |||
print("You cannot move right, as there is an obstacle in your path!") | |||
else: | else: | ||
print("The player is at position " + str(grid.index(5))) | print("The player is at position " + str(grid.index(5))) | ||
grid.insert(current_player_position + 1, grid.pop(current_player_position)) | |||
grid.insert(current_player_position + 1, grid.pop(current_player_position)) | |||
print("The new player is at position " + str(grid.index(5))) | print("The new player is at position " + str(grid.index(5))) | ||
elif where_do_you_want_go == "left": | elif where_do_you_want_go == "left": | ||
current_player_position = grid.index(5) | |||
# | # | ||
# let's make sure we are not at the bottom row, which is 600 to 624 | # let's make sure we are not at the bottom row, which is 600 to 624 | ||
Line 117: | Line 136: | ||
if grid.index(5) % 25 == 0: | if grid.index(5) % 25 == 0: | ||
print("You can't go left anymore.") | print("You can't go left anymore.") | ||
elif grid[current_player_position - 1] == 3 or grid[current_player_position - 1] == 4: | |||
print("You cannot move left, as there is an obstacle in your path!") | |||
else: | else: | ||
print("The player is at position " + str(grid.index(5))) | print("The player is at position " + str(grid.index(5))) | ||
grid.insert(current_player_position - 1, grid.pop(current_player_position)) | grid.insert(current_player_position - 1, grid.pop(current_player_position)) | ||
print("The new player is at position " + str(grid.index(5))) | print("The new player is at position " + str(grid.index(5))) | ||
return | return | ||
def inventory(): | |||
inventory = {'axe':1, 'pick axe':1, 'food':10} | |||
inventory_table = prettytable(['Item', 'Quantity']) | |||
inventory_table.align["Item"] = "l" | |||
for i,e in inventory.iteritems(): | |||
inventory_table.add_row([str(i), e]) | |||
print(inventory_table) | |||
foo = input("Press enter to contnue...") | |||
return | |||
Line 129: | Line 158: | ||
while True: | while True: | ||
draw_board() | draw_board() | ||
choice = | print("Your current position is: " + str(grid.index(5))) | ||
print("Other information should appear here. ") | |||
print("Up, you see " + str(grid[grid.index(5)-25])) | |||
print("\n") | |||
choice = input("What do you want to do (type help for help) > ") | |||
if choice == "99": | if choice == "99": | ||
break | break | ||
Line 137: | Line 170: | ||
move('up') | move('up') | ||
elif choice == "r": | elif choice == "r": | ||
move('right') | move('right') | ||
elif choice == "l": | elif choice == "l": | ||
move('left') | move('left') | ||
elif choice == "d": | elif choice == "d": | ||
move('down') | |||
move('down') | elif choice == "i": | ||
inventory() | |||
return | return | ||
main() | main() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Take | == Take this farther == | ||
You want to go farther? | You want to go farther? | ||
* add color to this | * add color to this | ||
* add many types of different entities (google nethack) | * add many types of different entities (google nethack) | ||
* move the board to 50 x 50 | * move the board to 50 x 50 | ||
* add an option to SAVE / RESTORE a game | * add an option to SAVE / RESTORE a game | ||
Make a decent map. | |||
* [https://www.google.pl/search?q=3d+text+grid&safe=strict&espv=2&biw=1280&bih=594&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi0svKLp9bLAhXLVSwKHfEJBOQQ_AUIBigB#safe=strict&tbm=isch&q=ascii+map+rpg&imgrc=_ click here to see a google image search result for ascii maps] | |||
== How you will be assessed == | == How you will be assessed == |
Latest revision as of 11:42, 22 September 2020
![](/images/d/d1/Square.png)
This a problem set for you to work through [1]
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
The Problem[edit]
This problem is designed to test your skill and knowledge of functions, variables and computational thinking.
You have a simple 10 x 10 grid, with your character at the center (5,5). You can move up, down, left and right. Add:
- terrain (add water, add hills, add town, add logical terrain (trees should be like a forest, water like a lake)).
- a bad guy
- combat
- basic AI
- basic statistics for the bad guy (or you) like Hit Points, Armour, Power, etc...
The basic game[edit]
import random
import prettytable
import colorama
from colorama import Fore, Back, Style
grid = []
# let's make our map. We are making a 25 x 25 map
# for our terrain, we are going to randomly insert a number from 1 to 4
#
# 1 is normal ground ...
# 2 is normal ground ...
# 3 is a tree .*.
# 4 is a mountain .^.
# 5 is the player [T]
for i in range(0,625):
grid.append(random.randrange(1,5))
# now we can manually change our terrain. This code puts a basic mountain range
# at the very top of our map.
grid.insert(0,4)
grid.insert(1,4)
grid.insert(2,4)
grid.insert(3,4)
grid.insert(4,4)
grid.insert(5,4)
# now we place the player
grid.insert(317,5)
# and let's keep track of where the player is.
current_player_location = 317
# remember we have a 25 x 25 grid, which we are representing in a simple list.
# this means row 1 is 0 to 24, row 2 is 25 to 49, row 3 is 50 to 74, etc...
# You should remember this when you work on the move code.
#
def draw_board():
row = 0
for i in range(0,625):
if grid[i] == 1:
print(Style.DIM + Fore.YELLOW + "..." + Style.RESET_ALL ,end='')
elif grid[i] == 2:
print(Style.DIM + Fore.YELLOW + "..." + Style.RESET_ALL ,end='')
elif grid[i] == 3:
print("." + Fore.GREEN + "*" + Style.RESET_ALL + ".",end='')
elif grid[i] == 4:
print("." + Style.DIM + Fore.WHITE + "^" + Style.RESET_ALL + ".",end='')
elif grid[i] == 5:
print("[" + Style.BRIGHT + Fore.RED + "T" + Style.RESET_ALL + "]",end='')
else:
print("ERR"),
row = row + 1
if row == 25:
print ("\n")
row = 0
return
def move(where_do_you_want_go):
global current_player_location
current_player_position = grid.index(5)
if where_do_you_want_go == "up":
#
# let's make sure we are not at the top row, which is 0 to 24
#
if grid.index(5) <=25:
print("You can't go up anymore.")
elif grid[current_player_position - 25] == 3 or grid[current_player_position - 25] == 4:
print("You cannot go up, as there is an obstacle in your path!")
else:
print("The player is at position " + str(grid.index(5)))
grid[current_player_position - 25] = 5
grid[current_player_position] = 1
print("The new player is at position " + str(grid.index(5)))
elif where_do_you_want_go == "down":
current_player_position = grid.index(5)
#
# let's make sure we are not at the bottom row, which is 600 to 624
#
if grid.index(5) >=600:
print("You can't go down anymore.")
elif grid[current_player_position + 25] == 3 or grid[current_player_position + 25] == 4:
print("You cannot move down, as there is an obstacle in your path!")
else:
print("The player is at position " + str(grid.index(5)))
grid[current_player_position + 25] = 5
grid[current_player_position] = 1
print("The new player is at position " + str(grid.index(5)))
elif where_do_you_want_go == "right":
current_player_position = grid.index(5)
#
# let's make sure we are not at the bottom row, which is 600 to 624
#
if grid.index(5) % 25 == 24:
print("You can't go right anymore.")
elif grid[current_player_position + 1] == 3 or grid[current_player_position + 1] == 4:
print("You cannot move right, as there is an obstacle in your path!")
else:
print("The player is at position " + str(grid.index(5)))
grid.insert(current_player_position + 1, grid.pop(current_player_position))
print("The new player is at position " + str(grid.index(5)))
elif where_do_you_want_go == "left":
current_player_position = grid.index(5)
#
# let's make sure we are not at the bottom row, which is 600 to 624
#
if grid.index(5) % 25 == 0:
print("You can't go left anymore.")
elif grid[current_player_position - 1] == 3 or grid[current_player_position - 1] == 4:
print("You cannot move left, as there is an obstacle in your path!")
else:
print("The player is at position " + str(grid.index(5)))
grid.insert(current_player_position - 1, grid.pop(current_player_position))
print("The new player is at position " + str(grid.index(5)))
return
def inventory():
inventory = {'axe':1, 'pick axe':1, 'food':10}
inventory_table = prettytable(['Item', 'Quantity'])
inventory_table.align["Item"] = "l"
for i,e in inventory.iteritems():
inventory_table.add_row([str(i), e])
print(inventory_table)
foo = input("Press enter to contnue...")
return
def main():
while True:
draw_board()
print("Your current position is: " + str(grid.index(5)))
print("Other information should appear here. ")
print("Up, you see " + str(grid[grid.index(5)-25]))
print("\n")
choice = input("What do you want to do (type help for help) > ")
if choice == "99":
break
elif choice == "help":
help()
elif choice == "u":
move('up')
elif choice == "r":
move('right')
elif choice == "l":
move('left')
elif choice == "d":
move('down')
elif choice == "i":
inventory()
return
main()
Take this farther[edit]
You want to go farther?
- add color to this
- add many types of different entities (google nethack)
- move the board to 50 x 50
- add an option to SAVE / RESTORE a game
Make a decent map.
How you will be assessed[edit]
Every problem set is a formative assignment. Please click here to see how you will be graded