dimanche 20 décembre 2020

In python turtle, when I want to show random motion of balls ,they are not bouncing off the edge of my screen

import turtle
import random
import time

we confirm ball is on left side of screen

def atLeftEdge(ball,screen_width):
    if ball.xcor()<screen_width/2: 
        return True
    else:
        return False

we confirm ball is on right side

def atRightEdge(ball,screen_width):
    if ball.xcor()>screen_width/2:
        return True
    else:
        return False

we confirm ball is on top edge

def atTopEdge(balls,screen_height):

    if ball.ycor()>screen_height/2:
        return True
    else:
        return False

we confirm ball is on bottom edge

def atBottomEdge(balls,screen_height):
    
    if ball.ycor()<-screen_height/2:
        return True
    else:
        return False

Now ball must bounce when it reaches edge of screen

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

program starts here ,this is the main part of program where we take input from user and call all functions

 #------------------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')

ask user to enter execution time and number of balls

num_sec=int(input("enter no of seconds to run"))
num_balls=int(input("enter no of balls in sim"))

create balls

balls=createBalls(num_balls)

set start time

start_time=time.time()

begin simulation

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

    

    



Aucun commentaire:

Enregistrer un commentaire