繁体   English   中英

乌龟 - 不感人的颜色

[英]Turtle - Not Touching Color

我正在用Turtle的图片制作游戏。 这是代码

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()

我想这样做,以便当角色没有碰到黄色瓷砖时,它会向下漂浮,直到它接触黄色瓷砖。 我想,也许,如果它没有触及深黄色

有帮助吗?

我想要发生什么的例子

你可以检查角色是否在y轴的特定程度以下(例如10); 如果高于该值,则y轴减小,如果达到该极限,它将停止(在该循环中使用break / if语句)(10)

下面是我对你的代码的修改,试图简化它,添加向下运动,并让它来测试地板边界:

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()

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

在此输入图像描述

您需要修改up()逻辑以限制移动到舞台顶部。 同样forward()backward()相对于两侧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM