障碍物在玩家输入时冻结

2024-04-23 19:43:46 发布

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

我的Python龟游戏障碍在玩家收到输入后冻结,然后继续:

# Written in Python 3
# By 
# February 4th, 2019
# Purpose: Mimic the no-wifi Google Chrome dinosaur game
# Bonus 6
import turtle # Used for graphics
from turtle import Screen # Used for inputs
import random# Used to generate a random number
import time

# Creates a turtle with the proper size and color
player = turtle.Turtle()
player.shape("square")
player.color("#cc0000")
player.turtlesize(1.5, 1.5)
player.penup()
player.goto(-50, 0)

# Creates the ground
ground = turtle.Turtle()
ground.shape("square")
ground.turtlesize(0.25, 300)
ground.penup()
ground.goto(0, -18)

# This function makes the square jump, unbinds 'Up', return to the ground, then rebinds 'Up'
def jump():
    Screen().onkey(null, 'Up')
    player.speed(2)
    if player.ycor() == 0:
        player.goto((player.xcor()), (player.ycor()+100))
    print("G")
    player.speed(1.5)
    player.goto(-50, 0)
    Screen().onkey(jump, 'Up')

# Blank function
def null():
    n =1

Screen().onkey(jump, 'Up')
Screen().listen()

# Ignore this
x = 3 * random.sample(range(4), 4)
print (x)
print (x[1])

# Creating obstacles (not finished, just moves)
obst1 = turtle.Turtle()
obst1.shape("square")
obst1.turtlesize(3, 2)
obst1.penup()
obst1.goto(300,0)
obst1.speed(1)
obst1.setx(-300)

我想让障碍物在我跳的时候继续移动。我只有python3及其标准模块。因为某些原因,我不能下载PIP或者其他类似的东西。我试着模仿谷歌Chrome上的恐龙游戏。我对这类事情还不熟悉,所以请尽可能详细地解释任何建议。谢谢!你知道吗


Tags: theimportrandomscreenusedplayershapeup
1条回答
网友
1楼 · 发布于 2024-04-23 19:43:46

按照编写代码的方式,一次只能移动一只海龟。下面是对您的代码的修改,其中用户控制播放器,但障碍物由计时器控制,以便它们可以同时移动:

from turtle import Screen, Turtle

# This function unbinds 'Up', makes the square jump, return to the ground, then rebinds 'Up'
def jump():
    screen.onkey(None, 'Up')

    if player.ycor() == 0:
        player.forward(150)
        player.backward(150)

    screen.onkey(jump, 'Up')

def move():
    obstacle.forward(6)
    if obstacle.xcor() > -400:
        screen.ontimer(move, 100)

# Creates the ground
ground = Turtle('square')
ground.turtlesize(0.25, 45)
ground.penup()
ground.sety(-15)

# Creates a turtle with the proper size and color
player = Turtle('square')
player.color('red')
player.turtlesize(1.5)
player.speed('slowest')
player.penup()
player.setx(-100)
player.setheading(90)

screen = Screen()
screen.onkey(jump, 'Up')
screen.listen()

# Creating obstacles (not finished, just moves)
obstacle = Turtle('square')
obstacle.turtlesize(3, 1.5)
obstacle.speed('fastest')
obstacle.penup()
obstacle.setposition(400, 15)
obstacle.setheading(180)

move()

screen.mainloop()

这应该更接近于模拟你想要达到的运动类型。你知道吗

相关问题 更多 >