首页 > 解决方案 > 在 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()
        

    

    

标签: pythonrandombouncepython-turtle

解决方案


我已将 s 更改为elifs,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:

也在atTopEdgeatBottomEdge我认为你的意思是

def atTopEdge (ball , screen_height) :

def atBottomEdge(ball , screen_height):

推荐阅读