python - 在 python turtle 中,当我想显示球的随机运动时,它们不会从我的屏幕边缘反弹
问题描述
import turtle
import random
import time
我们确认球在屏幕左侧
def atLeftEdge(ball,screen_width):
if ball.xcor()<screen_width/2:
return True
else:
return False
我们确认球在右侧
def atRightEdge(ball,screen_width):
if ball.xcor()>screen_width/2:
return True
else:
return False
我们确认球在上边缘
def atTopEdge(balls,screen_height):
if ball.ycor()>screen_height/2:
return True
else:
return False
我们确认球在底部边缘
def atBottomEdge(balls,screen_height):
if ball.ycor()<-screen_height/2:
return True
else:
return False
现在球在到达屏幕边缘时必须反弹
def bounceBall(ball,new_direction):
if new_direction=='left'or new_direction=='right':
new_heading=180-ball.heading()
elif new_direction=='up'or new_direction=='down':
new_heading=360-ball.heading()
return new_heading
def createBalls(num_balls):
balls=[]
for x in range(0,num_balls):
new_ball=turtle.Turtle()
new_ball.shape('circle')
new_ball.fillcolor('black')
new_ball.speed(0)
new_ball.penup()
new_ball.setheading(random.randint(1,359)) #random angle between 1 to 359
balls.append(new_ball)
return balls
程序从这里开始,这是程序的主要部分,我们从用户那里获取输入并调用所有函数
#------------------MAIN-------------------------------------------------
print("The program stimulates bouncing balls in a turtle screen"\
"for a specified number of seconds")
#TODO:create turtle graphics window object
#set up screen
screen_width=800
screen_height=600
turtle.setup(screen_width,screen_height)
#get reference to turtle window by calling Screen method
window=turtle.Screen()
window.title('Random balls on screen')
window.bgcolor('violet')
要求用户输入执行时间和球数
num_sec=int(input("enter no of seconds to run"))
num_balls=int(input("enter no of balls in sim"))
创建球
balls=createBalls(num_balls)
设置开始时间
start_time=time.time()
开始模拟
terminate=False
while not terminate:
for x in range(0,len(balls)):
balls[x].forward(40)
if atLeftEdge(balls[x],screen_width):
balls[x].setheading(bounceBall(balls[x],'right'))
elif atRightEdge(balls[x],screen_width):
balls[x].setheading(bounceBall(balls[x],'left'))
elif atTopEdge(balls[x],screen_height):
balls[x].setheading(bounceBall(balls[x],'down'))
elif atBottomEdge(balls[x],screen_height):
balls[x].setheading(bounceBall(balls[x],'up'))
if time.time()-start_time>num_sec:
terminate =True
#exit on close window
turtle.exitonclick()
解决方案
我已将 s 更改为elif
s,if
因为这些是独立事件。
if atLeftEdge(balls[x],screen_width):
balls[x].setheading(bounceBall(balls[x],'right'))
if atRightEdge(balls[x],screen_width):
balls[x].setheading(bounceBall(balls[x],'left'))
if atTopEdge(balls[x],screen_height):
balls[x].setheading(bounceBall(balls[x],'down'))
if atBottomEdge(balls[x],screen_height):
balls[x].setheading(bounceBall(balls[x],'up'))
同样在atLeftEdge
功能上,您需要以这种方式编写您的 if :
if ball.xcor()<-screen_width/2:
也在atTopEdge
,atBottomEdge
我认为你的意思是
def atTopEdge (ball , screen_height) :
和
def atBottomEdge(ball , screen_height):
推荐阅读
- r - 如何在 csv 文件中忽略带引号的换行符以创建 Hive 外部表?
- wordpress - 隐藏 Woocommerce wc-message: "item*" 已删除。撤消?
- django - Django 在同一个项目中处理多个应用程序
- c# - 为什么 Selenium 无法读取下拉列表的文本,即使它们存在于 HTML 中。(C#,VS2019,硒)
- google-cloud-platform - 谷歌云平台负载均衡器 - ERR_TOO_MANY_REDIRECTS
- javascript - 使用 maxDate 的 bootstrap-datetimepicker 有时不显示日期
- flutter - 在内部存储器(根文件夹)中创建目录 Flutter
- python - 如何使用 Python 将具有非英文字符的变量打印到命令提示符
- javascript - 在反应 js 中使用 OOP
- javascript - 如何基于对象数组中的相似值(不是属性)对对象数组执行加/减运算