简体   繁体   English

玩家输入期间障碍物冻结 - Python Turtle

[英]Obstacle freezes during player input - Python Turtle

My Python turtle game obstacle freezes when the player receives input and continues afterward:当玩家收到输入时,我的 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)

I want the obstacle to continue moving while I jump.我希望障碍物在我跳跃时继续移动。 I only have Python 3 and its standard modules.我只有 Python 3 及其标准模块。 I cannot download PIP or anything else like it, for some reason.由于某种原因,我无法下载 PIP 或其他类似的东西。 I'm trying to mimic the dinosaur game from Google Chrome.我正在尝试从 Google Chrome 模仿恐龙游戏。 I'm new to this sort of thing so please explain any suggestions in as much detail as possible.我是这种事情的新手,所以请尽可能详细地解释任何建议。 Thanks a lot!非常感谢!

The way you wrote your code, you can only move one turtle at a time.根据您编写代码的方式,一次只能移动一只乌龟。 Below is a rework of your code where the user controls the player, but the obstacle is controlled by a timer so they can move at the same time:以下是用户控制玩家的代码的返工,但障碍物由计时器控制,因此他们可以同时移动:

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

This should more closely simulate the type of motion you're trying to achieve.这应该更接近地模拟您要实现的运动类型。

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

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