为了好玩,我用python开发了一个游戏。 在这里,我在一个房间内生成宝石,用户可以收集这些宝石。 到目前为止,宝石是在房间内生成的,但它们不是随机生成的。此外,当用户从房间中取出宝石后,当再次回到房间时,它是可见的
我找不到一种方法来实现在房间内随机生成gem,何时获取gem用户级别必须升级,返回同一房间后gem必须更改
多谢各位
import random
# Rooms have items, players can carry items
class Item:
def __init__(self, name, level):
self.name = name
self.level = level
# Create Gems
White_Gem = Item("White Gem","1")
Yellow_Gem = Item("Yellow Gem","2")
Green_Gem = Item("Green Gem","3")
Red_Gem = Item("Red Gem","4")
Blue_Gem = Item("Blue Gem","5")
def make_gem():
gem_in = random.randrange(2)
if gem_in == (0 or 1):
return 0
gem = random.randrange(5)
if gem == 0:
gem = White_Gem
elif gem == 1:
gem = Yellow_Gem
elif gem == 2:
gem = Green_Gem
elif gem == 3:
gem = Red_Gem
elif gem == 4:
gem = Blue_Gem
return gem
# Room class
class Room(object):
""" Room """
def __init__(self,name,description,gem_y_n):
self.name = name
self.description = description
self.gem_y_n = gem_y_n
if self.gem_y_n == "y":
gem = make_gem()
self.gem = gem
# Rooms
# last input is coordinates (n,e,s,w)
#
# ++++ Room Map +++++
#
# ===================
# | |
# | |
# | 1 |
# | |
# | 3 2 |
# | 4 5 |
# | 6 |
# | 7 8 9 |
# | 10 |
# | 12 11 |
# ===================
#
# Template:
# room# = Room(name,description,gem in it? (y/n))
# Then add the coordinates below the room creators
room1 = Room("Bedroom","You are you in your own bedroom.\nTo the south, there is a garden past the back door.", "n")
room2 = Room("Garden","You are in a garden with many flowers and a narrow stone path. \nTo the north, you see the backdoor of your house that enters your bedroom.\nA pathway leads west.","y")
room3 = Room("Pathway","You are in a narrow stone path with hedges on both sides of you.\nTo the east, there is a garden.","y")
room4 = Room("kitchen","You are in a kitchen now.\nTo the north, there is a Pathway.\nTo the east, there is a dining room.\nTo the south, there is a familyroom","y")
room5 = Room("diningroom","You are in a dining room now.\nTo the north, there is a garden.\nTo the west, there is a kitchen.","y")
room6 = Room("familyroom","You are in a family room.\nTo the north, there is a kitchen.","y")
room7 = Room("room7","You are in a room 7.","y")
room8 = Room("room8","You are in a room 8.","y")
room9 = Room("room9","You are in a room 9.","y")
room10 = Room("room10","You are in a room 10.","y")
room11 = Room("room11","You are in a room 11.","y")
room12 = Room("room12","You are in a room 12.","y")
# Room coordinates (had to create all the rooms to assign them to coordinates)
room1.coordinates = [0,0,room2,0]
room2.coordinates = [room1,0,room5,room3]
room3.coordinates = [0,room2,room4,0]
room4.coordinates = [room3,room5,room6,0]
room5.coordinates = [room2,0,0,room4]
room6.coordinates = [room4,0,0,0]
room7.coordinates = [room6,room8,0,0]
room8.coordinates = [0,room9,0,room7]
room9.coordinates = [0,0,room10,room8]
room10.coordinates = [room9,0,room11,0]
room11.coordinates = [room10,0,0,room12]
room12.coordinates = [0,room11,0,0]
# Character class
class Player(object):
def __init__(self,name,location = room1):
self.name = name
self.location = location
def look(self):
place = self.location
print (place.description)
if place.gem_y_n == "y":
pick = input("If you want to pick the Gem y/n :")
if place.gem != 0 and pick == "y":
room_gem = place.gem
print ("You pick a",room_gem.name,"in this room.\n")
else:
print ("")
user = Player("Shan")
currentchar = user
print ("Welcome to Mouche's first text based game.")
print ("")
print ('Type "commands" to see the command list')
print ("You are currently:", currentchar.name)
# Menu
# "commands" shows the commands available
# "look" looks around in the current room
#
while True:
command = input("")
if command == "commands":
print ('"n","e","s", and "w" make your character go north, east, south, and west respectively')
print ('"end" to break')
print ('"look" to look around the room')
if command == "look":
currentchar.look()
if command == ("n" or "north"):
if currentchar.location.coordinates[0] == 0:
print ("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[0]
currentchar.look()
if command == ("e" or "east"):
if currentchar.location.coordinates[1] == 0:
print ("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[1]
currentchar.look()
if command == ("s" or "south"):
if currentchar.location.coordinates[2] == 0:
print ("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[2]
currentchar.look()
if command == ("w" or "west"):
if currentchar.location.coordinates[3] == 0:
print ("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[3]
currentchar.look()
if command == "end":
break
要随机生成某些内容,可以使用一个随机数,然后检查它是否等于预定数,如下所示:
如果你想要一个更高的机会产卵宝石,你可以改变随机数范围
相关问题 更多 >
编程相关推荐