乌龟不碰C

2024-10-01 07:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用乌龟的图案做游戏。这是密码

import turtle
import time

width = -462
height = 387

cellHeight = 387

turtle.title('Tutorial Game')

##TurtleImage
turtle.addshape('cell1.gif')
turtle.addshape('platformTile.gif')

##Render
def renderScreen():
    #Background
    turtle.bgcolor('green')
    ##Roof
    for i in range(3):
        roof()
        global cellHeight
        cellHeight -= 32
    ##Floor
    cellHeight = -387
    for i in range(2):
        floor()
        global cellHeight
        cellHeight += 32
    char()

def roof():
    turtle.shape('platformTile.gif')
    turtle.goto(width, cellHeight)
    for i in range(30):
        turtle.stamp()
        turtle.forward(32)

def floor():
    turtle.shape('platformTile.gif')
    turtle.goto(width, cellHeight)
    for i in range(30):
        turtle.stamp()
        turtle.forward(32)

def char():
    turtle.shape('cell1.gif')
    turtle.showturtle()
    turtle.goto(0, 0)
    turtle.onkey(forward, 'd')
    turtle.onkey(backward, 'a')
    turtle.onkey(up, 'space')
    turtle.listen()

##Movement

def forward():
    turtle.forward(32)

def backward():
    turtle.backward(32)

def jump():
    turtle.setheading(90)
    turtle.forward(32)
    time.sleep(0.5)
    turtle.forward(32)
    time.sleep(0.5)
    turtle.setheading(270)
    turtle.forward(32)
    time.sleep(0.5)
    turtle.forward(32)
    turtle.setheading(0)

def up():
    turtle.setheading(90)
    turtle.forward(32)
    turtle.setheading(0)

turtle.penup()
turtle.hideturtle()
turtle.speed(0)

renderScreen()


 turtle.done()

我想让它这样,当角色没有接触到黄色的瓷砖时,它会漂浮下来,直到它接触到黄色的瓷砖。我在想,也许,如果它没有接触到暗黄色

有什么帮助吗?在

Example of what I want to happen


Tags: infortimedefrangewidthgifforward
2条回答

您可以检查字符是否低于y轴的特定度数(例如10);如果它高于该值,它的y轴将减小;如果达到该限制(10),它将停止(在该循环中使用break/if语句)

下面是我对您的代码的重新编写,它试图简化它,添加向下运动,并让它测试楼层边界:

from turtle import Turtle, Screen

CELL_SIZE = 32
WIDTH, HEIGHT = 30 * CELL_SIZE, 24 * CELL_SIZE  # screen size in cell units

PLATFORM_HEIGHT = 2 * CELL_SIZE  # height of platform
LIMIT = PLATFORM_HEIGHT - HEIGHT / 2

STAMP_UNIT = 20  # turtle's default cursor size

def renderPlatform():
    """ Go to the bottom of the screen and stamp the platform """

    turtle = Turtle('square', visible=False)
    turtle.speed('fastest')
    turtle.penup()
    turtle.goto(0, PLATFORM_HEIGHT / 2 - HEIGHT / 2)
    turtle.setheading(90)
    turtle.shapesize(WIDTH / STAMP_UNIT, PLATFORM_HEIGHT / STAMP_UNIT)
    turtle.color('yellow')
    turtle.stamp()

def forward():
    character.forward(CELL_SIZE)

def backward():
    character.backward(CELL_SIZE)

def up():
    character.sety(character.ycor() + CELL_SIZE)

def down():
    character.sety(character.ycor() - CELL_SIZE)

    if character.ycor() - CELL_SIZE / 2 <= LIMIT:  # test for collision with platform
        character.stamp()  # leave a marker
        character.hideturtle()
        character.goto(0, CELL_SIZE / 2)  # start anew floating downward
        character.showturtle()

    screen.ontimer(down, 1000)  # move character downward once a second

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.title('Tutorial Game')
screen.bgcolor('green')

renderPlatform()

character = Turtle('square')
character.shapesize(CELL_SIZE / STAMP_UNIT)
character.penup()
character.color('red')
character.sety(CELL_SIZE / 2)  # might want to start higher in actual game

screen.onkey(forward, 'd')
screen.onkey(backward, 'a')
screen.onkey(up, 'space')

screen.listen()

screen.ontimer(down, 1000)  # start character falling

screen.mainloop()

希望您可以在解决方案中使用这些逻辑。

enter image description here

您需要修改您的up()逻辑,将移动限制在阶段顶部。对于侧面,同上forward()和{}。

相关问题 更多 >