Python多彩弹珠球,面对对象编程练习

""" Python多彩弹珠球,面对对象编程练习,请阅读以下源代码,把ball模块写好。提示:
在ball.py文件中应该建立一个继承自Turtle的Ball类。  """

import pygame
from turtle import *
from random import randint,choice
from time import sleep
from ball import *
 
 
# 请阅读下面的程序,说出每一行代码的意思,把Ball类写出来

width,height = 480,360
colorList=['red','orange','yellow','green','cyan','blue','purple','white','gray']

pygame.mixer.init()
碰撞声 = pygame.mixer.Sound('sound.wav')
pygame.mixer.music.load("mmx3frst.wav")
pygame.mixer.music.play(-1,0)     # -1表示循环播放,0表示从音乐流最开始播放

screen = Screen()
screen.bgcolor("cyan")            # 设定背景颜色       
screen.delay(0)                   # 延时为 0豪秒
screen.title("python面向对象编程练习by李兴球")
screen.setup(width,height)        # 设置屏幕宽高
 
balls = [Ball() for _ in range(10)]
ball_amount = len(balls)
index = 0

while True:
    ball = balls[index]
    index = index + 1
    index = index % ball_amount 
    ball.fd(1)         
    if abs(ball.xcor()) > width/2:
        ball.setheading(180-ball.heading()) # 由于方向实际上是和x轴的夹角,碰到垂直边缘后的方向为180-原方向
        碰撞声.play()            
    if abs(ball.ycor()) > height/2:
        ball.setheading( - ball.heading())  # 水平方向碰撞,方向取反
        碰撞声.play() 
    

    

Python多彩弹珠球,面对对象编程练习