海龟模块中的游戏玩家无法获得不同的速度
Can't get different speeds for game players in turtle module
在 YouTube 上名为“Falling Skies”的小游戏中,我无法为好玩家和坏玩家获得正确的速度:GitHub Gist 它使用了 Python 乌龟模块。我该如何解决这个问题? (我卡在了小游戏教程的第 6 部分。)
import turtle # Our module's
import random
# Screen
ms = turtle.Screen()
ms.title("Falling Piece's mini_game by Rafa94")
ms.bgcolor("purple")
ms.setup(width=800, height=600)
ms.tracer(0)
#player
player = turtle.Turtle()
player.speed(0)
player.shape("turtle")
player.color("blue")
player.penup()
player.goto(0, -250) # y is postive in the up direction y is negative in the down direction
player.direction = "stop"
# create a list of good players # good represents good_player
goods =[]#Empty list
#Addgood players
for _ in range(20): # we are making a set of 20 players
good = turtle.Turtle() # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
good.speed(0)
good.shape("circle")
good.color("red")
good.penup()
good.goto(0, 250) # y is postive in the up direction y is negative in the down direction
good.speed = random.randint(5,8)
goods.append(good)
# create a list of bad players # bad represents bad_players
bads = [] # Empty list
# Addbad players
for _ in range(20): # we are making a set of 20 players
bad = turtle.Turtle() # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
bad.speed(10)
bad.shape("circle")
bad.color("yellow")
bad.penup()
bad.goto(0, 250) # y is postive in the up direction y is negative in the down direction
bad.speed = random.randint(5, 8)
bads.append(bad)
#Functions
def go_left():
player.direction = "left"
def go_right():
player.direction = "right"
#keyboard Binding
ms.listen() # it is bascally saying listen for keyboard input < ^ >
ms.onkeypress(go_left, "Left")
ms.onkeypress(go_right, "Right")
#Main game loop # while something is true it will repeat
while True:
# update screen
ms.update()
#Move player
if player.direction == "left":
x = player.xcor()
x -= + 3
player.setx(x)
if player.direction == "right":
x = player.xcor()
x += + 3
player.setx(x)
# Move Good Player
for good in goods:
y = good.ycor()
y -= good.speed # We want the ball to be falling at a smooth speed
good.sety(y)
# Check it off the Screen
if y < -300:
x = random.randint(-380, 380)
y = random.randint(300, 400)
good.goto(x, y)
# check for collision with player
if good.distance(player) < 20:
x = random.randint(-380, 380)
y = random.randint(300, 400)
good.goto(x, y)
# Move bad Player
for bad in bads:
y = bad.ycor()
y -= bad.speed # We want the ball to be falling at a slow speed
bad.sety(y)
# Check it off the Screen
if y < -300:
x = random.randint(-380, 380)
y = random.randint(300, 400)
bad.goto(x, y)
# check for collision with player
if bad.distance(player) < 20:
x = random.randint(-380, 380)
y = random.randint(300, 400)
bad.goto(x, y)
ms.mainloop()
如果我理解你的需求,你希望好人和坏人以不同的速度移动。关键是理解程序中有两个 speed 的概念:turtle 的 speed()
方法:
player.speed(0)
good.speed(0)
bad.speed(10)
这个跟图形绘制的速度有关,不是我们感兴趣的。还有游戏自带的实例变量speed
作者挂掉的海龟实例:
good.speed = randint(5, 8)
bad.speed = randint(5, 8)
y -= good.speed
y -= bad.speed
这可能不应该被称为 .speed
,因为海龟已经有一个 .speed
方法。所以我们将其重命名为 .velocity
。这个速度就是物体在屏幕上移动的速度,也是我们感兴趣的。
你把good.speed(0)
和bad.speed(10)
设置错了,因为它们不是绘图,只是移动乌龟光标,效果不大。我们要做的是改变:
good.velocity = randint(1, 4) # was .speed
bad.velocity = randint(5, 8) # was .speed
并将 .speed
实例变量的所有其他用途修复为 .velocity
:
y -= good.velocity # was .speed
y -= bad.velocity # was .speed
除其他外,我已经对以下代码进行了此更改。您会发现好人的移动速度比坏人快 2.5 倍:
# Our module's
from turtle import Screen, Turtle
from random import randint
# Functions
def go_left():
player.direction = 'left'
def go_right():
player.direction = 'right'
# Screen
screen = Screen()
screen.setup(width=800, height=600)
screen.title("Falling Piece's mini_game by Rafa94")
screen.bgcolor('purple')
screen.tracer(0)
# player
player = Turtle()
player.shape('turtle')
player.speed('fastest')
player.color('blue')
player.penup()
player.sety(-250) # y is positive in the up direction; negative in the down direction
player.direction = 'stop'
# Create a list of good players
# Good represents good_player
goods = [] # Empty list
# Add good players
for _ in range(20): # We are making a set of 20 players
# We want the other players basically across from each other
# thats we copyied the code one on -y and one on +y (first
# player in the bottom, second player on top of Screen)
good = Turtle()
good.shape('circle')
good.speed('fastest')
good.color('red')
good.penup()
good.goto(-100, 250) # y is positive in the up direction; negative in the down direction
good.velocity = randint(1, 4)
goods.append(good)
# Create a list of bad players
# Bad represents bad_players
bads = [] # Empty list
# Add bad players
for _ in range(20): # we are making a set of 20 players
# We want the other player basically across from each other
# thats we copyied the code one on -y and one on +y (first
# player in the bottom, second player on top of Screen)
bad = Turtle()
bad.shape('circle')
bad.speed('fastest')
bad.color('yellow')
bad.penup()
bad.goto(100, 250) # y is positive in the up direction; negative in the down direction
bad.velocity = randint(5, 8)
bads.append(bad)
# Keyboard Binding
screen.onkeypress(go_left, 'Left')
screen.onkeypress(go_right, 'Right')
screen.listen() # Basically saying listen for keyboard input
# Main game loop
# While something is true it will repeat
while True:
# Move player
x = player.xcor()
if player.direction == 'left':
x -= 3
elif player.direction == 'right':
x += 3
player.setx(x)
# Move Good Player
for good in goods:
y = good.ycor() - good.velocity # We want the ball to be falling at a smooth speed
good.sety(y)
if y < -300:
# Check if it's off the Screen
x = randint(-380, 380)
y = randint(300, 400)
good.goto(x, y)
elif good.distance(player) < 20:
# Check for collision with player
x = randint(-380, 380)
y = randint(300, 400)
good.goto(x, y)
# Move bad Player
for bad in bads:
y = bad.ycor() - bad.velocity # We want the ball to be falling at a slow speed
bad.sety(y)
if y < -300:
# Check if it's off the Screen
x = randint(-380, 380)
y = randint(300, 400)
bad.goto(x, y)
elif bad.distance(player) < 20:
# Check for collision with player
x = randint(-380, 380)
y = randint(300, 400)
bad.goto(x, y)
# Update screen
screen.update()
screen.mainloop()
在 YouTube 上名为“Falling Skies”的小游戏中,我无法为好玩家和坏玩家获得正确的速度:GitHub Gist 它使用了 Python 乌龟模块。我该如何解决这个问题? (我卡在了小游戏教程的第 6 部分。)
import turtle # Our module's
import random
# Screen
ms = turtle.Screen()
ms.title("Falling Piece's mini_game by Rafa94")
ms.bgcolor("purple")
ms.setup(width=800, height=600)
ms.tracer(0)
#player
player = turtle.Turtle()
player.speed(0)
player.shape("turtle")
player.color("blue")
player.penup()
player.goto(0, -250) # y is postive in the up direction y is negative in the down direction
player.direction = "stop"
# create a list of good players # good represents good_player
goods =[]#Empty list
#Addgood players
for _ in range(20): # we are making a set of 20 players
good = turtle.Turtle() # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
good.speed(0)
good.shape("circle")
good.color("red")
good.penup()
good.goto(0, 250) # y is postive in the up direction y is negative in the down direction
good.speed = random.randint(5,8)
goods.append(good)
# create a list of bad players # bad represents bad_players
bads = [] # Empty list
# Addbad players
for _ in range(20): # we are making a set of 20 players
bad = turtle.Turtle() # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
bad.speed(10)
bad.shape("circle")
bad.color("yellow")
bad.penup()
bad.goto(0, 250) # y is postive in the up direction y is negative in the down direction
bad.speed = random.randint(5, 8)
bads.append(bad)
#Functions
def go_left():
player.direction = "left"
def go_right():
player.direction = "right"
#keyboard Binding
ms.listen() # it is bascally saying listen for keyboard input < ^ >
ms.onkeypress(go_left, "Left")
ms.onkeypress(go_right, "Right")
#Main game loop # while something is true it will repeat
while True:
# update screen
ms.update()
#Move player
if player.direction == "left":
x = player.xcor()
x -= + 3
player.setx(x)
if player.direction == "right":
x = player.xcor()
x += + 3
player.setx(x)
# Move Good Player
for good in goods:
y = good.ycor()
y -= good.speed # We want the ball to be falling at a smooth speed
good.sety(y)
# Check it off the Screen
if y < -300:
x = random.randint(-380, 380)
y = random.randint(300, 400)
good.goto(x, y)
# check for collision with player
if good.distance(player) < 20:
x = random.randint(-380, 380)
y = random.randint(300, 400)
good.goto(x, y)
# Move bad Player
for bad in bads:
y = bad.ycor()
y -= bad.speed # We want the ball to be falling at a slow speed
bad.sety(y)
# Check it off the Screen
if y < -300:
x = random.randint(-380, 380)
y = random.randint(300, 400)
bad.goto(x, y)
# check for collision with player
if bad.distance(player) < 20:
x = random.randint(-380, 380)
y = random.randint(300, 400)
bad.goto(x, y)
ms.mainloop()
如果我理解你的需求,你希望好人和坏人以不同的速度移动。关键是理解程序中有两个 speed 的概念:turtle 的 speed()
方法:
player.speed(0)
good.speed(0)
bad.speed(10)
这个跟图形绘制的速度有关,不是我们感兴趣的。还有游戏自带的实例变量speed
作者挂掉的海龟实例:
good.speed = randint(5, 8)
bad.speed = randint(5, 8)
y -= good.speed
y -= bad.speed
这可能不应该被称为 .speed
,因为海龟已经有一个 .speed
方法。所以我们将其重命名为 .velocity
。这个速度就是物体在屏幕上移动的速度,也是我们感兴趣的。
你把good.speed(0)
和bad.speed(10)
设置错了,因为它们不是绘图,只是移动乌龟光标,效果不大。我们要做的是改变:
good.velocity = randint(1, 4) # was .speed
bad.velocity = randint(5, 8) # was .speed
并将 .speed
实例变量的所有其他用途修复为 .velocity
:
y -= good.velocity # was .speed
y -= bad.velocity # was .speed
除其他外,我已经对以下代码进行了此更改。您会发现好人的移动速度比坏人快 2.5 倍:
# Our module's
from turtle import Screen, Turtle
from random import randint
# Functions
def go_left():
player.direction = 'left'
def go_right():
player.direction = 'right'
# Screen
screen = Screen()
screen.setup(width=800, height=600)
screen.title("Falling Piece's mini_game by Rafa94")
screen.bgcolor('purple')
screen.tracer(0)
# player
player = Turtle()
player.shape('turtle')
player.speed('fastest')
player.color('blue')
player.penup()
player.sety(-250) # y is positive in the up direction; negative in the down direction
player.direction = 'stop'
# Create a list of good players
# Good represents good_player
goods = [] # Empty list
# Add good players
for _ in range(20): # We are making a set of 20 players
# We want the other players basically across from each other
# thats we copyied the code one on -y and one on +y (first
# player in the bottom, second player on top of Screen)
good = Turtle()
good.shape('circle')
good.speed('fastest')
good.color('red')
good.penup()
good.goto(-100, 250) # y is positive in the up direction; negative in the down direction
good.velocity = randint(1, 4)
goods.append(good)
# Create a list of bad players
# Bad represents bad_players
bads = [] # Empty list
# Add bad players
for _ in range(20): # we are making a set of 20 players
# We want the other player basically across from each other
# thats we copyied the code one on -y and one on +y (first
# player in the bottom, second player on top of Screen)
bad = Turtle()
bad.shape('circle')
bad.speed('fastest')
bad.color('yellow')
bad.penup()
bad.goto(100, 250) # y is positive in the up direction; negative in the down direction
bad.velocity = randint(5, 8)
bads.append(bad)
# Keyboard Binding
screen.onkeypress(go_left, 'Left')
screen.onkeypress(go_right, 'Right')
screen.listen() # Basically saying listen for keyboard input
# Main game loop
# While something is true it will repeat
while True:
# Move player
x = player.xcor()
if player.direction == 'left':
x -= 3
elif player.direction == 'right':
x += 3
player.setx(x)
# Move Good Player
for good in goods:
y = good.ycor() - good.velocity # We want the ball to be falling at a smooth speed
good.sety(y)
if y < -300:
# Check if it's off the Screen
x = randint(-380, 380)
y = randint(300, 400)
good.goto(x, y)
elif good.distance(player) < 20:
# Check for collision with player
x = randint(-380, 380)
y = randint(300, 400)
good.goto(x, y)
# Move bad Player
for bad in bads:
y = bad.ycor() - bad.velocity # We want the ball to be falling at a slow speed
bad.sety(y)
if y < -300:
# Check if it's off the Screen
x = randint(-380, 380)
y = randint(300, 400)
bad.goto(x, y)
elif bad.distance(player) < 20:
# Check for collision with player
x = randint(-380, 380)
y = randint(300, 400)
bad.goto(x, y)
# Update screen
screen.update()
screen.mainloop()