我用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()
我想这样做,以便当角色不接触黄色瓷砖时,它会向下漂移,直到它接触黄色瓷砖。我想,如果它没有触及深黄色那么
任何帮助?
答案 0 :(得分:0)
你可以检查角色是否低于y轴的特定度数(例如10);如果高于该值,则y轴减小,并且如果达到该极限(10),它将停止(在该循环/ if语句中使用break)
答案 1 :(得分:0)
下面是我对你的代码的修改,试图简化它,添加向下运动,并让它来测试地板边界:
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()
。