0

我正在使用 Python 3 中的 Turtle 制作游戏。

我的主要问题是乌龟可以通过游戏中的“绘制”线移动。它基本上是一个随机的“flappy-bird”游戏,每次运行程序时都会有所不同。

有没有可能阻止海龟穿过它画的线?

该程序创建了玩家应该能够穿过的墙壁和洞,就像飞扬的小鸟一样,它是一个管道上具有不同长度的管孔管。

如果我print(hole)在程序结束时运行它会返回None. 为什么是这样?

这是我的程序:

import turtle
import random
GameOver=False
import math
import time


board = turtle.Screen()
board.screensize(50.50)
board.bgcolor("black")
board.title("Labyrinten")
Hast_på_ritande_av_bana= 0
TURTLE_SIZE = 20
playerspeed = 50
n=1 #variabeler som gör att den hoppar ett steg åt höger när den gör raderna t.ex. ((930/8)*n)
b=2 #

global player


#SÄTTER UPP SKÄRM
border = turtle.Turtle()
border.speed(Hast_på_ritande_av_bana)

border.hideturtle()
border.color("white")
border.shape("triangle")
border.penup()
border.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.pendown()
border.showturtle()

border.rt(90)
border.fd(780)
border.penup()

#ritar banan

for rader in range(4): #creating the maze

    border.lt(90)
    border.fd(930/8)
    border.pendown()
    border.lt(90)
    wall_ett = border.fd(random.randint(50,630))    #lower wall on row 1
    border.penup()
    hole=border.fd(random.randint(30,70))           #hole on row 1
    border.pendown()
    wall_ett_ett = border.goto (TURTLE_SIZE/2 - board.window_width()/2 + (930/8*n), board.window_height()/2 - TURTLE_SIZE/2) #higher wall on row 1
    border.rt(90)
    border.fd(930/8)
    border.rt(90)
    wall_två_ett = border.fd(random.randint(50, 630)) #lower wall on row 2
    border.penup()
    hole2 = border.fd(random.randint(30, 70))         #hole on row 2
    border.pendown()
    wall_två_två = border.goto(TURTLE_SIZE/2 - board.window_width()/2 + (930/8*b), + TURTLE_SIZE/2 - board.window_height()/2 ) # higher wall on row 2
    n = n + 2
    b = b + 2

border.goto(TURTLE_SIZE/2 - board.window_width()/2 , TURTLE_SIZE/2 - board.window_height()/2 )
border.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.penup()
border.lt(90)
border.fd(930/8)
border.pendown()
border.goto(-TURTLE_SIZE + board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.hideturtle()



player=turtle.Turtle()
player.color("green")
player.shape("turtle")
player.hideturtle()
player.penup()
player.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
player.rt(90)
player.fd(30)
player.lt(90)
player.fd((930/8)/2)
player.rt(90)
player.showturtle()
coordinates = player.pos()







#Skapa spelaren (som går igenom banan)
def spawna_spelare():
    spawn = turtle.Turtle()
    spawn.color("green")
    spawn.shape("turtle")
    spawn.hideturtle()
    spawn.penup()

    spawn.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
    spawn.rt(90)
    spawn.fd(30)
    spawn.lt(90)
    spawn.fd((930/8)*(random.randint(1,8)))
    spawn.rt(90)

    spawn.showturtle()

#Gå med spelaren
def move_left():
    x  = player.xcor()
    x -= playerspeed

    if x  < -460:
        x = -460
    player.setx(x)

def move_right():

    x = player.xcor()
    x += playerspeed
    if x > 450:
        x = 450
    player.setx(x)


    if x.distance(wall_ett) >20: #or wall_ett_ett or wall_två_ett or wall_två_två) == 1:  #
        player.shape("triangle")

def move_down():
    y = player.ycor()
    y -= playerspeed
    if y < -375:
        y = -375
    player.sety(y)

def move_up():
    y = player.ycor()
    y += playerspeed

    if y > 380:
        y = 380
    player.sety(y)

turtle.listen()
turtle.onkey(spawna_spelare, "space")
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_down, "Down")
turtle.onkey(move_up, "Up")





wall_ett = border.xcor()
wall_två_ett=border.xcor()
print(wall_ett)
print(wall_ett)
print(hole)







board.mainloop()

4

1 回答 1

0

因此,您需要以一种您可以事后检查玩家请求的移动是否跨越任何墙壁(或边界)的方式绘制墙壁。

这是一个建议:

SCREEN_WIDTH = 800
HOLE_HEIGHT = 50
SCREEN_HEIGHT  = 600
NUM_WALLS = 8

board = turtle.Screen()
board.setup(SCREEN_WIDTH+10, SCREEN_HEIGHT+10)
board.screensize(SCREEN_WIDTH, SCREEN_HEIGHT)
board.setworldcoordinates(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)

border = turtle.Turtle()
border.speed(10) # draw walls as fast as possible
border.color('white')
border.hideturtle()
# list of the drawn walls, each wall is represented by a tuple:
# (wall_x, wall_hole_lower_y, wall_hole_upper_y)
walls = [] 
for wall_ix in range(NUM_WALLS):
    # compute coordinates of wall and store them
    wall_x = int((1+wall_ix) * SCREEN_WIDTH / (1+NUM_WALLS))
    wall_ybottom = random.randint(0, SCREEN_HEIGHT - HOLE_HEIGHT)
    wall_ytop = wall_ybottom + HOLE_HEIGHT
    walls.append((wall_x, wall_ytop, wall_ybottom))
    # draw wall
    border.penup()
    border.goto(wall_x, 0)
    border.pendown()
    border.goto(wall_x, wall_ybottom)
    border.penup()
    border.goto(wall_x, wall_ytop)
    border.pendown()
    border.goto(wall_x, SCREEN_HEIGHT)

def move_right():
    old_x  = player.xcor()
    new_x = old_x + playerspeed
    y = player.ycor()

    for wall_x, wall_ytop, wall_ybottom in walls:
        if old_x <= wall_x <= new_x:
            # player is possibly moving across the wall, check that y is valid
            if wall_ybottom < y < wall_ytop:
                # OK ! player moves through the hole
                player.setx(new_x)
            else:
                # invalid, player is crossing a wall line
                # resetting to old position, but you might want to stop the game
                player.setx(old_x)
于 2020-03-24T14:39:41.570 回答