在Pygame的多层次迷宫游戏中创建类时遇到问题

时间:2019-05-16 09:51:06

标签: python python-3.x pygame maze multi-level

以下是将构成我要创建的迷宫游戏的代码输出,这可以称为玩家进入的初始房间:

  • 红色矩形构成了我的角色,我已将其编码为在房间内移动
  • 蓝色矩形外部的绿松石矩形形成通道:北,东,南,西
  • 深蓝色矩形形成一个边界,红色矩形无法穿过该边界。
  • 代码应允许红色矩形在接近开放通道时移动到另一个房间(我们称此房间为2),第二个房间应与第一个房间相同(即具有4个通道)
  • 在其中一个房间中有一个称为获胜通道的游戏结束,因此在上面的示例中,如果北通道被称为获胜通道,而我的红色矩形接近该游戏,则游戏结束
  • 如果它接近已关闭的通道,则应输出类似“您不能去那里”的信息。

我遇到的问题是使代码从文本文件中读取信息,该文件将指定有多少个房间,有多少个房间有开放通道,有多少个房间有封闭通道以及哪个房间有中奖权通道。一旦代码知道例如有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()

0 个答案:

没有答案