I'm making a game in Turtle's graphics. Here's the code
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()
I want to make it so that when the character isn't touching the yellow tiles, it floats down until it is touching the yellow tiles. I'm thinking, maybe, if it isn't touching the dark yellow color
Any help?
you can check if the character goes below a specific degree in the y-axis (for example 10); where its y-axis decreases if it is higher than that, and it will stop (use break in that loop/if statement) if it reaches that limit (the 10)
Below is my rework of your code that attempts to simplify it, add the downward motion, and get it to test the floor boundary:
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()
Hopefully you can use some of this logic in your solution.
You'll need to modify your up()
logic to limit the movement to the top of the stage. Ditto forward()
and backward()
with respect to the sides.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.