以下是将构成我要创建的迷宫游戏的代码输出,这可以称为玩家进入的初始房间:
我遇到的问题是使代码从文本文件中读取信息,该文件将指定有多少个房间,有多少个房间有开放通道,有多少个房间有封闭通道以及哪个房间有中奖权通道。一旦代码知道例如有4个房间,它将创建上图的4个实例,然后向用户显示一个随机房间,然后用户必须使用开放的通道浏览不同的房间,直到找到获胜的通道。 。
我粘贴了以下代码,非常感谢您的帮助,或者如果您有任何意见,请随时询问:
import pygame
import random
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()
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#
#Would all of these passageways go into one class or multiple classes?#
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()
#Maybe I should move the below code into a function#
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()
pygame.quit()