arena=[]
def grid(place):
for i in range(8):
place.append(["O"]*8)
def string(alist):
for i in alist:
print(" ".join(i))
grid(arena)
previous_postion_y= 2
previous_postion_x= 2
postion_y= 0
postion_x= 7
arena[postion_x][postion_y] = "x"
string(arena)
game = True
while True:
try:
while game:
print("")
print("")
print("")
print("You are displayed on the grid as: X")
print("")
print("You can now move Left, Right, Up, Down or stop")
print("")
print("")
move= input("What move would you like to make: ")
move = move.lower()
if move == "stop" or move== "s":
clearpage()
print("you will be transported to the main menu")
print("")
print("")
print("Press enter to continue...")
start()
elif move == "up" or move =="u":
spaces = int(input("How many spaces would you like to move."))
arena[postion_x][postion_y] = "O"
postion_x = postion_x - spaces
elif move == "down" or move == "d":
spaces = int(input("How many spaces would you like to move."))
arena[postion_x][postion_y] = "O"
postion_x = postion_x + spaces
elif move == "left" or move == "l":
spaces = int(input("How many spaces would you like to move."))
arena[postion_x][postion_y] = "O"
postion_y = postion_y - spaces
elif move == "right" or move =="r":
spaces = int(input("How many spaces would you like to move."))
arena[postion_x][postion_y] = "O"
postion_y = postion_y + spaces
else:
print("")
print("")
print("")
print("Please enter one of the following commands.")
print("Left.")
print("Right.")
print("Up.")
print("Down")
print("Stop")
print("")
input("Please press enter to continue...")
arena[postion_x][postion_y] = "X"
arena[postion_x][postion_y] = "X"
string(arena)
print("")
except IndexError:
print("")
print("")
print("You cant leave the premises of the grid")
print("")
print("Please enter a valid direction")
print("")
print("")
input("Press enter to continue...")
arena[postion_x][postion_y] = "X"
string(arena)
我需要在这个游戏中插入箱子和土匪,但不知道该怎么做 游戏的目的是每个胸部将增加10点,强盗将导致所有点数的损失。 有10个箱子和5个强盗
答案 0 :(得分:0)
作为您问题的一般方法,您可能会发现下面发布的代码可用作起点。它将生成一个随机的5x5'竞技场'(网格),其中每个单元格可能包含强盗或胸部。
from random import Random
class GridItem(object):
pass
class Robber(GridItem):
def __repr__(self):
return 'R'
class Chest(GridItem):
def __repr__(self):
return 'C'
class EmptySpace(GridItem):
def __repr__(self):
return ' '
def create_arena(width, height, robber_gen_chance, chest_gen_chance):
random = Random()
arena = []
for x in range(width):
column = []
for y in range(height):
if random.random() <= robber_gen_chance:
column.append(Robber())
elif random.random() <= chest_gen_chance:
column.append(Chest())
else:
column.append(EmptySpace())
arena.append(column)
return arena
def print_arena(arena):
for row in arena:
print(row)
if __name__ == '__main__':
arena = create_arena(5, 5, 0.1, 0.1)
print_arena(arena)
<强>输出强>
[ , , , , C]
[R, , , , ]
[ , , , C, ]
[ , , , , ]
[ , , , , ]
我刚看到你的规定:
有10个箱子和5个强盗
这是一个替代实施方案,可以保证每个舞台上都有指定数量的强盗和箱子。他们的位置将随机生成,如果竞技场太小而不适合他们,则会产生例外:
from random import Random
class GridItem(object):
pass
class Robber(GridItem):
def __repr__(self):
return 'R'
class Chest(GridItem):
def __repr__(self):
return 'C'
class EmptySpace(GridItem):
def __repr__(self):
return ' '
def create_arena(width, height, num_robbers, num_chests):
total_cells = width * height
if total_cells < num_robbers + num_chests:
raise ValueError('Not enough spaces for robbers and chests')
random = Random()
arena = [[EmptySpace() for _ in range(height)] for _ in range(width)]
available_spaces = set(range(total_cells))
robbers_at = set(random.sample(available_spaces, num_robbers))
available_spaces.difference_update(robbers_at)
chests_at = set(random.sample(available_spaces, num_chests))
# A robber and chest should never share the same position
assert robbers_at.isdisjoint(chests_at)
for position in robbers_at:
i, j = position // width, position % width
arena[i][j] = Robber()
for position in chests_at:
i, j = position // width, position % width
arena[i][j] = Chest()
return arena
def print_arena(arena):
for row in arena:
print(row)
if __name__ == '__main__':
arena = create_arena(10, 10, 5, 10)
print_arena(arena)
<强>输出强>
[ , , , , , , R, , R, C]
[ , , , , , , , C, , C]
[ , , , , , , , , , ]
[ , , , , , , C, R, , ]
[ , , , , C, , , , , ]
[C, , , , , , , C, , ]
[ , , , , , , , C, , ]
[ , , , , , , , R, , ]
[ , , , R, , , , , C, ]
[ , , C, , , , , , , ]