如何在python海龟游戏中加载不同的关卡

Posted

技术标签:

【中文标题】如何在python海龟游戏中加载不同的关卡【英文标题】:How to load different levels in python turtle game 【发布时间】:2021-05-12 02:59:45 【问题描述】:

尝试用 Python Turtle 制作一个简单的迷宫游戏。使用图章方法,我已经制作了第一级的轮廓,并且可以完美加载。我想添加第二个级别并访问它,但我不知道如何。任何帮助将不胜感激。

P.S - 我对堆栈溢出很陌生,所以我不知道我应该在这里放多少代码,但期待帮助,我发布了完整的代码。提前致谢

import turtle
import math


wn = turtle.Screen()
wn.bgcolor("black")
wn.title("A maze game")
wn.setup(700,700)

class Pen(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("square")
        self.color("white")
        self.penup()
        self.speed(0)


class Treasure(turtle.Turtle):
    def __init__(self, x, y):
        turtle.Turtle.__init__(self)
        self.shape("square")
        self.color(color_1)
        self.penup()
        self.speed(0)
        self.gold = 100
        self.goto(x, y)

def destroy(self):
    self.goto(2000, 2000)
    self.hideturtle()

def change_color(self):
     self.color(color_2)

color_1 = ("white")
color_2 = ("gold")

class Player(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("circle")
        self.color("red")
        self.penup()
        self.speed(0)
        self.gold = 0

def go_up(self):
    move_to_x = player.xcor()
    move_to_y = player.ycor() + 24
    
    if (move_to_x, move_to_y) not in walls:
        self.goto(move_to_x, move_to_y)
    
    
def go_down(self):
    move_to_x = player.xcor()
    move_to_y = player.ycor() - 24
    
    if (move_to_x, move_to_y) not in walls:
        self.goto(move_to_x, move_to_y)
    

def go_left(self):
    move_to_x = player.xcor() - 24
    move_to_y = player.ycor() 
    
    if (move_to_x, move_to_y) not in walls:
        self.goto(move_to_x, move_to_y)
    

def go_right(self):
    move_to_x = player.xcor() + 24
    move_to_y = player.ycor() 
    
    if (move_to_x, move_to_y) not in walls:
        self.goto(move_to_x, move_to_y)

def is_close_to(self, other):
    a = self.xcor()-other.xcor()
    b = self.ycor()-other.ycor()
    distance = math.sqrt((a ** 2) + (b ** 2))

    if distance < 50:
        return True
    else:
        return False
        
     
levels = [""]

level_1 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X  P              T     X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

treasures = []

levels.append(level_1)

def setup_maze(level):
    for y in range(len(level)):
        for x in range(len(level[y])):
            character = level[y][x]
            screen_x = -288 + (x * 24)
            screen_y = 288 - (y * 24)

        if character == "X":
            pen.goto(screen_x, screen_y)
            pen.stamp()

            walls.append((screen_x, screen_y))

        if character == "P":
            player.goto(screen_x, screen_y)

        if character == "T":
            treasures.append(Treasure(screen_x, screen_y))
           

pen = Pen()
player = Player()

walls = []

setup_maze(levels[1])

turtle.listen()
turtle.onkey(player.go_left,"Left")
turtle.onkey(player.go_right,"Right")
turtle.onkey(player.go_up,"Up")
turtle.onkey(player.go_down,"Down")

wn.tracer(0)

while True:
    for treasure in treasures:
        if player.is_close_to(treasure):
            treasure.change_color()
                   
wn.update()

【问题讨论】:

【参考方案1】:

要加载新关卡,您需要将世界的当前状态重置为已知的基本状态,然后设置新关卡。后者你似乎想通了,前者涉及清屏、重新初始化属性、清游戏状态。

首先,您需要清除窗口。正如this answer 告诉我们的那样,这可以通过调用turtle.Screen().reset() 来完成,或者在你的情况下是wm.reset()

完成后,你需要重新初始化屏幕的所有属性,比如设置bgcolortitle等类似的东西。现在这些发生在文件的开头,但是您需要在程序的生命周期中多次执行它们,因此您应该将它们放在单独的函数中,例如init_screen,这样您就可以调用它一次你'已经重置了。

最后,您需要重置游戏状态。在您的情况下,这将是您的 treasureswalls 列表,但是当您添加更多功能时,这可能包括不需要在一个级别和下一个级别之间保留的任何其他数据。实际上,一旦您调用setup_maze,这些数据就会失效,因此首先清除它们然后在内部循环中设置新级别是有意义的。

【讨论】:

以上是关于如何在python海龟游戏中加载不同的关卡的主要内容,如果未能解决你的问题,请参考以下文章

如何在HTML5 / JavaScript中加载/卸载图像

如何在 Unity 中加载分数?

如何在 python 中加载带有参数的 python 模块?

如何在python中加载m4a文件

如何在android中加载不同的xml文件以改变方向

如何在 Visual C++ 中加载不同语言的对话框?