我需要创建一个迷宫游戏,我为这个程序选择了pygame,下面我已经写出了迷宫游戏是如何工作的。当游戏开始时,第一个动作必须是初始化迷宫的配置中的房间,通道和项目的结构,由文本文件中的数据表示:
概念模型
迷宫有一套任意数量的房间
客房通过通道连接,一个房间包含4个通道:北,东,南,西
每个Passage with isExit == false将连接两个房间,可能是相同的。
这些段落是双向的,即会带你回到你来自的地方。
每个具有isExit == true的段落将连接到一个房间。
只有一个房间有一个出口通道,一旦玩家通过游戏结束
在房间里还有物品,宝物和威胁,我的代码中只需要1个宝藏和1个威胁:
每个Treasure都有一个整数值,例如Gold(值100)。
每个威胁都可能被一个动作击败,例如巨魔可能会被称为俱乐部的行为击败。
一旦收集了宝藏或威胁被击败,它将在剩余的游戏中从房间消失。
一旦配置了游戏,用户就应该从配置文件存入随机房间。现在我只想写配置文件中有4个房间:
用户可以看到当前房间中的宝藏和威胁。
•用户可以拿起宝藏,在这种情况下,宝藏将从房间中移除,并且宝藏的价值会增加到玩家的财富中。
•用户可以以硬币的形式(即名称==“硬币”的宝藏实例)在房间中存入财富,每个值为1.存入一枚硬币从玩家的财富中减去1。存放硬币可能有助于识别已经访问过的房间。
•用户必须在离开房间之前消除所有威胁。
•通过执行击败它的行动来消除威胁。用户必须发现这是哪个操作,并且必须指定要对哪个威胁执行操作。
•一旦没有威胁,用户可以沿着其中一个方向离开房间。
•如果用户选择了没有Passage的方向,程序将宣布“你可能不会那样”。
•如果选择的Passage是退出游戏,则游戏完成。
到目前为止我已经编写了这段代码,但是我对编码通道感到困惑,因为我不知道是为它创建一个类还是4个通道的数组,我也不确定我是否正确编码了按钮或者我是否正确需要一个硬币和财富类:
python
import pygame
pygame.init()
win = pygame.display.set_mode((700,700))
pygame.display.set_caption("Maze Game")
def draw_rect_outline(surface, rect, color, width=1):
x, y, w, h = rect
width = max(width, 1)
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def coins_button(msg,x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y, w, h)) # Coin Button when hovering over#
else:
pygame.draw.rect(win, ic, (x,y, w, h)) # Coin Button when not hovering over, only the colour changes#
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg , smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
win.blit(textSurf, textRect)
def wealth_button(msg,x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y, w, h)) # Wealth Button#
else:
pygame.draw.rect(win, ic, (x,y, w, h)) # Wealth Button#
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg , smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
win.blit(textSurf, textRect)
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 20
def draw(self,win):
pygame.draw.rect(win, (255, 0, 0), (self.x, self.y, self.width, self.height)) #Drawing the Character#
def redrawgamewindow():
win.fill((0,0,0))
user.draw(win)
pygame.draw.rect(win, (0, 100, 255, 155), (80, 80, 550, 550), 10) # Blue Rectangle#
pygame.draw.rect(win, (64,224,208), (350, 30, 40, 40)) # North Passage#
pygame.draw.rect(win, (64,224,208), (350, 643, 40, 40)) # South Passage#
pygame.draw.rect(win, (64,224,208), (643, 350, 40, 40)) # East Passage
pygame.draw.rect(win, (64,224,208), (30, 350, 40, 40)) # West Passage#
pygame.display.update()
user = player(100, 100, 40, 60)
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed() # Moving the player around the room #
if keys [pygame.K_LEFT]:
user.x -= user.vel
if keys [pygame.K_RIGHT]:
user.x += user.vel
if keys [pygame.K_UP]:
user.y -= user.vel
if keys [pygame.K_DOWN]:
user.y += user.vel
redrawgamewindow()
coins_button(("Coins ="),500,95, 120, 40,(64, 224, 208),(0, 139, 139))
wealth_button(("Wealth ="),500,150, 120, 40,(64, 224, 208),(0, 139, 139))
pygame.quit()
发布于 2019-05-16 08:52:38
我会id
为每个房间都有一个独特的,这可能是一个简单的序数,或一个字符串。
每个房间都有一个exit_list
,每个方向的入口有北,东,南,西,上,下。这为非连接浪费了一些空间,但很简单。如果exit_list[0]
不是 None
,它包含了id
房间这个通道连接到在的北的方向,同样exist_list[1]
是东,等等等等。显然,如果退出是None
,有一个在给定的方向没有任何关系。
但是,不要仅使用1,2,3等方向代码,而是定义枚举类型并使用更符合可读性的符号名称Dir.NORTH
(而不是0
)。此外,如果您决定添加东北等,则可以更轻松地促进更改。
例如:
import enum
all_locations = {} # Dictionary of id-string : Location for all rooms
# Direction names and position-code
class Dir( enum.IntEnum ):
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
UP = 4
DOWN = 5
class Location:
def __init__( self, id, desc, exits_list ):
self.id = id # "root", or "room1", or "lake1" etc.
self.description = desc # "You stand at a crossroads..."
self.exits = exits_list # [ "lake1", None, None, "pit", ... ]
def hasExit( self, direction ):
return ( self.exits[ direction ] != None )
def getExit( self, direction ):
return self.exits[ direction ]
这允许代码测试方向,如果有链接,则获取下一个位置的ID。该ID可用于查询字典(或数字,列表)以查找下一个位置。
all_locations = loadLocations( map_filename )
current_location = all_locations[ "root" ]
player_input = getPlayerInput()
if ( player_input == 'north' ): # TODO - write better command parser
next_location = current_location.getExit( Dir.NORTH )
if ( next_location != None ):
current_location = all_locations[ next_location ] # move player
else:
print("No Exit North")
elif ( player_input == 'east' ):
next_location = current_location.getExit( Dir.EAST )
if ( next_location != None ):
current_location = all_locations[ next_location ]
else:
print("No Exit East")
显然这是一个基本的命令处理系统,但它说明了房间的联系。
https://stackoverflow.com/questions/-100006732
复制相似问题